forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet-send
executable file
·86 lines (80 loc) · 2.68 KB
/
wallet-send
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
#!/usr/bin/env node
'use strict';
const program = require('../ts_build/program');
const { Wallet } = require('../ts_build/wallet');
const promptly = require('promptly');
const https = require('https');
program
.version(require('../package.json').version)
.option('--name <name>', 'REQUIRED - Wallet Name')
.option('--to <to>', 'REQUIRED - string address')
.option('--amount <amount>', 'REQUIRED - number amount (in btc/bch/eth/xrp)')
.option('--path [path]', 'optional - Custom wallet storage path')
.option('--token [token]', 'optional - ERC-20 token to send')
.option('--storageType [storageType]', 'optional - name of the database to use (default Level)')
.parse(process.argv);
const main = async () => {
const { name, path, to, amount, target, token, storageType } = program;
let wallet;
let data;
try {
data = await getCurrencies();
} catch (err) {
console.error(err);
}
const currencies = JSON.parse(data);
try {
wallet = await Wallet.loadWallet({ name, path, storageType });
const chain = wallet.chain;
const currency = token || chain;
const { decimals } = currencies.data.find(element => element.code === currency);
const scale = Math.pow(10, decimals);
const lastAddress = await wallet.deriveAddress(0);
const recipients = [{ address: to, amount: amount * scale }];
const feerate = await wallet.getNetworkFee();
const feeRate = JSON.parse(feerate).feerate || 20 * scale;
let nonce;
if (!['BTC', 'BCH'].includes(chain)) {
nonce = await wallet.getNonce();
}
const utxos = await wallet.getUtxosArray();
const params = {
feeRate,
nonce: Number(nonce),
utxos,
recipients,
from: lastAddress,
token
};
const tx = await wallet.newTx(params);
console.log('unsignedRawTx: ', tx);
const passphrase = await promptly.password('Wallet Password:');
wallet = await wallet.unlock(passphrase);
const signedTx = await wallet.signTx({ tx, passphrase });
console.log('signedRawTx: ', signedTx);
const confirmed = await promptly.confirm('broadcast? (y/n)');
if (!confirmed) {
return;
}
const transaction = await wallet.broadcast({ tx:signedTx });
console.log('txid: ', transaction.txid);
console.log('Transaction broadcasted');
} catch (e) {
console.error(e);
}
};
function getCurrencies() {
return new Promise((resolve, reject) => {
https.get('https://bitpay.com/currencies', res => {
if (res.statusCode !== 200) {
reject(new Error('Request Failed'));
}
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => resolve(data.toString()));
});
});
}
main();