Skip to content

Commit a7443d1

Browse files
author
Darren Nelsen
committed
merge changes from v8 branch
2 parents c747dde + 4be78d5 commit a7443d1

File tree

36 files changed

+8748
-8645
lines changed

36 files changed

+8748
-8645
lines changed

packages/bitcore-client/bin/wallet-tx

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,42 @@ program
88
.option('--name <name>', 'REQUIRED - Wallet Name')
99
.option(
1010
'--recipients <recipients>',
11-
'REQUIRED - Address of receiver, or json array \'[{"address": "abc1234", "amount": xxxxxx}]\''
11+
'REQUIRED - JSON array \'[{"address": "abc1234", "amount": xxxxxx}]\''
1212
)
13-
.option('--fee <fee>', 'REQUIRED - The transaction fee to pay')
1413
.option('--utxos <utxos>', 'REQUIRED - Unspent transactions that can be spent')
1514
.option('--change <change>', 'REQUIRED - Change addresses')
15+
.option('--fee [fee]', 'optional - custom transaction fee to pay')
16+
.option('--target [target]', 'optional - custom target block for confirmation')
1617
.option('--path [path]', 'optional - Custom wallet storage path')
1718
.parse(process.argv);
1819

1920
const main = async () => {
2021
const { name, path } = program;
2122
let wallet;
2223
try {
23-
const { recipients, utxos, change, fee } = program;
24+
const { recipients, utxos, change, fee, target } = program;
2425
wallet = await Wallet.loadWallet({ name, path });
26+
27+
const parsedUtxos = utxos.includes('[') ? JSON.parse(utxos) : [];
28+
const parsedRecipients = recipients.includes('[') ? JSON.parse(recipients) : [];
29+
30+
if (!parsedRecipients.length || !parsedUtxos.length) {
31+
throw new Error('invalid arguments');
32+
}
33+
34+
const utxoBytes = 148;
35+
const outputBytes = 34;
36+
const transactionHeader = 10;
37+
const calculatedNetworkFeeKb = await wallet.getNetworkFee({ target });
38+
const netWorkFeeSat = calculatedNetworkFeeKb.feerate * 1E8/1000;
39+
40+
const transactionSize = (parsedUtxos.length * utxoBytes + parsedRecipients.length * outputBytes + transactionHeader + parsedUtxos.length);
41+
const totalTransactionFee = transactionSize * netWorkFeeSat;
42+
2543
let params = { recipients, utxos, change, fee };
26-
params.recipients = recipients.includes('[') ? JSON.parse(recipients) : recipients;
27-
params.utxos = params.utxos ? JSON.parse(params.utxos) : [];
44+
params.fee = Number(fee) || totalTransactionFee;
45+
params.recipients = parsedRecipients;
46+
params.utxos = parsedUtxos;
2847
const tx = await wallet.newTx(params);
2948
console.log(tx.uncheckedSerialize());
3049
} catch (e) {

packages/bitcore-client/lib/client.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ Client.prototype.getCoins = async function (params) {
5959
});
6060
};
6161

62+
Client.prototype.getFee = async function (params) {
63+
const { target } = params;
64+
const url = `${this.baseUrl}/fee/${target}`;
65+
return request.get(url, {
66+
json: true
67+
});
68+
}
69+
6270
Client.prototype.importAddresses = async function(params) {
63-
const { payload, pubKey } = params;
71+
const { payload, pubKey } = params;
6472
const url = `${this.baseUrl}/wallet/${pubKey}`;
6573
const signature = this.sign({ method: 'POST', url, payload});
6674

packages/bitcore-client/lib/wallet.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Wallet {
1111
if (!this.masterKey) {
1212
return new Wallet(this.create(params));
1313
}
14-
this.baseUrl = this.baseUrl || `http://127.0.0.1:3000/api/${this.chain}/${this.network}`;
14+
this.baseUrl = this.baseUrl || `https://api.bitcore.io/api/${this.chain}/${this.network}`;
1515
this.client = new Client({
1616
baseUrl: this.baseUrl,
1717
authKey: this.getAuthSigningKey()
@@ -129,6 +129,11 @@ class Wallet {
129129
return this.client.getBalance({ pubKey: this.xPubKey});
130130
}
131131

132+
getNetworkFee(params) {
133+
const target = params.target || 2;
134+
return this.client.getFee({ target });
135+
}
136+
132137
getUtxos() {
133138
return this.client.getCoins({
134139
pubKey: this.xPubKey,

0 commit comments

Comments
 (0)