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