@btc-vision/bitcoin 6.5.1 → 6.5.3

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 (103) hide show
  1. package/browser/chunks/crypto-0PweVewC.js +2033 -0
  2. package/browser/chunks/{payments-BE4vwHhV.js → payments-CgasufRS.js} +410 -408
  3. package/browser/chunks/psbt-BIwOrKer.js +4096 -0
  4. package/browser/chunks/{script-COWGdiOo.js → script-CROJPzz_.js} +96 -96
  5. package/browser/chunks/{transaction-BiXwH2v4.js → transaction-DchBu35N.js} +158 -147
  6. package/browser/chunks/{utils-BKmkTzNZ.js → utils-CO5kmxe9.js} +225 -223
  7. package/browser/crypto.d.ts +1 -1
  8. package/browser/hooks/HookedSigner.d.ts +1 -1
  9. package/browser/index.d.ts +23 -1
  10. package/browser/index.js +6 -6
  11. package/browser/payments/index.d.ts +2 -2
  12. package/browser/payments/lazy.d.ts +1 -1
  13. package/browser/psbt/bip371.d.ts +5 -1
  14. package/browser/psbt.d.ts +1 -1
  15. package/browser/typeforce.d.ts +38 -0
  16. package/browser/types.d.ts +22 -20
  17. package/build/address.js +2 -2
  18. package/build/bip66.js +2 -2
  19. package/build/block.js +2 -2
  20. package/build/crypto.d.ts +1 -1
  21. package/build/crypto.js +2 -3
  22. package/build/hooks/HookedSigner.d.ts +1 -1
  23. package/build/index.d.ts +23 -1
  24. package/build/payments/bip341.js +1 -1
  25. package/build/payments/index.d.ts +2 -2
  26. package/build/payments/lazy.d.ts +1 -1
  27. package/build/payments/p2op.js +3 -3
  28. package/build/payments/p2pk.js +1 -1
  29. package/build/payments/p2pkh.js +3 -3
  30. package/build/payments/p2sh.js +3 -3
  31. package/build/payments/p2tr.js +9 -5
  32. package/build/payments/p2wpkh.js +3 -3
  33. package/build/payments/p2wsh.js +2 -2
  34. package/build/psbt/bip371.d.ts +5 -1
  35. package/build/psbt/bip371.js +10 -7
  36. package/build/psbt/psbtutils.js +5 -4
  37. package/build/psbt.d.ts +1 -1
  38. package/build/psbt.js +78 -45
  39. package/build/script.js +2 -2
  40. package/build/script_signature.js +7 -7
  41. package/build/transaction.js +22 -10
  42. package/build/tsconfig.tsbuildinfo +1 -0
  43. package/build/types.d.ts +22 -20
  44. package/build/types.js +10 -9
  45. package/package.json +32 -57
  46. package/src/address.ts +2 -2
  47. package/src/bip66.ts +2 -2
  48. package/src/block.ts +8 -5
  49. package/src/crypto.ts +3 -4
  50. package/src/ecc_lib.ts +1 -1
  51. package/src/hooks/HookedSigner.ts +1 -1
  52. package/src/index.ts +34 -12
  53. package/src/payments/bip341.ts +1 -1
  54. package/src/payments/embed.ts +1 -2
  55. package/src/payments/index.ts +4 -4
  56. package/src/payments/lazy.ts +3 -3
  57. package/src/payments/p2op.ts +4 -3
  58. package/src/payments/p2pk.ts +1 -1
  59. package/src/payments/p2pkh.ts +3 -3
  60. package/src/payments/p2sh.ts +13 -5
  61. package/src/payments/p2tr.ts +8 -9
  62. package/src/payments/p2wpkh.ts +3 -3
  63. package/src/payments/p2wsh.ts +4 -4
  64. package/src/psbt/bip371.ts +22 -13
  65. package/src/psbt/psbtutils.ts +8 -5
  66. package/src/psbt.ts +127 -80
  67. package/src/script.ts +4 -4
  68. package/src/script_signature.ts +7 -7
  69. package/src/transaction.ts +31 -18
  70. package/src/typeforce.d.ts +38 -0
  71. package/src/types.ts +34 -29
  72. package/test/address.spec.ts +12 -4
  73. package/test/bitcoin.core.spec.ts +1 -1
  74. package/test/block.spec.ts +1 -1
  75. package/test/bufferutils.spec.ts +1 -1
  76. package/test/crypto.spec.ts +3 -2
  77. package/test/fixtures/address.json +1 -1
  78. package/test/integration/addresses.spec.ts +1 -1
  79. package/test/integration/bip32.spec.ts +2 -2
  80. package/test/integration/blocks.spec.ts +1 -1
  81. package/test/integration/cltv.spec.ts +3 -3
  82. package/test/integration/csv.spec.ts +3 -3
  83. package/test/integration/payments.spec.ts +1 -1
  84. package/test/integration/taproot.spec.ts +8 -7
  85. package/test/integration/transactions.spec.ts +2 -2
  86. package/test/payments.spec.ts +4 -3
  87. package/test/psbt.spec.ts +106 -74
  88. package/test/script.spec.ts +73 -7
  89. package/test/script_number.spec.ts +1 -1
  90. package/test/script_signature.spec.ts +1 -1
  91. package/test/transaction.spec.ts +1 -1
  92. package/test/tsconfig.json +1 -1
  93. package/test/types.spec.ts +1 -1
  94. package/vitest.config.ts +16 -0
  95. package/.babelrc +0 -13
  96. package/.mocharc.json +0 -13
  97. package/browser/chunks/crypto-C6FlKKmp.js +0 -2006
  98. package/browser/chunks/psbt-Dlosf9CT.js +0 -3853
  99. package/cjs/package.json +0 -3
  100. package/gulpfile.js +0 -42
  101. package/src/crypto/crypto-browser.js +0 -75
  102. package/test/ts-node-register.js +0 -7
  103. package/webpack.config.js +0 -79
@@ -4,7 +4,7 @@ export declare function sha256(buffer: Buffer): Buffer;
4
4
  export declare function hash160(buffer: Buffer): Buffer;
5
5
  export declare function hash256(buffer: Buffer): Buffer;
6
6
  export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
7
- export type TaggedHashPrefix = typeof TAGS[number];
7
+ export type TaggedHashPrefix = (typeof TAGS)[number];
8
8
  type TaggedHashPrefixes = {
9
9
  [key in TaggedHashPrefix]: Buffer;
10
10
  };
@@ -1,4 +1,4 @@
1
- import { BIP32Interface } from 'bip32';
1
+ import { BIP32Interface } from '@btc-vision/bip32';
2
2
  import { ECPairInterface } from 'ecpair';
3
3
  import { Signer, SignerAlternative, SignerAsync } from '../psbt.js';
4
4
  export declare function hookSigner(keyPair: Signer | SignerAlternative | SignerAsync | BIP32Interface | ECPairInterface): void;
@@ -1,4 +1,4 @@
1
- export type { PsbtInput, PsbtInputUpdate, PsbtOutput, TapBip32Derivation, TapInternalKey, TapKeySig, TapLeaf, TapLeafScript, TapMerkleRoot, TapScriptSig, TapTree, } from 'bip174/src/lib/interfaces.js';
1
+ import { PsbtInput as _PsbtInput, PsbtInputUpdate as _PsbtInputUpdate, PsbtOutput as _PsbtOutput, TapBip32Derivation as _TapBip32Derivation, TapInternalKey as _TapInternalKey, TapKeySig as _TapKeySig, TapLeaf as _TapLeaf, TapLeafScript as _TapLeafScript, TapMerkleRoot as _TapMerkleRoot, TapScriptSig as _TapScriptSig, TapTree as _TapTree } from 'bip174/src/lib/interfaces.js';
2
2
  import * as networks from './networks.js';
3
3
  import * as address from './address.js';
4
4
  import * as payments from './payments/index.js';
@@ -20,6 +20,28 @@ export { initEccLib } from './ecc_lib.js';
20
20
  export { PaymentType } from './payments/index.js';
21
21
  export type { Payment, PaymentCreator, PaymentOpts, Stack, StackElement, P2WSHPayment, P2PKPayment, BasePayment, P2SHPayment, P2TRPayment, P2WPKHPayment, P2PKHPayment, P2MSPayment, EmbedPayment, P2OPPayment, P2OPPaymentParams, StackFunction, } from './payments/index.js';
22
22
  export type { Input as TxInput, Output as TxOutput } from './transaction.js';
23
+ export interface PsbtInput extends _PsbtInput {
24
+ }
25
+ export interface PsbtOutput extends _PsbtOutput {
26
+ }
27
+ export interface TapInternalKey extends _TapInternalKey {
28
+ }
29
+ export interface TapLeaf extends _TapLeaf {
30
+ }
31
+ export interface TapScriptSig extends _TapScriptSig {
32
+ }
33
+ export interface TapKeySig extends _TapKeySig {
34
+ }
35
+ export interface TapTree extends _TapTree {
36
+ }
37
+ export interface TapMerkleRoot extends _TapMerkleRoot {
38
+ }
39
+ export interface TapLeafScript extends _TapLeafScript {
40
+ }
41
+ export interface TapBip32Derivation extends _TapBip32Derivation {
42
+ }
43
+ export interface PsbtInputUpdate extends _PsbtInputUpdate {
44
+ }
23
45
  export * from './psbt/bip371.js';
24
46
  export * from './address.js';
25
47
  export * from './bufferutils.js';
package/browser/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { A as t, B as e, F as r, z as i, y as o, C as p, D as S, P as T, _ as c, e as n, X as E, a0 as u, o as I, w as P, q as _, a as F, c as h, H as f, f as R, K as k, j as d, a2 as m, x as A, h as U, V as l, M as O, U as g, N as y, O as B, S as H, T as N, Q as w, R as M, l as W, m as b, E as G, n as K, p as L, $ as V, Z as x, Y as X, s as C, a1 as D, k as v, v as z, u as Z, I as j, J as q, G as J, L as Q, t as Y, r as $, W as ss } from "./chunks/psbt-Dlosf9CT.js";
2
- import { L as ts, M as es, P as rs, k as is, i as os, r as ps, l as Ss, f as Ts, j as cs, t as ns } from "./chunks/payments-BE4vwHhV.js";
3
- import { o as us, b as Is } from "./chunks/script-COWGdiOo.js";
4
- import { e as _s, T as Fs, c as hs, h as fs, d as Rs, r as ks, a as ds, b as ms, t as As } from "./chunks/crypto-C6FlKKmp.js";
5
- import { B as ls, T as Os } from "./chunks/transaction-BiXwH2v4.js";
6
- import { f as ys, B as Bs, T as Hs, c as Ns, e as ws, r as Ms, d as Ws, w as bs } from "./chunks/utils-BKmkTzNZ.js";
1
+ import { A as t, B as e, F as r, z as i, y as o, C as p, D as S, P as T, _ as c, e as n, X as E, a0 as u, o as I, w as P, q as _, a as F, c as h, H as f, f as R, K as k, j as d, a2 as m, x as A, h as U, V as l, M as O, U as g, N as y, O as B, S as H, T as N, Q as w, R as M, l as W, m as b, E as G, n as K, p as L, $ as V, Z as x, Y as X, s as C, a1 as D, k as v, v as z, u as Z, I as j, J as q, G as J, L as Q, t as Y, r as $, W as ss } from "./chunks/psbt-BIwOrKer.js";
2
+ import { L as ts, M as es, P as rs, k as is, i as os, r as ps, l as Ss, f as Ts, j as cs, t as ns } from "./chunks/payments-CgasufRS.js";
3
+ import { o as us, b as Is } from "./chunks/script-CROJPzz_.js";
4
+ import { d as _s, T as Fs, c as hs, h as fs, b as Rs, r as ks, s as ds, a as ms, t as As } from "./chunks/crypto-0PweVewC.js";
5
+ import { B as ls, T as Os } from "./chunks/transaction-DchBu35N.js";
6
+ import { f as ys, B as Bs, T as Hs, c as Ns, e as ws, r as Ms, d as Ws, w as bs } from "./chunks/utils-CO5kmxe9.js";
7
7
  export {
8
8
  ls as Block,
9
9
  ys as BufferReader,
@@ -1,5 +1,5 @@
1
- import { Network } from '../networks';
2
- import { Taptree } from '../types';
1
+ import { Network } from '../networks.js';
2
+ import { Taptree } from '../types.js';
3
3
  export * from './bip341.js';
4
4
  export * from './embed.js';
5
5
  export * from './lazy.js';
@@ -1,2 +1,2 @@
1
- export declare function prop<T extends {}>(object: T, name: string, f: () => T[keyof T]): void;
1
+ export declare function prop<T extends object>(object: T, name: string, f: () => T[keyof T]): void;
2
2
  export declare function value<T>(f: () => T): () => T;
@@ -1,5 +1,8 @@
1
1
  import { PsbtInput, PsbtOutput, TapLeaf } from 'bip174/src/lib/interfaces.js';
2
2
  import { Taptree } from '../types.js';
3
+ interface PsbtOutputWithScript extends PsbtOutput {
4
+ script?: Buffer;
5
+ }
3
6
  export declare const toXOnly: (pubKey: Buffer | Uint8Array) => Buffer;
4
7
  export declare function tapScriptFinalizer(inputIndex: number, input: PsbtInput, tapLeafHashToFinalize?: Buffer): {
5
8
  finalScriptWitness: Buffer | undefined;
@@ -8,9 +11,10 @@ export declare function serializeTaprootSignature(sig: Buffer, sighashType?: num
8
11
  export declare function isTaprootInput(input: PsbtInput): boolean;
9
12
  export declare function isTaprootOutput(output: PsbtOutput, script?: Buffer): boolean;
10
13
  export declare function checkTaprootInputFields(inputData: PsbtInput, newInputData: PsbtInput, action: string): void;
11
- export declare function checkTaprootOutputFields(outputData: PsbtOutput, newOutputData: PsbtOutput, action: string): void;
14
+ export declare function checkTaprootOutputFields(outputData: PsbtOutputWithScript, newOutputData: PsbtOutput, action: string): void;
12
15
  export declare function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Buffer;
13
16
  export declare function tapTreeToList(tree: Taptree): TapLeaf[];
14
17
  export declare function tapTreeFromList(leaves?: TapLeaf[]): Taptree;
15
18
  export declare function checkTaprootInputForSigs(input: PsbtInput, action: string): boolean;
16
19
  export declare function getTapKeySigFromWitness(finalScriptWitness?: Buffer): Buffer | undefined;
20
+ export {};
package/browser/psbt.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Psbt as PsbtBase } from 'bip174';
2
2
  import { KeyValue, PartialSig, PsbtGlobal, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces.js';
3
- import { BIP32Interface } from 'bip32';
3
+ import { BIP32Interface } from '@btc-vision/bip32';
4
4
  import { ECPairInterface } from 'ecpair';
5
5
  import { Network } from './networks.js';
6
6
  import { Transaction } from './transaction.js';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Type definitions for typeforce library
3
+ */
4
+
5
+ /**
6
+ * Typeforce type validator - validates that a value matches a given type.
7
+ */
8
+ type TypeforceValidator = (value: unknown) => boolean;
9
+
10
+ /**
11
+ * Interface representing the typeforce library's API.
12
+ */
13
+ interface TypeforceLib {
14
+ (type: unknown, value: unknown): void;
15
+ Number: TypeforceValidator;
16
+ Array: TypeforceValidator;
17
+ Boolean: TypeforceValidator;
18
+ String: TypeforceValidator;
19
+ Buffer: TypeforceValidator;
20
+ Hex: TypeforceValidator;
21
+ Null: TypeforceValidator;
22
+ Function: TypeforceValidator;
23
+ UInt8: TypeforceValidator;
24
+ UInt32: TypeforceValidator;
25
+ UInt53: TypeforceValidator;
26
+ BufferN: (n: number) => TypeforceValidator;
27
+ maybe: (type: unknown) => TypeforceValidator;
28
+ tuple: (...types: unknown[]) => TypeforceValidator;
29
+ oneOf: (...types: unknown[]) => TypeforceValidator;
30
+ arrayOf: (type: unknown) => TypeforceValidator;
31
+ Object: TypeforceValidator;
32
+ anyOf: (...types: unknown[]) => TypeforceValidator;
33
+ }
34
+
35
+ declare module 'typeforce' {
36
+ const typeforce: TypeforceLib;
37
+ export = typeforce;
38
+ }
@@ -1,4 +1,6 @@
1
- export declare const typeforce: any;
1
+ import { default as typeforce } from 'typeforce';
2
+ export { typeforce };
3
+ export type TypeforceValidator = (value: unknown) => boolean;
2
4
  export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
3
5
  export declare function isPoint(p: Buffer | number | undefined | null): boolean;
4
6
  export declare function Satoshi(value: number): boolean;
@@ -11,27 +13,27 @@ export interface Tapleaf {
11
13
  version?: number;
12
14
  }
13
15
  export declare const TAPLEAF_VERSION_MASK = 254;
14
- export declare function isTapleaf(o: any): o is Tapleaf;
16
+ export declare function isTapleaf(o: unknown): o is Tapleaf;
15
17
  export type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
16
- export declare function isTaptree(scriptTree: any): scriptTree is Taptree;
18
+ export declare function isTaptree(scriptTree: unknown): scriptTree is Taptree;
17
19
  export interface TinySecp256k1Interface {
18
20
  isXOnlyPoint(p: Uint8Array): boolean;
19
21
  xOnlyPointAddTweak(p: Uint8Array, tweak: Uint8Array): XOnlyPointAddTweakResult | null;
20
22
  }
21
- export declare const Buffer256bit: any;
22
- export declare const Hash160bit: any;
23
- export declare const Hash256bit: any;
24
- export declare const Number: any;
25
- export declare const Array: any;
26
- export declare const Boolean: any;
27
- export declare const String: any;
28
- export declare const Buffer: any;
29
- export declare const Hex: any;
30
- export declare const maybe: any;
31
- export declare const tuple: any;
32
- export declare const UInt8: any;
33
- export declare const UInt32: any;
34
- export declare const Function: any;
35
- export declare const BufferN: any;
36
- export declare const Null: any;
37
- export declare const oneOf: any;
23
+ export declare const Buffer256bit: TypeforceValidator;
24
+ export declare const Hash160bit: TypeforceValidator;
25
+ export declare const Hash256bit: TypeforceValidator;
26
+ export declare const Number: TypeforceValidator;
27
+ export declare const Array: TypeforceValidator;
28
+ export declare const Boolean: TypeforceValidator;
29
+ export declare const String: TypeforceValidator;
30
+ export declare const Buffer: TypeforceValidator;
31
+ export declare const Hex: TypeforceValidator;
32
+ export declare const maybe: (type: unknown) => TypeforceValidator;
33
+ export declare const tuple: (...types: unknown[]) => TypeforceValidator;
34
+ export declare const UInt8: TypeforceValidator;
35
+ export declare const UInt32: TypeforceValidator;
36
+ export declare const Function: TypeforceValidator;
37
+ export declare const BufferN: (n: number) => TypeforceValidator;
38
+ export declare const Null: TypeforceValidator;
39
+ export declare const oneOf: (...types: unknown[]) => TypeforceValidator;
package/build/address.js CHANGED
@@ -114,7 +114,7 @@ export function fromBech32(address) {
114
114
  };
115
115
  }
116
116
  export function toBase58Check(hash, version) {
117
- typeforce(tuple(Hash160bit, UInt8), arguments);
117
+ typeforce(tuple(Hash160bit, UInt8), [hash, version]);
118
118
  const payload = Buffer.allocUnsafe(21);
119
119
  payload.writeUInt8(version, 0);
120
120
  hash.copy(payload, 1);
@@ -216,5 +216,5 @@ export function toOutputScript(address, network) {
216
216
  }
217
217
  }
218
218
  }
219
- return Buffer.from(address, 'hex');
219
+ throw new TypeError(address + ' has no matching Script');
220
220
  }
package/build/bip66.js CHANGED
@@ -63,8 +63,8 @@ export function decode(buffer) {
63
63
  if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
64
64
  throw new Error('S value excessively padded');
65
65
  return {
66
- r: buffer.slice(4, 4 + lenR),
67
- s: buffer.slice(6 + lenR),
66
+ r: buffer.subarray(4, 4 + lenR),
67
+ s: buffer.subarray(6 + lenR),
68
68
  };
69
69
  }
70
70
  export function encode(r, s) {
package/build/block.js CHANGED
@@ -31,7 +31,7 @@ export class Block {
31
31
  if (buffer.length === 80)
32
32
  return block;
33
33
  const readTransaction = () => {
34
- const tx = Transaction.fromBuffer(bufferReader.buffer.slice(bufferReader.offset), true);
34
+ const tx = Transaction.fromBuffer(bufferReader.buffer.subarray(bufferReader.offset), true);
35
35
  bufferReader.offset += tx.byteLength();
36
36
  return tx;
37
37
  };
@@ -71,7 +71,7 @@ export class Block {
71
71
  getWitnessCommit() {
72
72
  if (!txesHaveWitnessCommit(this.transactions))
73
73
  return null;
74
- const witnessCommits = this.transactions[0].outs.filter((out) => out.script.slice(0, 6).equals(Buffer.from('6a24aa21a9ed', 'hex'))).map((out) => out.script.slice(6, 38));
74
+ const witnessCommits = this.transactions[0].outs.filter((out) => out.script.subarray(0, 6).equals(Buffer.from('6a24aa21a9ed', 'hex'))).map((out) => out.script.subarray(6, 38));
75
75
  if (witnessCommits.length === 0)
76
76
  return null;
77
77
  const result = witnessCommits[witnessCommits.length - 1];
package/build/crypto.d.ts CHANGED
@@ -4,7 +4,7 @@ export declare function sha256(buffer: Buffer): Buffer;
4
4
  export declare function hash160(buffer: Buffer): Buffer;
5
5
  export declare function hash256(buffer: Buffer): Buffer;
6
6
  export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
7
- export type TaggedHashPrefix = typeof TAGS[number];
7
+ export type TaggedHashPrefix = (typeof TAGS)[number];
8
8
  type TaggedHashPrefixes = {
9
9
  [key in TaggedHashPrefix]: Buffer;
10
10
  };
package/build/crypto.js CHANGED
@@ -1,6 +1,5 @@
1
- import { ripemd160 as _ripemd160 } from '@noble/hashes/ripemd160';
2
- import { sha1 as _sha1 } from '@noble/hashes/sha1';
3
- import { sha256 as _sha256 } from '@noble/hashes/sha256';
1
+ import { ripemd160 as _ripemd160, sha1 as _sha1 } from '@noble/hashes/legacy.js';
2
+ import { sha256 as _sha256 } from '@noble/hashes/sha2.js';
4
3
  export function ripemd160(buffer) {
5
4
  return Buffer.from(_ripemd160(Uint8Array.from(buffer)));
6
5
  }
@@ -1,4 +1,4 @@
1
- import { BIP32Interface } from 'bip32';
1
+ import { BIP32Interface } from '@btc-vision/bip32';
2
2
  import { ECPairInterface } from 'ecpair';
3
3
  import { Signer, SignerAlternative, SignerAsync } from '../psbt.js';
4
4
  export declare function hookSigner(keyPair: Signer | SignerAlternative | SignerAsync | BIP32Interface | ECPairInterface): void;
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { PsbtInput, PsbtInputUpdate, PsbtOutput, TapBip32Derivation, TapInternalKey, TapKeySig, TapLeaf, TapLeafScript, TapMerkleRoot, TapScriptSig, TapTree, } from 'bip174/src/lib/interfaces.js';
1
+ import { PsbtInput as _PsbtInput, PsbtInputUpdate as _PsbtInputUpdate, PsbtOutput as _PsbtOutput, TapBip32Derivation as _TapBip32Derivation, TapInternalKey as _TapInternalKey, TapKeySig as _TapKeySig, TapLeaf as _TapLeaf, TapLeafScript as _TapLeafScript, TapMerkleRoot as _TapMerkleRoot, TapScriptSig as _TapScriptSig, TapTree as _TapTree } from 'bip174/src/lib/interfaces.js';
2
2
  import * as networks from './networks.js';
3
3
  import * as address from './address.js';
4
4
  import * as payments from './payments/index.js';
@@ -20,6 +20,28 @@ export { initEccLib } from './ecc_lib.js';
20
20
  export { PaymentType } from './payments/index.js';
21
21
  export type { Payment, PaymentCreator, PaymentOpts, Stack, StackElement, P2WSHPayment, P2PKPayment, BasePayment, P2SHPayment, P2TRPayment, P2WPKHPayment, P2PKHPayment, P2MSPayment, EmbedPayment, P2OPPayment, P2OPPaymentParams, StackFunction, } from './payments/index.js';
22
22
  export type { Input as TxInput, Output as TxOutput } from './transaction.js';
23
+ export interface PsbtInput extends _PsbtInput {
24
+ }
25
+ export interface PsbtOutput extends _PsbtOutput {
26
+ }
27
+ export interface TapInternalKey extends _TapInternalKey {
28
+ }
29
+ export interface TapLeaf extends _TapLeaf {
30
+ }
31
+ export interface TapScriptSig extends _TapScriptSig {
32
+ }
33
+ export interface TapKeySig extends _TapKeySig {
34
+ }
35
+ export interface TapTree extends _TapTree {
36
+ }
37
+ export interface TapMerkleRoot extends _TapMerkleRoot {
38
+ }
39
+ export interface TapLeafScript extends _TapLeafScript {
40
+ }
41
+ export interface TapBip32Derivation extends _TapBip32Derivation {
42
+ }
43
+ export interface PsbtInputUpdate extends _PsbtInputUpdate {
44
+ }
23
45
  export * from './psbt/bip371.js';
24
46
  export * from './address.js';
25
47
  export * from './bufferutils.js';
@@ -12,7 +12,7 @@ export function rootHashFromPath(controlBlock, leafHash) {
12
12
  const m = (controlBlock.length - 33) / 32;
13
13
  let kj = leafHash;
14
14
  for (let j = 0; j < m; j++) {
15
- const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
15
+ const ej = controlBlock.subarray(33 + 32 * j, 65 + 32 * j);
16
16
  if (kj.compare(ej) < 0) {
17
17
  kj = tapBranchHash(kj, ej);
18
18
  }
@@ -1,5 +1,5 @@
1
- import { Network } from '../networks';
2
- import { Taptree } from '../types';
1
+ import type { Network } from '../networks.js';
2
+ import type { Taptree } from '../types.js';
3
3
  export * from './bip341.js';
4
4
  export * from './embed.js';
5
5
  export * from './lazy.js';
@@ -1,2 +1,2 @@
1
- export declare function prop<T extends {}>(object: T, name: string, f: () => T[keyof T]): void;
1
+ export declare function prop<T extends object>(object: T, name: string, f: () => T[keyof T]): void;
2
2
  export declare function value<T>(f: () => T): () => T;
@@ -64,7 +64,7 @@ export function p2op(a, opts) {
64
64
  else {
65
65
  throw new TypeError('Unsupported push opcode in P2OP script');
66
66
  }
67
- return a.output.slice(pushPos, pushPos + progLen);
67
+ return a.output.subarray(pushPos, pushPos + progLen);
68
68
  }
69
69
  if (a.address) {
70
70
  const dec = _address();
@@ -79,7 +79,7 @@ export function p2op(a, opts) {
79
79
  lazy.prop(o, 'hash160', () => {
80
80
  if (!o.program)
81
81
  return;
82
- return o.program.slice(1);
82
+ return o.program.subarray(1);
83
83
  });
84
84
  lazy.prop(o, 'output', () => {
85
85
  if (!o.program)
@@ -126,7 +126,7 @@ export function p2op(a, opts) {
126
126
  throw new TypeError(`Witness program must be 2–40 bytes. Was ${prog.length} bytes`);
127
127
  if (a.deploymentVersion !== undefined && a.deploymentVersion !== prog[0])
128
128
  throw new TypeError('deploymentVersion mismatch');
129
- if (a.hash160 && !a.hash160.equals(prog.slice(1)))
129
+ if (a.hash160 && !a.hash160.equals(prog.subarray(1)))
130
130
  throw new TypeError('hash160 mismatch');
131
131
  }
132
132
  return Object.assign(o, a);
@@ -32,7 +32,7 @@ export function p2pk(a, opts) {
32
32
  lazy.prop(o, 'pubkey', () => {
33
33
  if (!a.output)
34
34
  return;
35
- return a.output.slice(1, -1);
35
+ return a.output.subarray(1, -1);
36
36
  });
37
37
  lazy.prop(o, 'signature', () => {
38
38
  if (!a.input)
@@ -24,7 +24,7 @@ export function p2pkh(a, opts) {
24
24
  const _address = lazy.value(() => {
25
25
  const payload = Buffer.from(bs58check.default.decode(a.address));
26
26
  const version = payload.readUInt8(0);
27
- const hash = payload.slice(1);
27
+ const hash = payload.subarray(1);
28
28
  return { version, hash };
29
29
  });
30
30
  const _chunks = lazy.value(() => {
@@ -46,7 +46,7 @@ export function p2pkh(a, opts) {
46
46
  });
47
47
  lazy.prop(o, 'hash', () => {
48
48
  if (a.output)
49
- return a.output.slice(3, 23);
49
+ return a.output.subarray(3, 23);
50
50
  if (a.address)
51
51
  return _address().hash;
52
52
  if (a.pubkey || o.pubkey)
@@ -125,7 +125,7 @@ export function p2pkh(a, opts) {
125
125
  a.output[24] !== OPS.OP_CHECKSIG) {
126
126
  throw new TypeError('Output is invalid');
127
127
  }
128
- const hash2 = a.output.slice(3, 23);
128
+ const hash2 = a.output.subarray(3, 23);
129
129
  if (hash.length > 0 && !hash.equals(hash2))
130
130
  throw new TypeError('Hash mismatch');
131
131
  else
@@ -36,7 +36,7 @@ export function p2sh(a, opts) {
36
36
  const _address = lazy.value(() => {
37
37
  const payload = Buffer.from(bs58check.default.decode(a.address));
38
38
  const version = payload.readUInt8(0);
39
- const hash = payload.slice(1);
39
+ const hash = payload.subarray(1);
40
40
  return { version, hash };
41
41
  });
42
42
  const _chunks = lazy.value(() => {
@@ -62,7 +62,7 @@ export function p2sh(a, opts) {
62
62
  });
63
63
  lazy.prop(o, 'hash', () => {
64
64
  if (a.output)
65
- return a.output.slice(2, 22);
65
+ return a.output.subarray(2, 22);
66
66
  if (a.address)
67
67
  return _address().hash;
68
68
  if (o.redeem && o.redeem.output)
@@ -116,7 +116,7 @@ export function p2sh(a, opts) {
116
116
  a.output[1] !== 0x14 ||
117
117
  a.output[22] !== OPS.OP_EQUAL)
118
118
  throw new TypeError('Output is invalid');
119
- const hash2 = a.output.slice(2, 22);
119
+ const hash2 = a.output.subarray(2, 22);
120
120
  if (hash.length > 0 && !hash.equals(hash2))
121
121
  throw new TypeError('Hash mismatch');
122
122
  else
@@ -111,7 +111,7 @@ export function p2tr(a, opts) {
111
111
  if (a.pubkey)
112
112
  return a.pubkey;
113
113
  if (a.output)
114
- return a.output.slice(2);
114
+ return a.output.subarray(2);
115
115
  if (a.address)
116
116
  return _address().data;
117
117
  if (o.internalPubkey) {
@@ -125,7 +125,7 @@ export function p2tr(a, opts) {
125
125
  return a.internalPubkey;
126
126
  const witness = _witness();
127
127
  if (witness && witness.length > 1)
128
- return witness[witness.length - 1].slice(1, 33);
128
+ return witness[witness.length - 1].subarray(1, 33);
129
129
  });
130
130
  lazy.prop(o, 'signature', () => {
131
131
  if (a.signature)
@@ -176,10 +176,10 @@ export function p2tr(a, opts) {
176
176
  if (a.output) {
177
177
  if (a.output.length !== 34 || a.output[0] !== OPS.OP_1 || a.output[1] !== 0x20)
178
178
  throw new TypeError('Output is invalid');
179
- if (pubkey.length > 0 && !pubkey.equals(a.output.slice(2)))
179
+ if (pubkey.length > 0 && !pubkey.equals(a.output.subarray(2)))
180
180
  throw new TypeError('Pubkey mismatch');
181
181
  else
182
- pubkey = a.output.slice(2);
182
+ pubkey = a.output.subarray(2);
183
183
  }
184
184
  if (a.internalPubkey) {
185
185
  const tweakedKey = tweakKey(a.internalPubkey, o.hash);
@@ -188,6 +188,10 @@ export function p2tr(a, opts) {
188
188
  else
189
189
  pubkey = tweakedKey.x;
190
190
  }
191
+ if (pubkey && pubkey.length) {
192
+ if (!getEccLib().isXOnlyPoint(pubkey))
193
+ throw new TypeError('Invalid pubkey for p2tr');
194
+ }
191
195
  const hashTree = _hashTree();
192
196
  if (a.hash && hashTree) {
193
197
  if (!a.hash.equals(hashTree.hash))
@@ -232,7 +236,7 @@ export function p2tr(a, opts) {
232
236
  const m = (controlBlock.length - 33) / 32;
233
237
  if (m > 128)
234
238
  throw new TypeError(`The script path is too long. Got ${m}, expected max 128.`);
235
- const internalPubkey = controlBlock.slice(1, 33);
239
+ const internalPubkey = controlBlock.subarray(1, 33);
236
240
  if (a.internalPubkey && !a.internalPubkey.equals(internalPubkey))
237
241
  throw new TypeError('Internal pubkey mismatch');
238
242
  if (!getEccLib().isXOnlyPoint(internalPubkey))
@@ -45,7 +45,7 @@ export function p2wpkh(a, opts) {
45
45
  });
46
46
  lazy.prop(o, 'hash', () => {
47
47
  if (a.output)
48
- return a.output.slice(2, 22);
48
+ return a.output.subarray(2, 22);
49
49
  if (a.address)
50
50
  return _address().data;
51
51
  if (a.pubkey || o.pubkey)
@@ -100,10 +100,10 @@ export function p2wpkh(a, opts) {
100
100
  if (a.output) {
101
101
  if (a.output.length !== 22 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x14)
102
102
  throw new TypeError('Output is invalid');
103
- if (hash.length > 0 && !hash.equals(a.output.slice(2)))
103
+ if (hash.length > 0 && !hash.equals(a.output.subarray(2)))
104
104
  throw new TypeError('Hash mismatch');
105
105
  else
106
- hash = a.output.slice(2);
106
+ hash = a.output.subarray(2);
107
107
  }
108
108
  if (a.pubkey) {
109
109
  const pkh = bcrypto.hash160(a.pubkey);
@@ -63,7 +63,7 @@ export function p2wsh(a, opts) {
63
63
  });
64
64
  lazy.prop(o, 'hash', () => {
65
65
  if (a.output)
66
- return a.output.slice(2);
66
+ return a.output.subarray(2);
67
67
  if (a.address)
68
68
  return _address().data;
69
69
  if (o.redeem && o.redeem.output)
@@ -133,7 +133,7 @@ export function p2wsh(a, opts) {
133
133
  if (a.output) {
134
134
  if (a.output.length !== 34 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x20)
135
135
  throw new TypeError('Output is invalid');
136
- const hash2 = a.output.slice(2);
136
+ const hash2 = a.output.subarray(2);
137
137
  if (hash.length > 0 && !hash.equals(hash2))
138
138
  throw new TypeError('Hash mismatch');
139
139
  else
@@ -1,5 +1,8 @@
1
1
  import { PsbtInput, PsbtOutput, TapLeaf } from 'bip174/src/lib/interfaces.js';
2
2
  import { Taptree } from '../types.js';
3
+ interface PsbtOutputWithScript extends PsbtOutput {
4
+ script?: Buffer;
5
+ }
3
6
  export declare const toXOnly: (pubKey: Buffer | Uint8Array) => Buffer;
4
7
  export declare function tapScriptFinalizer(inputIndex: number, input: PsbtInput, tapLeafHashToFinalize?: Buffer): {
5
8
  finalScriptWitness: Buffer | undefined;
@@ -8,9 +11,10 @@ export declare function serializeTaprootSignature(sig: Buffer, sighashType?: num
8
11
  export declare function isTaprootInput(input: PsbtInput): boolean;
9
12
  export declare function isTaprootOutput(output: PsbtOutput, script?: Buffer): boolean;
10
13
  export declare function checkTaprootInputFields(inputData: PsbtInput, newInputData: PsbtInput, action: string): void;
11
- export declare function checkTaprootOutputFields(outputData: PsbtOutput, newOutputData: PsbtOutput, action: string): void;
14
+ export declare function checkTaprootOutputFields(outputData: PsbtOutputWithScript, newOutputData: PsbtOutput, action: string): void;
12
15
  export declare function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Buffer;
13
16
  export declare function tapTreeToList(tree: Taptree): TapLeaf[];
14
17
  export declare function tapTreeFromList(leaves?: TapLeaf[]): Taptree;
15
18
  export declare function checkTaprootInputForSigs(input: PsbtInput, action: string): boolean;
16
19
  export declare function getTapKeySigFromWitness(finalScriptWitness?: Buffer): Buffer | undefined;
20
+ export {};
@@ -4,7 +4,7 @@ import { LEAF_VERSION_TAPSCRIPT, MAX_TAPTREE_DEPTH, rootHashFromPath, tapleafHas
4
4
  import { p2tr } from '../payments/p2tr.js';
5
5
  import { isP2TR, pubkeyPositionInScript, signatureBlocksAction, witnessStackToScriptWitness, } from './psbtutils.js';
6
6
  export const toXOnly = (pubKey) => {
7
- const buffer = pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
7
+ const buffer = pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
8
8
  return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
9
9
  };
10
10
  export function tapScriptFinalizer(inputIndex, input, tapLeafHashToFinalize) {
@@ -51,7 +51,7 @@ function checkTaprootScriptPubkey(outputData, newOutputData) {
51
51
  const tapInternalKey = newOutputData.tapInternalKey || outputData.tapInternalKey;
52
52
  const tapTree = newOutputData.tapTree || outputData.tapTree;
53
53
  if (tapInternalKey) {
54
- const { script: scriptPubkey } = outputData;
54
+ const scriptPubkey = outputData.script;
55
55
  const script = getTaprootScripPubkey(tapInternalKey, tapTree);
56
56
  if (scriptPubkey && !scriptPubkey.equals(script))
57
57
  throw new Error('Error adding output. Script or address missmatch.');
@@ -63,6 +63,8 @@ function getTaprootScripPubkey(tapInternalKey, tapTree) {
63
63
  internalPubkey: tapInternalKey,
64
64
  scriptTree,
65
65
  });
66
+ if (!output)
67
+ throw new Error('Failed to generate taproot script pubkey');
66
68
  return output;
67
69
  }
68
70
  export function tweakInternalPubKey(inputIndex, input) {
@@ -91,8 +93,8 @@ export function checkTaprootInputForSigs(input, action) {
91
93
  }
92
94
  function decodeSchnorrSignature(signature) {
93
95
  return {
94
- signature: signature.slice(0, 64),
95
- hashType: signature.slice(64)[0] || Transaction.SIGHASH_DEFAULT,
96
+ signature: signature.subarray(0, 64),
97
+ hashType: signature.subarray(64)[0] || Transaction.SIGHASH_DEFAULT,
96
98
  };
97
99
  }
98
100
  function extractTaprootSigs(input) {
@@ -111,7 +113,7 @@ function extractTaprootSigs(input) {
111
113
  export function getTapKeySigFromWitness(finalScriptWitness) {
112
114
  if (!finalScriptWitness)
113
115
  return;
114
- const witness = finalScriptWitness.slice(2);
116
+ const witness = finalScriptWitness.subarray(2);
115
117
  if (witness.length === 64 || witness.length === 65)
116
118
  return witness;
117
119
  }
@@ -223,11 +225,12 @@ function addPubkeyPositionInScript(script, tss) {
223
225
  }, tss);
224
226
  }
225
227
  function findTapLeafToFinalize(input, inputIndex, leafHashToFinalize) {
226
- if (!input.tapScriptSig || !input.tapScriptSig.length)
228
+ const { tapScriptSig } = input;
229
+ if (!tapScriptSig || !tapScriptSig.length)
227
230
  throw new Error(`Can not finalize taproot input #${inputIndex}. No tapleaf script signature provided.`);
228
231
  const tapLeaf = (input.tapLeafScript || [])
229
232
  .sort((a, b) => a.controlBlock.length - b.controlBlock.length)
230
- .find((leaf) => canFinalizeLeaf(leaf, input.tapScriptSig, leafHashToFinalize));
233
+ .find((leaf) => canFinalizeLeaf(leaf, tapScriptSig, leafHashToFinalize));
231
234
  if (!tapLeaf)
232
235
  throw new Error(`Can not finalize taproot input #${inputIndex}. Signature for tapleaf script not found.`);
233
236
  return tapLeaf;