Skip to content

Commit b9eb259

Browse files
authored
Merge pull request #1992 from matiu/bug/cli-paypro
Bug/cli paypro
2 parents 4138b7c + dfe2474 commit b9eb259

File tree

17 files changed

+313
-74
lines changed

17 files changed

+313
-74
lines changed

packages/bitcore-wallet-client/lib/paypro.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var $ = require('preconditions').singleton();
2+
const URL = require('url');
23
var Bitcore = require('bitcore-lib');
34
var Bitcore_ = {
45
btc: Bitcore,
@@ -10,14 +11,23 @@ var PayPro = {};
1011

1112
PayPro._nodeRequest = function(opts, cb) {
1213
opts.agent = false;
14+
1315
var http = opts.httpNode || (opts.proto === 'http' ? require("http") : require("https"));
1416

15-
var fn = opts.method == 'POST' ? 'post' : 'get';
17+
const url = URL.parse(opts.url);
18+
let ropts = {
19+
headers: opts.headers,
20+
method: opts.method || 'GET',
21+
hostname: url.host,
22+
port:url.port || (opts.proto === 'http' ? 80 : 443),
23+
path:url.path,
24+
protocol: url.protocol,
25+
agent: false,
26+
};
1627

17-
http[fn](opts, function(res) {
28+
var req = http.request(ropts, function(res) {
1829
var data = []; // List of Buffer objects
1930

20-
2131
if (res.statusCode != 200)
2232
return cb(new Error('HTTP Request Error: ' + res.statusCode + ' ' + res.statusMessage + ' ' + ( data ? data : '' ) ));
2333

@@ -29,6 +39,12 @@ PayPro._nodeRequest = function(opts, cb) {
2939
return cb(null, data);
3040
});
3141
});
42+
43+
req.on("error", function(error) {
44+
return cb(error);
45+
});
46+
47+
req.end();
3248
};
3349

3450
PayPro._browserRequest = function(opts, cb) {

packages/bitcore-wallet-client/test/paypro.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('paypro', function() {
3737
};
3838

3939
httpNode = {};
40-
httpNode.get = function(opts, cb) {
40+
httpNode.request = function(opts, cb) {
4141
var res = {};
4242
res.statusCode = httpNode.error || 200;
4343
if (httpNode.error == 404)

packages/bitcore-wallet-service/lib/storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ Storage.prototype.getTxHistoryCacheV8 = function(walletId, skip, limit, cb) {
818818
if (lastPosition <= 0)
819819
return cb(null, []);
820820

821-
console.log('[storage.js.750:first/lastPosition:]',firstPosition + '/'+lastPosition); //TODO
821+
//console.log('[storage.js.750:first/lastPosition:]',firstPosition + '/'+lastPosition); //TODO
822822

823823
self.db.collection(collections.CACHE).find({
824824
walletId: walletId,

packages/bitcore-wallet-service/scripts/v8tool

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Client = require('../lib/blockchainexplorers/v8/client.js');
88
const coin = 'BCH';
99
const network = 'mainnet';
1010
const authKey = process.argv[2];
11+
const path = process.argv[3] || 'addresses';
1112

1213
if (!authKey)
1314
throw "provide authKey"
@@ -32,7 +33,7 @@ let client = new Client({
3233
});
3334

3435

35-
const url = `${baseUrl}/wallet/${pubKey}/addresses`;
36+
const url = `${baseUrl}/wallet/${pubKey}/${path}`;
3637
const signature = client.sign({ method: 'GET', url });
3738
const payload = {};
3839

packages/bitcore-wallet/bin/cli-utils.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var _ = require('lodash');
22
var url = require('url');
33
var read = require('read')
44
var log = require('npmlog');
5-
var Client = require('../../bitcore-wallet-client');
5+
var Client = require('bitcore-wallet-client');
66
var FileStorage = require('./filestorage');
77
var sjcl = require('sjcl');
88

@@ -93,16 +93,18 @@ Utils.getClient = function(args, opts, cb) {
9393
opts = opts || {};
9494

9595
var filename = args.file || process.env['WALLET_FILE'] || process.env['HOME'] + '/.wallet.dat';
96-
var host = args.host || process.env['BWS_HOST'] || 'https://bws.bitpay.com/bws/api';
96+
var host = args.host || process.env['BWS_HOST'] || 'https://bws.bitpay.com/';
9797

9898
var storage = new FileStorage({
9999
filename: filename,
100100
});
101101

102102
var client = new Client({
103-
baseUrl: host,
103+
baseUrl: url.resolve(host, '/bws/api'),
104104
verbose: args.verbose,
105105
supportStaffWalletId: opts.walletId,
106+
timeout: 20 * 60 * 1000,
107+
//timeout: 1000,
106108
});
107109

108110
storage.load(function(err, walletData) {
@@ -213,7 +215,7 @@ Utils.findOneTxProposal = function(txps, id) {
213215
if (matches.length > 1) {
214216
console.log('More than one TX Proposals match:' + id);
215217
Utils.renderTxProposals(txps);
216-
program.exit(1);
218+
process.exit(1);
217219
}
218220

219221
return matches[0];
@@ -232,17 +234,23 @@ Utils.parseAmount = function(text) {
232234
var regex = '^(\\d*(\\.\\d{0,8})?)\\s*(' + _.keys(Utils.UNITS2).join('|') + ')?$';
233235
var match = new RegExp(regex, 'i').exec(text.trim());
234236

235-
if (!match || match.length === 0) throw new Error('Invalid amount');
237+
if (!match || match.length === 0) {
238+
Utils.die('Invalid amount: ' + text);
239+
}
236240

237241
var amount = parseFloat(match[1]);
238242
if (!_.isNumber(amount) || _.isNaN(amount)) throw new Error('Invalid amount');
239243

240244
var unit = (match[3] || 'sat').toLowerCase();
241245
var rate = Utils.UNITS2[unit];
242-
if (!rate) throw new Error('Invalid unit')
246+
if (!rate) {
247+
Utils.die('Invalid unit: ' + unit);
248+
}
243249

244250
var amountSat = parseFloat((amount * rate).toPrecision(12));
245-
if (amountSat != Math.round(amountSat)) throw new Error('Invalid amount');
251+
if (amountSat != Math.round(amountSat)) {
252+
Utils.die('Invalid amount: ' + amount + ' ' + unit);
253+
}
246254

247255
return amountSat;
248256
};
@@ -251,7 +259,7 @@ Utils.configureCommander = function(program) {
251259
program
252260
.version('0.0.1')
253261
.option('-f, --file <filename>', 'Wallet file')
254-
.option('-h, --host <host>', 'Bitcore Wallet Service URL (eg: http://localhost:3000/bws/api')
262+
.option('-h, --host <host>', 'Bitcore Wallet Service URL (eg: http://localhost:3001/copay/api')
255263
.option('-v, --verbose', 'be verbose')
256264

257265
return program;

packages/bitcore-wallet/bin/wallet-address

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
var program = require('commander');
4+
var bitcoreCash = require('bitcore-wallet-client').BitcoreCash;
45
var utils = require('./cli-utils');
56
program = utils.configureCommander(program);
67

@@ -13,6 +14,11 @@ utils.getClient(program, {
1314
}, function(client) {
1415
client.createAddress({}, function(err, x) {
1516
utils.die(err);
17+
18+
if (client.credentials.coin == 'bch') {
19+
x.address = (new bitcoreCash.Address(x.address)).toCashAddress();
20+
}
21+
1622
console.log('* New Address %s ', x.address);
1723
});
1824
});

packages/bitcore-wallet/bin/wallet-addresses

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var _ = require('lodash');
44
var program = require('commander');
5+
var bitcoreCash = require('bitcore-wallet-client').BitcoreCash;
56
var utils = require('./cli-utils');
67
program = utils.configureCommander(program);
78

@@ -19,6 +20,11 @@ utils.getClient(program, { mustExist: true }, function (client) {
1920
if (x.length > 0) {
2021
console.log('* Addresses:');
2122
_.each(x, function(a) {
23+
if (client.credentials.coin == 'bch') {
24+
a.address = (new bitcoreCash.Address(a.address)).toCashAddress();
25+
}
26+
27+
2228
console.log(' ', a.address);
2329
});
2430
} else {

packages/bitcore-wallet/bin/wallet-balance

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,24 @@ var utils = require('./cli-utils');
55
program = utils.configureCommander(program);
66
program
77
.option('-c, --coin <coin>', 'coin (btc/bch)')
8-
.option('-n, --network <network>', 'network of the coin')
9-
.option('-w, --wallet <walletId>', 'wallet id')
108
.parse(process.argv);
119

10+
11+
program.option = function(){};;
12+
1213
var args = program.args;
1314

1415

1516
var opts = {};
1617
if (program.coin) {
17-
opts.coin = program.coin;
18-
}
19-
20-
if (program.network) {
21-
opts.network = program.network;
22-
}
23-
24-
if(program.wallet) {
25-
opts.wallet = program.wallet;
18+
opts.coin = program.coin
2619
}
2720

2821
utils.getClient(program, {
2922
mustExist: true
3023
}, function(client) {
3124
client.getBalance(opts, function(err, x) {
3225
utils.die(err);
33-
console.log('* Wallet balance %s (Locked %s)', utils.renderAmount(x.totalAmount, opts.coin), utils.renderAmount(x.lockedAmount, opts.coin));
26+
console.log('* Wallet balance %s (Locked %s)', utils.renderAmount(x.totalAmount), utils.renderAmount(x.lockedAmount));
3427
});
3528
});

packages/bitcore-wallet/bin/wallet-broadcast

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ program = utils.configureCommander(program);
77

88
program
99
.usage('[options] <txpid>')
10-
.option('--path <path>', 'REQUIRED - Where wallet is stored')
1110
.parse(process.argv);
1211

1312
var args = program.args;

packages/bitcore-wallet/bin/wallet-create

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var utils = require('./cli-utils');
66
program = utils.configureCommander(program);
77

88
program
9-
.option('-n, --network <network>', 'Network for the wallet')
9+
.option('-t, --testnet', 'Create a Testnet Wallet')
1010
.option('-p, --password', 'Encrypt wallet. Will ask password interactively')
1111
.option('-c, --coin <coin>', 'coin (btc/bch)')
1212
.usage('[options] <walletName> <m-n> [copayerName] <passphrase>')
@@ -19,8 +19,8 @@ if (!args[0])
1919
var walletName = args[0];
2020
var copayerName = args[2] || process.env.USER;
2121
var passphrase = args[3];
22+
var network = program.testnet ? 'testnet' : 'livenet';
2223
var coin = program.coin ? program.coin : 'btc';
23-
var network = program.network ? program.network : 'livenet'
2424

2525
var mn;
2626
try {
@@ -38,9 +38,10 @@ utils.getClient(program, {
3838
language: 'en',
3939
coin: coin,
4040
});
41+
4142
client.createWallet(walletName, copayerName, mn[0], mn[1], {
4243
network: network,
43-
coin: coin
44+
coin: coin,
4445
}, function(err, secret) {
4546
utils.die(err);
4647
console.log(' * ' + _.capitalize(network) + ' Wallet Created.');

0 commit comments

Comments
 (0)