@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.
Files changed (81) hide show
  1. package/package.json +9 -3
  2. package/ts_build/src/transactions/index.d.ts +1 -1
  3. package/ts_build/src/transactions/index.d.ts.map +1 -1
  4. package/ts_build/src/transactions/index.js.map +1 -1
  5. package/ts_build/src/types/derivation.d.ts +1 -0
  6. package/ts_build/src/types/derivation.d.ts.map +1 -1
  7. package/.mocharc.json +0 -6
  8. package/LICENSE +0 -21
  9. package/mcr.config.js +0 -17
  10. package/src/constants/address.ts +0 -7
  11. package/src/constants/algorithms.ts +0 -9
  12. package/src/constants/arc.ts +0 -4
  13. package/src/constants/chains.ts +0 -68
  14. package/src/constants/feeMinimums.ts +0 -23
  15. package/src/constants/index.ts +0 -40
  16. package/src/constants/tokens.ts +0 -233
  17. package/src/constants/units.ts +0 -311
  18. package/src/derivation/arb/index.ts +0 -3
  19. package/src/derivation/base/index.ts +0 -3
  20. package/src/derivation/bch/index.ts +0 -11
  21. package/src/derivation/btc/index.ts +0 -68
  22. package/src/derivation/doge/index.ts +0 -6
  23. package/src/derivation/eth/index.ts +0 -86
  24. package/src/derivation/index.ts +0 -140
  25. package/src/derivation/ltc/index.ts +0 -6
  26. package/src/derivation/matic/index.ts +0 -3
  27. package/src/derivation/op/index.ts +0 -3
  28. package/src/derivation/paths.ts +0 -45
  29. package/src/derivation/sol/index.ts +0 -81
  30. package/src/derivation/xrp/index.ts +0 -63
  31. package/src/index.ts +0 -48
  32. package/src/message/btc.ts +0 -28
  33. package/src/message/eth.ts +0 -47
  34. package/src/message/index.ts +0 -71
  35. package/src/message/sol.ts +0 -3
  36. package/src/message/xrp.ts +0 -4
  37. package/src/transactions/arb/index.ts +0 -14
  38. package/src/transactions/base/index.ts +0 -13
  39. package/src/transactions/bch/index.ts +0 -25
  40. package/src/transactions/btc/index.ts +0 -316
  41. package/src/transactions/doge/index.ts +0 -31
  42. package/src/transactions/erc20/abi.ts +0 -418
  43. package/src/transactions/erc20/index.ts +0 -57
  44. package/src/transactions/eth/index.ts +0 -192
  45. package/src/transactions/eth-multisig/abi.ts +0 -724
  46. package/src/transactions/eth-multisig/index.ts +0 -172
  47. package/src/transactions/index.ts +0 -85
  48. package/src/transactions/ltc/index.ts +0 -31
  49. package/src/transactions/matic/index.ts +0 -14
  50. package/src/transactions/matic-multisig/index.ts +0 -172
  51. package/src/transactions/op/index.ts +0 -14
  52. package/src/transactions/sol/index.ts +0 -306
  53. package/src/transactions/spl/index.ts +0 -192
  54. package/src/transactions/xrp/index.ts +0 -167
  55. package/src/types/derivation.ts +0 -34
  56. package/src/types/index.ts +0 -3
  57. package/src/types/message.ts +0 -15
  58. package/src/types/utils.ts +0 -1
  59. package/src/types/validation.ts +0 -4
  60. package/src/utils/bigint.ts +0 -238
  61. package/src/utils/index.ts +0 -188
  62. package/src/validation/arb/index.ts +0 -8
  63. package/src/validation/arc/index.ts +0 -8
  64. package/src/validation/base/index.ts +0 -8
  65. package/src/validation/bch/index.ts +0 -17
  66. package/src/validation/btc/index.ts +0 -17
  67. package/src/validation/doge/index.ts +0 -16
  68. package/src/validation/eth/index.ts +0 -29
  69. package/src/validation/index.ts +0 -45
  70. package/src/validation/ltc/index.ts +0 -16
  71. package/src/validation/matic/index.ts +0 -8
  72. package/src/validation/op/index.ts +0 -8
  73. package/src/validation/sol/index.ts +0 -34
  74. package/src/validation/xrp/index.ts +0 -50
  75. package/test/address.test.ts +0 -190
  76. package/test/bigint.test.ts +0 -373
  77. package/test/deriver.test.ts +0 -126
  78. package/test/transactions.test.ts +0 -2103
  79. package/test/utils.test.ts +0 -446
  80. package/test/validation.test.ts +0 -282
  81. package/tsconfig.json +0 -29
@@ -1,316 +0,0 @@
1
- import assert from 'assert';
2
- import BitcoreLib from '@bitpay-labs/bitcore-lib';
3
- import type { Key } from '../../types/derivation';
4
-
5
- const $ = BitcoreLib.util.preconditions;
6
-
7
- interface TssSig {
8
- r: string;
9
- s: string;
10
- v: number;
11
- pubKey: string;
12
- };
13
-
14
- export class BTCTxProvider {
15
- lib = BitcoreLib;
16
-
17
- selectCoins(
18
- recipients: Array<{ amount: number | string }>,
19
- utxos: EveryUtxoType[],
20
- fee?: number
21
- ): EveryUtxoType[] {
22
- // Only sort by block height if utxos are bitcore-node style
23
- if (utxos.length > 0 && utxos.every(utxo => utxo.mintHeight != undefined)) {
24
- utxos = utxos.sort(function(a, b) {
25
- return a.mintHeight - b.mintHeight;
26
- });
27
- }
28
-
29
- let index = 0;
30
- let utxoSum = 0;
31
- const recepientSum = recipients.reduce((sum, cur) => sum + Number(cur.amount), fee || 0);
32
- while (utxoSum < recepientSum) {
33
- assert(index < utxos.length, 'insufficient funds');
34
- const utxo = utxos[index];
35
- utxoSum += Number(utxo.satoshis ?? utxo.value ?? this.lib.Unit.fromBTC(utxo.amount ?? 0).toSatoshis());
36
- index += 1;
37
- }
38
- const filteredUtxos = utxos.slice(0, index);
39
- return filteredUtxos;
40
- }
41
-
42
- /**
43
- * Standardize utxo for internal functionality.
44
- * Accepts either a bitcore-node or a lib (bitcore-lib, bitcore-lib-cash, etc.) utxo.
45
- * Handles both lib style utxos: UnspentOutput properties and UnspentOutput.toObject properties.
46
- *
47
- * @param utxo either a bitcore-node or lib utxo
48
- * @returns utxo in the standard, internally used format
49
- */
50
- standardizeUtxo(utxo: EveryUtxoType): UtxoType {
51
- const outputIndex = utxo.outputIndex ?? utxo.mintIndex ?? utxo.vout;
52
- const satoshis = utxo.satoshis ?? utxo.value ?? (utxo.amount != undefined ? this.lib.Unit.fromBTC(utxo.amount ?? 0).toSatoshis() : undefined);
53
- const txId = utxo.txId ?? utxo.mintTxid ?? utxo.txid;
54
- const script = utxo.script ?? utxo.scriptPubKey;
55
- $.checkArgument(outputIndex != undefined, 'output index required (outputIndex|mintIndex|vout)');
56
- $.checkArgument(satoshis != undefined, 'satoshis required (satoshis|value|amount)');
57
- $.checkArgument(txId, 'txid required (txid|txId|mintTxid)');
58
- $.checkArgument(script || utxo.address, 'script or address required (script|scriptPubKey|address)');
59
-
60
- return {
61
- satoshis: Number(satoshis),
62
- txId,
63
- outputIndex: Number(outputIndex),
64
- script: script != undefined ? new this.lib.Script(script).toHex() : undefined,
65
- address: utxo.address != undefined ? new this.lib.Address(utxo.address).toString() : undefined
66
- };
67
- }
68
-
69
- create(params: {
70
- recipients: Array<{ address: string; amount: number | string }>;
71
- utxos?: EveryUtxoType[];
72
- change?: string;
73
- feeRate?: number | string;
74
- fee?: number | string;
75
- isSweep?: boolean;
76
- replaceByFee?: boolean;
77
- lockUntilDate?: number;
78
- lockUntilBlock?: number;
79
- }): string {
80
- const { recipients, utxos = [], change, feeRate, fee, isSweep, replaceByFee, lockUntilDate, lockUntilBlock } = params;
81
- const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, Number(fee));
82
- const btcUtxos = filteredUtxos.map(utxo => this.standardizeUtxo(utxo));
83
- const tx = new this.lib.Transaction().from(btcUtxos);
84
- if (fee) {
85
- tx.fee(fee);
86
- }
87
- if (feeRate) {
88
- tx.feePerByte(Number(feeRate));
89
- }
90
- if (change) {
91
- tx.change(change);
92
- }
93
- for (const recipient of recipients) {
94
- tx.to(recipient.address, Number(recipient.amount));
95
- }
96
- if (replaceByFee && typeof tx.enableRBF === 'function') {
97
- tx.enableRBF();
98
- }
99
- if (lockUntilBlock > 0) {
100
- tx.lockUntilBlockHeight(lockUntilBlock);
101
- } else if (lockUntilDate > 0) {
102
- tx.lockUntilDate(lockUntilDate);
103
- }
104
- return tx.uncheckedSerialize();
105
- }
106
-
107
- getSignature() {
108
- throw new Error('function getSignature not implemented for UTXO coins');
109
- }
110
-
111
- transformSignatureObject(params: { obj: any; sigtype?: number }): string {
112
- const { obj, sigtype } = params;
113
- const { v } = obj;
114
- let { r, s, i, nhashtype } = obj;
115
- if (typeof r === 'string') {
116
- r = Buffer.from(r.startsWith('0x') ? r.slice(2) : r, 'hex');
117
- } else if (r instanceof Uint8Array || Array.isArray(r)) {
118
- r = Buffer.from(r);
119
- } else if (typeof r.toBuffer === 'function') {
120
- r = r.toBuffer();
121
- }
122
- r = this.lib.crypto.BN.fromBuffer(r);
123
-
124
- if (typeof s === 'string') {
125
- s = Buffer.from(s.startsWith('0x') ? s.slice(2) : s, 'hex');
126
- } else if (s instanceof Uint8Array || Array.isArray(s)) {
127
- s = Buffer.from(s);
128
- } else if (typeof s.toBuffer === 'function') {
129
- s = s.toBuffer();
130
- }
131
- s = this.lib.crypto.BN.fromBuffer(s);
132
-
133
- i = parseInt(i) || parseInt(v);
134
- nhashtype = sigtype ?? nhashtype;
135
-
136
- return new this.lib.crypto.Signature({ r, s, i, nhashtype }).toString();
137
- }
138
-
139
- applySignature(params: {
140
- tx: BitcoreLib.Transaction;
141
- signature: SignatureType;
142
- index: number;
143
- sigtype?: number;
144
- }): BitcoreLib.Transaction {
145
- const { index, sigtype, tx } = params;
146
- let { signature } = params;
147
- assert(tx instanceof this.lib.Transaction, 'tx must be an instance of Transaction');
148
- assert(signature instanceof this.lib.Transaction.Signature || (signature?.r && signature?.s), 'signature must be a valid signature object');
149
-
150
- if (signature.r) {
151
- const nhashtype = sigtype ?? signature.sigtype ?? signature.nhashtype ?? this.lib.crypto.Signature.SIGHASH_ALL;
152
- signature = new this.lib.Transaction.Signature({
153
- publicKey: signature.pubKey,
154
- inputIndex: index,
155
- outputIndex: tx.inputs[index].outputIndex,
156
- prevTxId: tx.inputs[index].prevTxId,
157
- signature: this.transformSignatureObject({ obj: signature, sigtype: nhashtype }),
158
- sigtype: nhashtype,
159
- });
160
- }
161
- tx.applySignature(signature);
162
- return tx;
163
- }
164
-
165
- getHash(params: { tx: TransactionType }): string {
166
- const bitcoreTx = new this.lib.Transaction(params.tx);
167
- return bitcoreTx.hash;
168
- }
169
-
170
- sign(params: {
171
- tx: TransactionType;
172
- keys: Key[];
173
- utxos: EveryUtxoType[];
174
- sigtype?: number;
175
- pubkeys?: any[];
176
- threshold?: number;
177
- opts?: any;
178
- }): string {
179
- const { tx, keys, pubkeys, sigtype, threshold, opts } = params;
180
- $.checkArgument(keys.every(k => !!k.privKey), 'Keys must contain private key data for signing');
181
-
182
- const utxos = params.utxos || [];
183
- const bitcoreTx = new this.lib.Transaction(tx);
184
- const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
185
- const applicableUtxos = this.getRelatedUtxos({
186
- outputs: bitcoreTx.inputs,
187
- utxos: btcUtxos
188
- });
189
- bitcoreTx.associateInputs(applicableUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubkeys, threshold, opts);
190
- const uniqePrivKeys = Object.values(keys.reduce((map, key) => {
191
- // Need to preserve (un)compressed property, so don't use key.privKey.toString();
192
- const pk = new this.lib.PrivateKey(key.privKey);
193
- map[pk.publicKey.toString()] = pk;
194
- return map;
195
- }, {}));
196
- const signedTx = bitcoreTx.sign(uniqePrivKeys, sigtype).toString();
197
- return signedTx;
198
- }
199
-
200
- getRelatedUtxos(params: {
201
- outputs: BitcoreLib.Transaction.Input[];
202
- utxos: UtxoType[];
203
- }): UtxoType[] {
204
- const { outputs, utxos } = params;
205
- const txids = outputs.map(output => output.toObject().prevTxId);
206
- return utxos.filter(utxo => txids.includes(utxo.txId));
207
- }
208
-
209
- getOutputsFromTx(params: {
210
- tx: BitcoreLib.Transaction;
211
- }): Array<{ address: string | BitcoreLib.Script; satoshis: number }> {
212
- return params.tx.outputs.map(({ script, satoshis }) => {
213
- const address = script;
214
- return { address, satoshis };
215
- });
216
- }
217
-
218
- getSigningAddresses(params: {
219
- tx: TransactionType;
220
- utxos: EveryUtxoType[];
221
- }): (string | undefined)[] {
222
- const { tx, utxos } = params;
223
- const bitcoreTx = new this.lib.Transaction(tx);
224
- const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
225
- const applicableUtxos = this.getRelatedUtxos({
226
- outputs: bitcoreTx.inputs,
227
- utxos: btcUtxos
228
- });
229
- return applicableUtxos.map(utxo => utxo.address);
230
- }
231
-
232
- getSighash(params: {
233
- tx: TransactionType;
234
- index: number;
235
- utxos?: EveryUtxoType[];
236
- pubKey?: string | BitcoreLib.PublicKey | BitcoreLib.HDPublicKey;
237
- path?: string;
238
- sigtype?: number;
239
- // Multisig params for `associateInputs()`
240
- /** Multisig public keys */
241
- pubKeys?: string[] | BitcoreLib.PublicKey[];
242
- /** Threshold for multisig */
243
- threshold?: number;
244
- /** Options for multisig */
245
- opts?: any;
246
- // end Multisig params for `associateInputs()`
247
- }): string {
248
- const { index, utxos, path, sigtype, pubKeys, threshold, opts } = params;
249
- let { tx, pubKey } = params;
250
-
251
- if (!(tx instanceof this.lib.Transaction)) {
252
- tx = new this.lib.Transaction(tx);
253
- }
254
- if (utxos) {
255
- const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
256
- tx.associateInputs(btcUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubKeys, threshold, opts);
257
- }
258
- $.checkState(tx.inputs[index].output instanceof this.lib.Transaction.Output, 'Input must have all utxo info');
259
-
260
- pubKey = pubKey?.toString();
261
- if (pubKey) {
262
- try {
263
- pubKey = new this.lib.PublicKey(pubKey);
264
- } catch {
265
- $.checkArgument(path, '`path` param is required to derive child key');
266
- pubKey = new this.lib.HDPublicKey(pubKey).deriveChild(path).publicKey;
267
- }
268
- }
269
- // Not all input types require the public key
270
- $.checkState(!pubKey || pubKey instanceof this.lib.PublicKey, 'Invalid public key');
271
-
272
- return tx.inputs[index].getSighash(tx, pubKey, index, sigtype).toString('hex');
273
- }
274
- }
275
-
276
- type SignatureType = BitcoreLib.Transaction.Signature | BitcoreLib.crypto.Signature | TssSig;
277
-
278
- /** Transaction data that can be converted into a Transaction via Transaction(tx) */
279
- type TransactionType = BitcoreLib.Transaction | string | Buffer | object;
280
-
281
- /**
282
- * Standard utxo type used for internal processing.
283
- * Property names are from bitcore-lib's UnspentOutput.
284
- * Note, UnspentOutput addresses and scripts are Address and Script classes respectively,
285
- * here they are both strings.
286
- */
287
- export type UtxoType = {
288
- txId: string;
289
- outputIndex: number;
290
- satoshis: number;
291
- script?: string;
292
- address?: string;
293
- };
294
-
295
- /**
296
- * Utxo type for functions where the received utxo type is unknown.
297
- * Could either be in the format of UnspentOutput, UnspentOutput.toObject, or from bitcore-node.
298
- */
299
- export type EveryUtxoType = Partial<{
300
- // UnspentOutput properties
301
- txId: string;
302
- outputIndex: number | string;
303
- satoshis: number | string;
304
- script: string | BitcoreLib.Script;
305
- address: string | BitcoreLib.Address;
306
- // bitcore-node specific properties
307
- mintTxid: string;
308
- mintIndex: number;
309
- mintHeight: number;
310
- value: number;
311
- // UnspentOutput.toObject specific properties
312
- txid: string;
313
- amount: number;
314
- vout: number;
315
- scriptPubKey: string;
316
- }>;
@@ -1,31 +0,0 @@
1
- import BitcoreLibDoge from '@bitpay-labs/bitcore-lib-doge';
2
- import { BTCTxProvider, EveryUtxoType } from '../btc';
3
-
4
- export class DOGETxProvider extends BTCTxProvider {
5
- lib = BitcoreLibDoge;
6
- create(params: {
7
- recipients: Array<{ address: string; amount: number | string }>;
8
- utxos?: EveryUtxoType[];
9
- change?: string;
10
- feeRate?: number | string;
11
- fee?: number | string;
12
- }): string {
13
- const { recipients, utxos = [], change, feeRate, fee = 20000 } = params;
14
- const filteredUtxos = this.selectCoins(recipients, utxos, Number(fee));
15
- const btcUtxos = filteredUtxos.map(utxo => this.standardizeUtxo(utxo));
16
- const tx = new this.lib.Transaction().from(btcUtxos);
17
- if (fee) {
18
- tx.fee(fee);
19
- }
20
- if (feeRate) {
21
- tx.feePerKb(Number(feeRate) * 1000); // feeRate is in feePerByte
22
- }
23
- if (change) {
24
- tx.change(change);
25
- }
26
- for (const recipient of recipients) {
27
- tx.to(recipient.address, Number(recipient.amount));
28
- }
29
- return tx.uncheckedSerialize();
30
- }
31
- }