@btc-vision/bitcoin 6.4.11 → 6.5.2

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 (105) hide show
  1. package/browser/chunks/crypto-0PweVewC.js +2033 -0
  2. package/browser/chunks/payments-CgasufRS.js +1047 -0
  3. package/browser/chunks/psbt-BIwOrKer.js +4096 -0
  4. package/browser/chunks/script-CROJPzz_.js +318 -0
  5. package/browser/chunks/transaction-DchBu35N.js +432 -0
  6. package/browser/chunks/utils-CO5kmxe9.js +761 -0
  7. package/browser/crypto/crypto.d.ts +1 -1
  8. package/browser/crypto.d.ts +1 -1
  9. package/browser/hooks/HookedSigner.d.ts +1 -1
  10. package/browser/index.d.ts +6 -4
  11. package/browser/index.js +92 -2
  12. package/browser/payments/index.d.ts +2 -2
  13. package/browser/payments/lazy.d.ts +1 -1
  14. package/browser/psbt/bip371.d.ts +5 -1
  15. package/browser/psbt.d.ts +1 -1
  16. package/browser/typeforce.d.ts +38 -0
  17. package/browser/types.d.ts +22 -20
  18. package/build/address.js +2 -2
  19. package/build/bip66.js +2 -2
  20. package/build/block.js +2 -2
  21. package/build/crypto.d.ts +1 -1
  22. package/build/crypto.js +2 -3
  23. package/build/hooks/HookedSigner.d.ts +1 -1
  24. package/build/index.d.ts +6 -4
  25. package/build/index.js +2 -2
  26. package/build/payments/bip341.js +1 -1
  27. package/build/payments/index.d.ts +2 -2
  28. package/build/payments/lazy.d.ts +1 -1
  29. package/build/payments/p2op.js +3 -3
  30. package/build/payments/p2pk.js +1 -1
  31. package/build/payments/p2pkh.js +3 -3
  32. package/build/payments/p2sh.js +3 -3
  33. package/build/payments/p2tr.js +9 -5
  34. package/build/payments/p2wpkh.js +3 -3
  35. package/build/payments/p2wsh.js +2 -2
  36. package/build/psbt/bip371.d.ts +5 -1
  37. package/build/psbt/bip371.js +10 -7
  38. package/build/psbt/psbtutils.js +5 -4
  39. package/build/psbt.d.ts +1 -1
  40. package/build/psbt.js +78 -45
  41. package/build/script.js +2 -2
  42. package/build/script_signature.js +7 -7
  43. package/build/transaction.js +22 -10
  44. package/build/tsconfig.tsbuildinfo +1 -0
  45. package/build/types.d.ts +22 -20
  46. package/build/types.js +10 -9
  47. package/package.json +37 -63
  48. package/src/address.ts +2 -2
  49. package/src/bip66.ts +2 -2
  50. package/src/block.ts +8 -5
  51. package/src/crypto.ts +3 -4
  52. package/src/ecc_lib.ts +1 -1
  53. package/src/hooks/HookedSigner.ts +1 -1
  54. package/src/index.ts +6 -6
  55. package/src/payments/bip341.ts +1 -1
  56. package/src/payments/embed.ts +1 -2
  57. package/src/payments/index.ts +4 -4
  58. package/src/payments/lazy.ts +3 -3
  59. package/src/payments/p2op.ts +4 -3
  60. package/src/payments/p2pk.ts +1 -1
  61. package/src/payments/p2pkh.ts +3 -3
  62. package/src/payments/p2sh.ts +13 -5
  63. package/src/payments/p2tr.ts +8 -9
  64. package/src/payments/p2wpkh.ts +3 -3
  65. package/src/payments/p2wsh.ts +4 -4
  66. package/src/psbt/bip371.ts +22 -13
  67. package/src/psbt/psbtutils.ts +8 -5
  68. package/src/psbt.ts +127 -80
  69. package/src/script.ts +4 -4
  70. package/src/script_signature.ts +7 -7
  71. package/src/transaction.ts +31 -18
  72. package/src/typeforce.d.ts +38 -0
  73. package/src/types.ts +34 -29
  74. package/test/address.spec.ts +12 -4
  75. package/test/bitcoin.core.spec.ts +1 -1
  76. package/test/block.spec.ts +1 -1
  77. package/test/bufferutils.spec.ts +1 -1
  78. package/test/crypto.spec.ts +3 -2
  79. package/test/fixtures/address.json +1 -1
  80. package/test/integration/addresses.spec.ts +1 -1
  81. package/test/integration/bip32.spec.ts +2 -2
  82. package/test/integration/blocks.spec.ts +1 -1
  83. package/test/integration/cltv.spec.ts +3 -3
  84. package/test/integration/csv.spec.ts +3 -3
  85. package/test/integration/payments.spec.ts +1 -1
  86. package/test/integration/taproot.spec.ts +8 -7
  87. package/test/integration/transactions.spec.ts +2 -2
  88. package/test/payments.spec.ts +4 -3
  89. package/test/psbt.spec.ts +106 -74
  90. package/test/script.spec.ts +73 -7
  91. package/test/script_number.spec.ts +1 -1
  92. package/test/script_signature.spec.ts +1 -1
  93. package/test/transaction.spec.ts +1 -1
  94. package/test/tsconfig.json +1 -1
  95. package/test/types.spec.ts +1 -1
  96. package/vite.config.browser.ts +93 -0
  97. package/vitest.config.ts +16 -0
  98. package/.babelrc +0 -4
  99. package/.mocharc.json +0 -13
  100. package/browser/index.js.LICENSE.txt +0 -14
  101. package/cjs/package.json +0 -3
  102. package/gulpfile.js +0 -42
  103. package/src/crypto/crypto-browser.js +0 -75
  104. package/test/ts-node-register.js +0 -7
  105. package/webpack.config.js +0 -79
@@ -65,7 +65,7 @@ export function p2wpkh(a: Omit<P2WPKHPayment, 'name'>, opts?: PaymentOpts): P2WP
65
65
  return bech32.encode(network.bech32, words);
66
66
  });
67
67
  lazy.prop(o, 'hash', () => {
68
- if (a.output) return a.output.slice(2, 22);
68
+ if (a.output) return a.output.subarray(2, 22);
69
69
  if (a.address) return _address().data;
70
70
  if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
71
71
  });
@@ -111,9 +111,9 @@ export function p2wpkh(a: Omit<P2WPKHPayment, 'name'>, opts?: PaymentOpts): P2WP
111
111
  if (a.output) {
112
112
  if (a.output.length !== 22 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x14)
113
113
  throw new TypeError('Output is invalid');
114
- if (hash.length > 0 && !hash.equals(a.output.slice(2)))
114
+ if (hash.length > 0 && !hash.equals(a.output.subarray(2)))
115
115
  throw new TypeError('Hash mismatch');
116
- else hash = a.output.slice(2);
116
+ else hash = a.output.subarray(2);
117
117
  }
118
118
 
119
119
  if (a.pubkey) {
@@ -82,10 +82,10 @@ export function p2wsh(a: Omit<P2WSHPayment, 'name'>, opts?: PaymentOpts): P2WSHP
82
82
  if (!o.hash) return;
83
83
  const words = bech32.toWords(o.hash);
84
84
  words.unshift(0x00);
85
- return bech32.encode(network!.bech32, words);
85
+ return bech32.encode(network.bech32, words);
86
86
  });
87
87
  lazy.prop(o, 'hash', () => {
88
- if (a.output) return a.output.slice(2);
88
+ if (a.output) return a.output.subarray(2);
89
89
  if (a.address) return _address().data;
90
90
  if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output);
91
91
  });
@@ -129,7 +129,7 @@ export function p2wsh(a: Omit<P2WSHPayment, 'name'>, opts?: PaymentOpts): P2WSHP
129
129
  });
130
130
  lazy.prop(o, 'name', () => {
131
131
  const nameParts = ['p2wsh'];
132
- if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name!);
132
+ if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name);
133
133
  return nameParts.join('-');
134
134
  });
135
135
 
@@ -152,7 +152,7 @@ export function p2wsh(a: Omit<P2WSHPayment, 'name'>, opts?: PaymentOpts): P2WSHP
152
152
  if (a.output) {
153
153
  if (a.output.length !== 34 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x20)
154
154
  throw new TypeError('Output is invalid');
155
- const hash2 = a.output.slice(2);
155
+ const hash2 = a.output.subarray(2);
156
156
  if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
157
157
  else hash = hash2;
158
158
  }
@@ -9,6 +9,10 @@ import {
9
9
  } from 'bip174/src/lib/interfaces.js';
10
10
  import { isTapleaf, isTaptree, Tapleaf, Taptree } from '../types.js';
11
11
 
12
+ interface PsbtOutputWithScript extends PsbtOutput {
13
+ script?: Buffer;
14
+ }
15
+
12
16
  import { Transaction } from '../transaction.js';
13
17
 
14
18
  import {
@@ -27,7 +31,7 @@ import {
27
31
  } from './psbtutils.js';
28
32
 
29
33
  export const toXOnly = (pubKey: Buffer | Uint8Array): Buffer => {
30
- const buffer = pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
34
+ const buffer = pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
31
35
 
32
36
  return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
33
37
  };
@@ -60,7 +64,7 @@ export function tapScriptFinalizer(
60
64
  }
61
65
 
62
66
  export function serializeTaprootSignature(sig: Buffer, sighashType?: number): Buffer {
63
- const sighashTypeByte = sighashType ? Buffer.from([sighashType!]) : Buffer.from([]);
67
+ const sighashTypeByte = sighashType ? Buffer.from([sighashType]) : Buffer.from([]);
64
68
 
65
69
  return Buffer.concat([sig, sighashTypeByte]);
66
70
  }
@@ -100,7 +104,7 @@ export function checkTaprootInputFields(
100
104
  }
101
105
 
102
106
  export function checkTaprootOutputFields(
103
- outputData: PsbtOutput,
107
+ outputData: PsbtOutputWithScript,
104
108
  newOutputData: PsbtOutput,
105
109
  action: string,
106
110
  ): void {
@@ -108,14 +112,17 @@ export function checkTaprootOutputFields(
108
112
  checkTaprootScriptPubkey(outputData, newOutputData);
109
113
  }
110
114
 
111
- function checkTaprootScriptPubkey(outputData: PsbtOutput, newOutputData: PsbtOutput): void {
115
+ function checkTaprootScriptPubkey(
116
+ outputData: PsbtOutputWithScript,
117
+ newOutputData: PsbtOutput,
118
+ ): void {
112
119
  if (!newOutputData.tapTree && !newOutputData.tapInternalKey) return;
113
120
 
114
121
  const tapInternalKey = newOutputData.tapInternalKey || outputData.tapInternalKey;
115
122
  const tapTree = newOutputData.tapTree || outputData.tapTree;
116
123
 
117
124
  if (tapInternalKey) {
118
- const { script: scriptPubkey } = outputData as any;
125
+ const scriptPubkey = outputData.script;
119
126
  const script = getTaprootScripPubkey(tapInternalKey, tapTree);
120
127
  if (scriptPubkey && !scriptPubkey.equals(script))
121
128
  throw new Error('Error adding output. Script or address missmatch.');
@@ -128,7 +135,8 @@ function getTaprootScripPubkey(tapInternalKey: TapInternalKey, tapTree?: TapTree
128
135
  internalPubkey: tapInternalKey,
129
136
  scriptTree,
130
137
  });
131
- return output!;
138
+ if (!output) throw new Error('Failed to generate taproot script pubkey');
139
+ return output;
132
140
  }
133
141
 
134
142
  export function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Buffer {
@@ -186,8 +194,8 @@ function decodeSchnorrSignature(signature: Buffer): {
186
194
  hashType: number;
187
195
  } {
188
196
  return {
189
- signature: signature.slice(0, 64),
190
- hashType: signature.slice(64)[0] || Transaction.SIGHASH_DEFAULT,
197
+ signature: signature.subarray(0, 64),
198
+ hashType: signature.subarray(64)[0] || Transaction.SIGHASH_DEFAULT,
191
199
  };
192
200
  }
193
201
 
@@ -205,7 +213,7 @@ function extractTaprootSigs(input: PsbtInput): Buffer[] {
205
213
 
206
214
  export function getTapKeySigFromWitness(finalScriptWitness?: Buffer): Buffer | undefined {
207
215
  if (!finalScriptWitness) return;
208
- const witness = finalScriptWitness.slice(2);
216
+ const witness = finalScriptWitness.subarray(2);
209
217
  // todo: add schnorr signature validation
210
218
  if (witness.length === 64 || witness.length === 65) return witness;
211
219
  }
@@ -358,7 +366,7 @@ function sortSignatures(input: PsbtInput, tapLeaf: TapLeafScript): Buffer[] {
358
366
  .filter((tss) => tss.leafHash.equals(leafHash))
359
367
  .map((tss) => addPubkeyPositionInScript(tapLeaf.script, tss))
360
368
  .sort((t1, t2) => t2.positionInScript - t1.positionInScript)
361
- .map((t) => t.signature) as Buffer[];
369
+ .map((t) => t.signature);
362
370
  }
363
371
 
364
372
  /**
@@ -384,13 +392,14 @@ function findTapLeafToFinalize(
384
392
  inputIndex: number,
385
393
  leafHashToFinalize?: Buffer,
386
394
  ): TapLeafScript {
387
- if (!input.tapScriptSig || !input.tapScriptSig.length)
395
+ const { tapScriptSig } = input;
396
+ if (!tapScriptSig || !tapScriptSig.length)
388
397
  throw new Error(
389
398
  `Can not finalize taproot input #${inputIndex}. No tapleaf script signature provided.`,
390
399
  );
391
400
  const tapLeaf = (input.tapLeafScript || [])
392
401
  .sort((a, b) => a.controlBlock.length - b.controlBlock.length)
393
- .find((leaf) => canFinalizeLeaf(leaf, input.tapScriptSig!, leafHashToFinalize));
402
+ .find((leaf) => canFinalizeLeaf(leaf, tapScriptSig, leafHashToFinalize));
394
403
 
395
404
  if (!tapLeaf)
396
405
  throw new Error(
@@ -419,7 +428,7 @@ function canFinalizeLeaf(
419
428
  });
420
429
  const whiteListedHash = !hash || hash.equals(leafHash);
421
430
  return (
422
- whiteListedHash && tapScriptSig!.find((tss) => tss.leafHash.equals(leafHash)) !== undefined
431
+ whiteListedHash && tapScriptSig.find((tss) => tss.leafHash.equals(leafHash)) !== undefined
423
432
  );
424
433
  }
425
434
 
@@ -14,12 +14,14 @@ import { Transaction } from '../transaction.js';
14
14
  import { toXOnly } from './bip371.js';
15
15
  import { p2op } from '../payments/p2op.js';
16
16
 
17
- function isPaymentFactory(payment: any): (script: Buffer) => boolean {
17
+ type PaymentFunction = (opts: { output: Buffer }) => unknown;
18
+
19
+ function isPaymentFactory(payment: PaymentFunction): (script: Buffer) => boolean {
18
20
  return (script: Buffer): boolean => {
19
21
  try {
20
22
  payment({ output: script });
21
23
  return true;
22
- } catch (err) {
24
+ } catch {
23
25
  return false;
24
26
  }
25
27
  };
@@ -299,12 +301,13 @@ export function signatureBlocksAction(
299
301
  * @returns An array of signatures extracted from the PsbtInput object.
300
302
  */
301
303
  function extractPartialSigs(input: PsbtInput): Buffer[] {
302
- let pSigs: PartialSig[] = [];
303
- if ((input.partialSig || []).length === 0) {
304
+ const { partialSig } = input;
305
+ let pSigs: PartialSig[];
306
+ if (!partialSig || partialSig.length === 0) {
304
307
  if (!input.finalScriptSig && !input.finalScriptWitness) return [];
305
308
  pSigs = getPsigsFromInputFinalScripts(input);
306
309
  } else {
307
- pSigs = input.partialSig!;
310
+ pSigs = partialSig;
308
311
  }
309
312
  return pSigs.map((p) => p.signature);
310
313
  }