@bitpay-labs/bitcore-client 11.11.4 → 11.11.6
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/package.json +14 -9
- package/ts_build/src/wallet.d.ts +3 -3
- package/.mocharc.json +0 -6
- package/LICENSE +0 -21
- package/mcr.config.js +0 -16
- package/src/client.ts +0 -275
- package/src/encryption.ts +0 -191
- package/src/index.ts +0 -21
- package/src/storage/level.ts +0 -171
- package/src/storage/mongo.ts +0 -191
- package/src/storage/textFile.ts +0 -327
- package/src/storage.ts +0 -271
- package/src/stream-util.ts +0 -80
- package/src/types/index.d.ts +0 -2
- package/src/types/storage.d.ts +0 -1
- package/src/types/wallet.d.ts +0 -36
- package/src/utils.ts +0 -35
- package/src/wallet.ts +0 -1221
- package/test/unit/data/ethMigrationTestWallet.fixture.ts +0 -68
- package/test/unit/wallet.arc.test.ts +0 -10
- package/test/unit/wallet.test.ts +0 -1177
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts +0 -51
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts.map +0 -1
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js +0 -68
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js.map +0 -1
- package/ts_build/test/unit/wallet.arc.test.d.ts +0 -2
- package/ts_build/test/unit/wallet.arc.test.d.ts.map +0 -1
- package/ts_build/test/unit/wallet.arc.test.js +0 -12
- package/ts_build/test/unit/wallet.arc.test.js.map +0 -1
- package/ts_build/test/unit/wallet.test.d.ts +0 -2
- package/ts_build/test/unit/wallet.test.d.ts.map +0 -1
- package/ts_build/test/unit/wallet.test.js +0 -1037
- package/ts_build/test/unit/wallet.test.js.map +0 -1
- package/tsconfig.json +0 -40
- package/tsconfig.prod.json +0 -6
package/test/unit/wallet.test.ts
DELETED
|
@@ -1,1177 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
import * as chai from 'chai';
|
|
3
|
-
import * as CWC from '@bitpay-labs/crypto-wallet-core';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
import os from 'os';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
import { AddressTypes, IWalletExt, Wallet } from '../../src/wallet';
|
|
8
|
-
import { Encryption } from '../../src/encryption';
|
|
9
|
-
import crypto from 'crypto';
|
|
10
|
-
import request from 'request-promise-native';
|
|
11
|
-
import requestStream from 'request';
|
|
12
|
-
import { Server } from 'http';
|
|
13
|
-
import sinon from 'sinon';
|
|
14
|
-
import { StorageType } from '../../src/types/storage';
|
|
15
|
-
import supertest from 'supertest';
|
|
16
|
-
import { utils } from '../../src/utils';
|
|
17
|
-
import ethMigrationTestWalletFixture from './data/ethMigrationTestWallet.fixture';
|
|
18
|
-
|
|
19
|
-
const { Api: bcnApi } = require('../../../../bitcore-node/build/src/services/api');
|
|
20
|
-
const { Storage: bcnStorage } = require('../../../../bitcore-node/build/src/services/storage');
|
|
21
|
-
const { loadModules } = require('../../../../bitcore-node/build/src/modules');
|
|
22
|
-
|
|
23
|
-
const should = chai.should();
|
|
24
|
-
const expect = chai.expect;
|
|
25
|
-
|
|
26
|
-
const libMap = {
|
|
27
|
-
BTC: CWC.BitcoreLib,
|
|
28
|
-
BCH: CWC.BitcoreLibCash,
|
|
29
|
-
LTC: CWC.BitcoreLibLtc,
|
|
30
|
-
DOGE: CWC.BitcoreLibDoge
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
describe('Wallet', function() {
|
|
34
|
-
const sandbox = sinon.createSandbox();
|
|
35
|
-
const storageType = 'Level';
|
|
36
|
-
const baseUrl = 'http://127.0.0.1:3000/api';
|
|
37
|
-
let walletName;
|
|
38
|
-
let wallet: Wallet;
|
|
39
|
-
let api;
|
|
40
|
-
const walletsToCleanup: Array<{ name: string; storageType?: StorageType; path?: string }> = [];
|
|
41
|
-
const createWalletForTest = async (params: Partial<IWalletExt>) => {
|
|
42
|
-
if (!params.name) {
|
|
43
|
-
throw new Error('Tests must provide a wallet name');
|
|
44
|
-
}
|
|
45
|
-
const createdWallet = await Wallet.create(params);
|
|
46
|
-
walletsToCleanup.push({
|
|
47
|
-
name: params.name,
|
|
48
|
-
storageType: params.storageType,
|
|
49
|
-
path: params.path
|
|
50
|
-
});
|
|
51
|
-
return createdWallet;
|
|
52
|
-
};
|
|
53
|
-
before(async function() {
|
|
54
|
-
this.timeout(20000);
|
|
55
|
-
await bcnStorage.start({
|
|
56
|
-
dbHost: process.env.DB_HOST || 'localhost',
|
|
57
|
-
dbPort: process.env.DB_PORT || '27017',
|
|
58
|
-
dbName: process.env.DB_NAME || 'bitcore-client-tests'
|
|
59
|
-
});
|
|
60
|
-
loadModules();
|
|
61
|
-
const httpServer: Server = await bcnApi.start();
|
|
62
|
-
api = supertest(httpServer);
|
|
63
|
-
await Wallet.deleteWallet({ name: walletName });
|
|
64
|
-
});
|
|
65
|
-
after(async function() {
|
|
66
|
-
this.timeout(20000);
|
|
67
|
-
await bcnApi.stop();
|
|
68
|
-
await bcnStorage.stop();
|
|
69
|
-
});
|
|
70
|
-
beforeEach(function() {
|
|
71
|
-
sandbox.stub(request, 'Request').callsFake(function(args) {
|
|
72
|
-
args.url = args.url.replace('https://api.bitcore.io/api', baseUrl);
|
|
73
|
-
args.url = args.url.replace(baseUrl, '/api');
|
|
74
|
-
const req = api[args.method.toLowerCase()](args.url);
|
|
75
|
-
for (const [key, value] of Object.entries(args.headers)) {
|
|
76
|
-
req.set(key, value);
|
|
77
|
-
}
|
|
78
|
-
req.send(args.body);
|
|
79
|
-
return req;
|
|
80
|
-
});
|
|
81
|
-
sandbox.stub(requestStream, 'defaults').callsFake(function(args) {
|
|
82
|
-
console.log(args);
|
|
83
|
-
throw new Error('Need to implement requestStream stub in tests');
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
afterEach(async function() {
|
|
87
|
-
while (walletsToCleanup.length) {
|
|
88
|
-
const walletToCleanup = walletsToCleanup.pop();
|
|
89
|
-
if (walletToCleanup) {
|
|
90
|
-
await Wallet.deleteWallet(walletToCleanup);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
sandbox.restore();
|
|
94
|
-
});
|
|
95
|
-
for (const chain of ['BTC', 'BCH', 'LTC', 'DOGE', 'ETH', 'XRP', 'MATIC']) {
|
|
96
|
-
for (const addressType of Object.keys(AddressTypes[chain] || { 'pubkeyhash': 1 })) {
|
|
97
|
-
|
|
98
|
-
it(`should create a wallet for chain and addressType: ${chain} ${addressType}`, async function() {
|
|
99
|
-
walletName = 'BitcoreClientTest' + chain + addressType;
|
|
100
|
-
|
|
101
|
-
wallet = await createWalletForTest({
|
|
102
|
-
chain,
|
|
103
|
-
network: 'mainnet',
|
|
104
|
-
name: walletName,
|
|
105
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
106
|
-
password: 'abc123',
|
|
107
|
-
lite: false,
|
|
108
|
-
addressType,
|
|
109
|
-
storageType,
|
|
110
|
-
baseUrl,
|
|
111
|
-
version: 0
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
expect(wallet.addressType).to.equal(AddressTypes[chain]?.[addressType] || 'pubkeyhash');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it(`should generate an address for chain and addressType: ${chain} ${addressType}`, function() {
|
|
118
|
-
const address = wallet.deriveAddress(0, false);
|
|
119
|
-
expect(address).to.exist;
|
|
120
|
-
switch (chain) {
|
|
121
|
-
case 'BTC':
|
|
122
|
-
case 'BCH':
|
|
123
|
-
case 'DOGE':
|
|
124
|
-
case 'LTC':
|
|
125
|
-
const a = new libMap[chain].Address(address);
|
|
126
|
-
expect(a.toString(true)).to.equal(address);
|
|
127
|
-
expect(a.type).to.equal(wallet.addressType);
|
|
128
|
-
break;
|
|
129
|
-
case 'XRP':
|
|
130
|
-
// TODO verify XRP address
|
|
131
|
-
break;
|
|
132
|
-
default:
|
|
133
|
-
expect(CWC.Web3.utils.isAddress(address)).to.equal(true);
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
describe('bumpTxFee', function() {
|
|
141
|
-
describe('UTXO', function() {
|
|
142
|
-
walletName = 'BitcoreClientTestBumpFee-UTXO';
|
|
143
|
-
|
|
144
|
-
beforeEach(async function() {
|
|
145
|
-
wallet = await createWalletForTest({
|
|
146
|
-
name: walletName,
|
|
147
|
-
chain: 'BTC',
|
|
148
|
-
network: 'testnet',
|
|
149
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
150
|
-
password: 'abc123',
|
|
151
|
-
storageType,
|
|
152
|
-
baseUrl,
|
|
153
|
-
// version: 0
|
|
154
|
-
});
|
|
155
|
-
await wallet.unlock('abc123');
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it('should throw an error if changeIdx is null for UTXO chains', async () => {
|
|
159
|
-
try {
|
|
160
|
-
await wallet.bumpTxFee({});
|
|
161
|
-
expect.fail('Expected method to throw');
|
|
162
|
-
} catch (err) {
|
|
163
|
-
expect(err.message).to.equal('Must provide changeIdx for UTXO chains');
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it('should throw an error if neither rawTx nor txid is provided', async () => {
|
|
168
|
-
try {
|
|
169
|
-
await wallet.bumpTxFee({ changeIdx: 0 });
|
|
170
|
-
expect.fail('Expected method to throw');
|
|
171
|
-
} catch (err) {
|
|
172
|
-
expect(err.message).to.equal('Must provide either rawTx or txid');
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should bump the fee of a transaction with feeTarget', async function() {
|
|
177
|
-
sandbox.stub(wallet, 'getTransactionByTxid').resolves({ '_id': '65982807d85e75f781a0d56f', 'txid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'network': 'regtest', 'chain': 'BTC', 'blockHeight': -1, 'blockHash': '', 'blockTime': '2024-01-05T16:02:15.678Z', 'blockTimeNormalized': '2024-01-05T16:02:15.678Z', 'coinbase': false, 'locktime': -1, 'inputCount': 5, 'outputCount': 1, 'size': 780, 'fee': 2409, 'value': 950891, 'confirmations': 0, 'coins': { 'inputs': [{ '_id': '659825bed85e75f781a01812', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 1, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '330716a75c23512202e45ffee478bdce7a33f298edf7eef30a42dbde06746c48', 'mintHeight': 154, 'spentHeight': -1, 'address': 'n2vpyzbBPBEni9FgkeQSnXvAQAfnRmKxFZ', 'script': '76a914eade85745090388e64a9341a82d8f94371430d1a88ac', 'value': 244000, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982587d85e75f781a0046f', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 1, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '7b83f82a22f94fc3e615743a5e6fb2345ed79aad8a2b564af2ace49da1e27ebb', 'mintHeight': 152, 'spentHeight': -1, 'address': 'muEU9KaNEw5doymy59ScrgEE3Eq4CQ45U9', 'script': '76a91496739b4ef4e793c05a78624da38d96f2c4059c6b88ac', 'value': 239400, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982511d85e75f7819fdd40', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': 'fb4d8039024c80a655fea29e01faf202f30e936af5738e31c5841a84215ca4d6', 'mintHeight': 149, 'spentHeight': -1, 'address': 'muEU9KaNEw5doymy59ScrgEE3Eq4CQ45U9', 'script': '76a91496739b4ef4e793c05a78624da38d96f2c4059c6b88ac', 'value': 239400, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '659824d7d85e75f7819fc75d', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '8221ed6891f6ba82ab72bb8298a661e21b47b04408861b0c3268c4a04a0435b3', 'mintHeight': 145, 'spentHeight': -1, 'address': 'miEzS8fSvtDjwkEjY14FEuWumFKg8nTPV9', 'script': '76a9141de381fc20d34d20e11a5543f536a4b74f495a9888ac', 'value': 228000, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982389d85e75f7819f57ff', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '95f713c2d7e09ab57ec3016bb767b26e6ee849cf487e86d8ed6126ae1b893e0f', 'mintHeight': 143, 'spentHeight': -1, 'address': 'msnM9VB5usfBahZNo57ZUq9uzwRxQXksUz', 'script': '76a914868ad3308906626182d1c6cd703cc7ce78b3a28d88ac', 'value': 2500, 'confirmations': -1, 'sequenceNumber': 4294967293 }], 'outputs': [{ '_id': '65982807d85e75f781a0d55d', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '', 'mintTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintHeight': -1, 'spentHeight': -2, 'address': 'mwwrGzk9q8xugY97C8BiNbcgqXNyjH4kp8', 'script': '76a914b4376347e4cffb1e9a475b2661bbe74de3c1f86a88ac', 'value': 950891, 'confirmations': -1 }] } });
|
|
178
|
-
sandbox.stub(wallet, 'getNetworkFee').resolves({ feerate: 0.00064803, blocks: 2 }); // this is a real response from mainnet
|
|
179
|
-
sandbox.stub(wallet.client, 'importAddresses').resolves();
|
|
180
|
-
sandbox.spy(CWC.BitcoreLib.Transaction.prototype, 'feePerByte');
|
|
181
|
-
|
|
182
|
-
await wallet.generateAddressPair(1310066242, true);
|
|
183
|
-
await wallet.generateAddressPair(1087984800, true);
|
|
184
|
-
await wallet.generateAddressPair(1310064953, true);
|
|
185
|
-
await wallet.generateAddressPair(1310062823, true);
|
|
186
|
-
|
|
187
|
-
const { tx: newTx } = await wallet.bumpTxFee({
|
|
188
|
-
txid: '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5',
|
|
189
|
-
changeIdx: 0,
|
|
190
|
-
feeTarget: 2
|
|
191
|
-
});
|
|
192
|
-
CWC.BitcoreLib.Transaction.prototype.feePerByte.callCount.should.equal(1);
|
|
193
|
-
CWC.BitcoreLib.Transaction.prototype.feePerByte.args[0][0].should.equal(65);
|
|
194
|
-
expect(newTx).to.equal('0200000005486c7406dedb420af3eef7ed98f2337acebd78e4fe5fe4022251235ca71607330100000000fdffffffbb7ee2a19de4acf24a562b8aad9ad75e34b26f5e3a7415e6c34ff9222af8837b0100000000fdffffffd6a45c21841a84c5318e73f56a930ef302f2fa019ea2fe55a6804c0239804dfb0000000000fdffffffb335044aa0c468320c1b860844b0471be261a69882bb72ab82baf69168ed21820000000000fdffffff0f3e891bae2661edd8867e48cf49e86e6eb267b76b01c37eb59ae0d7c213f7950000000000fdffffff01c4c40d00000000001976a914b4376347e4cffb1e9a475b2661bbe74de3c1f86a88ac00000000');
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('should bump the fee of a transaction with feeRate', async function() {
|
|
198
|
-
sandbox.stub(wallet, 'getTransactionByTxid').resolves({ '_id': '65982807d85e75f781a0d56f', 'txid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'network': 'regtest', 'chain': 'BTC', 'blockHeight': -1, 'blockHash': '', 'blockTime': '2024-01-05T16:02:15.678Z', 'blockTimeNormalized': '2024-01-05T16:02:15.678Z', 'coinbase': false, 'locktime': -1, 'inputCount': 5, 'outputCount': 1, 'size': 780, 'fee': 2409, 'value': 950891, 'confirmations': 0, 'coins': { 'inputs': [{ '_id': '659825bed85e75f781a01812', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 1, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '330716a75c23512202e45ffee478bdce7a33f298edf7eef30a42dbde06746c48', 'mintHeight': 154, 'spentHeight': -1, 'address': 'n2vpyzbBPBEni9FgkeQSnXvAQAfnRmKxFZ', 'script': '76a914eade85745090388e64a9341a82d8f94371430d1a88ac', 'value': 244000, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982587d85e75f781a0046f', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 1, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '7b83f82a22f94fc3e615743a5e6fb2345ed79aad8a2b564af2ace49da1e27ebb', 'mintHeight': 152, 'spentHeight': -1, 'address': 'muEU9KaNEw5doymy59ScrgEE3Eq4CQ45U9', 'script': '76a91496739b4ef4e793c05a78624da38d96f2c4059c6b88ac', 'value': 239400, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982511d85e75f7819fdd40', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': 'fb4d8039024c80a655fea29e01faf202f30e936af5738e31c5841a84215ca4d6', 'mintHeight': 149, 'spentHeight': -1, 'address': 'muEU9KaNEw5doymy59ScrgEE3Eq4CQ45U9', 'script': '76a91496739b4ef4e793c05a78624da38d96f2c4059c6b88ac', 'value': 239400, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '659824d7d85e75f7819fc75d', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '8221ed6891f6ba82ab72bb8298a661e21b47b04408861b0c3268c4a04a0435b3', 'mintHeight': 145, 'spentHeight': -1, 'address': 'miEzS8fSvtDjwkEjY14FEuWumFKg8nTPV9', 'script': '76a9141de381fc20d34d20e11a5543f536a4b74f495a9888ac', 'value': 228000, 'confirmations': -1, 'sequenceNumber': 4294967293 }, { '_id': '65982389d85e75f7819f57ff', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintTxid': '95f713c2d7e09ab57ec3016bb767b26e6ee849cf487e86d8ed6126ae1b893e0f', 'mintHeight': 143, 'spentHeight': -1, 'address': 'msnM9VB5usfBahZNo57ZUq9uzwRxQXksUz', 'script': '76a914868ad3308906626182d1c6cd703cc7ce78b3a28d88ac', 'value': 2500, 'confirmations': -1, 'sequenceNumber': 4294967293 }], 'outputs': [{ '_id': '65982807d85e75f781a0d55d', 'chain': 'BTC', 'network': 'regtest', 'coinbase': false, 'mintIndex': 0, 'spentTxid': '', 'mintTxid': '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5', 'mintHeight': -1, 'spentHeight': -2, 'address': 'mwwrGzk9q8xugY97C8BiNbcgqXNyjH4kp8', 'script': '76a914b4376347e4cffb1e9a475b2661bbe74de3c1f86a88ac', 'value': 950891, 'confirmations': -1 }] } });
|
|
199
|
-
sandbox.stub(wallet.client, 'importAddresses').resolves();
|
|
200
|
-
sandbox.spy(CWC.BitcoreLib.Transaction.prototype, 'feePerByte');
|
|
201
|
-
|
|
202
|
-
await wallet.generateAddressPair(1310066242, true);
|
|
203
|
-
await wallet.generateAddressPair(1087984800, true);
|
|
204
|
-
await wallet.generateAddressPair(1310064953, true);
|
|
205
|
-
await wallet.generateAddressPair(1310062823, true);
|
|
206
|
-
|
|
207
|
-
const { tx: newTx } = await wallet.bumpTxFee({
|
|
208
|
-
txid: '8b78e4d2ac2211472454f940445210b6487aaa0f889e18066eb3f623352607f5',
|
|
209
|
-
changeIdx: 0,
|
|
210
|
-
feeRate: 2
|
|
211
|
-
});
|
|
212
|
-
CWC.BitcoreLib.Transaction.prototype.feePerByte.callCount.should.equal(1);
|
|
213
|
-
CWC.BitcoreLib.Transaction.prototype.feePerByte.args[0][0].should.equal(2);
|
|
214
|
-
expect(newTx).to.equal('0200000005486c7406dedb420af3eef7ed98f2337acebd78e4fe5fe4022251235ca71607330100000000fdffffffbb7ee2a19de4acf24a562b8aad9ad75e34b26f5e3a7415e6c34ff9222af8837b0100000000fdffffffd6a45c21841a84c5318e73f56a930ef302f2fa019ea2fe55a6804c0239804dfb0000000000fdffffffb335044aa0c468320c1b860844b0471be261a69882bb72ab82baf69168ed21820000000000fdffffff0f3e891bae2661edd8867e48cf49e86e6eb267b76b01c37eb59ae0d7c213f7950000000000fdffffff01b4850e00000000001976a914b4376347e4cffb1e9a475b2661bbe74de3c1f86a88ac00000000');
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
describe('EVM', function() {
|
|
219
|
-
walletName = 'BitcoreClientTestBumpFee-EVM';
|
|
220
|
-
|
|
221
|
-
beforeEach(async function() {
|
|
222
|
-
wallet = await createWalletForTest({
|
|
223
|
-
name: walletName,
|
|
224
|
-
chain: 'ETH',
|
|
225
|
-
network: 'testnet',
|
|
226
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
227
|
-
password: 'abc123',
|
|
228
|
-
storageType,
|
|
229
|
-
baseUrl,
|
|
230
|
-
version: 0
|
|
231
|
-
});
|
|
232
|
-
await wallet.unlock('abc123');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
it('should throw an error if neither rawTx nor txid is provided', async () => {
|
|
237
|
-
try {
|
|
238
|
-
await wallet.bumpTxFee({ changeIdx: 0 });
|
|
239
|
-
expect.fail('Expected method to throw');
|
|
240
|
-
} catch (err) {
|
|
241
|
-
expect(err.message).to.equal('Must provide either rawTx or txid');
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('should bump the fee of a transaction with feeTarget', async function() {
|
|
246
|
-
sandbox.stub(wallet, 'getTransactionByTxid').resolves({ 'txid': '0x0cf410cfe7fb268ad06ae115edfa8a30a8dea3979336a647b09b5a789c4b53d5', 'network': 'regtest', 'chain': 'ETH', 'blockHeight': 43245, 'blockHash': '0x9edb8d10883a360f7ff0c26860b6a159f5b7a74226949a4365691a879fafcdfc', 'blockTime': '2024-01-08T16:31:46.000Z', 'blockTimeNormalized': '2024-01-08T16:31:46.000Z', 'fee': 42000000000000, 'value': 1000000000000000000, 'gasLimit': 200000, 'gasPrice': 2000000000, 'nonce': 0, 'to': '0x7ee308b49e36Ab516cd0186B3a47CFD31d2499A1', 'from': '0x5FbdD2712d05D1a73e0b3Eba5efE8c3d42a336C3', 'effects': [], 'data': '0x', 'internal': [], 'calls': [], 'confirmations': 33 });
|
|
247
|
-
sandbox.stub(wallet, 'getNetworkFee').resolves({ feerate: 26550000000, blocks: 2 }); // this is a real response from mainnet
|
|
248
|
-
sandbox.stub(wallet.client, 'importAddresses').resolves();
|
|
249
|
-
|
|
250
|
-
const { tx: newTx, params } = await wallet.bumpTxFee({
|
|
251
|
-
txid: '0x0cf410cfe7fb268ad06ae115edfa8a30a8dea3979336a647b09b5a789c4b53d5',
|
|
252
|
-
feeTarget: 2
|
|
253
|
-
});
|
|
254
|
-
params.gasPrice.should.equal(26550000000);
|
|
255
|
-
expect(newTx).to.equal('0xf08085062e80d98083030d40947ee308b49e36ab516cd0186b3a47cfd31d2499a1880de0b6b3a76400008083aa36a78080');
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('should bump the fee of a transaction with feeRate', async function() {
|
|
259
|
-
sandbox.stub(wallet, 'getTransactionByTxid').resolves({ 'txid': '0x0cf410cfe7fb268ad06ae115edfa8a30a8dea3979336a647b09b5a789c4b53d5', 'network': 'regtest', 'chain': 'ETH', 'blockHeight': 43245, 'blockHash': '0x9edb8d10883a360f7ff0c26860b6a159f5b7a74226949a4365691a879fafcdfc', 'blockTime': '2024-01-08T16:31:46.000Z', 'blockTimeNormalized': '2024-01-08T16:31:46.000Z', 'fee': 42000000000000, 'value': 1000000000000000000, 'gasLimit': 200000, 'gasPrice': 2000000000, 'nonce': 0, 'to': '0x7ee308b49e36Ab516cd0186B3a47CFD31d2499A1', 'from': '0x5FbdD2712d05D1a73e0b3Eba5efE8c3d42a336C3', 'effects': [], 'data': '0x', 'internal': [], 'calls': [], 'confirmations': 33 });
|
|
260
|
-
sandbox.stub(wallet.client, 'importAddresses').resolves();
|
|
261
|
-
|
|
262
|
-
const { tx: newTx, params } = await wallet.bumpTxFee({
|
|
263
|
-
txid: '0x0cf410cfe7fb268ad06ae115edfa8a30a8dea3979336a647b09b5a789c4b53d5',
|
|
264
|
-
feeRate: 300
|
|
265
|
-
});
|
|
266
|
-
params.gasPrice.should.equal(CWC.Web3.utils.toWei('300', 'gwei'));
|
|
267
|
-
expect(newTx).to.equal('0xf0808545d964b80083030d40947ee308b49e36ab516cd0186b3a47cfd31d2499a1880de0b6b3a76400008083aa36a78080');
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
describe('getLocalAddress', function() {
|
|
273
|
-
for (const storageType of ['Level', 'Mongo', 'TextFile'] as StorageType[]) {
|
|
274
|
-
describe(storageType, function() {
|
|
275
|
-
let wallet;
|
|
276
|
-
const walletName = 'BitcoreClientTestGetLocalAddress' + storageType;
|
|
277
|
-
let address1, caddress1, address2, caddress2, address3, caddress3;
|
|
278
|
-
let path;
|
|
279
|
-
if (storageType === 'Mongo' && (process.env.DB_NAME || process.env.DB_HOST || process.env.DB_PORT)) {
|
|
280
|
-
path = `mongodb://${process.env.DB_HOST || 'localhost'}:${process.env.DB_PORT || 27017}/${process.env.DB_NAME || 'bitcoreWallets-test'}`;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
before(async function() {
|
|
284
|
-
this.timeout(5000);
|
|
285
|
-
try {
|
|
286
|
-
wallet = await Wallet.create({
|
|
287
|
-
name: walletName,
|
|
288
|
-
chain: 'BTC',
|
|
289
|
-
network: 'regtest',
|
|
290
|
-
password: 'abc123',
|
|
291
|
-
storageType,
|
|
292
|
-
path,
|
|
293
|
-
baseUrl,
|
|
294
|
-
version: 0
|
|
295
|
-
});
|
|
296
|
-
await wallet.unlock('abc123');
|
|
297
|
-
// 3 address pairs
|
|
298
|
-
[address1, caddress1] = await wallet.nextAddressPair(true);
|
|
299
|
-
[address2, caddress2] = await wallet.nextAddressPair(true);
|
|
300
|
-
[address3, caddress3] = await wallet.nextAddressPair(true);
|
|
301
|
-
} catch (e) {
|
|
302
|
-
wallet?.storage.close();
|
|
303
|
-
throw e;
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
after(async function() {
|
|
308
|
-
await Wallet.deleteWallet({ name: walletName, storageType, path });
|
|
309
|
-
wallet?.storage.close();
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
it('should return the local address', async function() {
|
|
313
|
-
const localAddress = await wallet.getLocalAddress(address2);
|
|
314
|
-
expect(localAddress.address).to.equal(address2);
|
|
315
|
-
expect(localAddress.path).to.equal('m/0/1');
|
|
316
|
-
expect(localAddress.pubKey).to.exist;
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
describe('importKeys', function() {
|
|
323
|
-
walletName = 'BitcoreClientTestImportKeys';
|
|
324
|
-
let requestStub;
|
|
325
|
-
let sleepStub;
|
|
326
|
-
|
|
327
|
-
beforeEach(async function() {
|
|
328
|
-
wallet = await createWalletForTest({
|
|
329
|
-
name: walletName,
|
|
330
|
-
chain: 'BTC',
|
|
331
|
-
network: 'testnet',
|
|
332
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
333
|
-
password: 'abc123',
|
|
334
|
-
storageType,
|
|
335
|
-
baseUrl,
|
|
336
|
-
version: 0
|
|
337
|
-
});
|
|
338
|
-
await wallet.unlock('abc123');
|
|
339
|
-
requestStub = sandbox.stub(wallet.client, '_request').resolves();
|
|
340
|
-
sleepStub = sandbox.stub(utils, 'sleep').resolves();
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it('should import 1 key', async function() {
|
|
344
|
-
const keys = [];
|
|
345
|
-
for (let i = 0; i < 1; i++) {
|
|
346
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
347
|
-
keys.push({
|
|
348
|
-
privKey: pk,
|
|
349
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
await wallet.importKeys({
|
|
353
|
-
keys,
|
|
354
|
-
rederiveAddys: false
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
requestStub.callCount.should.equal(1);
|
|
358
|
-
sleepStub.callCount.should.equal(0);
|
|
359
|
-
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
it('should import <100 keys', async function() {
|
|
363
|
-
const keys = [];
|
|
364
|
-
for (let i = 0; i < 100; i++) {
|
|
365
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
366
|
-
keys.push({
|
|
367
|
-
privKey: pk,
|
|
368
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
await wallet.importKeys({
|
|
372
|
-
keys,
|
|
373
|
-
rederiveAddys: false
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
requestStub.callCount.should.equal(1);
|
|
377
|
-
sleepStub.callCount.should.equal(0);
|
|
378
|
-
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
it('should import >100 keys', async function() {
|
|
382
|
-
const keys = [];
|
|
383
|
-
for (let i = 0; i < 101; i++) {
|
|
384
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
385
|
-
keys.push({
|
|
386
|
-
privKey: pk,
|
|
387
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
await wallet.importKeys({
|
|
391
|
-
keys,
|
|
392
|
-
rederiveAddys: false
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
requestStub.callCount.should.equal(2);
|
|
396
|
-
sleepStub.callCount.should.equal(1);
|
|
397
|
-
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
it('can derive the public key if not included', async function () {
|
|
401
|
-
const keys = [];
|
|
402
|
-
for (let i = 0; i < 101; i++) {
|
|
403
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
404
|
-
keys.push({
|
|
405
|
-
privKey: pk,
|
|
406
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
410
|
-
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
411
|
-
|
|
412
|
-
await wallet.importKeys({
|
|
413
|
-
keys,
|
|
414
|
-
rederiveAddys: false
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
getPublicKeyStub.callCount.should.be.greaterThan(0);
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
it('encrypts key.privKey only', async function () {
|
|
421
|
-
const keys = [];
|
|
422
|
-
for (let i = 0; i < 1; i++) {
|
|
423
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
424
|
-
keys.push({
|
|
425
|
-
privKey: pk,
|
|
426
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
const addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
430
|
-
|
|
431
|
-
await wallet.importKeys({
|
|
432
|
-
keys,
|
|
433
|
-
rederiveAddys: false
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
addKeysSafeStub.calledOnce.should.equal(true);
|
|
437
|
-
const savedKeys = addKeysSafeStub.firstCall.args[0].keys;
|
|
438
|
-
|
|
439
|
-
for (const originalKey of keys) {
|
|
440
|
-
const matchingSavedKey = savedKeys.find(sk => {
|
|
441
|
-
// Match on pubKey only if it was in originalKey
|
|
442
|
-
return !(originalKey.pubKey && sk.pubKey === originalKey.pubKey) && sk.address === originalKey.address;
|
|
443
|
-
});
|
|
444
|
-
expect(matchingSavedKey).to.exist;
|
|
445
|
-
matchingSavedKey.privKey.should.not.equal(originalKey.privKey);
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
it('can rederive addresses', async function () {
|
|
450
|
-
const keys = [];
|
|
451
|
-
for (let i = 0; i < 1; i++) {
|
|
452
|
-
const pk = crypto.randomBytes(32).toString('hex');
|
|
453
|
-
keys.push({
|
|
454
|
-
privKey: pk,
|
|
455
|
-
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
459
|
-
const getAddressStub = sandbox.stub(CWC.Deriver, 'getAddress').returns('mockedAddress');
|
|
460
|
-
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
461
|
-
|
|
462
|
-
await wallet.importKeys({
|
|
463
|
-
keys,
|
|
464
|
-
rederiveAddys: true
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
getAddressStub.callCount.should.be.greaterThan(0);
|
|
468
|
-
});
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
describe('signTx', function() {
|
|
472
|
-
let txStub: sinon.SinonStub;
|
|
473
|
-
afterEach(async function() {
|
|
474
|
-
sandbox.restore();
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
describe('BTC (UTXO) decrypts ciphertext to WIF', function() {
|
|
478
|
-
walletName = 'BitcoreClientTestSignTxV2-BTC';
|
|
479
|
-
let wallet: Wallet;
|
|
480
|
-
|
|
481
|
-
beforeEach(async function() {
|
|
482
|
-
wallet = await createWalletForTest({
|
|
483
|
-
name: walletName,
|
|
484
|
-
chain: 'BTC',
|
|
485
|
-
network: 'testnet',
|
|
486
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
487
|
-
password: 'abc123',
|
|
488
|
-
storageType,
|
|
489
|
-
baseUrl
|
|
490
|
-
});
|
|
491
|
-
await wallet.unlock('abc123');
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
it('should decrypt stored ciphertext and hand WIF to Transactions.sign', async function() {
|
|
495
|
-
const pk = new CWC.BitcoreLib.PrivateKey(undefined, 'testnet');
|
|
496
|
-
const address = pk.toAddress().toString();
|
|
497
|
-
const privBuf = CWC.Deriver.privateKeyToBuffer('BTC', pk.toString());
|
|
498
|
-
// v2 key encryption uses the key's pubKey as the IV salt (not the wallet pubKey)
|
|
499
|
-
const encPriv = Encryption.encryptBuffer(privBuf, pk.publicKey.toString(), wallet.unlocked.encryptionKey).toString('hex');
|
|
500
|
-
privBuf.fill(0);
|
|
501
|
-
|
|
502
|
-
sandbox.stub(wallet.storage, 'getStoredKeys').resolves([
|
|
503
|
-
{
|
|
504
|
-
address,
|
|
505
|
-
privKey: encPriv,
|
|
506
|
-
pubKey: pk.publicKey.toString()
|
|
507
|
-
}
|
|
508
|
-
]);
|
|
509
|
-
sandbox.stub(wallet, 'derivePrivateKey').resolves({
|
|
510
|
-
address: 'change',
|
|
511
|
-
privKey: pk.toString(),
|
|
512
|
-
pubKey: pk.publicKey.toString(),
|
|
513
|
-
path: 'm/1/0'
|
|
514
|
-
});
|
|
515
|
-
sandbox.stub(wallet, 'importKeys').resolves();
|
|
516
|
-
|
|
517
|
-
let capturedPayload;
|
|
518
|
-
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
519
|
-
capturedPayload = payload;
|
|
520
|
-
return 'signed';
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
const utxos = [{ address, value: 1 }];
|
|
524
|
-
await wallet.signTx({ tx: 'raw', utxos });
|
|
525
|
-
|
|
526
|
-
txStub.calledOnce.should.equal(true);
|
|
527
|
-
capturedPayload.keys[0].privKey.should.equal(pk.toWIF());
|
|
528
|
-
capturedPayload.key.privKey.should.equal(pk.toWIF());
|
|
529
|
-
});
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
describe('ETH (account) decrypts ciphertext to hex and skips plaintext', function() {
|
|
533
|
-
walletName = 'BitcoreClientTestSignTxV2-ETH';
|
|
534
|
-
let wallet: Wallet;
|
|
535
|
-
|
|
536
|
-
beforeEach(async function() {
|
|
537
|
-
wallet = await createWalletForTest({
|
|
538
|
-
name: walletName,
|
|
539
|
-
chain: 'ETH',
|
|
540
|
-
network: 'testnet',
|
|
541
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
542
|
-
password: 'abc123',
|
|
543
|
-
storageType,
|
|
544
|
-
baseUrl
|
|
545
|
-
});
|
|
546
|
-
await wallet.unlock('abc123');
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
it('should decrypt stored ciphertext and hand hex privKey to Transactions.sign', async function() {
|
|
550
|
-
const privHex = crypto.randomBytes(32).toString('hex');
|
|
551
|
-
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
552
|
-
const pubKey = CWC.Deriver.getPublicKey('ETH', wallet.network, privBufForPubKey);
|
|
553
|
-
privBufForPubKey.fill(0);
|
|
554
|
-
const privBuf = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
555
|
-
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
556
|
-
privBuf.fill(0);
|
|
557
|
-
|
|
558
|
-
let capturedPayload;
|
|
559
|
-
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
560
|
-
capturedPayload = payload;
|
|
561
|
-
return 'signed';
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
const signingKeys = [{ address: '0xabc', privKey: encPriv, pubKey }];
|
|
565
|
-
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
566
|
-
|
|
567
|
-
txStub.calledOnce.should.equal(true);
|
|
568
|
-
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
569
|
-
});
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
describe('XRP (account) decrypts ciphertext to uppercase hex and skips plaintext', function() {
|
|
573
|
-
walletName = 'BitcoreClientTestSignTxV2-XRP';
|
|
574
|
-
let wallet: Wallet;
|
|
575
|
-
|
|
576
|
-
beforeEach(async function() {
|
|
577
|
-
wallet = await createWalletForTest({
|
|
578
|
-
name: walletName,
|
|
579
|
-
chain: 'XRP',
|
|
580
|
-
network: 'testnet',
|
|
581
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
582
|
-
password: 'abc123',
|
|
583
|
-
storageType,
|
|
584
|
-
baseUrl
|
|
585
|
-
});
|
|
586
|
-
await wallet.unlock('abc123');
|
|
587
|
-
});
|
|
588
|
-
|
|
589
|
-
it('should decrypt stored ciphertext and hand uppercase hex privKey to Transactions.sign', async function() {
|
|
590
|
-
const privHex = crypto.randomBytes(32).toString('hex').toUpperCase();
|
|
591
|
-
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
592
|
-
const pubKey = CWC.Deriver.getPublicKey('XRP', wallet.network, privBufForPubKey);
|
|
593
|
-
privBufForPubKey.fill(0);
|
|
594
|
-
const privBuf = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
595
|
-
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
596
|
-
privBuf.fill(0);
|
|
597
|
-
|
|
598
|
-
let capturedPayload;
|
|
599
|
-
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
600
|
-
capturedPayload = payload;
|
|
601
|
-
return 'signed';
|
|
602
|
-
});
|
|
603
|
-
|
|
604
|
-
const signingKeys = [{ address: 'rabc', privKey: encPriv, pubKey }];
|
|
605
|
-
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
606
|
-
|
|
607
|
-
txStub.calledOnce.should.equal(true);
|
|
608
|
-
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
609
|
-
});
|
|
610
|
-
});
|
|
611
|
-
|
|
612
|
-
describe('SOL (account) decrypts ciphertext to base58 and skips plaintext', function() {
|
|
613
|
-
walletName = 'BitcoreClientTestSignTxV2-SOL';
|
|
614
|
-
let wallet: Wallet;
|
|
615
|
-
|
|
616
|
-
beforeEach(async function() {
|
|
617
|
-
wallet = await createWalletForTest({
|
|
618
|
-
name: walletName,
|
|
619
|
-
chain: 'SOL',
|
|
620
|
-
network: 'devnet',
|
|
621
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
622
|
-
password: 'abc123',
|
|
623
|
-
storageType,
|
|
624
|
-
baseUrl
|
|
625
|
-
});
|
|
626
|
-
await wallet.unlock('abc123');
|
|
627
|
-
});
|
|
628
|
-
|
|
629
|
-
it('should decrypt stored ciphertext and hand base58 privKey to Transactions.sign', async function() {
|
|
630
|
-
const privBufForPubKey = crypto.randomBytes(32);
|
|
631
|
-
const pubKey = CWC.Deriver.getPublicKey('SOL', wallet.network, privBufForPubKey);
|
|
632
|
-
const privBuf = Buffer.from(privBufForPubKey);
|
|
633
|
-
privBufForPubKey.fill(0);
|
|
634
|
-
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
635
|
-
const expectedPrivKey = CWC.Deriver.bufferToPrivateKey_TEMP('SOL', wallet.network, Buffer.from(privBuf));
|
|
636
|
-
privBuf.fill(0);
|
|
637
|
-
|
|
638
|
-
let capturedPayload;
|
|
639
|
-
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
640
|
-
capturedPayload = payload;
|
|
641
|
-
return Promise.resolve('signed');
|
|
642
|
-
});
|
|
643
|
-
|
|
644
|
-
const signingKeys = [{ address: pubKey, privKey: encPriv, pubKey }];
|
|
645
|
-
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
646
|
-
|
|
647
|
-
txStub.calledOnce.should.equal(true);
|
|
648
|
-
capturedPayload.keys[0].privKey.should.equal(expectedPrivKey);
|
|
649
|
-
});
|
|
650
|
-
});
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
describe('derivePrivateKey', function () {
|
|
654
|
-
let wallet: Wallet;
|
|
655
|
-
const createUnlockedWallet = async (params: { chain: string; network: string; xpriv: string }) => {
|
|
656
|
-
walletName = `BitcoreClientTestDerivePrivateKey-${params.chain}`;
|
|
657
|
-
wallet = await createWalletForTest({
|
|
658
|
-
name: walletName,
|
|
659
|
-
chain: params.chain,
|
|
660
|
-
network: params.network,
|
|
661
|
-
xpriv: params.xpriv,
|
|
662
|
-
password: 'abc123',
|
|
663
|
-
storageType,
|
|
664
|
-
baseUrl
|
|
665
|
-
});
|
|
666
|
-
await wallet.unlock('abc123');
|
|
667
|
-
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
668
|
-
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
669
|
-
return wallet;
|
|
670
|
-
};
|
|
671
|
-
|
|
672
|
-
const walletVectors = {
|
|
673
|
-
BTC: {
|
|
674
|
-
chain: 'BTC',
|
|
675
|
-
network: 'mainnet',
|
|
676
|
-
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
677
|
-
expected: {
|
|
678
|
-
address: '14FubqQhpG1dhTSgD5nRsiQRJEEcxVojRf',
|
|
679
|
-
privKey: '79cab08ffc77750721329a0033c43fd1e5c32e9e2da273c18e5e36abb05cca32',
|
|
680
|
-
pubKey: '03d69dd136b999433a9f6c8f38076831ec0d3a3cf7a555bec8bc8c6d76fc266231',
|
|
681
|
-
path: 'm/0/0'
|
|
682
|
-
}
|
|
683
|
-
},
|
|
684
|
-
ETH: {
|
|
685
|
-
chain: 'ETH',
|
|
686
|
-
network: 'mainnet',
|
|
687
|
-
xpriv: 'xprv9ypBjKErGMqCdzd44hfSdy1Vk6PGtU3si8ogZcow7rA23HTxMi9XfT99EKmiNdLMr9BAZ9S8ZKCYfN1eCmzYSmXYHje1jnYQseV1VJDDfdS',
|
|
688
|
-
expected: {
|
|
689
|
-
address: '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36',
|
|
690
|
-
privKey: '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340',
|
|
691
|
-
pubKey: '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad',
|
|
692
|
-
path: 'm/0/0'
|
|
693
|
-
}
|
|
694
|
-
},
|
|
695
|
-
XRP: {
|
|
696
|
-
chain: 'XRP',
|
|
697
|
-
network: 'mainnet',
|
|
698
|
-
xpriv: 'xprvA58pn8bWSyoRGvEY97ALTHP4Dj6t47Q3PTBUEw78CF91kALMwhs7D2GutQSvpRN6ACR4RX4HbF3KmF7zDf48gR8nwG7DqLp6ezUcMiPHDtV',
|
|
699
|
-
expected: {
|
|
700
|
-
address: 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN',
|
|
701
|
-
privKey: 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C',
|
|
702
|
-
pubKey: '03DBEEC5E9E76DA09C5B502A67136BC2D73423E8902A7C35A8CBC0C5A6AC0469E8',
|
|
703
|
-
path: 'm/0/0'
|
|
704
|
-
}
|
|
705
|
-
},
|
|
706
|
-
SOL: {
|
|
707
|
-
chain: 'SOL',
|
|
708
|
-
network: 'mainnet',
|
|
709
|
-
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
710
|
-
expected: {
|
|
711
|
-
address: '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD',
|
|
712
|
-
privKey: 'E4Tp4nTgMCa5dtGwqvkWoMGrJC7FKRNjcpeFFXi4nNb9',
|
|
713
|
-
pubKey: '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66'
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
};
|
|
717
|
-
|
|
718
|
-
it('derives the expected BTC key material', async function () {
|
|
719
|
-
await createUnlockedWallet(walletVectors.BTC);
|
|
720
|
-
const result = await wallet.derivePrivateKey(false, 0);
|
|
721
|
-
expect(result).to.deep.equal(walletVectors.BTC.expected);
|
|
722
|
-
});
|
|
723
|
-
|
|
724
|
-
it('derives the expected ETH key material', async function () {
|
|
725
|
-
await createUnlockedWallet(walletVectors.ETH);
|
|
726
|
-
const result = await wallet.derivePrivateKey(false, 0);
|
|
727
|
-
expect(result).to.deep.equal(walletVectors.ETH.expected);
|
|
728
|
-
});
|
|
729
|
-
|
|
730
|
-
it('derives the expected XRP key material', async function () {
|
|
731
|
-
await createUnlockedWallet(walletVectors.XRP);
|
|
732
|
-
const result = await wallet.derivePrivateKey(false, 0);
|
|
733
|
-
expect(result).to.deep.equal(walletVectors.XRP.expected);
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
it('derives the expected SOL key material', async function () {
|
|
737
|
-
await createUnlockedWallet(walletVectors.SOL);
|
|
738
|
-
const result = await wallet.derivePrivateKey(false, 0);
|
|
739
|
-
expect(result).to.deep.equal(walletVectors.SOL.expected);
|
|
740
|
-
});
|
|
741
|
-
});
|
|
742
|
-
|
|
743
|
-
describe('unlock', function () {
|
|
744
|
-
it('performs wallet migration for previous wallet versions', async () => {
|
|
745
|
-
const fixture = ethMigrationTestWalletFixture;
|
|
746
|
-
const wallet = new Wallet(fixture.wallet as any);
|
|
747
|
-
const tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bitcore-client-migration-unlock-'));
|
|
748
|
-
const backupDir = path.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
749
|
-
const loadWalletStub = sandbox.stub();
|
|
750
|
-
const getStoredKeysStub = sandbox.stub();
|
|
751
|
-
const addKeysSafeStub = sandbox.stub();
|
|
752
|
-
const saveWalletStub = sandbox.stub();
|
|
753
|
-
|
|
754
|
-
wallet.storage = {
|
|
755
|
-
loadWallet: loadWalletStub,
|
|
756
|
-
getStoredKeys: getStoredKeysStub,
|
|
757
|
-
addKeysSafe: addKeysSafeStub,
|
|
758
|
-
saveWallet: saveWalletStub
|
|
759
|
-
} as any;
|
|
760
|
-
|
|
761
|
-
loadWalletStub.resolves(fixture.rawWallet);
|
|
762
|
-
getStoredKeysStub.resolves(fixture.storedKeys);
|
|
763
|
-
addKeysSafeStub.resolves();
|
|
764
|
-
saveWalletStub.resolves();
|
|
765
|
-
|
|
766
|
-
const homedirStub = sandbox.stub(os, 'homedir').returns(tempHomeDir);
|
|
767
|
-
sandbox.stub(wallet, 'getAddresses').resolves(fixture.addresses);
|
|
768
|
-
|
|
769
|
-
await wallet.unlock(fixture.password);
|
|
770
|
-
|
|
771
|
-
// Assert wallet.storage methods are called (from migrateWallet)
|
|
772
|
-
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
773
|
-
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
774
|
-
|
|
775
|
-
expect(wallet.version).to.equal(2);
|
|
776
|
-
expect(wallet.unlocked).to.exist;
|
|
777
|
-
expect(Buffer.isBuffer(wallet.unlocked?.encryptionKey)).to.equal(true);
|
|
778
|
-
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
779
|
-
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
780
|
-
expect(fs.readFileSync(path.join(backupDir, `${fixture.name}.v1.bak`), 'utf8')).to.equal(fixture.rawWallet);
|
|
781
|
-
expect(fs.readFileSync(path.join(backupDir, `${fixture.name}_keys.v1.bak`), 'utf8')).to.equal(JSON.stringify(fixture.storedKeys));
|
|
782
|
-
homedirStub.restore();
|
|
783
|
-
fs.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
784
|
-
});
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
describe('migrateWallet', function () {
|
|
788
|
-
let wallet: Wallet;
|
|
789
|
-
let decryptedEncryptionKey: Buffer | undefined;
|
|
790
|
-
let loadWalletStub: sinon.SinonStub;
|
|
791
|
-
let getStoredKeysStub: sinon.SinonStub;
|
|
792
|
-
let addKeysSafeStub: sinon.SinonStub;
|
|
793
|
-
let saveWalletStub: sinon.SinonStub;
|
|
794
|
-
let homedirStub: sinon.SinonStub;
|
|
795
|
-
let tempHomeDir: string;
|
|
796
|
-
let backupDir: string;
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
beforeEach(async function () {
|
|
800
|
-
wallet = new Wallet(ethMigrationTestWalletFixture.wallet as any);
|
|
801
|
-
tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bitcore-client-migration-'));
|
|
802
|
-
backupDir = path.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
803
|
-
wallet.storage = {
|
|
804
|
-
loadWallet: async () => undefined,
|
|
805
|
-
getStoredKeys: async () => [],
|
|
806
|
-
addKeysSafe: async () => undefined,
|
|
807
|
-
saveWallet: async () => undefined
|
|
808
|
-
} as any;
|
|
809
|
-
|
|
810
|
-
loadWalletStub = sandbox.stub(wallet.storage, 'loadWallet').resolves(ethMigrationTestWalletFixture.rawWallet);
|
|
811
|
-
getStoredKeysStub = sandbox.stub(wallet.storage, 'getStoredKeys').resolves(ethMigrationTestWalletFixture.storedKeys);
|
|
812
|
-
addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
813
|
-
saveWalletStub = sandbox.stub(wallet.storage, 'saveWallet').resolves();
|
|
814
|
-
homedirStub = sandbox.stub(os, 'homedir').returns(tempHomeDir);
|
|
815
|
-
sandbox.stub(wallet, 'getAddresses').resolves(ethMigrationTestWalletFixture.addresses);
|
|
816
|
-
|
|
817
|
-
decryptedEncryptionKey = Encryption.decryptEncryptionKey(
|
|
818
|
-
ethMigrationTestWalletFixture.wallet.encryptionKey,
|
|
819
|
-
ethMigrationTestWalletFixture.password,
|
|
820
|
-
true
|
|
821
|
-
) as Buffer;
|
|
822
|
-
});
|
|
823
|
-
|
|
824
|
-
afterEach(function () {
|
|
825
|
-
homedirStub?.restore();
|
|
826
|
-
if (tempHomeDir) {
|
|
827
|
-
fs.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
|
-
|
|
831
|
-
it('should back up existing raw wallet & keys (separately) before migration', async () => {
|
|
832
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
833
|
-
|
|
834
|
-
expect(loadWalletStub.calledOnceWithExactly({ name: ethMigrationTestWalletFixture.name, raw: true })).to.be.true;
|
|
835
|
-
expect(getStoredKeysStub.calledOnceWithExactly({
|
|
836
|
-
addresses: ethMigrationTestWalletFixture.addresses,
|
|
837
|
-
name: ethMigrationTestWalletFixture.name
|
|
838
|
-
})).to.be.true;
|
|
839
|
-
expect(fs.readFileSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v1.bak`), 'utf8')).to.equal(
|
|
840
|
-
ethMigrationTestWalletFixture.rawWallet
|
|
841
|
-
);
|
|
842
|
-
expect(fs.readFileSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}_keys.v1.bak`), 'utf8')).to.equal(
|
|
843
|
-
JSON.stringify(ethMigrationTestWalletFixture.storedKeys)
|
|
844
|
-
);
|
|
845
|
-
});
|
|
846
|
-
|
|
847
|
-
it('should overwrite existing keys', async () => {
|
|
848
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
849
|
-
|
|
850
|
-
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
851
|
-
const { name, keys } = addKeysSafeStub.firstCall.args[0];
|
|
852
|
-
expect(name).to.equal(ethMigrationTestWalletFixture.name);
|
|
853
|
-
expect(keys).to.have.length(ethMigrationTestWalletFixture.storedKeys.length);
|
|
854
|
-
|
|
855
|
-
const expectedKeys = ethMigrationTestWalletFixture.storedKeys.map(storedKey => {
|
|
856
|
-
// Added keys encrypt privKey, convert to hex, and store that on key.privKey
|
|
857
|
-
const decryptedKey = JSON.parse(
|
|
858
|
-
Encryption.decryptPrivateKey(storedKey.encKey, storedKey.pubKey, decryptedEncryptionKey)
|
|
859
|
-
);
|
|
860
|
-
const privKeyBuffer = CWC.Deriver.privateKeyToBuffer(wallet.chain, decryptedKey.privKey);
|
|
861
|
-
const encryptedPrivKey = Encryption.encryptBuffer(privKeyBuffer, storedKey.pubKey, decryptedEncryptionKey).toString('hex');
|
|
862
|
-
privKeyBuffer.fill(0);
|
|
863
|
-
return {
|
|
864
|
-
...decryptedKey,
|
|
865
|
-
privKey: encryptedPrivKey
|
|
866
|
-
};
|
|
867
|
-
});
|
|
868
|
-
|
|
869
|
-
// Stored keys exhibit new encryption process
|
|
870
|
-
expect(keys).to.deep.equal(expectedKeys);
|
|
871
|
-
});
|
|
872
|
-
|
|
873
|
-
it('should overwrite existing wallet', async () => {
|
|
874
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
875
|
-
|
|
876
|
-
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
877
|
-
const savedWallet = saveWalletStub.firstCall.args[0].wallet;
|
|
878
|
-
|
|
879
|
-
expect(savedWallet.version).to.equal(2);
|
|
880
|
-
expect(savedWallet.password).to.equal(ethMigrationTestWalletFixture.wallet.password);
|
|
881
|
-
expect(savedWallet.masterKey).to.not.equal(ethMigrationTestWalletFixture.wallet.masterKey);
|
|
882
|
-
|
|
883
|
-
const migratedMasterKey = JSON.parse(savedWallet.masterKey);
|
|
884
|
-
const originalMasterKey = JSON.parse(
|
|
885
|
-
Encryption.decryptPrivateKey(
|
|
886
|
-
ethMigrationTestWalletFixture.wallet.masterKey,
|
|
887
|
-
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
888
|
-
decryptedEncryptionKey
|
|
889
|
-
)
|
|
890
|
-
);
|
|
891
|
-
|
|
892
|
-
const migratedXpriv = Encryption.decryptToBuffer(
|
|
893
|
-
migratedMasterKey.xprivkey,
|
|
894
|
-
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
895
|
-
decryptedEncryptionKey
|
|
896
|
-
);
|
|
897
|
-
const migratedPrivateKey = Encryption.decryptToBuffer(
|
|
898
|
-
migratedMasterKey.privateKey,
|
|
899
|
-
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
900
|
-
decryptedEncryptionKey
|
|
901
|
-
);
|
|
902
|
-
|
|
903
|
-
expect(wallet.version).to.equal(2);
|
|
904
|
-
|
|
905
|
-
// Decrypted master keys same as prior master keys
|
|
906
|
-
expect(migratedXpriv.toString('hex')).to.equal(
|
|
907
|
-
CWC.BitcoreLib.encoding.Base58Check.decode(originalMasterKey.xprivkey).toString('hex')
|
|
908
|
-
);
|
|
909
|
-
expect(migratedPrivateKey.toString('hex')).to.equal(originalMasterKey.privateKey);
|
|
910
|
-
});
|
|
911
|
-
|
|
912
|
-
it('should throw if the raw wallet cannot be loaded', async () => {
|
|
913
|
-
loadWalletStub.resolves(undefined);
|
|
914
|
-
|
|
915
|
-
try {
|
|
916
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
917
|
-
expect.fail('Expected migrateWallet to throw');
|
|
918
|
-
} catch (err) {
|
|
919
|
-
expect(err.message).to.equal('Migration failed - wallet not found');
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
expect(getStoredKeysStub.called).to.equal(false);
|
|
923
|
-
expect(addKeysSafeStub.called).to.equal(false);
|
|
924
|
-
expect(saveWalletStub.called).to.equal(false);
|
|
925
|
-
});
|
|
926
|
-
|
|
927
|
-
it('should throw if the decrypted masterKey is malformed', async () => {
|
|
928
|
-
wallet.masterKey = Encryption.encryptPrivateKey(
|
|
929
|
-
JSON.stringify({ invalid: true }),
|
|
930
|
-
wallet.pubKey,
|
|
931
|
-
decryptedEncryptionKey.toString('hex')
|
|
932
|
-
);
|
|
933
|
-
|
|
934
|
-
try {
|
|
935
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
936
|
-
expect.fail('Expected migrateWallet to throw');
|
|
937
|
-
} catch (err) {
|
|
938
|
-
expect(err.message).to.equal('Migration failure: masterKey is not formatted as expected');
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
expect(addKeysSafeStub.called).to.equal(false);
|
|
942
|
-
expect(saveWalletStub.called).to.equal(false);
|
|
943
|
-
});
|
|
944
|
-
|
|
945
|
-
it('should throw if addKeysSafe fails and should not save the wallet', async () => {
|
|
946
|
-
addKeysSafeStub.rejects(new Error('write failed'));
|
|
947
|
-
|
|
948
|
-
try {
|
|
949
|
-
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
950
|
-
expect.fail('Expected migrateWallet to throw');
|
|
951
|
-
} catch (err) {
|
|
952
|
-
expect(err.message).to.equal(
|
|
953
|
-
'Migration failure: keys not successfully stored. Use backups to restore prior wallet and keys.'
|
|
954
|
-
);
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
958
|
-
expect(saveWalletStub.called).to.equal(false);
|
|
959
|
-
});
|
|
960
|
-
|
|
961
|
-
it('should no-op when the wallet is already on the current version', async () => {
|
|
962
|
-
wallet.version = 2;
|
|
963
|
-
const warnStub = sandbox.stub(console, 'warn');
|
|
964
|
-
|
|
965
|
-
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
966
|
-
|
|
967
|
-
expect(migratedWallet).to.equal(wallet);
|
|
968
|
-
expect(warnStub.calledOnceWithExactly('Wallet migration unnecessarily called - wallet is current version')).to.equal(true);
|
|
969
|
-
expect(loadWalletStub.called).to.equal(false);
|
|
970
|
-
expect(getStoredKeysStub.called).to.equal(false);
|
|
971
|
-
expect(addKeysSafeStub.called).to.equal(false);
|
|
972
|
-
expect(saveWalletStub.called).to.equal(false);
|
|
973
|
-
expect(fs.existsSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v2.bak`))).to.equal(false);
|
|
974
|
-
});
|
|
975
|
-
|
|
976
|
-
it('should no-op when the wallet version is newer than the current version', async () => {
|
|
977
|
-
wallet.version = 3;
|
|
978
|
-
const warnStub = sandbox.stub(console, 'warn');
|
|
979
|
-
|
|
980
|
-
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
981
|
-
|
|
982
|
-
expect(migratedWallet).to.equal(wallet);
|
|
983
|
-
expect(
|
|
984
|
-
warnStub.calledOnceWithExactly('Wallet version 3 greater than expected current wallet version 2')
|
|
985
|
-
).to.equal(true);
|
|
986
|
-
expect(loadWalletStub.called).to.equal(false);
|
|
987
|
-
expect(getStoredKeysStub.called).to.equal(false);
|
|
988
|
-
expect(addKeysSafeStub.called).to.equal(false);
|
|
989
|
-
expect(saveWalletStub.called).to.equal(false);
|
|
990
|
-
expect(fs.existsSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v3.bak`))).to.equal(false);
|
|
991
|
-
});
|
|
992
|
-
});
|
|
993
|
-
|
|
994
|
-
describe('getBalance', function() {
|
|
995
|
-
walletName = 'BitcoreClientTestGetBalance';
|
|
996
|
-
beforeEach(async function() {
|
|
997
|
-
wallet = await createWalletForTest({
|
|
998
|
-
name: walletName,
|
|
999
|
-
chain: 'MATIC',
|
|
1000
|
-
network: 'testnet',
|
|
1001
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
1002
|
-
password: 'abc123',
|
|
1003
|
-
storageType: 'Level',
|
|
1004
|
-
version: 0,
|
|
1005
|
-
});
|
|
1006
|
-
await wallet.unlock('abc123');
|
|
1007
|
-
});
|
|
1008
|
-
|
|
1009
|
-
it('should get correct token balance with conflicting token objects and old token object', async function() {
|
|
1010
|
-
// Old token object (no name)
|
|
1011
|
-
wallet.tokens.push({
|
|
1012
|
-
symbol: 'USDC',
|
|
1013
|
-
address: '0x123',
|
|
1014
|
-
decimals: '6',
|
|
1015
|
-
});
|
|
1016
|
-
wallet.tokens.push({
|
|
1017
|
-
symbol: 'USDC',
|
|
1018
|
-
address: '0xabc',
|
|
1019
|
-
decimals: '6',
|
|
1020
|
-
name: 'USDCn_m'
|
|
1021
|
-
});
|
|
1022
|
-
sinon.stub(wallet.client, 'getBalance').callsFake(async function(params) {
|
|
1023
|
-
switch (params.payload.tokenContractAddress) {
|
|
1024
|
-
case '0x123':
|
|
1025
|
-
return 1;
|
|
1026
|
-
case '0xabc':
|
|
1027
|
-
return 2;
|
|
1028
|
-
default:
|
|
1029
|
-
return 0;
|
|
1030
|
-
}
|
|
1031
|
-
});
|
|
1032
|
-
|
|
1033
|
-
const balance1 = await wallet.getBalance({ token: 'USDC' });
|
|
1034
|
-
const balance2 = await wallet.getBalance({ token: 'USDC', tokenName: 'USDC_m' });
|
|
1035
|
-
const balance3 = await wallet.getBalance({ tokenName: 'USDCn_m' });
|
|
1036
|
-
const balance4 = await wallet.getBalance({ token: 'USDC', tokenName: 'USDCn_m' });
|
|
1037
|
-
balance1.should.equal(1);
|
|
1038
|
-
balance2.should.equal(1);
|
|
1039
|
-
balance3.should.equal(2);
|
|
1040
|
-
balance4.should.equal(2);
|
|
1041
|
-
});
|
|
1042
|
-
});
|
|
1043
|
-
|
|
1044
|
-
describe('getTokenObj', function() {
|
|
1045
|
-
walletName = 'BitcoreClientTestGetTokenObj';
|
|
1046
|
-
beforeEach(async function() {
|
|
1047
|
-
wallet = await createWalletForTest({
|
|
1048
|
-
name: walletName,
|
|
1049
|
-
chain: 'MATIC',
|
|
1050
|
-
network: 'testnet',
|
|
1051
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
1052
|
-
password: 'abc123',
|
|
1053
|
-
storageType: 'Level',
|
|
1054
|
-
version: 0,
|
|
1055
|
-
});
|
|
1056
|
-
await wallet.unlock('abc123');
|
|
1057
|
-
});
|
|
1058
|
-
|
|
1059
|
-
it('should get correct token object', async function() {
|
|
1060
|
-
// Old token object (no name)
|
|
1061
|
-
wallet.tokens.push({
|
|
1062
|
-
symbol: 'USDC',
|
|
1063
|
-
address: '0x123',
|
|
1064
|
-
decimals: '6',
|
|
1065
|
-
});
|
|
1066
|
-
wallet.tokens.push({
|
|
1067
|
-
symbol: 'USDC',
|
|
1068
|
-
address: '0xabc',
|
|
1069
|
-
decimals: '6',
|
|
1070
|
-
name: 'USDC.e'
|
|
1071
|
-
});
|
|
1072
|
-
const obj1 = wallet.getTokenObj({ token: 'USDC' });
|
|
1073
|
-
const obj2 = wallet.getTokenObj({ token: 'USDC', tokenName: 'USDC.other' }); // no object with "USDC.other"
|
|
1074
|
-
const obj3 = wallet.getTokenObj({ token: 'USDC', tokenName: 'USDC.e' });
|
|
1075
|
-
const obj4 = wallet.getTokenObj({ tokenName: 'USDC.e' });
|
|
1076
|
-
obj1.address.should.equal('0x123');
|
|
1077
|
-
obj2.address.should.equal('0x123');
|
|
1078
|
-
obj3.address.should.equal('0xabc');
|
|
1079
|
-
obj4.address.should.equal('0xabc');
|
|
1080
|
-
});
|
|
1081
|
-
|
|
1082
|
-
it('should fallback to old token object if matching `token` is given', async function() {
|
|
1083
|
-
// Old token object (no name)
|
|
1084
|
-
wallet.tokens.push({
|
|
1085
|
-
symbol: 'USDC',
|
|
1086
|
-
address: '0x123',
|
|
1087
|
-
decimals: '6',
|
|
1088
|
-
});
|
|
1089
|
-
|
|
1090
|
-
const obj1 = wallet.getTokenObj({ token: 'USDC' });
|
|
1091
|
-
obj1.address.should.equal('0x123');
|
|
1092
|
-
const obj2 = wallet.getTokenObj({ token: 'USDC', tokenName: 'USDC.e' }); // falls back
|
|
1093
|
-
obj2.address.should.equal('0x123');
|
|
1094
|
-
try {
|
|
1095
|
-
const obj3 = wallet.getTokenObj({ tokenName: 'USDC.e' }); // token not given, so cannot fall back
|
|
1096
|
-
should.not.exist(obj3);
|
|
1097
|
-
} catch (err) {
|
|
1098
|
-
err.message.should.equal('USDC.e not found on wallet ' + walletName);
|
|
1099
|
-
}
|
|
1100
|
-
});
|
|
1101
|
-
});
|
|
1102
|
-
|
|
1103
|
-
describe('rmToken', function() {
|
|
1104
|
-
walletName = 'BitcoreClientTestRmToken';
|
|
1105
|
-
const usdcLegacyObj = {
|
|
1106
|
-
symbol: 'USDC',
|
|
1107
|
-
address: '0x123',
|
|
1108
|
-
decimals: '6',
|
|
1109
|
-
};
|
|
1110
|
-
|
|
1111
|
-
const usdcObj = {
|
|
1112
|
-
symbol: 'USDC',
|
|
1113
|
-
address: '0xabc',
|
|
1114
|
-
decimals: '6',
|
|
1115
|
-
name: 'USDCn'
|
|
1116
|
-
};
|
|
1117
|
-
|
|
1118
|
-
const daiObj = {
|
|
1119
|
-
symbol: 'DAI',
|
|
1120
|
-
address: '0x1a2b3c',
|
|
1121
|
-
decimals: '6',
|
|
1122
|
-
name: 'DAIn'
|
|
1123
|
-
};
|
|
1124
|
-
|
|
1125
|
-
beforeEach(async function() {
|
|
1126
|
-
wallet = await createWalletForTest({
|
|
1127
|
-
chain: 'ETH',
|
|
1128
|
-
network: 'mainnet',
|
|
1129
|
-
name: walletName,
|
|
1130
|
-
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
1131
|
-
password: 'abc123',
|
|
1132
|
-
lite: false,
|
|
1133
|
-
storageType,
|
|
1134
|
-
baseUrl,
|
|
1135
|
-
version: 0
|
|
1136
|
-
});
|
|
1137
|
-
|
|
1138
|
-
wallet.tokens = [
|
|
1139
|
-
usdcLegacyObj,
|
|
1140
|
-
usdcObj,
|
|
1141
|
-
daiObj
|
|
1142
|
-
];
|
|
1143
|
-
});
|
|
1144
|
-
|
|
1145
|
-
it('should remove a legacy token object', function() {
|
|
1146
|
-
wallet.rmToken({ tokenName: 'USDC' });
|
|
1147
|
-
wallet.tokens.length.should.equal(2);
|
|
1148
|
-
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
|
|
1149
|
-
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcObj);
|
|
1150
|
-
});
|
|
1151
|
-
|
|
1152
|
-
it('should remove a token object', function() {
|
|
1153
|
-
wallet.rmToken({ tokenName: 'USDCn' });
|
|
1154
|
-
wallet.tokens.length.should.equal(2);
|
|
1155
|
-
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
|
|
1156
|
-
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcLegacyObj);
|
|
1157
|
-
});
|
|
1158
|
-
|
|
1159
|
-
it('should remove the correct token object regardless of order', function() {
|
|
1160
|
-
wallet.tokens = [
|
|
1161
|
-
usdcObj,
|
|
1162
|
-
daiObj,
|
|
1163
|
-
usdcLegacyObj // this should be ordered after usdcObj
|
|
1164
|
-
];
|
|
1165
|
-
|
|
1166
|
-
wallet.rmToken({ tokenName: 'USDC' });
|
|
1167
|
-
wallet.tokens.length.should.equal(2);
|
|
1168
|
-
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
|
|
1169
|
-
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcObj);
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
it('should not remove any unmatched token object', function() {
|
|
1173
|
-
wallet.rmToken({ tokenName: 'BOGUS' });
|
|
1174
|
-
wallet.tokens.length.should.equal(3);
|
|
1175
|
-
});
|
|
1176
|
-
});
|
|
1177
|
-
});
|