@bitpay-labs/crypto-wallet-core 11.10.4 → 11.10.11

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/.mocharc.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "enable-source-maps": true,
3
+ "exit": true,
4
+ "recursive": true,
5
+ "extension": ".test.js"
6
+ }
package/mcr.config.js ADDED
@@ -0,0 +1,17 @@
1
+ // @ts-check
2
+ /* eslint-disable */
3
+ const builder = require('../../coverage-builder.js').default;
4
+
5
+
6
+ /**
7
+ * @type {import('monocart-coverage-reports').CoverageReportOptions}
8
+ */
9
+ const config = builder({
10
+ bytes: 70,
11
+ statements: 60,
12
+ branches: 60,
13
+ functions: 60,
14
+ lines: 70
15
+ });
16
+
17
+ module.exports = config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitpay-labs/crypto-wallet-core",
3
- "version": "11.10.4",
3
+ "version": "11.10.11",
4
4
  "description": "A multi-currency support library for address derivation, private key creation, and transaction creation",
5
5
  "main": "./ts_build/src/index.js",
6
6
  "types": "./ts_build/src/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  "fix:errors": "eslint --fix --quiet .",
17
17
  "fix:all": "eslint --fix .",
18
18
  "fix": "npm run fix:errors",
19
- "test": "npm run compile && nyc mocha 'ts_build/test/**/*.test.js'",
19
+ "test": "npm run compile && mcr mocha ts_build/test",
20
20
  "pub": "npm run compile && npm publish"
21
21
  },
22
22
  "keywords": [
@@ -28,10 +28,10 @@
28
28
  "author": "Micah Riggan",
29
29
  "license": "MIT",
30
30
  "dependencies": {
31
- "@bitpay-labs/bitcore-lib": "^11.10.4",
32
- "@bitpay-labs/bitcore-lib-cash": "^11.10.4",
33
- "@bitpay-labs/bitcore-lib-doge": "^11.10.4",
34
- "@bitpay-labs/bitcore-lib-ltc": "^11.10.4",
31
+ "@bitpay-labs/bitcore-lib": "^11.10.9",
32
+ "@bitpay-labs/bitcore-lib-cash": "^11.10.9",
33
+ "@bitpay-labs/bitcore-lib-doge": "^11.10.9",
34
+ "@bitpay-labs/bitcore-lib-ltc": "^11.10.9",
35
35
  "@solana-program/compute-budget": "0.7.0",
36
36
  "@solana-program/memo": "0.7.0",
37
37
  "@solana-program/system": "0.7.0",
@@ -51,5 +51,5 @@
51
51
  "chai": "5.1.2",
52
52
  "typescript": "5.7.3"
53
53
  },
54
- "gitHead": "3cdd407df1a56d452b243083c884735757c10a2a"
54
+ "gitHead": "10bcfcf7cc6953e2af65d707b35d3e65e1254583"
55
55
  }
@@ -1,24 +1,24 @@
1
1
  import BitcoreLibCash from '@bitpay-labs/bitcore-lib-cash';
2
- import { BTCTxProvider } from '../btc';
2
+ import { BTCTxProvider, EveryUtxoType } from '../btc';
3
3
 
4
4
  export class BCHTxProvider extends BTCTxProvider {
5
5
  lib = BitcoreLibCash;
6
- create({ recipients, utxos = [], change, fee = 20000, isSweep }) {
7
- const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, fee);
8
- const btcUtxos = filteredUtxos.map(utxo => {
9
- const btcUtxo = Object.assign({}, utxo, {
10
- amount: utxo.value / 1e8,
11
- txid: utxo.mintTxid,
12
- outputIndex: utxo.mintIndex
13
- });
14
- return new this.lib.Transaction.UnspentOutput(btcUtxo);
15
- });
6
+ create(params: {
7
+ recipients: Array<{ address: string; amount: number | string }>;
8
+ utxos?: EveryUtxoType[];
9
+ change?: string;
10
+ fee?: number | string;
11
+ isSweep?: boolean;
12
+ }): string {
13
+ const { recipients, utxos = [], change, fee = 20000, isSweep } = params;
14
+ const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, Number(fee));
15
+ const btcUtxos = filteredUtxos.map(utxo => this.standardizeUtxo(utxo));
16
16
  const tx = new this.lib.Transaction().from(btcUtxos).feePerByte(Number(fee) + 2);
17
17
  if (change) {
18
18
  tx.change(change);
19
19
  }
20
20
  for (const recipient of recipients) {
21
- tx.to(recipient.address, parseInt(recipient.amount));
21
+ tx.to(recipient.address, Number(recipient.amount));
22
22
  }
23
23
  return tx.uncheckedSerialize();
24
24
  }
@@ -15,42 +15,71 @@ export class BTCTxProvider {
15
15
  lib = BitcoreLib;
16
16
 
17
17
  selectCoins(
18
- recipients: Array<{ amount: number }>,
19
- utxos: Array<{
20
- value: number;
21
- mintHeight: number;
22
- txid?: string;
23
- mintTxid?: string;
24
- mintIndex?: number;
25
- }>,
26
- fee: number
27
- ) {
28
- utxos = utxos.sort(function(a, b) {
29
- return a.mintHeight - b.mintHeight;
30
- });
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
+ }
31
28
 
32
29
  let index = 0;
33
30
  let utxoSum = 0;
34
31
  const recepientSum = recipients.reduce((sum, cur) => sum + Number(cur.amount), fee || 0);
35
32
  while (utxoSum < recepientSum) {
33
+ assert(index < utxos.length, 'insufficient funds');
36
34
  const utxo = utxos[index];
37
- utxoSum += Number(utxo.value);
35
+ utxoSum += Number(utxo.satoshis ?? utxo.value ?? this.lib.Unit.fromBTC(utxo.amount ?? 0).toSatoshis());
38
36
  index += 1;
39
37
  }
40
38
  const filteredUtxos = utxos.slice(0, index);
41
39
  return filteredUtxos;
42
40
  }
43
41
 
44
- create({ recipients, utxos = [], change, feeRate, fee, isSweep, replaceByFee, lockUntilDate, lockUntilBlock }) {
45
- const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, fee);
46
- const btcUtxos = filteredUtxos.map(utxo => {
47
- const btcUtxo = Object.assign({}, utxo, {
48
- amount: utxo.value / 1e8,
49
- txid: utxo.mintTxid,
50
- outputIndex: utxo.mintIndex
51
- });
52
- return new this.lib.Transaction.UnspentOutput(btcUtxo);
53
- });
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));
54
83
  const tx = new this.lib.Transaction().from(btcUtxos);
55
84
  if (fee) {
56
85
  tx.fee(fee);
@@ -62,7 +91,7 @@ export class BTCTxProvider {
62
91
  tx.change(change);
63
92
  }
64
93
  for (const recipient of recipients) {
65
- tx.to(recipient.address, parseInt(recipient.amount));
94
+ tx.to(recipient.address, Number(recipient.amount));
66
95
  }
67
96
  if (replaceByFee && typeof tx.enableRBF === 'function') {
68
97
  tx.enableRBF();
@@ -79,7 +108,7 @@ export class BTCTxProvider {
79
108
  throw new Error('function getSignature not implemented for UTXO coins');
80
109
  }
81
110
 
82
- transformSignatureObject(params: { obj: any; sigtype?: number }) {
111
+ transformSignatureObject(params: { obj: any; sigtype?: number }): string {
83
112
  const { obj, sigtype } = params;
84
113
  const { v } = obj;
85
114
  let { r, s, i, nhashtype } = obj;
@@ -107,7 +136,12 @@ export class BTCTxProvider {
107
136
  return new this.lib.crypto.Signature({ r, s, i, nhashtype }).toString();
108
137
  }
109
138
 
110
- applySignature(params: { tx: BitcoreLib.Transaction; signature: SignatureType; index: number; sigtype?: number }) {
139
+ applySignature(params: {
140
+ tx: BitcoreLib.Transaction;
141
+ signature: SignatureType;
142
+ index: number;
143
+ sigtype?: number;
144
+ }): BitcoreLib.Transaction {
111
145
  const { index, sigtype, tx } = params;
112
146
  let { signature } = params;
113
147
  assert(tx instanceof this.lib.Transaction, 'tx must be an instance of Transaction');
@@ -128,63 +162,77 @@ export class BTCTxProvider {
128
162
  return tx;
129
163
  }
130
164
 
131
- getHash(params: { tx: string }) {
165
+ getHash(params: { tx: TransactionType }): string {
132
166
  const bitcoreTx = new this.lib.Transaction(params.tx);
133
167
  return bitcoreTx.hash;
134
168
  }
135
169
 
136
- sign(params: { tx: string; keys: Array<Key>; utxos: any[]; pubkeys?: any[]; threshold?: number; opts: any }) {
137
- const { tx, keys, pubkeys, threshold, opts } = params;
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
+
138
182
  const utxos = params.utxos || [];
139
183
  const bitcoreTx = new this.lib.Transaction(tx);
184
+ const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
140
185
  const applicableUtxos = this.getRelatedUtxos({
141
186
  outputs: bitcoreTx.inputs,
142
- utxos
187
+ utxos: btcUtxos
143
188
  });
144
- bitcoreTx.associateInputs(applicableUtxos, pubkeys, threshold, opts);
189
+ bitcoreTx.associateInputs(applicableUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubkeys, threshold, opts);
145
190
  const uniqePrivKeys = Object.values(keys.reduce((map, key) => {
146
191
  // Need to preserve (un)compressed property, so don't use key.privKey.toString();
147
192
  const pk = new this.lib.PrivateKey(key.privKey);
148
193
  map[pk.publicKey.toString()] = pk;
149
194
  return map;
150
195
  }, {}));
151
- const signedTx = bitcoreTx.sign(uniqePrivKeys).toString();
196
+ const signedTx = bitcoreTx.sign(uniqePrivKeys, sigtype).toString();
152
197
  return signedTx;
153
198
  }
154
199
 
155
- getRelatedUtxos({ outputs, utxos }) {
200
+ getRelatedUtxos(params: {
201
+ outputs: BitcoreLib.Transaction.Input[];
202
+ utxos: UtxoType[];
203
+ }): UtxoType[] {
204
+ const { outputs, utxos } = params;
156
205
  const txids = outputs.map(output => output.toObject().prevTxId);
157
- const applicableUtxos = utxos.filter(utxo => txids.includes(utxo.txid || utxo.mintTxid));
158
- return applicableUtxos.map(utxo => {
159
- const btcUtxo = Object.assign({}, utxo, {
160
- amount: utxo.value / Math.pow(10, 8),
161
- txid: utxo.mintTxid,
162
- outputIndex: utxo.mintIndex
163
- });
164
- return new this.lib.Transaction.UnspentOutput(btcUtxo);
165
- });
206
+ return utxos.filter(utxo => txids.includes(utxo.txId));
166
207
  }
167
208
 
168
- getOutputsFromTx({ tx }) {
169
- return tx.outputs.map(({ script, satoshis }) => {
209
+ getOutputsFromTx(params: {
210
+ tx: BitcoreLib.Transaction;
211
+ }): Array<{ address: string | BitcoreLib.Script; satoshis: number }> {
212
+ return params.tx.outputs.map(({ script, satoshis }) => {
170
213
  const address = script;
171
214
  return { address, satoshis };
172
215
  });
173
216
  }
174
217
 
175
- getSigningAddresses({ tx, utxos }): string[] {
218
+ getSigningAddresses(params: {
219
+ tx: TransactionType;
220
+ utxos: EveryUtxoType[];
221
+ }): (string | undefined)[] {
222
+ const { tx, utxos } = params;
176
223
  const bitcoreTx = new this.lib.Transaction(tx);
224
+ const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
177
225
  const applicableUtxos = this.getRelatedUtxos({
178
226
  outputs: bitcoreTx.inputs,
179
- utxos
227
+ utxos: btcUtxos
180
228
  });
181
229
  return applicableUtxos.map(utxo => utxo.address);
182
230
  }
183
231
 
184
232
  getSighash(params: {
185
- tx: string | BitcoreLib.Transaction;
233
+ tx: TransactionType;
186
234
  index: number;
187
- utxos?: BitcoreLib.Transaction.UnspentOutput[];
235
+ utxos?: EveryUtxoType[];
188
236
  pubKey?: string | BitcoreLib.PublicKey | BitcoreLib.HDPublicKey;
189
237
  path?: string;
190
238
  sigtype?: number;
@@ -204,7 +252,8 @@ export class BTCTxProvider {
204
252
  tx = new this.lib.Transaction(tx);
205
253
  }
206
254
  if (utxos) {
207
- tx.associateInputs(utxos.map(this.lib.Transaction.UnspentOutput), pubKeys, threshold, opts);
255
+ const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
256
+ tx.associateInputs(btcUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubKeys, threshold, opts);
208
257
  }
209
258
  $.checkState(tx.inputs[index].output instanceof this.lib.Transaction.Output, 'Input must have all utxo info');
210
259
 
@@ -224,4 +273,44 @@ export class BTCTxProvider {
224
273
  }
225
274
  }
226
275
 
227
- type SignatureType = BitcoreLib.Transaction.Signature | BitcoreLib.crypto.Signature | TssSig;
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,18 +1,18 @@
1
1
  import BitcoreLibDoge from '@bitpay-labs/bitcore-lib-doge';
2
- import { BTCTxProvider } from '../btc';
2
+ import { BTCTxProvider, EveryUtxoType } from '../btc';
3
3
 
4
4
  export class DOGETxProvider extends BTCTxProvider {
5
5
  lib = BitcoreLibDoge;
6
- create({ recipients, utxos = [], change, feeRate, fee = 20000 }) {
7
- const filteredUtxos = this.selectCoins(recipients, utxos, fee);
8
- const btcUtxos = filteredUtxos.map(utxo => {
9
- const btcUtxo = Object.assign({}, utxo, {
10
- amount: utxo.value / 1e8,
11
- txid: utxo.mintTxid,
12
- outputIndex: utxo.mintIndex
13
- });
14
- return new this.lib.Transaction.UnspentOutput(btcUtxo);
15
- });
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
16
  const tx = new this.lib.Transaction().from(btcUtxos);
17
17
  if (fee) {
18
18
  tx.fee(fee);
@@ -24,7 +24,7 @@ export class DOGETxProvider extends BTCTxProvider {
24
24
  tx.change(change);
25
25
  }
26
26
  for (const recipient of recipients) {
27
- tx.to(recipient.address, parseInt(recipient.amount));
27
+ tx.to(recipient.address, Number(recipient.amount));
28
28
  }
29
29
  return tx.uncheckedSerialize();
30
30
  }
@@ -1,18 +1,18 @@
1
1
  import BitcoreLibLtc from '@bitpay-labs/bitcore-lib-ltc';
2
- import { BTCTxProvider } from '../btc';
2
+ import { BTCTxProvider, EveryUtxoType } from '../btc';
3
3
 
4
4
  export class LTCTxProvider extends BTCTxProvider {
5
5
  lib = BitcoreLibLtc;
6
- create({ recipients, utxos = [], change, feeRate, fee = 20000 }) {
7
- const filteredUtxos = this.selectCoins(recipients, utxos, fee);
8
- const btcUtxos = filteredUtxos.map(utxo => {
9
- const btcUtxo = Object.assign({}, utxo, {
10
- amount: utxo.value / 1e8,
11
- txid: utxo.mintTxid,
12
- outputIndex: utxo.mintIndex
13
- });
14
- return new this.lib.Transaction.UnspentOutput(btcUtxo);
15
- });
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
16
  const tx = new this.lib.Transaction().from(btcUtxos);
17
17
  if (fee) {
18
18
  tx.fee(fee);
@@ -24,7 +24,7 @@ export class LTCTxProvider extends BTCTxProvider {
24
24
  tx.change(change);
25
25
  }
26
26
  for (const recipient of recipients) {
27
- tx.to(recipient.address, parseInt(recipient.amount));
27
+ tx.to(recipient.address, Number(recipient.amount));
28
28
  }
29
29
  return tx.uncheckedSerialize();
30
30
  }
@@ -6,7 +6,13 @@ import bitcoreLibDoge from '@bitpay-labs/bitcore-lib-doge';
6
6
  import bitcoreLibLtc from '@bitpay-labs/bitcore-lib-ltc';
7
7
  import { Constants, Transactions } from '../src';
8
8
 
9
- describe('Transaction', function() {
9
+ describe('Transaction', function () {
10
+ const libs = {
11
+ BTC: bitcoreLib,
12
+ BCH: bitcoreLibCash,
13
+ DOGE: bitcoreLibDoge,
14
+ LTC: bitcoreLibLtc
15
+ };
10
16
  describe('create', () => {
11
17
  it('should create a BTC tx', () => {
12
18
  const recipients = [{ address: 'mpNpzMoprLnSBu8CWDunNCYeJq3Mzdk59V', amount: 1e8 }];
@@ -45,6 +51,171 @@ describe('Transaction', function() {
45
51
  expect(signed).to.eq(expected);
46
52
  });
47
53
 
54
+ describe('every utxo type: bitcore-node, UnspentOutput, and UnspentOutput.toObject', () => {
55
+ const keys = [{
56
+ address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
57
+ privKey: 'cSFjiifSbZ2hU4jTFwE993LCe2rkZGULCTGWTDWXzHvuXRKxpnc1'
58
+ }];
59
+
60
+ const bitcoreNodeUtxos =
61
+ [
62
+ {
63
+ mintTxid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
64
+ mintIndex: 1,
65
+ mintHeight: 100,
66
+ value: 90_000,
67
+ script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac',
68
+ address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
69
+ sequenceNumber: 4294967294
70
+ },
71
+ {
72
+ mintTxid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
73
+ mintIndex: 0,
74
+ mintHeight: 100,
75
+ value: 30_000,
76
+ script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac',
77
+ address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
78
+ sequenceNumber: 4294967294
79
+ }
80
+ ];
81
+
82
+ const unspentOutputToObjectUtxos = [
83
+ {
84
+ txid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
85
+ outputIndex: 1,
86
+ amount: 0.0009,
87
+ scriptPubKey: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
88
+ },
89
+ {
90
+ txid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
91
+ vout: 0,
92
+ amount: 0.0003,
93
+ scriptPubKey: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
94
+ }
95
+ ];
96
+
97
+ const recipients = [{ address: 'moVnNJpHHfssYJEnMTS5xXyGV8RhRQNRz5', amount: 100_000 }];
98
+ for (const chain of ['BTC', 'BCH', 'DOGE', 'LTC']) {
99
+ const unspentOutputUtxos = [
100
+ new libs[chain].Transaction.UnspentOutput({
101
+ txId: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
102
+ outputIndex: 1,
103
+ satoshis: 90_000,
104
+ script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
105
+ }),
106
+ new libs[chain].Transaction.UnspentOutput({
107
+ txId: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
108
+ outputIndex: 0,
109
+ satoshis: 30_000,
110
+ script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
111
+ })
112
+ ];
113
+ const utxoSet = [
114
+ bitcoreNodeUtxos,
115
+ unspentOutputUtxos,
116
+ unspentOutputToObjectUtxos
117
+ ];
118
+ let tx: string;
119
+ const lib = libs[chain];
120
+ it(`should create a tx with every utxo type for ${chain}`, () => {
121
+ const txs = utxoSet.map(utxos => Transactions.create({
122
+ chain,
123
+ recipients,
124
+ utxos
125
+ }));
126
+
127
+ tx = txs[0];
128
+ for (const _tx of txs.slice(1)) {
129
+ expect(tx, 'all transactions should be the same regardless of the utxo format').to.equal(_tx);
130
+ }
131
+
132
+ let expectedTx: string;
133
+ if (chain === 'DOGE') {
134
+ expectedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640000000000ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
135
+ } else {
136
+ expectedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640000000000ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
137
+ }
138
+ expect(tx).to.equal(expectedTx);
139
+ });
140
+
141
+ it(`should sign a tx with every utxo type ${chain}`, () => {
142
+ const signedTxs = utxoSet.map(utxos => Transactions.sign({
143
+ chain,
144
+ tx,
145
+ utxos,
146
+ keys,
147
+ sigtype: lib.crypto.Signature.SIGHASH_ALL
148
+ }));
149
+ const signedTx = signedTxs[0];
150
+ for (const _signedTx of signedTxs.slice(1)) {
151
+ expect(signedTx, 'signed transactions should all be the same regardless of utxo type').to.equal(_signedTx);
152
+ }
153
+
154
+ let expectedTx: string;
155
+ if (chain === 'DOGE') {
156
+ expectedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006a473044022015ceee1da23792e26cd1215c8327a35978c8b0494de9e69bab6d31ab3d0b56ad02205f9299162591672c6cde3d92d5819a11e1ad9f353a25d7a3ce494877ef27468901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b4830450221009f0b5dd0b4c0bd9bcf804e71020705eb9bda99a31cb2a4d123c364bdd394adeb022016939a51fb03c9efea4b7d53a7b90d7a3667fff71cd8365e366c81b1fdd0d3e001210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
157
+ } else {
158
+ expectedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006b483045022100c74ab4fe359e8efc73fc28dce989e28da47d3b45eb06ee305dfc0e675da8700802201076a067337a3c6b90fad2077a55154aa150460cb842bc86373026eb071a7ac901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b483045022100ed22907cc96b5367ef469225f7f718efa8fb4eb2dc5fbf3909777c5f024cf5e902206480082ad9b413a8a76128ab147287da30d907cfb1a159be814392fa1138012801210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
159
+ }
160
+ expect(signedTx).to.equal(expectedTx);
161
+ });
162
+
163
+ it(`should create valid sighashes for all utxo types ${chain}`, () => {
164
+ const bitcoreTx = lib.Transaction()
165
+ .from(unspentOutputUtxos)
166
+ .to(recipients[0].address, recipients[0].amount);
167
+ const signedTx = lib.Transaction()
168
+ .from(unspentOutputUtxos)
169
+ .to(recipients[0].address, recipients[0].amount);
170
+
171
+ for (let index = 0; index < unspentOutputUtxos.length; index++) {
172
+ const sighashes: string[] = [];
173
+ for (const utxos of utxoSet) {
174
+ sighashes.push(Transactions.getSighash({
175
+ chain,
176
+ tx,
177
+ utxos,
178
+ index,
179
+ sigtype: lib.crypto.Signature.SIGHASH_ALL
180
+ }));
181
+ }
182
+
183
+ // getSighash should work without utxos if a complete lib transaction is provided
184
+ sighashes.push(Transactions.getSighash({
185
+ chain,
186
+ tx: bitcoreTx,
187
+ index,
188
+ sigtype: lib.crypto.Signature.SIGHASH_ALL
189
+ }));
190
+
191
+ const sighash = sighashes[0];
192
+ for (const hash of sighashes) {
193
+ expect(sighash).to.equal(hash);
194
+ }
195
+ expect(sighash.length).to.equal(64);
196
+ const privateKey = new lib.PrivateKey(keys[0].privKey);
197
+ const publicKey = privateKey.toPublicKey();
198
+ const signature = lib.crypto.ECDSA.sign(Buffer.from(sighash, 'hex'), privateKey);
199
+ signature.pubKey = publicKey;
200
+
201
+ Transactions.applySignature({
202
+ chain,
203
+ tx: signedTx,
204
+ signature,
205
+ index
206
+ });
207
+ }
208
+ if (chain === 'DOGE') {
209
+ const serializedSignedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006a473044022015ceee1da23792e26cd1215c8327a35978c8b0494de9e69bab6d31ab3d0b56ad02205f9299162591672c6cde3d92d5819a11e1ad9f353a25d7a3ce494877ef27468901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b4830450221009f0b5dd0b4c0bd9bcf804e71020705eb9bda99a31cb2a4d123c364bdd394adeb022016939a51fb03c9efea4b7d53a7b90d7a3667fff71cd8365e366c81b1fdd0d3e001210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
210
+ expect(signedTx.serialize({ disableSmallFees: true, disableDustOutputs: true })).to.equal(serializedSignedTx);
211
+ } else {
212
+ const serializedSignedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006b483045022100c74ab4fe359e8efc73fc28dce989e28da47d3b45eb06ee305dfc0e675da8700802201076a067337a3c6b90fad2077a55154aa150460cb842bc86373026eb071a7ac901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b483045022100ed22907cc96b5367ef469225f7f718efa8fb4eb2dc5fbf3909777c5f024cf5e902206480082ad9b413a8a76128ab147287da30d907cfb1a159be814392fa1138012801210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
213
+ expect(signedTx.serialize()).to.equal(serializedSignedTx);
214
+ }
215
+ });
216
+ }
217
+ });
218
+
48
219
  it('should sign a BTC opreturn tx', () => {
49
220
  const tx =
50
221
  '0200000001ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffff0200000000000000000b6a096a07696f6e3a61626340420f00000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
@@ -1887,4 +2058,4 @@ describe('Transaction', function() {
1887
2058
  });
1888
2059
 
1889
2060
  });
1890
- });
2061
+ });