@bitpay-labs/bitcore-client 11.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +131 -0
- package/bin/multi-sig/wallet-derive-address +149 -0
- package/bin/wallet +31 -0
- package/bin/wallet-balance +62 -0
- package/bin/wallet-balance-all +77 -0
- package/bin/wallet-broadcast +35 -0
- package/bin/wallet-bump-tx +65 -0
- package/bin/wallet-check +62 -0
- package/bin/wallet-create +46 -0
- package/bin/wallet-decrypt +31 -0
- package/bin/wallet-delete +21 -0
- package/bin/wallet-derive +70 -0
- package/bin/wallet-flags +84 -0
- package/bin/wallet-import +80 -0
- package/bin/wallet-list +31 -0
- package/bin/wallet-paypro +175 -0
- package/bin/wallet-register +29 -0
- package/bin/wallet-send +146 -0
- package/bin/wallet-sign +64 -0
- package/bin/wallet-sign-message +55 -0
- package/bin/wallet-storage +79 -0
- package/bin/wallet-token +73 -0
- package/bin/wallet-token-rm +33 -0
- package/bin/wallet-tx +106 -0
- package/bin/wallet-tx-list +92 -0
- package/bin/wallet-utxos +28 -0
- package/package.json +71 -0
- package/src/client.ts +271 -0
- package/src/encryption.ts +139 -0
- package/src/index.ts +21 -0
- package/src/storage/level.ts +171 -0
- package/src/storage/mongo.ts +187 -0
- package/src/storage/textFile.ts +250 -0
- package/src/storage.ts +192 -0
- package/src/stream-util.ts +80 -0
- package/src/types/index.d.ts +2 -0
- package/src/types/storage.d.ts +1 -0
- package/src/types/wallet.d.ts +36 -0
- package/src/utils.ts +35 -0
- package/src/wallet.ts +983 -0
- package/test/unit/wallet.test.ts +552 -0
- package/ts_build/src/client.d.ts +77 -0
- package/ts_build/src/client.d.ts.map +1 -0
- package/ts_build/src/client.js +280 -0
- package/ts_build/src/client.js.map +1 -0
- package/ts_build/src/encryption.d.ts +27 -0
- package/ts_build/src/encryption.d.ts.map +1 -0
- package/ts_build/src/encryption.js +130 -0
- package/ts_build/src/encryption.js.map +1 -0
- package/ts_build/src/index.d.ts +11 -0
- package/ts_build/src/index.d.ts.map +1 -0
- package/ts_build/src/index.js +55 -0
- package/ts_build/src/index.js.map +1 -0
- package/ts_build/src/storage/level.d.ts +48 -0
- package/ts_build/src/storage/level.d.ts.map +1 -0
- package/ts_build/src/storage/level.js +194 -0
- package/ts_build/src/storage/level.js.map +1 -0
- package/ts_build/src/storage/mongo.d.ts +62 -0
- package/ts_build/src/storage/mongo.d.ts.map +1 -0
- package/ts_build/src/storage/mongo.js +173 -0
- package/ts_build/src/storage/mongo.js.map +1 -0
- package/ts_build/src/storage/textFile.d.ts +53 -0
- package/ts_build/src/storage/textFile.d.ts.map +1 -0
- package/ts_build/src/storage/textFile.js +268 -0
- package/ts_build/src/storage/textFile.js.map +1 -0
- package/ts_build/src/storage.d.ts +63 -0
- package/ts_build/src/storage.d.ts.map +1 -0
- package/ts_build/src/storage.js +172 -0
- package/ts_build/src/storage.js.map +1 -0
- package/ts_build/src/stream-util.d.ts +15 -0
- package/ts_build/src/stream-util.d.ts.map +1 -0
- package/ts_build/src/stream-util.js +81 -0
- package/ts_build/src/stream-util.js.map +1 -0
- package/ts_build/src/utils.d.ts +8 -0
- package/ts_build/src/utils.d.ts.map +1 -0
- package/ts_build/src/utils.js +37 -0
- package/ts_build/src/utils.js.map +1 -0
- package/ts_build/src/wallet.d.ts +229 -0
- package/ts_build/src/wallet.d.ts.map +1 -0
- package/ts_build/src/wallet.js +806 -0
- package/ts_build/src/wallet.js.map +1 -0
- package/ts_build/test/unit/wallet.test.d.ts +2 -0
- package/ts_build/test/unit/wallet.test.d.ts.map +1 -0
- package/ts_build/test/unit/wallet.test.js +533 -0
- package/ts_build/test/unit/wallet.test.js.map +1 -0
- package/tsconfig.json +40 -0
- package/tsconfig.prod.json +6 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
5
|
+
const promptly = require('promptly');
|
|
6
|
+
|
|
7
|
+
program
|
|
8
|
+
.version(require('../package.json').version)
|
|
9
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
10
|
+
.option('--storageType <storageType>', 'optional - name of the database to use (default Level)')
|
|
11
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
12
|
+
.parse(process.argv);
|
|
13
|
+
|
|
14
|
+
let wallet
|
|
15
|
+
|
|
16
|
+
const main = async () => {
|
|
17
|
+
const { name, path, storageType = 'Level' } = program.opts();
|
|
18
|
+
try {
|
|
19
|
+
const password = await promptly.password('Wallet Password:');
|
|
20
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
21
|
+
await wallet.unlock(password);
|
|
22
|
+
console.log(wallet);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
console.log(e);
|
|
25
|
+
console.error('Unable to decrypt wallet');
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
main()
|
|
30
|
+
.catch(console.error)
|
|
31
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
5
|
+
|
|
6
|
+
program
|
|
7
|
+
.version(require('../package.json').version)
|
|
8
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
|
|
9
|
+
.option('--storageType <storageType>', 'optional - name of the database to use (default level)')
|
|
10
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
11
|
+
.parse(process.argv);
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
const { name, path, storageType } = program.opts();
|
|
16
|
+
await Wallet.deleteWallet({ name, path, storageType });
|
|
17
|
+
console.log('Deleted');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main()
|
|
21
|
+
.catch(console.error);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const program = require('commander');
|
|
6
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
7
|
+
const promptly = require('promptly');
|
|
8
|
+
|
|
9
|
+
program
|
|
10
|
+
.version(require('../package.json').version)
|
|
11
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
12
|
+
.option('--gap <gap>', 'optional - UTXO chains only: Number of consecutive unused addresses to derive (will derive that many change addresses too) (default 20)')
|
|
13
|
+
.option('--noChange', 'optional - UTXO chains only: Should not derive change addresses')
|
|
14
|
+
.option('--startIdx <index>', 'optional - Starting address index. (default the wallet\'s next index)')
|
|
15
|
+
.option('--printIndex', 'optional - Print the wallet\'s next index (it will not derive addresses)')
|
|
16
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (default Level)')
|
|
17
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
18
|
+
.parse(process.argv);
|
|
19
|
+
|
|
20
|
+
let wallet;
|
|
21
|
+
|
|
22
|
+
const main = async () => {
|
|
23
|
+
const { name, path, storageType = 'Level', printIndex = false } = program.opts();
|
|
24
|
+
let { gap, startIdx, noChange = false } = program.opts();
|
|
25
|
+
|
|
26
|
+
if (printIndex) {
|
|
27
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
28
|
+
console.log('Next address index:', wallet.addressIndex);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const password = await promptly.password('Wallet Password:');
|
|
34
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
35
|
+
wallet = await wallet.unlock(password);
|
|
36
|
+
|
|
37
|
+
let unusedAddressCounter = 0;
|
|
38
|
+
|
|
39
|
+
let index = !isNaN(parseInt(startIdx)) ? parseInt(startIdx) : (wallet.addressIndex || 0);
|
|
40
|
+
gap = !isNaN(parseInt(gap)) ? parseInt(gap) : 20;
|
|
41
|
+
if (!wallet.isUtxoChain()) {
|
|
42
|
+
gap = 1;
|
|
43
|
+
noChange = true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
while (unusedAddressCounter < gap) {
|
|
47
|
+
const address = await wallet.generateAddressPair(index, !noChange);
|
|
48
|
+
|
|
49
|
+
const transactions = await wallet.client.getAddressTxos({ address });
|
|
50
|
+
|
|
51
|
+
if (!transactions.length || !wallet.isUtxoChain()) {
|
|
52
|
+
unusedAddressCounter++;
|
|
53
|
+
} else {
|
|
54
|
+
unusedAddressCounter = 0;
|
|
55
|
+
}
|
|
56
|
+
if (!noChange) {
|
|
57
|
+
console.log(`Current change index: ${index}: ${address}`);
|
|
58
|
+
} else {
|
|
59
|
+
console.log(`Current address index: ${index}: ${address}`);
|
|
60
|
+
}
|
|
61
|
+
index++;
|
|
62
|
+
}
|
|
63
|
+
} catch (e) {
|
|
64
|
+
console.error(e);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
main()
|
|
69
|
+
.catch(console.error)
|
|
70
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-flags
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const promptly = require('promptly');
|
|
5
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
6
|
+
|
|
7
|
+
function flagsDisplay() {
|
|
8
|
+
const flags = Object.keys(Wallet.XrpAccountFlags);
|
|
9
|
+
const half = flags.splice(flags.length / 2);
|
|
10
|
+
return 'Possible flags are: \n\t' + flags.map((f, i) => `${f} | ${half[i]}`).join('\n\t');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
program
|
|
14
|
+
.version(require('../package.json').version)
|
|
15
|
+
.description('Show or modify flag(s) on an XRP account wallet. Only one mod operation can be done at a time. \n' +
|
|
16
|
+
flagsDisplay() + '\n' +
|
|
17
|
+
'Examples: \n' +
|
|
18
|
+
'\t./wallet flags --name my-xrp-wallet \n' +
|
|
19
|
+
'\t./wallet flags --name my-xrp-wallet --set tfRequireDestTag \n' +
|
|
20
|
+
'\t./wallet flags --name my-xrp-wallet --set 65536'
|
|
21
|
+
)
|
|
22
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
|
|
23
|
+
.option('--set <flags>', 'optional - set wallet flag')
|
|
24
|
+
.option('--fee <fee>', 'optional - fee in drops')
|
|
25
|
+
.option('--index <index>', 'optional - index of the account (default: 0) (uncommon - only use this if you have multiple accounts)')
|
|
26
|
+
.option('--storageType <storageType>', 'optional - name of the database to use (default Level)')
|
|
27
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
28
|
+
.parse(process.argv);
|
|
29
|
+
|
|
30
|
+
let wallet;
|
|
31
|
+
|
|
32
|
+
const main = async () => {
|
|
33
|
+
const { name, set, index, path, storageType = 'Level' } = program.opts();
|
|
34
|
+
let { fee } = program.opts();
|
|
35
|
+
|
|
36
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
37
|
+
if (wallet.chain.toLowerCase() !== 'xrp') {
|
|
38
|
+
console.log('Wallet is not an XRP wallet');
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const account = wallet.deriveAddress(index || 0);
|
|
42
|
+
|
|
43
|
+
if (!set) {
|
|
44
|
+
const flags = await wallet.getAccountFlags({ index });
|
|
45
|
+
console.log(`Flags for account ${account}:`, flags);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!Wallet.XrpAccountFlags[set]) {
|
|
50
|
+
console.log('Invalid flag to set: ' + set);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
console.log('Setting flag ' + (isNaN(set) ? set : Wallet.XrpAccountFlags[set]) + ' on account ' + account);
|
|
54
|
+
|
|
55
|
+
const nonce = parseInt(await wallet.getNonce());
|
|
56
|
+
|
|
57
|
+
if (!fee) {
|
|
58
|
+
const feerate = await wallet.getNetworkFee();
|
|
59
|
+
fee = feerate.feerate;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const tx = await wallet.newTx({
|
|
63
|
+
type: 'AccountSet',
|
|
64
|
+
from: account,
|
|
65
|
+
flags: set,
|
|
66
|
+
nonce,
|
|
67
|
+
fee
|
|
68
|
+
});
|
|
69
|
+
const passphrase = await promptly.password('Wallet Password:');
|
|
70
|
+
await wallet.unlock(passphrase);
|
|
71
|
+
const signedTx = await wallet.signTx({ tx, passphrase });
|
|
72
|
+
console.log('SignedRawTx: ', signedTx);
|
|
73
|
+
const confirmed = await promptly.confirm('Broadcast? (y/n)');
|
|
74
|
+
if (!confirmed) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const transaction = await wallet.broadcast({ tx: signedTx });
|
|
78
|
+
console.log('Txid: ', transaction.txid);
|
|
79
|
+
console.log('Transaction broadcasted');
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
main()
|
|
83
|
+
.catch(console.error)
|
|
84
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const program = require('commander');
|
|
6
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
7
|
+
const promptly = require('promptly');
|
|
8
|
+
const { bitcoinCoreDecrypt } = require('../ts_build/src/encryption');
|
|
9
|
+
|
|
10
|
+
program
|
|
11
|
+
.version(require('../package.json').version)
|
|
12
|
+
.description('Imports keys/addresses from given --json or --file. If neither is given, this will simply sync the wallet\'s addresses with the server')
|
|
13
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
14
|
+
.option('--json <json>', 'optional - A json string to parse in the form {"address": "mXy1234", "privKey": "xxxxxxx", "pubKey": "yyyyyyyy"}')
|
|
15
|
+
.option('--file <file>', 'optional - A jsonl file with {"address": "mXy1234", "privKey": "xxxxxxx", "pubKey": "yyyyyyyy"} in it')
|
|
16
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
17
|
+
.option('--encrypted', 'optional - Bitcoin Core specific keys in which case we need to decrypt them before import')
|
|
18
|
+
.option('--rederive', 'optional - Use the pub keys to derive the addresses to match the wallet\'s addressType')
|
|
19
|
+
.option('--storageType <storageType>', 'optional - Name of database (default Level)')
|
|
20
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
21
|
+
.parse(process.argv);
|
|
22
|
+
|
|
23
|
+
const getFileKeys = file => {
|
|
24
|
+
let contents = fs.readFileSync(file).toString();
|
|
25
|
+
let fileKeys = contents
|
|
26
|
+
.split('\n')
|
|
27
|
+
.filter(line => line !== '')
|
|
28
|
+
.map(line => JSON.parse(line));
|
|
29
|
+
return fileKeys;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const parseKeys = keys => {
|
|
33
|
+
let parsed = JSON.parse(keys);
|
|
34
|
+
if (typeof parsed === 'object') {
|
|
35
|
+
if (parsed instanceof Array === false) {
|
|
36
|
+
return [parsed];
|
|
37
|
+
} else {
|
|
38
|
+
return parsed;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
let wallet;
|
|
44
|
+
|
|
45
|
+
const main = async () => {
|
|
46
|
+
const { name, path, file, json, storageType, encrypted, rederive } = program.opts();
|
|
47
|
+
try {
|
|
48
|
+
const password = await promptly.password('Wallet Password:');
|
|
49
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
50
|
+
wallet = await wallet.unlock(password);
|
|
51
|
+
let keyInput = [];
|
|
52
|
+
if (file) {
|
|
53
|
+
keyInput = getFileKeys(file);
|
|
54
|
+
} else if (json) {
|
|
55
|
+
keyInput = parseKeys(json);
|
|
56
|
+
}
|
|
57
|
+
if (encrypted) {
|
|
58
|
+
try {
|
|
59
|
+
const bitcoinCorePassword = await promptly.password('Enter Bitcoin Core Password:');
|
|
60
|
+
let decryptedParams = bitcoinCoreDecrypt(keyInput, bitcoinCorePassword);
|
|
61
|
+
keyInput = decryptedParams.jsonlDecrypted;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.error(e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (keyInput.length) {
|
|
67
|
+
await wallet.importKeys({ keys: keyInput, rederiveAddys: rederive });
|
|
68
|
+
console.log(`${keyInput.length} Key(s) Imported`);
|
|
69
|
+
} else {
|
|
70
|
+
const res = await wallet.syncAddresses(true);
|
|
71
|
+
console.log(res);
|
|
72
|
+
}
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error(e);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
main()
|
|
79
|
+
.catch(console.error)
|
|
80
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-list
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const program = require('commander');
|
|
5
|
+
const { Storage } = require('../ts_build/src/storage');
|
|
6
|
+
|
|
7
|
+
program
|
|
8
|
+
.version(require('../package.json').version)
|
|
9
|
+
.option('--chain <chain>', 'optional - filter the wallets by chain')
|
|
10
|
+
.option('--network <network>', 'optional - filter the wallets by network')
|
|
11
|
+
.option('--storageType <storageType>', 'optional - choose the database from where to list wallets')
|
|
12
|
+
.option('--path <path>', 'optional - where wallets are stored')
|
|
13
|
+
.parse(process.argv);
|
|
14
|
+
|
|
15
|
+
const main = async () => {
|
|
16
|
+
const { path, storageType, chain: chainParam, network: networkParam } = program.opts();
|
|
17
|
+
const storage = new Storage({ createIfMissing: false, errorIfExists: false, path, storageType });
|
|
18
|
+
const walletStream = await storage.listWallets();
|
|
19
|
+
walletStream.on('data', (wallet) => {
|
|
20
|
+
const { name, chain, network, baseUrl, storageType, tokens } = JSON.parse(wallet.toString());
|
|
21
|
+
if (chainParam && chainParam.toUpperCase() !== chain.toUpperCase()) return;
|
|
22
|
+
if (networkParam && networkParam.toUpperCase() !== network.toUpperCase()) return;
|
|
23
|
+
let walletObj = { name, chain, network, baseUrl, storageType };
|
|
24
|
+
if (tokens && tokens.length) {
|
|
25
|
+
walletObj.tokens = tokens;
|
|
26
|
+
}
|
|
27
|
+
console.log(walletObj);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
main();
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
5
|
+
const request = require('request-promise-native');
|
|
6
|
+
const promptly = require('promptly');
|
|
7
|
+
const cwc = require('@bitpay-labs/crypto-wallet-core');
|
|
8
|
+
const libs = {
|
|
9
|
+
BTC: cwc.BitcoreLib,
|
|
10
|
+
BCH: cwc.BitcoreLibCash,
|
|
11
|
+
DOGE: cwc.BitcoreLibDoge,
|
|
12
|
+
LTC: cwc.BitcoreLibLtc
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
program
|
|
16
|
+
.version(require('../package.json').version)
|
|
17
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
18
|
+
.requiredOption('--payProUri <payProUri>', 'REQUIRED - payment protocol URI')
|
|
19
|
+
.option('--nonce <nonce>', 'optional - Nonce')
|
|
20
|
+
.option('--from <from>', 'optional - From account for XRP')
|
|
21
|
+
.option('--token, <token>', 'optional - ERC-20 token to pay with')
|
|
22
|
+
.option('--storageType <storageType>', 'optional - name of the database to use (Mongo | Level | TextFile)')
|
|
23
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
24
|
+
.parse(process.argv);
|
|
25
|
+
|
|
26
|
+
let wallet;
|
|
27
|
+
|
|
28
|
+
const main = async () => {
|
|
29
|
+
const { name, path, token, nonce: inputNonce, from: inputFrom, storageType } = program.opts();
|
|
30
|
+
let { payProUri } = program.opts();
|
|
31
|
+
if (payProUri.includes(':?r=')) {
|
|
32
|
+
const split = payProUri.split(':?r=');
|
|
33
|
+
console.log(split.length);
|
|
34
|
+
payProUri = split[split.length - 1];
|
|
35
|
+
}
|
|
36
|
+
// Payment-Options
|
|
37
|
+
const paymentOptionsResponse = await doRequest({
|
|
38
|
+
method: 'GET',
|
|
39
|
+
uri: payProUri,
|
|
40
|
+
headers: {
|
|
41
|
+
'Accept' : 'application/payment-options',
|
|
42
|
+
'x-paypro-version' : '2'
|
|
43
|
+
},
|
|
44
|
+
agentOptions: {
|
|
45
|
+
rejectUnauthorized: false
|
|
46
|
+
},
|
|
47
|
+
json: true
|
|
48
|
+
});
|
|
49
|
+
let scaleMap = {};
|
|
50
|
+
const { expires, memo, paymentOptions } = paymentOptionsResponse;
|
|
51
|
+
const supportedCurrenciesArray = [];
|
|
52
|
+
console.log(memo);
|
|
53
|
+
console.log(`Payment expires at ${expires}`);
|
|
54
|
+
paymentOptions.forEach((paymentOption, index) => {
|
|
55
|
+
const { currency, network, estimatedAmount, requiredFeeRate, minerFee, decimals, selected } = paymentOption;
|
|
56
|
+
supportedCurrenciesArray.push(currency);
|
|
57
|
+
scaleMap[currency] = Math.pow(10, decimals);
|
|
58
|
+
const amount = estimatedAmount / scaleMap[currency];
|
|
59
|
+
console.log(`${index + 1}: ${amount} ${currency} (${network})`);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Payment-Request
|
|
63
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
64
|
+
const from = inputFrom || await wallet.deriveAddress(0);
|
|
65
|
+
let nonce;
|
|
66
|
+
const { chain } = wallet;
|
|
67
|
+
if (!['BTC', 'BCH', 'DOGE', 'LTC', 'SOL'].includes(chain)) {
|
|
68
|
+
nonce = inputNonce || await wallet.getNonce();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const currency = token || paymentOptions.find(o => o.chain === chain && o.native)?.currency;
|
|
72
|
+
if (!currency) {
|
|
73
|
+
throw new Error(`No supported currency for ${chain}. Try specifying --token`);
|
|
74
|
+
}
|
|
75
|
+
console.log(`selecting ${currency}`);
|
|
76
|
+
const paymentRequestResponse = await doRequest({
|
|
77
|
+
method: 'POST',
|
|
78
|
+
uri: payProUri,
|
|
79
|
+
json: { chain, currency },
|
|
80
|
+
headers: {
|
|
81
|
+
'Content-Type' : 'application/payment-request',
|
|
82
|
+
'x-paypro-version' : '2'
|
|
83
|
+
},
|
|
84
|
+
agentOptions: {
|
|
85
|
+
rejectUnauthorized: false
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// create transaction
|
|
90
|
+
const { instructions } = paymentRequestResponse;
|
|
91
|
+
console.log(JSON.stringify(instructions, null, 2));
|
|
92
|
+
let unsignedTransactions = [];
|
|
93
|
+
let signedTransactions = [];
|
|
94
|
+
let password;
|
|
95
|
+
for (const instruction of instructions) {
|
|
96
|
+
const { type, requiredFeeRate, outputs, gasPrice, to, value, data } = instruction;
|
|
97
|
+
const recipients = [{
|
|
98
|
+
address: to || outputs[0].address,
|
|
99
|
+
amount: value >= 0 ? value : outputs[0].amount
|
|
100
|
+
}];
|
|
101
|
+
const feeRate = requiredFeeRate || gasPrice;
|
|
102
|
+
let invoiceID;
|
|
103
|
+
if (outputs) {
|
|
104
|
+
invoiceID = outputs[0].invoiceID;
|
|
105
|
+
}
|
|
106
|
+
const utxos = await wallet.getUtxosArray();
|
|
107
|
+
const params = { recipients, feeRate, from, invoiceID, memo: invoiceID, data, value, utxos, gasPrice, tokenName: token };
|
|
108
|
+
params.nonce = Number(nonce);
|
|
109
|
+
params.change = !wallet.isUtxoChain() ? null : wallet.deriveAddress(wallet.addressIndex, true);
|
|
110
|
+
const changeIdx = params.change ? wallet.addressIndex : null;
|
|
111
|
+
const unsignedTx = await wallet.newTx(params);
|
|
112
|
+
console.log(`unsignedRawTx: ${unsignedTx}`);
|
|
113
|
+
|
|
114
|
+
// Sign tx
|
|
115
|
+
if (!password) {
|
|
116
|
+
password = await promptly.password('Wallet Password:');
|
|
117
|
+
}
|
|
118
|
+
wallet = await wallet.unlock(password);
|
|
119
|
+
const signedTx = await wallet.signTx({ tx: unsignedTx, passphrase: password, changeAddressIdx: changeIdx });
|
|
120
|
+
const txid = cwc.Transactions.getHash({ chain, tx: signedTx });
|
|
121
|
+
console.log(`signedRawTx: ${signedTx}`);
|
|
122
|
+
signedTransactions.push({ tx: signedTx });
|
|
123
|
+
console.log(`txid: ${txid}`);
|
|
124
|
+
|
|
125
|
+
let weightedSize;
|
|
126
|
+
if (['BTC', 'BCH', 'DOGE', 'LTC'].includes(chain)) {
|
|
127
|
+
const transaction = new libs[chain].Transaction(signedTx);
|
|
128
|
+
weightedSize = transaction._estimateSize();
|
|
129
|
+
}
|
|
130
|
+
unsignedTransactions.push({ tx: unsignedTx, weightedSize })
|
|
131
|
+
nonce++;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Payment Verification
|
|
135
|
+
const paymentVerificationResponse = await doRequest({
|
|
136
|
+
method: 'POST',
|
|
137
|
+
uri: payProUri,
|
|
138
|
+
json: { chain, currency, transactions: unsignedTransactions },
|
|
139
|
+
headers: {
|
|
140
|
+
'Content-Type' : 'application/payment-verification',
|
|
141
|
+
'x-paypro-version' : '2'
|
|
142
|
+
},
|
|
143
|
+
agentOptions: {
|
|
144
|
+
rejectUnauthorized: false
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
console.log(paymentVerificationResponse);
|
|
148
|
+
|
|
149
|
+
// send-payment
|
|
150
|
+
const sendPaymentResponse = await doRequest({
|
|
151
|
+
method: 'POST',
|
|
152
|
+
uri: payProUri,
|
|
153
|
+
json: { chain, currency, transactions: signedTransactions },
|
|
154
|
+
headers: {
|
|
155
|
+
'Content-Type' : 'application/payment',
|
|
156
|
+
'x-paypro-version' : '2'
|
|
157
|
+
},
|
|
158
|
+
agentOptions: {
|
|
159
|
+
rejectUnauthorized: false
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
console.log(sendPaymentResponse);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const doRequest = async (...opts) => {
|
|
166
|
+
try {
|
|
167
|
+
return await request(...opts);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
throw new Error(e.stack || e.message || e);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
main()
|
|
174
|
+
.catch(console.error)
|
|
175
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
5
|
+
|
|
6
|
+
program
|
|
7
|
+
.version(require('../package.json').version)
|
|
8
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
9
|
+
.option('--baseUrl <url>', 'optional - Change configured wallet baseUrl')
|
|
10
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (default: Level)')
|
|
11
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
12
|
+
.parse(process.argv);
|
|
13
|
+
|
|
14
|
+
let wallet;
|
|
15
|
+
|
|
16
|
+
const main = async () => {
|
|
17
|
+
const { name, path, baseUrl, storageType } = program.opts();
|
|
18
|
+
try {
|
|
19
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
20
|
+
await wallet.register({ baseUrl });
|
|
21
|
+
console.log('Wallet registered!');
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
main()
|
|
28
|
+
.catch(console.error)
|
|
29
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-send
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const program = require('commander');
|
|
6
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
7
|
+
const { utils } = require('../ts_build/src/utils');
|
|
8
|
+
const promptly = require('promptly');
|
|
9
|
+
const { Web3 } = require('@bitpay-labs/crypto-wallet-core');
|
|
10
|
+
|
|
11
|
+
program
|
|
12
|
+
.version(require('../package.json').version)
|
|
13
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
14
|
+
.requiredOption('--to <to>', 'REQUIRED - String address')
|
|
15
|
+
.requiredOption('--amount <amount>', 'REQUIRED - Number amount (in btc/bch/eth/xrp). \'max\' to send all')
|
|
16
|
+
.option('--token <tokenName>', 'Name of ERC-20 token to send')
|
|
17
|
+
.option('--feeRate <rate>', 'Fee rate in sats/byte (UTXO), Gwei (EVM), or drops (XRP)')
|
|
18
|
+
.option('--rbf', 'Enable Replace-by-Fee')
|
|
19
|
+
.option('--tag <tag>', 'Destination tag (XRP only)')
|
|
20
|
+
.option('--account <index>', 'EVM Account index to use (default 0)')
|
|
21
|
+
.option('--storageType <storageType>', 'Name of the database to use (default Level)')
|
|
22
|
+
.option('--path <path>', 'Custom wallet storage path')
|
|
23
|
+
.option('--blockHash <blockHash>', 'Recent hash (SOL only)')
|
|
24
|
+
.option('--blockNumber <blockNumber>', 'Recent block number (SOL only)')
|
|
25
|
+
.parse(process.argv);
|
|
26
|
+
|
|
27
|
+
let wallet;
|
|
28
|
+
|
|
29
|
+
const main = async () => {
|
|
30
|
+
const { name, path, to, amount, token: tokenName, storageType, rbf, tag, account = 0, blockHash, blockNumber } = program.opts();
|
|
31
|
+
let { feeRate: feeRateParam } = program.opts();
|
|
32
|
+
try {
|
|
33
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
34
|
+
const from = await wallet.deriveAddress(account); // only used for non-UTXO chains
|
|
35
|
+
const chain = wallet.chain;
|
|
36
|
+
const tokenObj = wallet.getTokenObj({ tokenName }); // null if no tokenName is provided
|
|
37
|
+
const currencyObj = await utils.getCurrencyObj(chain, wallet.isEvmChain() ? tokenObj?.address : null, !wallet.isMainnet());
|
|
38
|
+
const decimals = tokenObj?.decimals || currencyObj?.decimals;
|
|
39
|
+
const scale = Math.pow(10, decimals);
|
|
40
|
+
const recipients = [];
|
|
41
|
+
const sendMax = amount === 'max';
|
|
42
|
+
if (isNaN(amount) && !sendMax) {
|
|
43
|
+
throw new Error('Amount must be a number or \'max\'');
|
|
44
|
+
}
|
|
45
|
+
if (!sendMax || !wallet.isUtxoChain()) {
|
|
46
|
+
recipients.push({ address: to, amount: amount * scale });
|
|
47
|
+
}
|
|
48
|
+
const utxos = await wallet.getUtxosArray();
|
|
49
|
+
const params = {
|
|
50
|
+
utxos,
|
|
51
|
+
recipients: [],
|
|
52
|
+
from,
|
|
53
|
+
tokenName,
|
|
54
|
+
replaceByFee: rbf,
|
|
55
|
+
tag,
|
|
56
|
+
isSweep: sendMax
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
let feeRate = feeRateParam;
|
|
60
|
+
if (!feeRate) {
|
|
61
|
+
const feerate = await wallet.getNetworkFee();
|
|
62
|
+
feeRate = feerate.feerate;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (wallet.chain.toUpperCase() === 'XRP') {
|
|
66
|
+
params.nonce = parseInt(await wallet.getNonce());
|
|
67
|
+
params.fee = feeRateParam == null ? feeRate : parseInt(feeRateParam);
|
|
68
|
+
console.log('Fee:', params.fee, 'drops'); // XRP doesn't really have a fee "rate"
|
|
69
|
+
} else if (wallet.isUtxoChain()) {
|
|
70
|
+
feeRate = feeRate ?? 1e-8; // default to 1 sat/byte in case wallet.getNetworkFee() returns null
|
|
71
|
+
params.feeRate = Math.ceil(feeRateParam == null ? (feeRate * scale) : feeRateParam); // convert feeRate from BTC to sats/B
|
|
72
|
+
console.log('Fee rate:', params.feeRate, 'sats/byte');
|
|
73
|
+
} else if (wallet.chain.toUpperCase() === 'SOL') {
|
|
74
|
+
params.blockHash = blockHash;
|
|
75
|
+
params.blockHeight = blockNumber;
|
|
76
|
+
} else {
|
|
77
|
+
params.nonce = parseInt(await wallet.getNonce());
|
|
78
|
+
params.feeRate = feeRateParam == null ? feeRate : parseInt(Web3.utils.toWei(feeRateParam.toString(), 'gwei')); // convert feeRateParam from Gwei to Wei
|
|
79
|
+
console.log('Fee rate:', Web3.utils.fromWei(params.feeRate.toString(), 'Gwei'), 'Gwei');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!sendMax) {
|
|
83
|
+
params.recipients.push({ address: to, amount: amount * scale });
|
|
84
|
+
} else {
|
|
85
|
+
// Send max amount
|
|
86
|
+
if (wallet.isUtxoChain()) {
|
|
87
|
+
params.change = to; // send all
|
|
88
|
+
} else if (wallet.isEvmChain()) {
|
|
89
|
+
const balance = await wallet.getBalance({ tokenName, address: from, hex: true });
|
|
90
|
+
const tx = await wallet.newTx({ ...params, recipients: [{ address: to, amount: 1 }] }); // create dummy tx to estimate gas
|
|
91
|
+
const { data } = wallet.getLib().ethers.utils.parseTransaction(tx);
|
|
92
|
+
const gasLimit = await wallet.estimateGas({ to, from, data, value: BigInt(balance.confirmed).toString() });
|
|
93
|
+
const l1FeeWei = await wallet.getL1Fee(tx); // OP-stack chains have a "hidden" layer-1 data fee (in wei). Returns 0 for non-OP-stack chains.
|
|
94
|
+
const amtToSend = (BigInt(balance.confirmed) - (BigInt(gasLimit) * BigInt(params.feeRate)) - BigInt(l1FeeWei)).toString();
|
|
95
|
+
params.recipients.push({ address: to, amount: amtToSend });
|
|
96
|
+
params.gasLimit = gasLimit;
|
|
97
|
+
} else if (wallet.chain.toUpperCase() === 'XRP') {
|
|
98
|
+
const delWallet = await promptly.confirm('Would you like to send the reserve and delete your XRP wallet from the blockchain? (y/N)', { default: 'n' });
|
|
99
|
+
if (delWallet) {
|
|
100
|
+
const baseUrl = wallet.client.apiUrl;
|
|
101
|
+
const tip = JSON.parse(await wallet.client._request({ url: baseUrl + '/block/tip', method: 'GET' }));
|
|
102
|
+
if (params.nonce + 256 >= tip.height) {
|
|
103
|
+
throw new Error(`Cannot delete wallet: wallet sequence needs to be at least 256 less than the current ledger index. Current sequence: ${sequence}, current ledger: ${tip.height}`);
|
|
104
|
+
}
|
|
105
|
+
params.recipients = [{ address: to, amount: null }];
|
|
106
|
+
params.fee = 2 * scale; // 2 XRP fee for AccountDelete
|
|
107
|
+
params.type = 'AccountDelete';
|
|
108
|
+
} else {
|
|
109
|
+
const balance = await wallet.getBalance();
|
|
110
|
+
params.recipients = [{ address: to, amount: balance.confirmed }];
|
|
111
|
+
console.log(`Sending ${balance.confirmed / scale} XRP to ${to}`);
|
|
112
|
+
}
|
|
113
|
+
} else if (wallet.isSolanaChain()) {
|
|
114
|
+
const balance = await wallet.getBalance();
|
|
115
|
+
params.recipients = [{ address: to, amount: balance.confirmed - feeRate }];
|
|
116
|
+
console.log(`Sending ${(balance.confirmed - feeRate) / scale} SOL to ${to}`);
|
|
117
|
+
} else {
|
|
118
|
+
throw new Error(`sendMax not supported for ${wallet.chain}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
params.change = wallet.isUtxoChain() ? wallet.deriveAddress(wallet.addressIndex, true) : null;
|
|
124
|
+
const changeIdx = params.change ? wallet.addressIndex : null;
|
|
125
|
+
const tx = await wallet.newTx(params);
|
|
126
|
+
console.log('UnsignedRawTx: ', tx);
|
|
127
|
+
const passphrase = await promptly.password('Wallet Password:');
|
|
128
|
+
wallet = await wallet.unlock(passphrase);
|
|
129
|
+
const signedTx = await wallet.signTx({ tx, passphrase, changeAddressIdx: changeIdx });
|
|
130
|
+
console.log('SignedRawTx: ', signedTx);
|
|
131
|
+
const broadcast = await promptly.confirm('Broadcast? (y/n)');
|
|
132
|
+
if (!broadcast) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const transaction = await wallet.broadcast({ tx: signedTx });
|
|
136
|
+
console.log('Txid: ', transaction.txid);
|
|
137
|
+
console.log('Transaction broadcasted');
|
|
138
|
+
} catch (e) {
|
|
139
|
+
console.error(e);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
main()
|
|
145
|
+
.catch(console.error)
|
|
146
|
+
.finally(() => wallet?.storage?.close());
|