-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (96 loc) · 3.55 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const { Command } = require('commander');
require('dotenv').config();
const { getBitcoinBalance } = require('./api/getBitcoinBalance')
const { getBitcoinTransactions } = require('./api/getBitcoinTransactions')
const { generateUnusedBitcoinAddress } = require('./helper/generateUnusedBitcoinAddress')
const { generateBip39Mnemonic } = require('./helper/generateBip39Mnemonic')
const { loadWallet } = require('./helper/loadWallet')
const { listWallets } = require('./helper/listWallets')
const { saveWallet } = require('./helper/saveWallet')
const program = new Command();
program.version('1.0.0');
program
.command('create <walletName>')
.description('Create a new wallet')
.action(async (walletName) => {
const mnemonic = generateBip39Mnemonic();
const wallet = {
name: walletName,
mnemonic
}
saveWallet(walletName, wallet);
console.log(`Wallet "${walletName}" created.`);
console.log('Mnemonic:', mnemonic);
})
program
.command('import <walletName> <mnemonic>')
.description('Import a wallet from a Bip39 mnemonic')
.action(async (walletName, mnemonic) => {
const wallet = {
name: walletName,
mnemonic,
}
saveWallet(walletName, wallet);
console.log(`Wallet "${walletName}" imported.`)
})
program
.command('list')
.description('List all wallets')
.action(() => {
const wallets = listWallets();
console.log('Wallets:');
wallets.forEach((wallet) => {
console.log(`- ${wallet.name}`);
});
})
program
.command('balance <walletName')
.description('Get Bitcoin balance of a wallet')
.action(async (walletName) => {
const wallet = loadWallet(walletName);
if (!wallet) {
console.error(`Wallet "${walletName}" not found`)
return;
}
const apiKey = process.env.BLOCKCYPHER_API_KEY;
if (!apiKey) {
console.error('Blockcypher API key not found in .env file')
return;
}
const balance = await getBitcoinBalance(wallet.mnemonic, apiKey);
console.log(`Bitcoin balance for wallet "${walletName}" : ${balance} BTC`);
})
program
.command('transactions <walletName')
.description('Get list of Bitcoin transactions of a wallet')
.action(async (walletName) => {
const wallet = loadWallet(walletName);
if (!wallet) {
console.error(`Wallet "${walletName}" not found.`);
return;
}
const apiKey = process.env.BLOCKCYPHER_API_KEY;
if (!apiKey) {
console.error('BlockCypher API key not found in .env file.');
return;
}
const transactions = await getBitcoinTransactions(wallet.mnemonic, apiKey);
console.log(`Bitcoin transactions for wallet "${walletName}":`);
transactions.forEach((transaction) => {
console.log(transaction);
});
})
program
.command('generate-address <walletName>')
.description('Generate an unused Bitcoin address for a wallet')
.action(async (walletName) => {
const wallet = loadWallet(walletName);
if(!wallet) {
console.error(`Wallet "${walletName}" not found.`)
return;
}
const apiKey = process.env.BLOCKCYPHER_API_KEY;
const unusedAddress = await generateUnusedBitcoinAddress(wallet.mnemonic, apiKey);
console.log(`Unused Bitcoin address for wallet "${walletName}" : ${unusedAddress}`)
})
program.parse(process.argv)