@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
package/bin/wallet-sign
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const program = require('commander');
|
|
5
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
6
|
+
const promptly = require('promptly');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const { StreamUtil } = require('../ts_build/src/stream-util');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* USAGE -
|
|
12
|
+
* Pass in a raw transaction or a jsonl file of rawTransactions
|
|
13
|
+
* Pass in the bitcore wallet name or a jsonl file of encrypted bitcoin core keys or
|
|
14
|
+
* Pass in the utxos if you want to use specific utxos
|
|
15
|
+
*/
|
|
16
|
+
program
|
|
17
|
+
.version(require('../package.json').version)
|
|
18
|
+
.requiredOption('--name <name>', 'REQUIRED - Bitcore Wallet Name')
|
|
19
|
+
.option('--tx <tx>', 'The raw transaction to sign')
|
|
20
|
+
.option('--txfile <txfile>', 'Path to a jsonl file with rawTransaction and utxos')
|
|
21
|
+
.option('--utxos <utxos>', 'Array of utxos to use to sign transaction')
|
|
22
|
+
.option('--keys <keys>', 'jsonl file of {privKey, pubKey, address} pairs')
|
|
23
|
+
.option('--storageType <storageType>', 'optional - Name of the database (default - level)')
|
|
24
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
25
|
+
.parse(process.argv);
|
|
26
|
+
|
|
27
|
+
let wallet;
|
|
28
|
+
|
|
29
|
+
const main = async () => {
|
|
30
|
+
const { name, path, tx, txfile, keys, utxos, storageType } = program.opts();
|
|
31
|
+
let encryptedKeys;
|
|
32
|
+
let bitcoinCorePassword;
|
|
33
|
+
try {
|
|
34
|
+
const password = await promptly.password('Wallet Password:');
|
|
35
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
36
|
+
wallet = await wallet.unlock(password);
|
|
37
|
+
if (keys) {
|
|
38
|
+
let contents = fs.readFileSync(keys).toString();
|
|
39
|
+
encryptedKeys = contents
|
|
40
|
+
.split('\n')
|
|
41
|
+
.filter(line => line !== '')
|
|
42
|
+
.map(line => JSON.parse(line));
|
|
43
|
+
bitcoinCorePassword = await promptly.password('Enter Bitcoin Core Password:');
|
|
44
|
+
}
|
|
45
|
+
if (tx) {
|
|
46
|
+
let signedTx = await wallet.signTx({ tx, keys: encryptedKeys, utxos, passphrase: bitcoinCorePassword });
|
|
47
|
+
console.log(JSON.stringify(signedTx));
|
|
48
|
+
}
|
|
49
|
+
if (txfile) {
|
|
50
|
+
const readStream = fs.createReadStream(txfile, {flags: 'r', encoding: 'utf8'});
|
|
51
|
+
readStream
|
|
52
|
+
.pipe(StreamUtil.jsonlBufferToObjectMode()).on('error', console.error)
|
|
53
|
+
.pipe(StreamUtil.signTxStream(wallet, encryptedKeys, utxos, bitcoinCorePassword)).on('error', console.error)
|
|
54
|
+
.pipe(StreamUtil.objectModeToJsonlBuffer()).on('error', console.error)
|
|
55
|
+
.pipe(process.stdout).on('error', console.error);
|
|
56
|
+
}
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.error(e);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
main()
|
|
63
|
+
.catch(console.error)
|
|
64
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
const { BitcoreLib } = require('@bitpay-labs/crypto-wallet-core');
|
|
9
|
+
|
|
10
|
+
program
|
|
11
|
+
.version(require('../package.json').version)
|
|
12
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
13
|
+
.requiredOption('--index <index>', 'REQUIRED - Address index to sign message with')
|
|
14
|
+
.requiredOption('--message <message>', 'REQUIRED - message to be signed')
|
|
15
|
+
.option('--change', 'optional - If the address is a change address')
|
|
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, index, message, change, storageType } = program.opts();
|
|
24
|
+
try {
|
|
25
|
+
const password = await promptly.password('Wallet Password:');
|
|
26
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
27
|
+
wallet = await wallet.unlock(password);
|
|
28
|
+
|
|
29
|
+
const privateKey = await wallet.derivePrivateKey(change, index);
|
|
30
|
+
let signature;
|
|
31
|
+
let address;
|
|
32
|
+
if (wallet.chain === 'SOL') {
|
|
33
|
+
signature = await wallet.solSignMessage(privateKey.privKey, message);
|
|
34
|
+
address = wallet.deriveAddress(0, change);
|
|
35
|
+
} else {
|
|
36
|
+
const bitcoreMessage = new BitcoreLib.Message(message);
|
|
37
|
+
const bitcorePrivateKey = new BitcoreLib.PrivateKey(privateKey.privKey);
|
|
38
|
+
signature = bitcoreMessage.sign(bitcorePrivateKey);
|
|
39
|
+
address = wallet.deriveAddress(index, change);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log({
|
|
43
|
+
address,
|
|
44
|
+
message,
|
|
45
|
+
signature
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
} catch (e) {
|
|
49
|
+
console.error(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
main()
|
|
54
|
+
.catch(console.error)
|
|
55
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const program = require('commander');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const stream = require('stream');
|
|
8
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
9
|
+
const { Storage } = require('../ts_build/src/storage');
|
|
10
|
+
const { StreamUtil } = require('../ts_build/src/stream-util');
|
|
11
|
+
|
|
12
|
+
program
|
|
13
|
+
.version(require('../package.json').version)
|
|
14
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet Name')
|
|
15
|
+
.option('--export', 'Export an existing wallet into an encrypted json')
|
|
16
|
+
.option('--import', 'Import an existing wallet from an encrypted json')
|
|
17
|
+
.option('--lite', 'optional - Import/export a read-only wallet')
|
|
18
|
+
.option('--file <file>', 'optional - Specify the file to import/export. Default: ./<name>.enc')
|
|
19
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (Mongo | Level | TextFile)')
|
|
20
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
21
|
+
.parse(process.argv);
|
|
22
|
+
|
|
23
|
+
let wallet;
|
|
24
|
+
|
|
25
|
+
const main = async () => {
|
|
26
|
+
const { name, path, lite, file, export: doExport, import: doImport, storageType = 'Level' } = program.opts();
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const fileName = file || name + '.enc';
|
|
30
|
+
if (doExport) {
|
|
31
|
+
wallet = await Wallet.loadWallet({ name, storageType, path });
|
|
32
|
+
if (lite) {
|
|
33
|
+
delete wallet.masterKey;
|
|
34
|
+
delete wallet.pubKey;
|
|
35
|
+
wallet.lite = true;
|
|
36
|
+
}
|
|
37
|
+
delete wallet.storage;
|
|
38
|
+
const readStream = new stream.Readable({ objectMode: true });
|
|
39
|
+
const writeStream = fs.createWriteStream(fileName);
|
|
40
|
+
readStream.push(wallet);
|
|
41
|
+
readStream.push(null);
|
|
42
|
+
readStream.pipe(StreamUtil.objectModeToJsonlBuffer()).pipe(writeStream);
|
|
43
|
+
console.log(`Exported wallet to ${name}.enc`);
|
|
44
|
+
}
|
|
45
|
+
if (doImport) {
|
|
46
|
+
const readStream = fs.createReadStream(fileName);
|
|
47
|
+
readStream.pipe(StreamUtil.jsonlBufferToObjectMode()).on('data', async (data) => {
|
|
48
|
+
if (!data) {
|
|
49
|
+
throw new Error('Could not read wallet file');
|
|
50
|
+
}
|
|
51
|
+
const wallet = new Wallet({ ...data, storageType });
|
|
52
|
+
if (name) {
|
|
53
|
+
wallet.name = name;
|
|
54
|
+
}
|
|
55
|
+
let storage = new Storage({
|
|
56
|
+
path,
|
|
57
|
+
errorIfExists: false,
|
|
58
|
+
createIfMissing: true,
|
|
59
|
+
storageType
|
|
60
|
+
});
|
|
61
|
+
let alreadyExists;
|
|
62
|
+
try {
|
|
63
|
+
alreadyExists = await this.loadWallet({ storage, name, storageType });
|
|
64
|
+
} catch (err) {}
|
|
65
|
+
if (alreadyExists) {
|
|
66
|
+
throw new Error('Wallet already exists');
|
|
67
|
+
}
|
|
68
|
+
await storage.saveWallet({ wallet });
|
|
69
|
+
console.log(`Imported ${fileName} into ${storageType} as ${wallet.name}`);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.error(e);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
main()
|
|
78
|
+
.catch(console.error)
|
|
79
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-token
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
program
|
|
8
|
+
.version(require('../package.json').version)
|
|
9
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
|
|
10
|
+
.requiredOption('--contractAddress <contractAddress>', 'REQUIRED - Token contract address')
|
|
11
|
+
.option('--tokenName <tokenName>', 'optional - Custom name for token (default - contract\'s ticker)')
|
|
12
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (Mongo | Level | TextFile)')
|
|
13
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
14
|
+
.parse(process.argv);
|
|
15
|
+
|
|
16
|
+
let wallet;
|
|
17
|
+
|
|
18
|
+
const main = async () => {
|
|
19
|
+
const { name, path, contractAddress, storageType, tokenName } = program.opts();
|
|
20
|
+
try {
|
|
21
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
22
|
+
if (!wallet.isEvmChain() && !wallet.isSolanaChain()) {
|
|
23
|
+
throw new Error('Cannot add token to non-EVM/SOL wallet.');
|
|
24
|
+
}
|
|
25
|
+
let token = await wallet.getToken(contractAddress);
|
|
26
|
+
if (!token) {
|
|
27
|
+
if (!wallet.isSolanaChain()) {
|
|
28
|
+
throw new Error(`Token with contract address ${contractAddress} not found.`);
|
|
29
|
+
}
|
|
30
|
+
console.log(`Token with mint address ${contractAddress} not found on your wallet.`);
|
|
31
|
+
const ans = await promptly.confirm('Would you like to add it? (y/N)', { default: 'n' });
|
|
32
|
+
if (!ans) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const password = await promptly.password('Wallet Password:');
|
|
36
|
+
wallet = await wallet.unlock(password);
|
|
37
|
+
const tx = await wallet.createAtaAccount(contractAddress)
|
|
38
|
+
const signedTx = await wallet.signTx({ tx });
|
|
39
|
+
const transaction = await wallet.broadcast({ tx: signedTx });
|
|
40
|
+
console.log(`Created ATA for ${contractAddress}. Transaction: ${transaction.txid}`);
|
|
41
|
+
token = await wallet.getToken(contractAddress); // re-fetch after creating ATA
|
|
42
|
+
}
|
|
43
|
+
const tokenObj = {
|
|
44
|
+
symbol: token.symbol || tokenName,
|
|
45
|
+
address: contractAddress,
|
|
46
|
+
decimals: token.decimals,
|
|
47
|
+
name: tokenName || token.symbol,
|
|
48
|
+
ataAddress: token.ataAddress || undefined,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
if (!tokenObj.name) {
|
|
52
|
+
throw new Error('tokenName is required for this wallet');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const existing = wallet.tokens.find(t => t.address === contractAddress || t.name === tokenObj.name || (t.name === undefined && t.symbol === tokenObj.name));
|
|
56
|
+
if (existing) {
|
|
57
|
+
console.log(`This wallet already has ${existing.name || existing.symbol} (symbol: ${existing.symbol}, contractAddress: ${existing.address})`);
|
|
58
|
+
const ans = await promptly.confirm('Would you like to replace it? (y/N)', { default: 'n' });
|
|
59
|
+
if (!ans) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
wallet.tokens = wallet.tokens.filter(t => t.address !== existing.address);
|
|
63
|
+
}
|
|
64
|
+
await wallet.addToken(tokenObj);
|
|
65
|
+
console.log(`Successfully added ${tokenObj.name} (${tokenObj.symbol})`);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.error(e);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
main()
|
|
72
|
+
.catch(console.error)
|
|
73
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
program
|
|
8
|
+
.version(require('../package.json').version)
|
|
9
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
|
|
10
|
+
.requiredOption('--tokenName <tokenName>', 'REQUIRED - Name of token to remove')
|
|
11
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (Mongo | Level | TextFile)')
|
|
12
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
13
|
+
.parse(process.argv);
|
|
14
|
+
|
|
15
|
+
let wallet;
|
|
16
|
+
|
|
17
|
+
const main = async () => {
|
|
18
|
+
const { name, path, tokenName, storageType } = program.opts();
|
|
19
|
+
try {
|
|
20
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
21
|
+
if (!wallet.isEvmChain() && !wallet.isSolanaChain()) {
|
|
22
|
+
throw new Error('Cannot remove token from non-EVM/SOL wallet.');
|
|
23
|
+
}
|
|
24
|
+
await wallet.rmToken({ tokenName });
|
|
25
|
+
console.log(`Successfully removed ${tokenName}`);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
main()
|
|
32
|
+
.catch(console.error)
|
|
33
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-tx
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
.requiredOption('--recipients <recipients>', 'REQUIRED - JSON array \'[{"address": "abc1234", "amount": xxxxxx}]\'')
|
|
10
|
+
.requiredOption('--utxos <utxos>', 'REQUIRED - Unspent transactions that can be spent')
|
|
11
|
+
.requiredOption('--change <change>', 'REQUIRED - Change address. `miner` will ensure there is no change output and all left-overs go towards the miner fee.')
|
|
12
|
+
.option('--fee <fee>', 'optional - Custom transaction fee to pay')
|
|
13
|
+
.option('--target <target>', 'optional - Custom target block for confirmation')
|
|
14
|
+
.option('--from <from>', 'optional - From address')
|
|
15
|
+
.option('--nonce <nonce>', 'optional - Nonce')
|
|
16
|
+
.option('--invoiceID <invoiceID>', 'optional - InvoiceId for XRP transactions')
|
|
17
|
+
.option('--token <currency>', 'optional - ERC20 token symbol')
|
|
18
|
+
.option('--tokenName <currency>', 'optional - ERC20 token custom name')
|
|
19
|
+
.option('--storageType <storageType>', 'optional - Name of the database to use (default Level)')
|
|
20
|
+
.option('--path <path>', 'optional - Custom wallet storage path')
|
|
21
|
+
.parse(process.argv);
|
|
22
|
+
|
|
23
|
+
let wallet;
|
|
24
|
+
|
|
25
|
+
const main = async () => {
|
|
26
|
+
const { name, path, storageType } = program.opts();
|
|
27
|
+
let totalTransactionFee;
|
|
28
|
+
let walletNonce;
|
|
29
|
+
try {
|
|
30
|
+
const { recipients, utxos, change, fee, target, from, nonce, invoiceID, token, tokenName } = program.opts();
|
|
31
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
32
|
+
|
|
33
|
+
function tryParse(json) {
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(json);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const parsedUtxos = tryParse(utxos) || [];
|
|
42
|
+
const parsedRecipients = tryParse(recipients) || [];
|
|
43
|
+
if (!parsedRecipients.length) {
|
|
44
|
+
throw new Error('invalid --recipients');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!parsedUtxos.length) {
|
|
48
|
+
throw new Error('invalid --utxos');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!change) {
|
|
52
|
+
throw new Error('invalid --change');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!fee) {
|
|
56
|
+
const utxoBytes = 148;
|
|
57
|
+
const outputBytes = 34;
|
|
58
|
+
const transactionHeader = 10;
|
|
59
|
+
const calculatedNetworkFeeKb = await wallet.getNetworkFee({ target });
|
|
60
|
+
const netWorkFeeSat = calculatedNetworkFeeKb.feerate * 1E8/1000;
|
|
61
|
+
const transactionSize = (parsedUtxos.length * utxoBytes + parsedRecipients.length * outputBytes + transactionHeader + parsedUtxos.length);
|
|
62
|
+
totalTransactionFee = transactionSize * netWorkFeeSat;
|
|
63
|
+
}
|
|
64
|
+
if (!nonce) {
|
|
65
|
+
walletNonce = await wallet.getNonce();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (change !== 'miner') {
|
|
69
|
+
const allKnownAddresses = await wallet.getAddresses();
|
|
70
|
+
if (!allKnownAddresses.includes(change)) {
|
|
71
|
+
throw new Error('This wallet will not be able to detect change funds sent to the --change address you specified. Try importing the key/address first.');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let params = { recipients, utxos, change, fee, from, nonce, token, tokenName };
|
|
76
|
+
params.fee = Number(fee) || totalTransactionFee;
|
|
77
|
+
params.nonce = Number(nonce) || walletNonce;
|
|
78
|
+
params.recipients = parsedRecipients;
|
|
79
|
+
params.utxos = parsedUtxos;
|
|
80
|
+
params.invoiceID = invoiceID;
|
|
81
|
+
const tx = await wallet.newTx(params);
|
|
82
|
+
|
|
83
|
+
console.log(JSON.stringify({ rawTx: tx }));
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error(e);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
function getCurrencies() {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
https.get('https://bitpay.com/currencies', res => {
|
|
92
|
+
if (res.statusCode !== 200) {
|
|
93
|
+
reject(new Error('Request Failed'));
|
|
94
|
+
}
|
|
95
|
+
let data = '';
|
|
96
|
+
res.on('data', (chunk) => {
|
|
97
|
+
data += chunk;
|
|
98
|
+
});
|
|
99
|
+
res.on('end', () => resolve(data.toString()));
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
main()
|
|
105
|
+
.catch(console.error)
|
|
106
|
+
.finally(() => wallet?.storage?.close());
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const program = require('commander');
|
|
4
|
+
const { Writable } = require('stream');
|
|
5
|
+
const { Wallet } = require('../ts_build/src/wallet');
|
|
6
|
+
const { utils } = require('../ts_build/src/utils');
|
|
7
|
+
|
|
8
|
+
program
|
|
9
|
+
.version(require('../package.json').version)
|
|
10
|
+
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
|
|
11
|
+
.option('--startDate <startDate>', 'Start date for the query')
|
|
12
|
+
.option('--endDate <endDate>', 'End date for the query')
|
|
13
|
+
.option('--startBlock <startBlock>', 'Start block for the query')
|
|
14
|
+
.option('--endBlock <endBlock>', 'End block for the query')
|
|
15
|
+
.option('--includeMempool', 'Include unconfirmed transactions')
|
|
16
|
+
.option('--token <tokenName>', 'Get token transactions by the token name')
|
|
17
|
+
.option('--raw', 'Output raw transaction data')
|
|
18
|
+
.option('--storageType <storageType>', 'Name of the database to use (default Level)')
|
|
19
|
+
.option('--path <path>', 'Custom wallet storage path')
|
|
20
|
+
.option('--limit <limit>', 'Limit the number of transactions returned')
|
|
21
|
+
.parse(process.argv);
|
|
22
|
+
|
|
23
|
+
let wallet;
|
|
24
|
+
|
|
25
|
+
const main = async () => {
|
|
26
|
+
const { name, path, startBlock, startDate, endBlock, endDate, includeMempool, storageType, token: tokenName, raw, limit } = program.opts();
|
|
27
|
+
try {
|
|
28
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
29
|
+
const list = wallet.listTransactions({ startBlock, startDate, endBlock, endDate, includeMempool, tokenName, limit });
|
|
30
|
+
list.pause(); // Pause list to prevent race condition where it starts streaming while awaiting getCurrencyObj
|
|
31
|
+
const tokenObj = wallet.getTokenObj({ tokenName }); // null if no tokenName provided
|
|
32
|
+
const currencyObj = await utils.getCurrencyObj(wallet.chain, tokenObj?.address, !wallet.isMainnet());
|
|
33
|
+
let txs = '[';
|
|
34
|
+
list.pipe(new Writable({
|
|
35
|
+
objectMode: true,
|
|
36
|
+
write: function(tx, _, next) {
|
|
37
|
+
txs += tx.toString();
|
|
38
|
+
next();
|
|
39
|
+
},
|
|
40
|
+
final: function(done) {
|
|
41
|
+
txs = JSON.parse(txs.trim().replaceAll('\n', ',') + ']');
|
|
42
|
+
for (const tx of txs) {
|
|
43
|
+
if (startDate && new Date(tx.blockTime) < new Date(startDate)) continue;
|
|
44
|
+
if (endDate && new Date(tx.blockTime) > new Date(endDate)) continue;
|
|
45
|
+
if (tokenObj?.name && !tx.currency) {
|
|
46
|
+
tx.currency = tokenObj.name;
|
|
47
|
+
}
|
|
48
|
+
if (raw) {
|
|
49
|
+
console.log(JSON.stringify(tx));
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (tx.category === 'fee') continue;
|
|
53
|
+
tx.chain = wallet.chain; // tx.chain is not always populated
|
|
54
|
+
console.log(`[${new Date(tx.blockTime).toLocaleString()}] ${tx.txid} ${catSymbol(tx)} ${displayAmt(tx, tokenObj || currencyObj)}`);
|
|
55
|
+
}
|
|
56
|
+
done();
|
|
57
|
+
}
|
|
58
|
+
}));
|
|
59
|
+
list.resume();
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.error(e);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function catSymbol(tx) {
|
|
66
|
+
switch (tx.category) {
|
|
67
|
+
case 'send':
|
|
68
|
+
return '=>';
|
|
69
|
+
case 'receive':
|
|
70
|
+
return '<=';
|
|
71
|
+
case 'move':
|
|
72
|
+
return '<>';
|
|
73
|
+
default:
|
|
74
|
+
return '=?';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function displayAmt(tx, currObj) {
|
|
79
|
+
const baseToCommon = sats => Math.abs(sats) / Math.pow(10, currObj.decimals);
|
|
80
|
+
const dc = currObj.displayCode || currObj.symbol;
|
|
81
|
+
const gwei = tx.gasPrice / 1e9; // NaN if not EVM
|
|
82
|
+
const drops = tx.chain === 'XRP' ? tx.fee : NaN;
|
|
83
|
+
const feeRate = tx.fee / (tx.vsize ?? tx.size);
|
|
84
|
+
const feeRateStr = !isNaN(gwei) ? ` - ${gwei.toFixed(1)} gwei` :
|
|
85
|
+
!isNaN(drops) ? '' :
|
|
86
|
+
` - ${feeRate.toFixed(2)} sat/vB`;
|
|
87
|
+
return `${baseToCommon(tx.satoshis)} ${dc}${!currObj.native ? /* hide fee for tokens */ '' : ` (${baseToCommon(tx.fee)} ${dc}${feeRateStr})`}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
main()
|
|
91
|
+
.catch(console.error)
|
|
92
|
+
.finally(() => wallet?.storage?.close());
|
package/bin/wallet-utxos
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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('--includeSpent', 'optional - Include spent transaction outputs')
|
|
10
|
+
.option('--storageType <storageType>', 'optional - Name of database (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, includeSpent, storageType } = program.opts();
|
|
18
|
+
try {
|
|
19
|
+
wallet = await Wallet.loadWallet({ name, path, storageType });
|
|
20
|
+
wallet.getUtxos({ includeSpent }).pipe(process.stdout);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error(e);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
main()
|
|
27
|
+
.catch(console.error)
|
|
28
|
+
.finally(() => wallet?.storage?.close());
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bitpay-labs/bitcore-client",
|
|
3
|
+
"description": "Wallet client for Bitcore-Node",
|
|
4
|
+
"version": "11.5.2",
|
|
5
|
+
"author": "Justin Langston <nitsujlangston@gmail.com>",
|
|
6
|
+
"main": "./ts_build/src/index.js",
|
|
7
|
+
"types": "./ts_build/src/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"bitcore-client": "./bin/wallet"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18.0.0"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/bitpay/bitcore/tree/master/packages/bitcore-client"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"build:prod": "tsc -p tsconfig.prod.json",
|
|
21
|
+
"clean": "rm -rf ./ts_build",
|
|
22
|
+
"pub": "npm run compile && npm publish",
|
|
23
|
+
"watch": "tsc --watch",
|
|
24
|
+
"precompile": "cd ../bitcore-node && npm run clean && npm run build:prod",
|
|
25
|
+
"compile": "npm run clean && npm run build",
|
|
26
|
+
"test": "npm run compile && mocha --exit -r tsx 'test/**/*.test.ts'",
|
|
27
|
+
"precommit": "npm run lint",
|
|
28
|
+
"lint": "eslint .",
|
|
29
|
+
"fix:errors": "eslint --fix --quiet .",
|
|
30
|
+
"fix:all": "eslint --fix .",
|
|
31
|
+
"fix": "npm run fix:errors"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@bitpay-labs/bitcore-mnemonic": "^11.5.1",
|
|
35
|
+
"@bitpay-labs/crypto-wallet-core": "^11.5.1",
|
|
36
|
+
"async": "2.5.0",
|
|
37
|
+
"bcrypt": "5.1.0",
|
|
38
|
+
"commander": "11.1.0",
|
|
39
|
+
"level-js": "4.0.2",
|
|
40
|
+
"leveldown": "6.1.1",
|
|
41
|
+
"levelup": "4.3.2",
|
|
42
|
+
"mkdirp": "0.5.1",
|
|
43
|
+
"mongodb": "^3.6.0",
|
|
44
|
+
"progress": "2.0.0",
|
|
45
|
+
"promptly": "3.0.3",
|
|
46
|
+
"request": "2.87.0",
|
|
47
|
+
"request-promise-native": "1.0.5",
|
|
48
|
+
"secp256k1": "3.7.1",
|
|
49
|
+
"source-map-support": "0.5.13"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/bcrypt": "3.0.0",
|
|
53
|
+
"@types/bcryptjs": "2.4.2",
|
|
54
|
+
"@types/chai": "5.0.1",
|
|
55
|
+
"@types/elliptic": "6.4.3",
|
|
56
|
+
"@types/ethereumjs-util": "5.2.0",
|
|
57
|
+
"@types/mocha": "10.0.10",
|
|
58
|
+
"@types/mongodb": "4.0.7",
|
|
59
|
+
"@types/node": "22.13.1",
|
|
60
|
+
"@types/request": "2.48.1",
|
|
61
|
+
"@types/request-promise-native": "1.0.15",
|
|
62
|
+
"@types/secp256k1": "3.5.0",
|
|
63
|
+
"chai": "5.1.2",
|
|
64
|
+
"mocha": "11.1.0",
|
|
65
|
+
"sinon": "19.0.2",
|
|
66
|
+
"supertest": "7.0.0",
|
|
67
|
+
"tsx": "^4.21.0",
|
|
68
|
+
"typescript": "5.7.3"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "e091d99915d099863ad3e78dbe867aa7780191e4"
|
|
71
|
+
}
|