@btc-vision/bitcoin 6.3.6 → 6.4.1

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 (71) hide show
  1. package/.mocharc.json +13 -0
  2. package/browser/address.d.ts +1 -1
  3. package/browser/index.js +1 -1
  4. package/browser/index.js.LICENSE.txt +3 -3
  5. package/browser/networks.d.ts +1 -0
  6. package/build/address.d.ts +2 -1
  7. package/build/address.js +68 -13
  8. package/build/block.js +2 -2
  9. package/build/bufferutils.js +5 -5
  10. package/build/networks.d.ts +1 -0
  11. package/build/networks.js +11 -0
  12. package/build/psbt/psbtutils.js +2 -2
  13. package/build/psbt.js +3 -7
  14. package/package.json +26 -26
  15. package/src/address.ts +91 -15
  16. package/src/block.ts +2 -2
  17. package/src/bufferutils.ts +15 -7
  18. package/src/index.ts +86 -86
  19. package/src/networks.ts +12 -0
  20. package/src/psbt/bip371.ts +441 -441
  21. package/src/psbt/psbtutils.ts +320 -319
  22. package/src/psbt.ts +8 -8
  23. package/test/address.spec.ts +55 -77
  24. package/test/bitcoin.core.spec.ts +47 -69
  25. package/test/block.spec.ts +23 -46
  26. package/test/bufferutils.spec.ts +32 -95
  27. package/test/crypto.spec.ts +9 -15
  28. package/test/fixtures/address.json +3 -3
  29. package/test/integration/addresses.spec.ts +12 -24
  30. package/test/integration/bip32.spec.ts +10 -31
  31. package/test/integration/blocks.spec.ts +2 -2
  32. package/test/integration/cltv.spec.ts +21 -63
  33. package/test/integration/csv.spec.ts +30 -105
  34. package/test/integration/payments.spec.ts +16 -41
  35. package/test/integration/taproot.spec.ts +31 -75
  36. package/test/integration/transactions.spec.ts +37 -138
  37. package/test/payments.spec.ts +95 -106
  38. package/test/payments.utils.ts +20 -63
  39. package/test/psbt.spec.ts +100 -229
  40. package/test/script.spec.ts +26 -50
  41. package/test/script_number.spec.ts +6 -9
  42. package/test/script_signature.spec.ts +7 -7
  43. package/test/transaction.spec.ts +46 -96
  44. package/test/ts-node-register.js +3 -1
  45. package/test/tsconfig.json +4 -1
  46. package/test/types.spec.ts +7 -12
  47. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  48. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/index.json +0 -1
  50. package/test/address.spec.js +0 -124
  51. package/test/bitcoin.core.spec.js +0 -170
  52. package/test/block.spec.js +0 -141
  53. package/test/bufferutils.spec.js +0 -427
  54. package/test/crypto.spec.js +0 -41
  55. package/test/integration/_regtest.js +0 -7
  56. package/test/integration/addresses.spec.js +0 -116
  57. package/test/integration/bip32.spec.js +0 -85
  58. package/test/integration/blocks.spec.js +0 -26
  59. package/test/integration/cltv.spec.js +0 -199
  60. package/test/integration/csv.spec.js +0 -362
  61. package/test/integration/payments.spec.js +0 -98
  62. package/test/integration/taproot.spec.js +0 -532
  63. package/test/integration/transactions.spec.js +0 -561
  64. package/test/payments.spec.js +0 -97
  65. package/test/payments.utils.js +0 -190
  66. package/test/psbt.spec.js +0 -1044
  67. package/test/script.spec.js +0 -151
  68. package/test/script_number.spec.js +0 -24
  69. package/test/script_signature.spec.js +0 -52
  70. package/test/transaction.spec.js +0 -269
  71. package/test/types.spec.js +0 -46
package/src/psbt.ts CHANGED
@@ -2,7 +2,6 @@ import { Psbt as PsbtBase } from 'bip174';
2
2
  import * as varuint from 'bip174/src/lib/converter/varint.js';
3
3
  import {
4
4
  Bip32Derivation,
5
- Transaction as ITransaction,
6
5
  KeyValue,
7
6
  PartialSig,
8
7
  PsbtGlobal,
@@ -13,6 +12,7 @@ import {
13
12
  PsbtOutputUpdate,
14
13
  TapKeySig,
15
14
  TapScriptSig,
15
+ Transaction as ITransaction,
16
16
  TransactionFromBuffer,
17
17
  } from 'bip174/src/lib/interfaces.js';
18
18
  import { checkForInput, checkForOutput } from 'bip174/src/lib/utils.js';
@@ -20,7 +20,6 @@ import { BIP32Interface } from 'bip32';
20
20
  import { ECPairInterface } from 'ecpair';
21
21
  import { fromOutputScript, toOutputScript } from './address.js';
22
22
  import { cloneBuffer, reverseBuffer } from './bufferutils.js';
23
- import { hookSigner } from './hooks/HookedSigner.js';
24
23
  import { payments } from './index.js';
25
24
  import { bitcoin as btcNetwork, Network } from './networks.js';
26
25
  import { tapleafHash } from './payments/bip341.js';
@@ -230,8 +229,7 @@ export class Psbt {
230
229
 
231
230
  clone(): Psbt {
232
231
  // TODO: more efficient cloning
233
- const res = Psbt.fromBuffer(this.data.toBuffer(), JSON.parse(JSON.stringify(this.opts)));
234
- return res;
232
+ return Psbt.fromBuffer(this.data.toBuffer(), JSON.parse(JSON.stringify(this.opts)));
235
233
  }
236
234
 
237
235
  setMaximumFeeRate(satoshiPerByte: number): void {
@@ -953,7 +951,7 @@ export class Psbt {
953
951
  keyPair: Signer | SignerAlternative | BIP32Interface | ECPairInterface,
954
952
  sighashTypes: number[] = [Transaction.SIGHASH_ALL],
955
953
  ): this {
956
- hookSigner(keyPair);
954
+ //hookSigner(keyPair);
957
955
 
958
956
  const { hash, sighashType } = getHashAndSighashType(
959
957
  this.data.inputs,
@@ -981,7 +979,7 @@ export class Psbt {
981
979
  tapLeafHashToSign?: Buffer,
982
980
  allowedSighashTypes: number[] = [Transaction.SIGHASH_DEFAULT],
983
981
  ): this {
984
- hookSigner(keyPair);
982
+ //hookSigner(keyPair);
985
983
 
986
984
  const hashesForSig = this.checkTaprootHashesForSig(
987
985
  inputIndex,
@@ -1027,7 +1025,7 @@ export class Psbt {
1027
1025
  keyPair: Signer | SignerAlternative | SignerAsync | BIP32Interface | ECPairInterface,
1028
1026
  sighashTypes: number[] = [Transaction.SIGHASH_ALL],
1029
1027
  ): Promise<void> {
1030
- hookSigner(keyPair);
1028
+ //hookSigner(keyPair);
1031
1029
 
1032
1030
  const { hash, sighashType } = getHashAndSighashType(
1033
1031
  this.data.inputs,
@@ -1056,7 +1054,7 @@ export class Psbt {
1056
1054
  tapLeafHash?: Buffer,
1057
1055
  sighashTypes: number[] = [Transaction.SIGHASH_DEFAULT],
1058
1056
  ): Promise<void> {
1059
- hookSigner(keyPair);
1057
+ //hookSigner(keyPair);
1060
1058
 
1061
1059
  const hashesForSig = this.checkTaprootHashesForSig(
1062
1060
  inputIndex,
@@ -1288,6 +1286,8 @@ function canFinalize(input: PsbtInput, script: Buffer, scriptType: string): bool
1288
1286
  case 'multisig':
1289
1287
  const p2ms = payments.p2ms({ output: script });
1290
1288
  return hasSigs(p2ms.m!, input.partialSig, p2ms.pubkeys);
1289
+ case 'nonstandard':
1290
+ return true;
1291
1291
  default:
1292
1292
  return false;
1293
1293
  }
@@ -1,13 +1,15 @@
1
- import * as assert from 'assert';
1
+ import assert from 'assert';
2
2
  import { describe, it } from 'mocha';
3
3
  import * as ecc from 'tiny-secp256k1';
4
- import * as baddress from '../src/address';
5
- import * as bscript from '../src/script';
6
- import * as fixtures from './fixtures/address.json';
4
+ import * as baddress from '../src/address.js';
5
+ import * as bscript from '../src/script.js';
6
+ import fixtures from './fixtures/address.json' with { type: 'json' };
7
7
 
8
- import { initEccLib } from '../src';
8
+ import { initEccLib, Network } from '../src/index.js';
9
9
 
10
- const NETWORKS = Object.assign(
10
+ import * as networks from '../src/networks.js';
11
+
12
+ const NETWORKS: Record<string, Network> = Object.assign(
11
13
  {
12
14
  litecoin: {
13
15
  messagePrefix: '\x19Litecoin Signed Message:\n',
@@ -20,12 +22,12 @@ const NETWORKS = Object.assign(
20
22
  wif: 0xb0,
21
23
  },
22
24
  },
23
- require('../src/networks'),
25
+ networks,
24
26
  );
25
27
 
26
28
  describe('address', () => {
27
29
  describe('fromBase58Check', () => {
28
- fixtures.standard.forEach(f => {
30
+ fixtures.standard.forEach((f) => {
29
31
  if (!f.base58check) return;
30
32
 
31
33
  it('decodes ' + f.base58check, () => {
@@ -36,17 +38,20 @@ describe('address', () => {
36
38
  });
37
39
  });
38
40
 
39
- fixtures.invalid.fromBase58Check.forEach(f => {
41
+ fixtures.invalid.fromBase58Check.forEach((f) => {
40
42
  it('throws on ' + f.exception, () => {
41
- assert.throws(() => {
42
- baddress.fromBase58Check(f.address);
43
- }, new RegExp(f.address + ' ' + f.exception));
43
+ assert.throws(
44
+ () => {
45
+ baddress.fromBase58Check(f.address);
46
+ },
47
+ new RegExp(f.address + ' ' + f.exception),
48
+ );
44
49
  });
45
50
  });
46
51
  });
47
52
 
48
53
  describe('fromBech32', () => {
49
- fixtures.standard.forEach(f => {
54
+ fixtures.standard.forEach((f) => {
50
55
  if (!f.bech32) return;
51
56
 
52
57
  it('decodes ' + f.bech32, () => {
@@ -58,61 +63,43 @@ describe('address', () => {
58
63
  });
59
64
  });
60
65
 
61
- fixtures.invalid.bech32.forEach(f => {
62
- it(
63
- 'decode fails for ' + f.address + '(' + f.exception + ')',
64
- () => {
65
- assert.throws(() => {
66
- baddress.fromBech32(f.address);
67
- }, new RegExp(f.exception));
68
- },
69
- );
66
+ fixtures.invalid.bech32.forEach((f) => {
67
+ it('decode fails for ' + f.address + '(' + f.exception + ')', () => {
68
+ assert.throws(() => {
69
+ baddress.fromBech32(f.address);
70
+ }, new RegExp(f.exception));
71
+ });
70
72
  });
71
73
  });
72
74
 
73
75
  describe('fromOutputScript', () => {
74
76
  initEccLib(ecc);
75
- fixtures.standard.forEach(f => {
76
- it(
77
- 'encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')',
78
- () => {
79
- const script = bscript.fromASM(f.script);
80
- const address = baddress.fromOutputScript(
81
- script,
82
- NETWORKS[f.network],
83
- );
84
-
85
- assert.strictEqual(
86
- address,
87
- f.base58check || f.bech32!.toLowerCase(),
88
- );
89
- },
90
- );
77
+ fixtures.standard.forEach((f) => {
78
+ it('encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', () => {
79
+ const script = bscript.fromASM(f.script);
80
+ const address = baddress.fromOutputScript(script, NETWORKS[f.network]);
81
+
82
+ assert.strictEqual(address, f.base58check || f.bech32!.toLowerCase());
83
+ });
91
84
  });
92
85
 
93
- fixtures.invalid.fromOutputScript.forEach(f => {
94
- it(
95
- 'throws when ' + f.script.slice(0, 30) + '... ' + f.exception,
96
- () => {
97
- const script = bscript.fromASM(f.script);
98
-
99
- assert.throws(() => {
100
- baddress.fromOutputScript(script, undefined);
101
- }, new RegExp(f.exception));
102
- },
103
- );
86
+ fixtures.invalid.fromOutputScript.forEach((f) => {
87
+ it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, () => {
88
+ const script = bscript.fromASM(f.script);
89
+
90
+ assert.throws(() => {
91
+ baddress.fromOutputScript(script, undefined);
92
+ }, new RegExp(f.exception));
93
+ });
104
94
  });
105
95
  });
106
96
 
107
97
  describe('toBase58Check', () => {
108
- fixtures.standard.forEach(f => {
98
+ fixtures.standard.forEach((f) => {
109
99
  if (!f.base58check) return;
110
100
 
111
101
  it('encodes ' + f.hash + ' (' + f.network + ')', () => {
112
- const address = baddress.toBase58Check(
113
- Buffer.from(f.hash, 'hex'),
114
- f.version,
115
- );
102
+ const address = baddress.toBase58Check(Buffer.from(f.hash, 'hex'), f.version);
116
103
 
117
104
  assert.strictEqual(address, f.base58check);
118
105
  });
@@ -120,7 +107,7 @@ describe('address', () => {
120
107
  });
121
108
 
122
109
  describe('toBech32', () => {
123
- fixtures.bech32.forEach(f => {
110
+ fixtures.bech32.forEach((f) => {
124
111
  if (!f.address) return;
125
112
  const data = Buffer.from(f.data, 'hex');
126
113
 
@@ -134,40 +121,31 @@ describe('address', () => {
134
121
 
135
122
  // TODO: These fixtures (according to TypeScript) have none of the data used below
136
123
  fixtures.invalid.bech32.forEach((f: any) => {
137
- if (!f.prefix || f.version === undefined || f.data === undefined)
138
- return;
124
+ if (!f.prefix || f.version === undefined || f.data === undefined) return;
139
125
 
140
126
  it('encode fails (' + f.exception, () => {
141
127
  assert.throws(() => {
142
- baddress.toBech32(
143
- Buffer.from(f.data, 'hex'),
144
- f.version,
145
- f.prefix,
146
- );
128
+ baddress.toBech32(Buffer.from(f.data, 'hex'), f.version, f.prefix);
147
129
  }, new RegExp(f.exception));
148
130
  });
149
131
  });
150
132
  });
151
133
 
152
134
  describe('toOutputScript', () => {
153
- fixtures.standard.forEach(f => {
154
- it(
155
- 'decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')',
156
- () => {
157
- const script = baddress.toOutputScript(
158
- (f.base58check || f.bech32)!,
159
- NETWORKS[f.network],
160
- );
161
-
162
- assert.strictEqual(bscript.toASM(script), f.script);
163
- },
164
- );
135
+ fixtures.standard.forEach((f) => {
136
+ it('decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', () => {
137
+ const script = baddress.toOutputScript(
138
+ (f.base58check || f.bech32)!,
139
+ NETWORKS[f.network],
140
+ );
141
+
142
+ assert.strictEqual(bscript.toASM(script), f.script);
143
+ });
165
144
  });
166
145
 
167
- fixtures.invalid.toOutputScript.forEach(f => {
146
+ fixtures.invalid.toOutputScript.forEach((f) => {
168
147
  it('throws when ' + (f.exception || f.paymentException), () => {
169
- const exception =
170
- f.paymentException || `${f.address} ${f.exception}`;
148
+ const exception = f.paymentException || `${f.address} ${f.exception}`;
171
149
  assert.throws(() => {
172
150
  baddress.toOutputScript(f.address, f.network as any);
173
151
  }, new RegExp(exception));
@@ -1,26 +1,26 @@
1
- import * as assert from 'assert';
2
- import * as base58 from 'bs58';
1
+ import assert from 'assert';
2
+ import base58 from 'bs58';
3
3
  import { describe, it } from 'mocha';
4
- import * as bitcoin from '..';
5
- import * as base58EncodeDecode from './fixtures/core/base58_encode_decode.json';
6
- import * as base58KeysInvalid from './fixtures/core/base58_keys_invalid.json';
7
- import * as base58KeysValid from './fixtures/core/base58_keys_valid.json';
8
- import * as blocksValid from './fixtures/core/blocks.json';
9
- import * as sigCanonical from './fixtures/core/sig_canonical.json';
10
- import * as sigNoncanonical from './fixtures/core/sig_noncanonical.json';
11
- import * as sigHash from './fixtures/core/sighash.json';
12
- import * as txValid from './fixtures/core/tx_valid.json';
4
+ import * as bitcoin from '../src/index.js';
5
+ import base58EncodeDecode from './fixtures/core/base58_encode_decode.json' with { type: 'json' };
6
+ import base58KeysInvalid from './fixtures/core/base58_keys_invalid.json' with { type: 'json' };
7
+ import base58KeysValid from './fixtures/core/base58_keys_valid.json' with { type: 'json' };
8
+ import blocksValid from './fixtures/core/blocks.json' with { type: 'json' };
9
+ import sigCanonical from './fixtures/core/sig_canonical.json' with { type: 'json' };
10
+ import sigNoncanonical from './fixtures/core/sig_noncanonical.json' with { type: 'json' };
11
+ import sigHash from './fixtures/core/sighash.json' with { type: 'json' };
12
+ import txValid from './fixtures/core/tx_valid.json' with { type: 'json' };
13
13
 
14
14
  describe('Bitcoin-core', () => {
15
15
  // base58EncodeDecode
16
16
  describe('base58', () => {
17
- base58EncodeDecode.forEach(f => {
17
+ base58EncodeDecode.forEach((f) => {
18
18
  const fhex = f[0];
19
19
  const fb58 = f[1];
20
20
 
21
21
  it('can decode ' + fb58, () => {
22
22
  const buffer = base58.decode(fb58);
23
- const actual = buffer.toString('hex');
23
+ const actual = Buffer.from(buffer).toString('hex');
24
24
 
25
25
  assert.strictEqual(actual, fhex);
26
26
  });
@@ -41,7 +41,7 @@ describe('Bitcoin-core', () => {
41
41
  script: 'scriptHash',
42
42
  };
43
43
 
44
- base58KeysValid.forEach(f => {
44
+ base58KeysValid.forEach((f) => {
45
45
  const expected = f[0];
46
46
  const hash = Buffer.from(f[1] as any, 'hex');
47
47
  const params = f[2] as any;
@@ -51,13 +51,11 @@ describe('Bitcoin-core', () => {
51
51
  const network: any = params.isTestnet
52
52
  ? bitcoin.networks.testnet
53
53
  : bitcoin.networks.bitcoin;
54
+
54
55
  const version = network[typeMap[params.addrType]];
55
56
 
56
- it('can export ' + expected, () => {
57
- assert.strictEqual(
58
- bitcoin.address.toBase58Check(hash, version),
59
- expected,
60
- );
57
+ it(`can export ${expected as string}`, () => {
58
+ assert.strictEqual(bitcoin.address.toBase58Check(hash, version), expected);
61
59
  });
62
60
  });
63
61
  });
@@ -71,7 +69,7 @@ describe('Bitcoin-core', () => {
71
69
  bitcoin.networks.testnet.scriptHash,
72
70
  ];
73
71
 
74
- base58KeysInvalid.forEach(f => {
72
+ base58KeysInvalid.forEach((f) => {
75
73
  const strng = f[0];
76
74
 
77
75
  it('throws on ' + strng, () => {
@@ -89,7 +87,7 @@ describe('Bitcoin-core', () => {
89
87
  });
90
88
 
91
89
  describe('Block.fromHex', () => {
92
- blocksValid.forEach(f => {
90
+ blocksValid.forEach((f) => {
93
91
  it('can parse ' + f.id, () => {
94
92
  const block = bitcoin.Block.fromHex(f.hex);
95
93
 
@@ -101,7 +99,7 @@ describe('Bitcoin-core', () => {
101
99
 
102
100
  // txValid
103
101
  describe('Transaction.fromHex', () => {
104
- txValid.forEach(f => {
102
+ txValid.forEach((f) => {
105
103
  // Objects that are only a single string are ignored
106
104
  if (f.length === 1) return;
107
105
 
@@ -116,10 +114,7 @@ describe('Bitcoin-core', () => {
116
114
  const input = inputs[i];
117
115
 
118
116
  // reverse because test data is reversed
119
- const prevOutHash = Buffer.from(
120
- input[0] as string,
121
- 'hex',
122
- ).reverse();
117
+ const prevOutHash = Buffer.from(input[0] as string, 'hex').reverse();
123
118
  const prevOutIndex = input[1];
124
119
 
125
120
  assert.deepStrictEqual(txIn.hash, prevOutHash);
@@ -133,7 +128,7 @@ describe('Bitcoin-core', () => {
133
128
 
134
129
  // sighash
135
130
  describe('Transaction', () => {
136
- sigHash.forEach(f => {
131
+ sigHash.forEach((f) => {
137
132
  // Objects that are only a single string are ignored
138
133
  if (f.length === 1) return;
139
134
 
@@ -154,59 +149,42 @@ describe('Bitcoin-core', () => {
154
149
 
155
150
  const hashTypeName = hashTypes.join(' | ');
156
151
 
157
- it(
158
- 'should hash ' +
159
- txHex.slice(0, 40) +
160
- '... (' +
161
- hashTypeName +
162
- ')',
163
- () => {
164
- const transaction = bitcoin.Transaction.fromHex(txHex);
165
- assert.strictEqual(transaction.toHex(), txHex);
166
-
167
- const script = Buffer.from(scriptHex, 'hex');
168
- const scriptChunks = bitcoin.script.decompile(script);
169
- assert.strictEqual(
170
- bitcoin.script.compile(scriptChunks!).toString('hex'),
171
- scriptHex,
172
- );
152
+ it('should hash ' + txHex.slice(0, 40) + '... (' + hashTypeName + ')', () => {
153
+ const transaction = bitcoin.Transaction.fromHex(txHex);
154
+ assert.strictEqual(transaction.toHex(), txHex);
173
155
 
174
- const hash = transaction.hashForSignature(
175
- inIndex,
176
- script,
177
- hashType,
178
- );
156
+ const script = Buffer.from(scriptHex, 'hex');
157
+ const scriptChunks = bitcoin.script.decompile(script);
158
+ assert.strictEqual(
159
+ bitcoin.script.compile(scriptChunks!).toString('hex'),
160
+ scriptHex,
161
+ );
179
162
 
180
- // reverse because test data is reversed
181
- assert.strictEqual(
182
- (hash.reverse() as Buffer).toString('hex'),
183
- expectedHash,
184
- );
163
+ const hash = transaction.hashForSignature(inIndex, script, hashType);
185
164
 
186
- assert.doesNotThrow(() =>
187
- transaction.hashForWitnessV0(
188
- inIndex,
189
- script,
190
- 0,
191
- // convert to UInt32
192
- hashType < 0 ? 0x100000000 + hashType : hashType,
193
- ),
194
- );
195
- },
196
- );
165
+ // reverse because test data is reversed
166
+ assert.strictEqual((hash.reverse() as Buffer).toString('hex'), expectedHash);
167
+
168
+ assert.doesNotThrow(() =>
169
+ transaction.hashForWitnessV0(
170
+ inIndex,
171
+ script,
172
+ 0,
173
+ // convert to UInt32
174
+ hashType < 0 ? 0x100000000 + hashType : hashType,
175
+ ),
176
+ );
177
+ });
197
178
  });
198
179
  });
199
180
 
200
181
  describe('script.signature.decode', () => {
201
- sigCanonical.forEach(hex => {
182
+ sigCanonical.forEach((hex) => {
202
183
  const buffer = Buffer.from(hex, 'hex');
203
184
 
204
185
  it('can parse ' + hex, () => {
205
186
  const parsed = bitcoin.script.signature.decode(buffer);
206
- const actual = bitcoin.script.signature.encode(
207
- parsed.signature,
208
- parsed.hashType,
209
- );
187
+ const actual = bitcoin.script.signature.encode(parsed.signature, parsed.hashType);
210
188
 
211
189
  assert.strictEqual(actual.toString('hex'), hex);
212
190
  });
@@ -1,8 +1,8 @@
1
- import * as assert from 'assert';
1
+ import assert from 'assert';
2
2
  import { beforeEach, describe, it } from 'mocha';
3
- import { Block } from '..';
3
+ import { Block } from '../src/index.js';
4
4
 
5
- import * as fixtures from './fixtures/block.json';
5
+ import fixtures from './fixtures/block.json' with { type: 'json' };
6
6
 
7
7
  describe('Block', () => {
8
8
  describe('version', () => {
@@ -18,34 +18,25 @@ describe('Block', () => {
18
18
  });
19
19
 
20
20
  describe('calculateTarget', () => {
21
- fixtures.targets.forEach(f => {
21
+ fixtures.targets.forEach((f) => {
22
22
  it('returns ' + f.expected + ' for 0x' + f.bits, () => {
23
23
  const bits = parseInt(f.bits, 16);
24
24
 
25
- assert.strictEqual(
26
- Block.calculateTarget(bits).toString('hex'),
27
- f.expected,
28
- );
25
+ assert.strictEqual(Block.calculateTarget(bits).toString('hex'), f.expected);
29
26
  });
30
27
  });
31
28
  });
32
29
 
33
30
  describe('fromBuffer/fromHex', () => {
34
- fixtures.valid.forEach(f => {
31
+ fixtures.valid.forEach((f) => {
35
32
  it('imports ' + f.description, () => {
36
33
  const block = Block.fromHex(f.hex);
37
34
 
38
35
  assert.strictEqual(block.version, f.version);
39
36
  assert.strictEqual(block.prevHash!.toString('hex'), f.prevHash);
40
- assert.strictEqual(
41
- block.merkleRoot!.toString('hex'),
42
- f.merkleRoot,
43
- );
37
+ assert.strictEqual(block.merkleRoot!.toString('hex'), f.merkleRoot);
44
38
  if (block.witnessCommit) {
45
- assert.strictEqual(
46
- block.witnessCommit.toString('hex'),
47
- f.witnessCommit,
48
- );
39
+ assert.strictEqual(block.witnessCommit.toString('hex'), f.witnessCommit);
49
40
  }
50
41
  assert.strictEqual(block.timestamp, f.timestamp);
51
42
  assert.strictEqual(block.bits, f.bits);
@@ -53,16 +44,13 @@ describe('Block', () => {
53
44
  assert.strictEqual(!block.transactions, f.hex.length === 160);
54
45
  if (f.size && f.strippedSize && f.weight) {
55
46
  assert.strictEqual(block.byteLength(false, true), f.size);
56
- assert.strictEqual(
57
- block.byteLength(false, false),
58
- f.strippedSize,
59
- );
47
+ assert.strictEqual(block.byteLength(false, false), f.strippedSize);
60
48
  assert.strictEqual(block.weight(), f.weight);
61
49
  }
62
50
  });
63
51
  });
64
52
 
65
- fixtures.invalid.forEach(f => {
53
+ fixtures.invalid.forEach((f) => {
66
54
  it('throws on ' + f.exception, () => {
67
55
  assert.throws(() => {
68
56
  Block.fromHex(f.hex);
@@ -72,7 +60,7 @@ describe('Block', () => {
72
60
  });
73
61
 
74
62
  describe('toBuffer/toHex', () => {
75
- fixtures.valid.forEach(f => {
63
+ fixtures.valid.forEach((f) => {
76
64
  let block: Block;
77
65
 
78
66
  beforeEach(() => {
@@ -87,7 +75,7 @@ describe('Block', () => {
87
75
  });
88
76
 
89
77
  describe('getHash/getId', () => {
90
- fixtures.valid.forEach(f => {
78
+ fixtures.valid.forEach((f) => {
91
79
  let block: Block;
92
80
 
93
81
  beforeEach(() => {
@@ -102,7 +90,7 @@ describe('Block', () => {
102
90
  });
103
91
 
104
92
  describe('getUTCDate', () => {
105
- fixtures.valid.forEach(f => {
93
+ fixtures.valid.forEach((f) => {
106
94
  let block: Block;
107
95
 
108
96
  beforeEach(() => {
@@ -124,7 +112,7 @@ describe('Block', () => {
124
112
  }, /Cannot compute merkle root for zero transactions/);
125
113
  });
126
114
 
127
- fixtures.valid.forEach(f => {
115
+ fixtures.valid.forEach((f) => {
128
116
  if (f.hex.length === 160) return;
129
117
 
130
118
  let block: Block;
@@ -135,35 +123,24 @@ describe('Block', () => {
135
123
 
136
124
  it('returns ' + f.merkleRoot + ' for ' + f.id, () => {
137
125
  assert.strictEqual(
138
- Block.calculateMerkleRoot(block.transactions!).toString(
139
- 'hex',
140
- ),
126
+ Block.calculateMerkleRoot(block.transactions!).toString('hex'),
141
127
  f.merkleRoot,
142
128
  );
143
129
  });
144
130
 
145
131
  if (f.witnessCommit) {
146
- it(
147
- 'returns witness commit ' +
148
- f.witnessCommit +
149
- ' for ' +
150
- f.id,
151
- () => {
152
- assert.strictEqual(
153
- Block.calculateMerkleRoot(
154
- block.transactions!,
155
- true,
156
- ).toString('hex'),
157
- f.witnessCommit,
158
- );
159
- },
160
- );
132
+ it('returns witness commit ' + f.witnessCommit + ' for ' + f.id, () => {
133
+ assert.strictEqual(
134
+ Block.calculateMerkleRoot(block.transactions!, true).toString('hex'),
135
+ f.witnessCommit,
136
+ );
137
+ });
161
138
  }
162
139
  });
163
140
  });
164
141
 
165
142
  describe('checkTxRoots', () => {
166
- fixtures.valid.forEach(f => {
143
+ fixtures.valid.forEach((f) => {
167
144
  if (f.hex.length === 160) return;
168
145
 
169
146
  let block: Block;
@@ -179,7 +156,7 @@ describe('Block', () => {
179
156
  });
180
157
 
181
158
  describe('checkProofOfWork', () => {
182
- fixtures.valid.forEach(f => {
159
+ fixtures.valid.forEach((f) => {
183
160
  let block: Block;
184
161
 
185
162
  beforeEach(() => {