@bitpay-labs/crypto-wallet-core 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 +9 -3
- package/ts_build/src/transactions/index.d.ts +1 -1
- package/ts_build/src/transactions/index.d.ts.map +1 -1
- package/ts_build/src/transactions/index.js.map +1 -1
- package/ts_build/src/types/derivation.d.ts +1 -0
- package/ts_build/src/types/derivation.d.ts.map +1 -1
- package/.mocharc.json +0 -6
- package/LICENSE +0 -21
- package/mcr.config.js +0 -17
- package/src/constants/address.ts +0 -7
- package/src/constants/algorithms.ts +0 -9
- package/src/constants/arc.ts +0 -4
- package/src/constants/chains.ts +0 -68
- package/src/constants/feeMinimums.ts +0 -23
- package/src/constants/index.ts +0 -40
- package/src/constants/tokens.ts +0 -233
- package/src/constants/units.ts +0 -311
- package/src/derivation/arb/index.ts +0 -3
- package/src/derivation/base/index.ts +0 -3
- package/src/derivation/bch/index.ts +0 -11
- package/src/derivation/btc/index.ts +0 -68
- package/src/derivation/doge/index.ts +0 -6
- package/src/derivation/eth/index.ts +0 -86
- package/src/derivation/index.ts +0 -140
- package/src/derivation/ltc/index.ts +0 -6
- package/src/derivation/matic/index.ts +0 -3
- package/src/derivation/op/index.ts +0 -3
- package/src/derivation/paths.ts +0 -45
- package/src/derivation/sol/index.ts +0 -81
- package/src/derivation/xrp/index.ts +0 -63
- package/src/index.ts +0 -48
- package/src/message/btc.ts +0 -28
- package/src/message/eth.ts +0 -47
- package/src/message/index.ts +0 -71
- package/src/message/sol.ts +0 -3
- package/src/message/xrp.ts +0 -4
- package/src/transactions/arb/index.ts +0 -14
- package/src/transactions/base/index.ts +0 -13
- package/src/transactions/bch/index.ts +0 -25
- package/src/transactions/btc/index.ts +0 -316
- package/src/transactions/doge/index.ts +0 -31
- package/src/transactions/erc20/abi.ts +0 -418
- package/src/transactions/erc20/index.ts +0 -57
- package/src/transactions/eth/index.ts +0 -192
- package/src/transactions/eth-multisig/abi.ts +0 -724
- package/src/transactions/eth-multisig/index.ts +0 -172
- package/src/transactions/index.ts +0 -85
- package/src/transactions/ltc/index.ts +0 -31
- package/src/transactions/matic/index.ts +0 -14
- package/src/transactions/matic-multisig/index.ts +0 -172
- package/src/transactions/op/index.ts +0 -14
- package/src/transactions/sol/index.ts +0 -306
- package/src/transactions/spl/index.ts +0 -192
- package/src/transactions/xrp/index.ts +0 -167
- package/src/types/derivation.ts +0 -34
- package/src/types/index.ts +0 -3
- package/src/types/message.ts +0 -15
- package/src/types/utils.ts +0 -1
- package/src/types/validation.ts +0 -4
- package/src/utils/bigint.ts +0 -238
- package/src/utils/index.ts +0 -188
- package/src/validation/arb/index.ts +0 -8
- package/src/validation/arc/index.ts +0 -8
- package/src/validation/base/index.ts +0 -8
- package/src/validation/bch/index.ts +0 -17
- package/src/validation/btc/index.ts +0 -17
- package/src/validation/doge/index.ts +0 -16
- package/src/validation/eth/index.ts +0 -29
- package/src/validation/index.ts +0 -45
- package/src/validation/ltc/index.ts +0 -16
- package/src/validation/matic/index.ts +0 -8
- package/src/validation/op/index.ts +0 -8
- package/src/validation/sol/index.ts +0 -34
- package/src/validation/xrp/index.ts +0 -50
- package/test/address.test.ts +0 -190
- package/test/bigint.test.ts +0 -373
- package/test/deriver.test.ts +0 -126
- package/test/transactions.test.ts +0 -2103
- package/test/utils.test.ts +0 -446
- package/test/validation.test.ts +0 -282
- package/tsconfig.json +0 -29
package/src/constants/units.ts
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
export const UNITS = {
|
|
2
|
-
btc: {
|
|
3
|
-
toSatoshis: 100000000,
|
|
4
|
-
full: {
|
|
5
|
-
maxDecimals: 8,
|
|
6
|
-
minDecimals: 8
|
|
7
|
-
},
|
|
8
|
-
short: {
|
|
9
|
-
maxDecimals: 6,
|
|
10
|
-
minDecimals: 2
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
bch: {
|
|
14
|
-
toSatoshis: 100000000,
|
|
15
|
-
full: {
|
|
16
|
-
maxDecimals: 8,
|
|
17
|
-
minDecimals: 8
|
|
18
|
-
},
|
|
19
|
-
short: {
|
|
20
|
-
maxDecimals: 6,
|
|
21
|
-
minDecimals: 2
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
sol: {
|
|
25
|
-
toSatoshis: 1e9,
|
|
26
|
-
full: {
|
|
27
|
-
maxDecimals: 9,
|
|
28
|
-
minDecimals: 9
|
|
29
|
-
},
|
|
30
|
-
short: {
|
|
31
|
-
maxDecimals: 6,
|
|
32
|
-
minDecimals: 2
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
eth: {
|
|
36
|
-
toSatoshis: 1e18,
|
|
37
|
-
full: {
|
|
38
|
-
maxDecimals: 8,
|
|
39
|
-
minDecimals: 8
|
|
40
|
-
},
|
|
41
|
-
short: {
|
|
42
|
-
maxDecimals: 6,
|
|
43
|
-
minDecimals: 2
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
matic: {
|
|
47
|
-
toSatoshis: 1e18,
|
|
48
|
-
full: {
|
|
49
|
-
maxDecimals: 8,
|
|
50
|
-
minDecimals: 8
|
|
51
|
-
},
|
|
52
|
-
short: {
|
|
53
|
-
maxDecimals: 6,
|
|
54
|
-
minDecimals: 2
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
arb: {
|
|
58
|
-
toSatoshis: 1e18,
|
|
59
|
-
full: {
|
|
60
|
-
maxDecimals: 8,
|
|
61
|
-
minDecimals: 8
|
|
62
|
-
},
|
|
63
|
-
short: {
|
|
64
|
-
maxDecimals: 6,
|
|
65
|
-
minDecimals: 2
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
base: {
|
|
69
|
-
toSatoshis: 1e18,
|
|
70
|
-
full: {
|
|
71
|
-
maxDecimals: 8,
|
|
72
|
-
minDecimals: 8
|
|
73
|
-
},
|
|
74
|
-
short: {
|
|
75
|
-
maxDecimals: 6,
|
|
76
|
-
minDecimals: 2
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
op: {
|
|
80
|
-
toSatoshis: 1e18,
|
|
81
|
-
full: {
|
|
82
|
-
maxDecimals: 8,
|
|
83
|
-
minDecimals: 8
|
|
84
|
-
},
|
|
85
|
-
short: {
|
|
86
|
-
maxDecimals: 6,
|
|
87
|
-
minDecimals: 2
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
arc: {
|
|
91
|
-
toSatoshis: 1e18,
|
|
92
|
-
full: {
|
|
93
|
-
maxDecimals: 6,
|
|
94
|
-
minDecimals: 6
|
|
95
|
-
},
|
|
96
|
-
short: {
|
|
97
|
-
maxDecimals: 6,
|
|
98
|
-
minDecimals: 2
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
xrp: {
|
|
102
|
-
toSatoshis: 1e6,
|
|
103
|
-
full: {
|
|
104
|
-
maxDecimals: 6,
|
|
105
|
-
minDecimals: 6
|
|
106
|
-
},
|
|
107
|
-
short: {
|
|
108
|
-
maxDecimals: 6,
|
|
109
|
-
minDecimals: 2
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
busd: {
|
|
113
|
-
toSatoshis: 1e18,
|
|
114
|
-
full: {
|
|
115
|
-
maxDecimals: 8,
|
|
116
|
-
minDecimals: 8
|
|
117
|
-
},
|
|
118
|
-
short: {
|
|
119
|
-
maxDecimals: 6,
|
|
120
|
-
minDecimals: 2
|
|
121
|
-
}
|
|
122
|
-
},
|
|
123
|
-
dai: {
|
|
124
|
-
toSatoshis: 1e18,
|
|
125
|
-
full: {
|
|
126
|
-
maxDecimals: 8,
|
|
127
|
-
minDecimals: 8
|
|
128
|
-
},
|
|
129
|
-
short: {
|
|
130
|
-
maxDecimals: 6,
|
|
131
|
-
minDecimals: 2
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
wbtc: {
|
|
135
|
-
toSatoshis: 1e8,
|
|
136
|
-
full: {
|
|
137
|
-
maxDecimals: 8,
|
|
138
|
-
minDecimals: 8
|
|
139
|
-
},
|
|
140
|
-
short: {
|
|
141
|
-
maxDecimals: 8,
|
|
142
|
-
minDecimals: 2
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
usdc: {
|
|
146
|
-
toSatoshis: 1e6,
|
|
147
|
-
full: {
|
|
148
|
-
maxDecimals: 8,
|
|
149
|
-
minDecimals: 8
|
|
150
|
-
},
|
|
151
|
-
short: {
|
|
152
|
-
maxDecimals: 6,
|
|
153
|
-
minDecimals: 2
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
pyusd: {
|
|
157
|
-
toSatoshis: 1e6,
|
|
158
|
-
full: {
|
|
159
|
-
maxDecimals: 8,
|
|
160
|
-
minDecimals: 8
|
|
161
|
-
},
|
|
162
|
-
short: {
|
|
163
|
-
maxDecimals: 6,
|
|
164
|
-
minDecimals: 2
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
usdp: {
|
|
168
|
-
toSatoshis: 1e18,
|
|
169
|
-
full: {
|
|
170
|
-
maxDecimals: 8,
|
|
171
|
-
minDecimals: 8
|
|
172
|
-
},
|
|
173
|
-
short: {
|
|
174
|
-
maxDecimals: 6,
|
|
175
|
-
minDecimals: 2
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
// backwards compatibility
|
|
179
|
-
pax: {
|
|
180
|
-
toSatoshis: 1e18,
|
|
181
|
-
full: {
|
|
182
|
-
maxDecimals: 8,
|
|
183
|
-
minDecimals: 8
|
|
184
|
-
},
|
|
185
|
-
short: {
|
|
186
|
-
maxDecimals: 6,
|
|
187
|
-
minDecimals: 2
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
gusd: {
|
|
191
|
-
toSatoshis: 1e2,
|
|
192
|
-
full: {
|
|
193
|
-
maxDecimals: 8,
|
|
194
|
-
minDecimals: 8
|
|
195
|
-
},
|
|
196
|
-
short: {
|
|
197
|
-
maxDecimals: 6,
|
|
198
|
-
minDecimals: 2
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
bit: {
|
|
202
|
-
toSatoshis: 100,
|
|
203
|
-
full: {
|
|
204
|
-
maxDecimals: 2,
|
|
205
|
-
minDecimals: 2
|
|
206
|
-
},
|
|
207
|
-
short: {
|
|
208
|
-
maxDecimals: 0,
|
|
209
|
-
minDecimals: 0
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
doge: {
|
|
213
|
-
toSatoshis: 100000000,
|
|
214
|
-
full: {
|
|
215
|
-
maxDecimals: 8,
|
|
216
|
-
minDecimals: 8
|
|
217
|
-
},
|
|
218
|
-
short: {
|
|
219
|
-
maxDecimals: 6,
|
|
220
|
-
minDecimals: 2
|
|
221
|
-
}
|
|
222
|
-
},
|
|
223
|
-
ltc: {
|
|
224
|
-
toSatoshis: 100000000,
|
|
225
|
-
full: {
|
|
226
|
-
maxDecimals: 8,
|
|
227
|
-
minDecimals: 8
|
|
228
|
-
},
|
|
229
|
-
short: {
|
|
230
|
-
maxDecimals: 6,
|
|
231
|
-
minDecimals: 2
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
shib: {
|
|
235
|
-
toSatoshis: 1e18,
|
|
236
|
-
full: {
|
|
237
|
-
maxDecimals: 8,
|
|
238
|
-
minDecimals: 8
|
|
239
|
-
},
|
|
240
|
-
short: {
|
|
241
|
-
maxDecimals: 6,
|
|
242
|
-
minDecimals: 2
|
|
243
|
-
}
|
|
244
|
-
},
|
|
245
|
-
ape: {
|
|
246
|
-
toSatoshis: 1e18,
|
|
247
|
-
full: {
|
|
248
|
-
maxDecimals: 8,
|
|
249
|
-
minDecimals: 8
|
|
250
|
-
},
|
|
251
|
-
short: {
|
|
252
|
-
maxDecimals: 6,
|
|
253
|
-
minDecimals: 2
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
euroc: {
|
|
257
|
-
toSatoshis: 1e6,
|
|
258
|
-
full: {
|
|
259
|
-
maxDecimals: 8,
|
|
260
|
-
minDecimals: 8
|
|
261
|
-
},
|
|
262
|
-
short: {
|
|
263
|
-
maxDecimals: 6,
|
|
264
|
-
minDecimals: 2
|
|
265
|
-
}
|
|
266
|
-
},
|
|
267
|
-
usdt: {
|
|
268
|
-
toSatoshis: 1e6,
|
|
269
|
-
full: {
|
|
270
|
-
maxDecimals: 8,
|
|
271
|
-
minDecimals: 8
|
|
272
|
-
},
|
|
273
|
-
short: {
|
|
274
|
-
maxDecimals: 6,
|
|
275
|
-
minDecimals: 2
|
|
276
|
-
}
|
|
277
|
-
},
|
|
278
|
-
'usdc.e': {
|
|
279
|
-
toSatoshis: 1e6,
|
|
280
|
-
full: {
|
|
281
|
-
maxDecimals: 8,
|
|
282
|
-
minDecimals: 8
|
|
283
|
-
},
|
|
284
|
-
short: {
|
|
285
|
-
maxDecimals: 6,
|
|
286
|
-
minDecimals: 2
|
|
287
|
-
}
|
|
288
|
-
},
|
|
289
|
-
weth: {
|
|
290
|
-
toSatoshis: 1e18,
|
|
291
|
-
full: {
|
|
292
|
-
maxDecimals: 8,
|
|
293
|
-
minDecimals: 8
|
|
294
|
-
},
|
|
295
|
-
short: {
|
|
296
|
-
maxDecimals: 6,
|
|
297
|
-
minDecimals: 2
|
|
298
|
-
}
|
|
299
|
-
},
|
|
300
|
-
xaut: {
|
|
301
|
-
toSatoshis: 1e6,
|
|
302
|
-
full: {
|
|
303
|
-
maxDecimals: 8,
|
|
304
|
-
minDecimals: 8
|
|
305
|
-
},
|
|
306
|
-
short: {
|
|
307
|
-
maxDecimals: 6,
|
|
308
|
-
minDecimals: 2
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import BitcoreLibCash from '@bitpay-labs/bitcore-lib-cash';
|
|
2
|
-
import { AbstractBitcoreLibDeriver } from '../btc';
|
|
3
|
-
|
|
4
|
-
export class BchDeriver extends AbstractBitcoreLibDeriver {
|
|
5
|
-
bitcoreLib = BitcoreLibCash;
|
|
6
|
-
|
|
7
|
-
getAddress(network: string, pubKey, addressType: string) {
|
|
8
|
-
pubKey = new this.bitcoreLib.PublicKey(pubKey);
|
|
9
|
-
return new this.bitcoreLib.Address(pubKey, network, addressType).toString(true);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import BitcoreLib from '@bitpay-labs/bitcore-lib';
|
|
2
|
-
import type { IDeriver } from '../../types/derivation';
|
|
3
|
-
|
|
4
|
-
export abstract class AbstractBitcoreLibDeriver implements IDeriver {
|
|
5
|
-
public abstract bitcoreLib;
|
|
6
|
-
|
|
7
|
-
deriveAddress(network, pubKey, addressIndex, isChange, addressType) {
|
|
8
|
-
const changeNum = isChange ? 1 : 0;
|
|
9
|
-
const path = `m/${changeNum}/${addressIndex}`;
|
|
10
|
-
return this.deriveAddressWithPath(network, pubKey, path, addressType);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
derivePrivateKey(network, xPriv, addressIndex, isChange, addressType) {
|
|
14
|
-
const changeNum = isChange ? 1 : 0;
|
|
15
|
-
const path = `m/${changeNum}/${addressIndex}`;
|
|
16
|
-
return this.derivePrivateKeyWithPath(network, xPriv, path, addressType);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
deriveAddressWithPath(network: string, xpubKey: string, path: string, addressType: string) {
|
|
20
|
-
const xpub = new this.bitcoreLib.HDPublicKey(xpubKey, network);
|
|
21
|
-
return this.getAddress(network, xpub.derive(path).publicKey, addressType);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
derivePrivateKeyWithPath(network: string, xprivKey: string, path: string, addressType: string) {
|
|
25
|
-
const xpriv = new this.bitcoreLib.HDPrivateKey(xprivKey, network);
|
|
26
|
-
const privKey = xpriv.deriveChild(path).privateKey;
|
|
27
|
-
const pubKey = privKey.publicKey;
|
|
28
|
-
const address = this.getAddress(network, pubKey, addressType);
|
|
29
|
-
return { address, privKey: privKey.toString(), pubKey: pubKey.toString(), path };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
getAddress(network: string, pubKey, addressType: string) {
|
|
33
|
-
pubKey = new this.bitcoreLib.PublicKey(pubKey);
|
|
34
|
-
return new this.bitcoreLib.Address(pubKey, network, addressType).toString();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
getPublicKey(network: string, privKey: Buffer): string {
|
|
38
|
-
if (!Buffer.isBuffer(privKey)) {
|
|
39
|
-
throw new Error('Expected privKey to be a Buffer');
|
|
40
|
-
}
|
|
41
|
-
// Force compressed pubkey (buffer does not encode compression flag)
|
|
42
|
-
const bn = this.bitcoreLib.crypto.BN.fromBuffer(privKey);
|
|
43
|
-
const key = new this.bitcoreLib.PrivateKey({ bn, network, compressed: true });
|
|
44
|
-
return key.publicKey.toString();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @returns {Buffer} raw secpk1 private key buffer (32 bytes, big-endian)
|
|
49
|
-
* @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
|
|
50
|
-
*/
|
|
51
|
-
privateKeyToBuffer(privKey: Buffer | string): Buffer {
|
|
52
|
-
if (Buffer.isBuffer(privKey)) return privKey; // forward compatibility
|
|
53
|
-
if (typeof privKey !== 'string') throw new Error(`Expected key to be a string, got ${typeof privKey}`);
|
|
54
|
-
|
|
55
|
-
const key = new this.bitcoreLib.PrivateKey(privKey);
|
|
56
|
-
return key.toBuffer();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
bufferToPrivateKey_TEMP(buf: Buffer, network: string): string {
|
|
60
|
-
// force compressed WIF without mutating instances
|
|
61
|
-
const bn = this.bitcoreLib.crypto.BN.fromBuffer(buf);
|
|
62
|
-
const key = new this.bitcoreLib.PrivateKey({ bn, network, compressed: true });
|
|
63
|
-
return key.toWIF();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
export class BtcDeriver extends AbstractBitcoreLibDeriver {
|
|
67
|
-
bitcoreLib = BitcoreLib;
|
|
68
|
-
}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import BitcoreLib from '@bitpay-labs/bitcore-lib';
|
|
2
|
-
import Web3 from 'web3';
|
|
3
|
-
import type { IDeriver } from '../../types/derivation';
|
|
4
|
-
|
|
5
|
-
export class EthDeriver implements IDeriver {
|
|
6
|
-
padTo32(msg) {
|
|
7
|
-
while (msg.length < 32) {
|
|
8
|
-
msg = Buffer.concat([Buffer.from([0]), msg]);
|
|
9
|
-
}
|
|
10
|
-
if (msg.length !== 32) {
|
|
11
|
-
throw new Error(`invalid key length: ${msg.length}`);
|
|
12
|
-
}
|
|
13
|
-
return msg;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
deriveAddress(network, xpubkey, addressIndex, isChange) {
|
|
17
|
-
const changeNum = isChange ? 1 : 0;
|
|
18
|
-
const path = `m/${changeNum}/${addressIndex}`;
|
|
19
|
-
return this.deriveAddressWithPath(network, xpubkey, path);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
addressFromPublicKeyBuffer(pubKey: Buffer): string {
|
|
23
|
-
const ecPoint = new BitcoreLib.PublicKey.fromBuffer(pubKey).point;
|
|
24
|
-
const x = ecPoint.getX().toBuffer({ size: 32 });
|
|
25
|
-
const y = ecPoint.getY().toBuffer({ size: 32 });
|
|
26
|
-
const paddedBuffer = Buffer.concat([x, y]);
|
|
27
|
-
const address = Web3.utils.keccak256(`0x${paddedBuffer.toString('hex')}`).slice(26);
|
|
28
|
-
return Web3.utils.toChecksumAddress(address);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
derivePrivateKey(network, xPriv, addressIndex, isChange) {
|
|
32
|
-
const changeNum = isChange ? 1 : 0;
|
|
33
|
-
const path = `m/${changeNum}/${addressIndex}`;
|
|
34
|
-
return this.derivePrivateKeyWithPath(network, xPriv, path);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
deriveAddressWithPath(network: string, xpubKey: string, path: string) {
|
|
38
|
-
const xpub = new BitcoreLib.HDPublicKey(xpubKey, network);
|
|
39
|
-
const derived = xpub.derive(path).publicKey;
|
|
40
|
-
return this.addressFromPublicKeyBuffer(derived.toBuffer());
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
derivePrivateKeyWithPath(network: string, xprivKey: string, path: string) {
|
|
44
|
-
const xpriv = new BitcoreLib.HDPrivateKey(xprivKey, network);
|
|
45
|
-
const derivedPrivKey = xpriv.derive(path);
|
|
46
|
-
const privKey = derivedPrivKey.privateKey.toString('hex');
|
|
47
|
-
const pubKeyObj = derivedPrivKey.publicKey;
|
|
48
|
-
const pubKey = pubKeyObj.toString('hex');
|
|
49
|
-
const pubKeyBuffer = pubKeyObj.toBuffer();
|
|
50
|
-
const address = this.addressFromPublicKeyBuffer(pubKeyBuffer);
|
|
51
|
-
return { address, privKey, pubKey, path };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
getAddress(network: string, pubKey) {
|
|
55
|
-
pubKey = new BitcoreLib.PublicKey(pubKey, network); // network not needed here since ETH doesn't differentiate addresses by network.
|
|
56
|
-
return this.addressFromPublicKeyBuffer(pubKey.toBuffer());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getPublicKey(network: string, privKey: Buffer): string {
|
|
60
|
-
if (!Buffer.isBuffer(privKey)) {
|
|
61
|
-
throw new Error('Expected privKey to be a Buffer');
|
|
62
|
-
}
|
|
63
|
-
const bn = BitcoreLib.crypto.BN.fromBuffer(privKey);
|
|
64
|
-
const key = new BitcoreLib.PrivateKey({ bn, network, compressed: true });
|
|
65
|
-
return key.publicKey.toString('hex');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @param {Buffer | string} privKey - expects hex-encoded string, as returned from EthDeriver.derivePrivateKey
|
|
70
|
-
* @returns {Buffer}
|
|
71
|
-
* @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
|
|
72
|
-
*/
|
|
73
|
-
privateKeyToBuffer(privKey: Buffer | string): Buffer {
|
|
74
|
-
if (Buffer.isBuffer(privKey)) return privKey;
|
|
75
|
-
if (typeof privKey !== 'string') throw new Error(`Expected string, got ${typeof privKey}`);
|
|
76
|
-
if (privKey.startsWith('0x')) {
|
|
77
|
-
privKey = privKey.slice(2);
|
|
78
|
-
};
|
|
79
|
-
// Expects to match return from derivePrivateKey's privKey.
|
|
80
|
-
return Buffer.from(privKey, 'hex');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
bufferToPrivateKey_TEMP(buf: Buffer, _network: string): string {
|
|
84
|
-
return buf.toString('hex');
|
|
85
|
-
}
|
|
86
|
-
}
|
package/src/derivation/index.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { ArbDeriver } from './arb';
|
|
2
|
-
import { BaseDeriver } from './base';
|
|
3
|
-
import { BchDeriver } from './bch';
|
|
4
|
-
import { BtcDeriver } from './btc';
|
|
5
|
-
import { DogeDeriver } from './doge';
|
|
6
|
-
import { EthDeriver } from './eth';
|
|
7
|
-
import { LtcDeriver } from './ltc';
|
|
8
|
-
import { MaticDeriver } from './matic';
|
|
9
|
-
import { OpDeriver } from './op';
|
|
10
|
-
import { Paths } from './paths';
|
|
11
|
-
import { SolDeriver } from './sol';
|
|
12
|
-
import { XrpDeriver } from './xrp';
|
|
13
|
-
import type { IDeriver } from '../types/derivation';
|
|
14
|
-
|
|
15
|
-
const derivers: { [chain: string]: IDeriver } = {
|
|
16
|
-
BTC: new BtcDeriver(),
|
|
17
|
-
BCH: new BchDeriver(),
|
|
18
|
-
ETH: new EthDeriver(),
|
|
19
|
-
XRP: new XrpDeriver(),
|
|
20
|
-
DOGE: new DogeDeriver(),
|
|
21
|
-
LTC: new LtcDeriver(),
|
|
22
|
-
MATIC: new MaticDeriver(),
|
|
23
|
-
ARB: new ArbDeriver(),
|
|
24
|
-
BASE: new BaseDeriver(),
|
|
25
|
-
OP: new OpDeriver(),
|
|
26
|
-
ARC: new EthDeriver(),
|
|
27
|
-
SOL: new SolDeriver()
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export class DeriverProxy {
|
|
31
|
-
private get(chain) {
|
|
32
|
-
const normalizedChain = chain.toUpperCase();
|
|
33
|
-
return derivers[normalizedChain];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* This is derives addresses using the conventional paths.
|
|
38
|
-
* @param chain
|
|
39
|
-
* @param network
|
|
40
|
-
* @param xpubKey
|
|
41
|
-
* @param addressIndex
|
|
42
|
-
* @param isChange
|
|
43
|
-
* @param addressType
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
deriveAddress(chain, network, xpubKey, addressIndex, isChange, addressType?) {
|
|
47
|
-
return this.get(chain).deriveAddress(network, xpubKey, addressIndex, isChange, addressType);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* This derives keys/addresses using the conventional paths.
|
|
52
|
-
* @param chain
|
|
53
|
-
* @param network
|
|
54
|
-
* @param privKey
|
|
55
|
-
* @param addressIndex
|
|
56
|
-
* @param isChange
|
|
57
|
-
* @param addressType
|
|
58
|
-
* @returns
|
|
59
|
-
*/
|
|
60
|
-
derivePrivateKey(chain, network, privKey, addressIndex, isChange, addressType?) {
|
|
61
|
-
return this.get(chain).derivePrivateKey(network, privKey, addressIndex, isChange, addressType);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* This derives addresses on a specific path.
|
|
66
|
-
* This should probably only be used when importing from another wallet
|
|
67
|
-
* where known paths are provided with their keys. Most of the BitPay
|
|
68
|
-
* codebase uses `deriveAddress()`
|
|
69
|
-
* @param chain
|
|
70
|
-
* @param network
|
|
71
|
-
* @param xpubKey
|
|
72
|
-
* @param path
|
|
73
|
-
* @param addressType
|
|
74
|
-
* @returns
|
|
75
|
-
*/
|
|
76
|
-
deriveAddressWithPath(chain, network, xpubKey, path, addressType) {
|
|
77
|
-
return this.get(chain).deriveAddressWithPath(network, xpubKey, path, addressType);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* This derives keys/addresses on a specific path.
|
|
82
|
-
* This should probably only be used when importing from another wallet
|
|
83
|
-
* where known paths are provided with their keys. Most of the BitPay
|
|
84
|
-
* codebase uses `derivePrivateKey()`
|
|
85
|
-
* @param chain
|
|
86
|
-
* @param network
|
|
87
|
-
* @param xprivKey
|
|
88
|
-
* @param path
|
|
89
|
-
* @param addressType
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
derivePrivateKeyWithPath(chain, network, xprivKey, path, addressType) {
|
|
93
|
-
return this.get(chain).derivePrivateKeyWithPath(network, xprivKey, path, addressType);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* This is a simple function for getting an address from a
|
|
98
|
-
* given pub key and chain. There is no derivation happening.
|
|
99
|
-
* @param chain
|
|
100
|
-
* @param network
|
|
101
|
-
* @param pubKey
|
|
102
|
-
* @param addressType
|
|
103
|
-
* @returns
|
|
104
|
-
*/
|
|
105
|
-
getAddress(chain, network, pubKey, addressType?) {
|
|
106
|
-
return this.get(chain).getAddress(network, pubKey, addressType);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Caller responsible for cleaning up privKey buffer
|
|
111
|
-
*/
|
|
112
|
-
getPublicKey(chain, network, privKey: Buffer) {
|
|
113
|
-
return this.get(chain).getPublicKey(network, privKey);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
pathFor(chain, network, account = 0) {
|
|
117
|
-
const normalizedChain = chain.toUpperCase();
|
|
118
|
-
const accountStr = `${account}'`;
|
|
119
|
-
const chainConfig = Paths[normalizedChain];
|
|
120
|
-
if (chainConfig) {
|
|
121
|
-
if (chainConfig[network]) {
|
|
122
|
-
return chainConfig[network] + accountStr;
|
|
123
|
-
} else {
|
|
124
|
-
return chainConfig.default + accountStr;
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
return Paths.BTC.default + accountStr;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
privateKeyToBuffer(chain, privateKey: Buffer | string): Buffer {
|
|
132
|
-
return this.get(chain).privateKeyToBuffer(privateKey);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
bufferToPrivateKey_TEMP(chain: string, network: string, buf: Buffer): string {
|
|
136
|
-
return this.get(chain).bufferToPrivateKey_TEMP(buf, network);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export default new DeriverProxy();
|
package/src/derivation/paths.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export const Paths = {
|
|
2
|
-
BTC: {
|
|
3
|
-
mainnet: "m/44'/0'/",
|
|
4
|
-
livenet: "m/44'/0'/",
|
|
5
|
-
default: "m/44'/1'/"
|
|
6
|
-
},
|
|
7
|
-
BCH: {
|
|
8
|
-
mainnet: "m/44'/145'/",
|
|
9
|
-
livenet: "m/44'/145'/",
|
|
10
|
-
default: "m/44'/1'/"
|
|
11
|
-
},
|
|
12
|
-
ETH: {
|
|
13
|
-
default: "m/44'/60'/"
|
|
14
|
-
},
|
|
15
|
-
XRP: {
|
|
16
|
-
default: "m/44'/144'/"
|
|
17
|
-
},
|
|
18
|
-
DOGE: {
|
|
19
|
-
default: "m/44'/3'/"
|
|
20
|
-
},
|
|
21
|
-
LTC: {
|
|
22
|
-
default: "m/44'/2'/"
|
|
23
|
-
},
|
|
24
|
-
MATIC: {
|
|
25
|
-
default: "m/44'/60'/", // the official matic derivation path is 966 but users will expect address to be same as ETH
|
|
26
|
-
},
|
|
27
|
-
ARB: {
|
|
28
|
-
default: "m/44'/60'/",
|
|
29
|
-
},
|
|
30
|
-
OP: {
|
|
31
|
-
default: "m/44'/60'/",
|
|
32
|
-
},
|
|
33
|
-
BASE: {
|
|
34
|
-
default: "m/44'/60'/",
|
|
35
|
-
},
|
|
36
|
-
ARC: {
|
|
37
|
-
default: "m/44'/60'/",
|
|
38
|
-
},
|
|
39
|
-
SOL: {
|
|
40
|
-
default: "m/44'/501'/",
|
|
41
|
-
},
|
|
42
|
-
default: {
|
|
43
|
-
testnet: "m/44'/1'/"
|
|
44
|
-
}
|
|
45
|
-
};
|