forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet-send
executable file
·142 lines (113 loc) · 3.57 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
var program = require('commander');
var utils = require('./cli-utils');
var _ = require('lodash');
var bwc = require('bitcore-wallet-client');
var Bitcore_ = {
bch: bwc.BitcoreCash,
btc: bwc.Bitcore,
};
program = utils.configureCommander(program);
program
.option('--fee <fee-policy>', 'Fee policy to use (default "normal") [urgent, priority/normal/economy/superEconomy] ')
.usage('[options] <address> <amount> [note]')
.description('Create a proposal for sending bitcoins to a destination address.\n The amount can be specified in bit, btc or sat (the default).');
program.on('--help', function() {
console.log(' Examples:');
console.log('');
console.log(' $ wallet-send n2HRFgtoihgAhx1qAEXcdBMjoMvAx7AcDc 500bit');
console.log(' $ wallet-send mgWeRvUC6d1LRPKtdDbvYEpaUEmApS4XrY 0.2btc "dinner with friends"');
console.log(' $ wallet-send https://paypro.url/1234 # will ask for confirmation ');
console.log('');
});
program.parse(process.argv);
var args = program.args;
if (!args[0])
program.help();
function confirmDiag(amountStr, note, cb) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`Confirm send ${amountStr} to ${note}? (y/N)`, (answer) => {
rl.close();
return cb(answer =='y');
});
};
function send(client, address, amount, fee, note, uri) {
var amount;
fee = fee || 'normal';
client.createTxProposal({
outputs: [{
toAddress: address,
amount: amount,
}],
message: note,
feeLevel: fee,
payProUrl: uri,
}, function(err, txp) {
utils.die(err);
client.publishTxProposal({
txp: txp
}, function(err) {
utils.die(err);
console.log(' * Tx created: ID %s [%s] RequiredSignatures:',
utils.shortID(txp.id), txp.status, txp.requiredSignatures);
});
});
};
var arg1 = args[0];
var uri;
utils.getClient(program, {
mustExist: true
}, function(client) {
var coin = client.credentials.coin;
var bitcore = Bitcore_[coin];
var uri, addr, amount, note;
// no backwards compat uri
if ((/^bitcoin(cash)?:\?r=[\w+]/).exec(arg1)) {
var coin2 = 'btc';
if (arg1.indexOf('bitcoincash') === 0) coin2 = 'bch';
if (coin != coin2) utils.die('Wallet / Payment Coin mismatch');
uri = arg1.replace(/bitcoin(cash)?:\?r=/, '');
} else {
// BIP21
try {
var parsed = new bitcore.URI(arg1);
if (!parsed.r) {
addr = parsed.address ? parsed.address.toString() : '';
note = parsed.message;
amount = parsed.amount ? parsed.amount : '';
} else {
uri = parsed.r;
}
} catch (e) {
uri = null;
}
}
//Send to URI or addr
if (uri) {
console.log('Fetching Payment from: ' + uri);
client.fetchPayPro({
payProUrl: uri,
}, function(err, paypro) {
if (err) {
utils.die(' Failed to fetch payment: ' + (_.isObject(err)? JSON.stringify(err) : err));
} else if (!paypro.verified) {
utils.die('Failed to verify payment protocol signatures');
}
var amountStr = utils.renderAmount(paypro.amount, coin);
confirmDiag(amountStr, paypro.memo, function(confirmed) {
if (!confirmed) utils.die('User canceled');
send(client, paypro.toAddress, paypro.amount, program.fee, paypro.memo, uri);
});
});
} else {
// Grab data from CLI if not set before
addr = addr || arg1;
amount = amount || utils.parseAmount(args[1]);
note = note || args[2];
send(client, addr, amount, program.fee, note);
}
});