@bitcoinerlab/descriptors 2.3.6 → 3.0.0

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.
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveMultipathDescriptor = resolveMultipathDescriptor;
4
+ /**
5
+ * Replaces the receive/change shorthand `/**` with its canonical multipath
6
+ * representation `/<0;1>/*`.
7
+ */
8
+ function expandReceiveChangeShorthand(descriptor) {
9
+ return descriptor.replace(/\/\*\*/g, '/<0;1>/*');
10
+ }
11
+ /**
12
+ * Parses a multipath tuple body from `<...>`.
13
+ *
14
+ * This implementation intentionally accepts only decimal numbers (no hardened
15
+ * suffixes) and enforces strict left-to-right increase as a safety feature to
16
+ * catch likely human errors such as `<1;0>`.
17
+ */
18
+ function parseMultipathTuple(tupleBody) {
19
+ const parts = tupleBody.split(';');
20
+ if (parts.length < 2)
21
+ throw new Error(`Error: multipath tuple must contain at least 2 values, got <${tupleBody}>`);
22
+ const values = parts.map(part => {
23
+ if (!/^(0|[1-9]\d*)$/.test(part))
24
+ throw new Error(`Error: multipath tuple values must be decimal numbers, got <${tupleBody}>`);
25
+ const value = Number(part);
26
+ if (!Number.isSafeInteger(value))
27
+ throw new Error(`Error: multipath tuple value overflow, got <${tupleBody}>`);
28
+ return value;
29
+ });
30
+ for (let i = 1; i < values.length; i++) {
31
+ const prev = values[i - 1];
32
+ const current = values[i];
33
+ if (prev === undefined || current === undefined)
34
+ throw new Error(`Error: invalid multipath tuple <${tupleBody}>`);
35
+ if (current <= prev)
36
+ throw new Error(`Error: multipath tuple values must be strictly increasing from left to right, got <${tupleBody}>`);
37
+ }
38
+ return values;
39
+ }
40
+ /**
41
+ * Resolves all multipath tuple segments (for example `/<0;1>/*`) in lockstep
42
+ * using the provided `change` value.
43
+ *
44
+ * - `/**` is first canonicalized to `/<0;1>/*`.
45
+ * - All tuples in the descriptor must have the same cardinality.
46
+ * - Tuple values must be strictly increasing decimal numbers.
47
+ * - `change` must match one of the values in each tuple.
48
+ */
49
+ function resolveMultipathDescriptor({ descriptor, change }) {
50
+ const canonicalDescriptor = expandReceiveChangeShorthand(descriptor);
51
+ const tupleMatches = Array.from(canonicalDescriptor.matchAll(/\/<([^<>]+)>/g), match => ({
52
+ token: match[0],
53
+ tupleBody: match[1],
54
+ values: parseMultipathTuple(match[1])
55
+ }));
56
+ if (tupleMatches.length === 0)
57
+ return canonicalDescriptor;
58
+ if (change === undefined)
59
+ throw new Error(`Error: change was not provided for multipath descriptor`);
60
+ if (!Number.isInteger(change) || change < 0)
61
+ throw new Error(`Error: invalid change ${change}`);
62
+ const tupleSize = tupleMatches[0]?.values.length;
63
+ if (!tupleSize)
64
+ throw new Error(`Error: invalid multipath tuple`);
65
+ for (const tupleMatch of tupleMatches) {
66
+ if (tupleMatch.values.length !== tupleSize)
67
+ throw new Error(`Error: all multipath tuples must have the same number of options`);
68
+ }
69
+ let resolvedDescriptor = canonicalDescriptor;
70
+ for (const tupleMatch of tupleMatches) {
71
+ if (!tupleMatch.values.includes(change))
72
+ throw new Error(`Error: change ${change} not found in multipath tuple <${tupleMatch.tupleBody}>`);
73
+ resolvedDescriptor = resolvedDescriptor.replaceAll(tupleMatch.token, `/${change}`);
74
+ }
75
+ return resolvedDescriptor;
76
+ }
@@ -0,0 +1,3 @@
1
+ import type { Network } from 'bitcoinjs-lib';
2
+ export declare function isBitcoinMainnet(network: Network): boolean;
3
+ export declare function coinTypeFromNetwork(network: Network): 0 | 1;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBitcoinMainnet = isBitcoinMainnet;
4
+ exports.coinTypeFromNetwork = coinTypeFromNetwork;
5
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
6
+ function isBitcoinMainnet(network) {
7
+ return (network.bech32 === bitcoinjs_lib_1.networks.bitcoin.bech32 &&
8
+ network.bip32.public === bitcoinjs_lib_1.networks.bitcoin.bip32.public &&
9
+ network.bip32.private === bitcoinjs_lib_1.networks.bitcoin.bip32.private &&
10
+ network.pubKeyHash === bitcoinjs_lib_1.networks.bitcoin.pubKeyHash &&
11
+ network.scriptHash === bitcoinjs_lib_1.networks.bitcoin.scriptHash &&
12
+ network.wif === bitcoinjs_lib_1.networks.bitcoin.wif);
13
+ }
14
+ function coinTypeFromNetwork(network) {
15
+ return isBitcoinMainnet(network) ? 0 : 1;
16
+ }
@@ -0,0 +1,7 @@
1
+ export declare function splitTopLevelComma({ expression, onError }: {
2
+ expression: string;
3
+ onError: (expression: string) => Error;
4
+ }): {
5
+ left: string;
6
+ right: string;
7
+ } | null;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitTopLevelComma = splitTopLevelComma;
4
+ function splitTopLevelComma({ expression, onError }) {
5
+ let braceDepth = 0;
6
+ let parenDepth = 0;
7
+ let commaIndex = -1;
8
+ for (let i = 0; i < expression.length; i++) {
9
+ const char = expression[i];
10
+ if (!char)
11
+ continue;
12
+ if (char === '{') {
13
+ braceDepth++;
14
+ }
15
+ else if (char === '}') {
16
+ if (braceDepth === 0)
17
+ throw onError(expression);
18
+ braceDepth--;
19
+ }
20
+ else if (char === '(') {
21
+ //Track miniscript argument lists so we don't split on commas inside them.
22
+ parenDepth++;
23
+ }
24
+ else if (char === ')') {
25
+ if (parenDepth === 0)
26
+ throw onError(expression);
27
+ parenDepth--;
28
+ }
29
+ else if (char === ',') {
30
+ if (braceDepth === 0 && parenDepth === 0) {
31
+ if (commaIndex !== -1)
32
+ throw onError(expression);
33
+ commaIndex = i;
34
+ }
35
+ }
36
+ }
37
+ if (braceDepth !== 0 || parenDepth !== 0)
38
+ throw onError(expression);
39
+ if (commaIndex === -1)
40
+ return null;
41
+ const left = expression.slice(0, commaIndex).trim();
42
+ const right = expression.slice(commaIndex + 1).trim();
43
+ if (!left || !right)
44
+ throw onError(expression);
45
+ return { left, right };
46
+ }
package/dist/psbt.d.ts CHANGED
@@ -1,40 +1,44 @@
1
- import type { PsbtInput } from 'bip174/src/lib/interfaces';
1
+ import type { PsbtInput, TapBip32Derivation, TapLeafScript } from 'bip174';
2
2
  import type { KeyInfo } from './types';
3
3
  import { Network, Psbt } from 'bitcoinjs-lib';
4
4
  /**
5
5
  * This function must do two things:
6
6
  * 1. Check if the `input` can be finalized. If it can not be finalized, throw.
7
7
  * ie. `Can not finalize input #${inputIndex}`
8
- * 2. Create the finalScriptSig and finalScriptWitness Buffers.
8
+ * 2. Create finalScriptSig and finalScriptWitness.
9
9
  */
10
10
  type FinalScriptsFunc = (inputIndex: number, // Which input is it?
11
11
  input: PsbtInput, // The PSBT input contents
12
- script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
12
+ script: Uint8Array, // The "meaningful" locking script (redeemScript for P2SH etc.)
13
13
  isSegwit: boolean, // Is it segwit?
14
14
  isP2SH: boolean, // Is it P2SH?
15
15
  isP2WSH: boolean) => {
16
- finalScriptSig: Buffer | undefined;
17
- finalScriptWitness: Buffer | undefined;
16
+ finalScriptSig: Uint8Array | undefined;
17
+ finalScriptWitness: Uint8Array | undefined;
18
18
  };
19
- export declare function finalScriptsFuncFactory(scriptSatisfaction: Buffer, network: Network): FinalScriptsFunc;
19
+ export declare function finalScriptsFuncFactory(scriptSatisfaction: Uint8Array, network: Network): FinalScriptsFunc;
20
20
  /**
21
- * Important: Read comments on descriptor.updatePsbt regarding not passing txHex
21
+ * Important: Read comments on Output.updatePsbtAsInput regarding not passing txHex
22
22
  */
23
- export declare function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysInfo, scriptPubKey, isSegwit, tapInternalKey, witnessScript, redeemScript, rbf }: {
23
+ export declare function addPsbtInput({ psbt, vout, txHex, txId, value, sequence, locktime, keysInfo, scriptPubKey, isSegwit, tapInternalKey, tapLeafScript, tapBip32Derivation, witnessScript, redeemScript, rbf }: {
24
24
  psbt: Psbt;
25
25
  vout: number;
26
26
  txHex?: string;
27
27
  txId?: string;
28
- value?: number;
28
+ value?: bigint;
29
29
  sequence: number | undefined;
30
30
  locktime: number | undefined;
31
31
  keysInfo: KeyInfo[];
32
- scriptPubKey: Buffer;
32
+ scriptPubKey: Uint8Array;
33
33
  isSegwit: boolean;
34
34
  /** for taproot **/
35
- tapInternalKey?: Buffer | undefined;
36
- witnessScript: Buffer | undefined;
37
- redeemScript: Buffer | undefined;
35
+ tapInternalKey?: Uint8Array | undefined;
36
+ /** for taproot script-path **/
37
+ tapLeafScript?: TapLeafScript[] | undefined;
38
+ /** for taproot **/
39
+ tapBip32Derivation?: TapBip32Derivation[] | undefined;
40
+ witnessScript: Uint8Array | undefined;
41
+ redeemScript: Uint8Array | undefined;
38
42
  rbf: boolean;
39
43
  }): number;
40
44
  export {};
package/dist/psbt.js CHANGED
@@ -3,43 +3,23 @@
3
3
  // Distributed under the MIT software license
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.finalScriptsFuncFactory = finalScriptsFuncFactory;
6
- exports.updatePsbt = updatePsbt;
6
+ exports.addPsbtInput = addPsbtInput;
7
7
  const bitcoinjs_lib_1 = require("bitcoinjs-lib");
8
- const varuint_bitcoin_1 = require("varuint-bitcoin");
9
- function reverseBuffer(buffer) {
8
+ const uint8array_tools_1 = require("uint8array-tools");
9
+ const bitcoinjs_lib_internals_1 = require("./bitcoinjs-lib-internals");
10
+ function reverseBytes(buffer) {
10
11
  if (buffer.length < 1)
11
12
  return buffer;
12
- let j = buffer.length - 1;
13
+ const copy = Uint8Array.from(buffer);
14
+ let j = copy.length - 1;
13
15
  let tmp = 0;
14
- for (let i = 0; i < buffer.length / 2; i++) {
15
- tmp = buffer[i];
16
- buffer[i] = buffer[j];
17
- buffer[j] = tmp;
16
+ for (let i = 0; i < copy.length / 2; i++) {
17
+ tmp = copy[i];
18
+ copy[i] = copy[j];
19
+ copy[j] = tmp;
18
20
  j--;
19
21
  }
20
- return buffer;
21
- }
22
- function witnessStackToScriptWitness(witness) {
23
- let buffer = Buffer.allocUnsafe(0);
24
- function writeSlice(slice) {
25
- buffer = Buffer.concat([buffer, Buffer.from(slice)]);
26
- }
27
- function writeVarInt(i) {
28
- const currentLen = buffer.length;
29
- const varintLen = (0, varuint_bitcoin_1.encodingLength)(i);
30
- buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
31
- (0, varuint_bitcoin_1.encode)(i, buffer, currentLen);
32
- }
33
- function writeVarSlice(slice) {
34
- writeVarInt(slice.length);
35
- writeSlice(slice);
36
- }
37
- function writeVector(vector) {
38
- writeVarInt(vector.length);
39
- vector.forEach(writeVarSlice);
40
- }
41
- writeVector(witness);
42
- return buffer;
22
+ return copy;
43
23
  }
44
24
  function finalScriptsFuncFactory(scriptSatisfaction, network) {
45
25
  const finalScriptsFunc = (_index, _input, lockingScript /*witnessScript or redeemScript*/, isSegwit, isP2SH, _isP2WSH) => {
@@ -53,7 +33,7 @@ function finalScriptsFuncFactory(scriptSatisfaction, network) {
53
33
  });
54
34
  if (!payment.witness)
55
35
  throw new Error(`Error: p2wsh failed producing a witness`);
56
- finalScriptWitness = witnessStackToScriptWitness(payment.witness);
36
+ finalScriptWitness = (0, bitcoinjs_lib_internals_1.witnessStackToScriptWitness)(payment.witness);
57
37
  }
58
38
  //p2sh-p2wsh
59
39
  else if (isSegwit && isP2SH) {
@@ -66,7 +46,7 @@ function finalScriptsFuncFactory(scriptSatisfaction, network) {
66
46
  });
67
47
  if (!payment.witness)
68
48
  throw new Error(`Error: p2sh-p2wsh failed producing a witness`);
69
- finalScriptWitness = witnessStackToScriptWitness(payment.witness);
49
+ finalScriptWitness = (0, bitcoinjs_lib_internals_1.witnessStackToScriptWitness)(payment.witness);
70
50
  finalScriptSig = payment.input;
71
51
  }
72
52
  //p2sh
@@ -84,9 +64,14 @@ function finalScriptsFuncFactory(scriptSatisfaction, network) {
84
64
  return finalScriptsFunc;
85
65
  }
86
66
  /**
87
- * Important: Read comments on descriptor.updatePsbt regarding not passing txHex
67
+ * Important: Read comments on Output.updatePsbtAsInput regarding not passing txHex
88
68
  */
89
- function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysInfo, scriptPubKey, isSegwit, tapInternalKey, witnessScript, redeemScript, rbf }) {
69
+ function addPsbtInput({ psbt, vout, txHex, txId, value, sequence, locktime, keysInfo, scriptPubKey, isSegwit, tapInternalKey, tapLeafScript, tapBip32Derivation, witnessScript, redeemScript, rbf }) {
70
+ if (value !== undefined && typeof value !== 'bigint')
71
+ throw new Error(`Error: value must be a bigint`);
72
+ if (value !== undefined && value < 0n)
73
+ throw new Error(`Error: value must be >= 0n`);
74
+ let normalizedValue = value;
90
75
  //Some data-sanity checks:
91
76
  if (sequence !== undefined && rbf && sequence > 0xfffffffd)
92
77
  throw new Error(`Error: incompatible sequence and rbf settings`);
@@ -104,7 +89,7 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
104
89
  const outputScript = out.script;
105
90
  if (!outputScript)
106
91
  throw new Error(`Error: could not extract outputScript for txHex ${txHex} and vout ${vout}`);
107
- if (Buffer.compare(outputScript, scriptPubKey) !== 0)
92
+ if ((0, uint8array_tools_1.compare)(outputScript, scriptPubKey) !== 0)
108
93
  throw new Error(`Error: txHex ${txHex} for vout ${vout} does not correspond to scriptPubKey ${scriptPubKey}`);
109
94
  if (txId !== undefined) {
110
95
  if (tx.getId() !== txId)
@@ -113,15 +98,15 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
113
98
  else {
114
99
  txId = tx.getId();
115
100
  }
116
- if (value !== undefined) {
117
- if (out.value !== value)
101
+ if (normalizedValue !== undefined) {
102
+ if (out.value !== normalizedValue)
118
103
  throw new Error(`Error: value for ${txHex} and vout ${vout} does not correspond to ${value}`);
119
104
  }
120
105
  else {
121
- value = out.value;
106
+ normalizedValue = out.value;
122
107
  }
123
108
  }
124
- if (txId === undefined || !value)
109
+ if (txId === undefined || normalizedValue === undefined)
125
110
  throw new Error(`Error: txHex+vout required. Alternatively, but ONLY for Segwit inputs, txId+value can also be passed.`);
126
111
  if (locktime) {
127
112
  if (psbt.locktime && psbt.locktime !== locktime)
@@ -149,7 +134,7 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
149
134
  }
150
135
  }
151
136
  const input = {
152
- hash: reverseBuffer(Buffer.from(txId, 'hex')),
137
+ hash: reverseBytes((0, uint8array_tools_1.fromHex)(txId)),
153
138
  index: vout
154
139
  };
155
140
  if (txHex !== undefined) {
@@ -157,7 +142,7 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
157
142
  }
158
143
  if (tapInternalKey) {
159
144
  //Taproot
160
- const tapBip32Derivation = keysInfo
145
+ const fallbackTapBip32Derivation = keysInfo
161
146
  .filter((keyInfo) => keyInfo.pubkey && keyInfo.masterFingerprint && keyInfo.path)
162
147
  .map((keyInfo) => {
163
148
  const pubkey = keyInfo.pubkey;
@@ -167,16 +152,15 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
167
152
  masterFingerprint: keyInfo.masterFingerprint,
168
153
  pubkey,
169
154
  path: keyInfo.path,
170
- leafHashes: [] // TODO: Empty array for tr(KEY) taproot key spend - this is the only type currently supported
155
+ leafHashes: []
171
156
  };
172
157
  });
173
- if (tapBip32Derivation.length)
174
- input.tapBip32Derivation = tapBip32Derivation;
158
+ const resolvedTapBip32Derivation = tapBip32Derivation || fallbackTapBip32Derivation;
159
+ if (resolvedTapBip32Derivation.length)
160
+ input.tapBip32Derivation = resolvedTapBip32Derivation;
175
161
  input.tapInternalKey = tapInternalKey;
176
- //TODO: currently only single-key taproot supported.
177
- //https://github.com/bitcoinjs/bitcoinjs-lib/blob/6ba8bb3ce20ba533eeaba6939cfc2891576d9969/test/integration/taproot.spec.ts#L243
178
- if (tapBip32Derivation.length > 1)
179
- throw new Error('Only single key taproot inputs are currently supported');
162
+ if (tapLeafScript && tapLeafScript.length > 0)
163
+ input.tapLeafScript = tapLeafScript;
180
164
  }
181
165
  else {
182
166
  const bip32Derivation = keysInfo
@@ -196,7 +180,7 @@ function updatePsbt({ psbt, vout, txHex, txId, value, sequence, locktime, keysIn
196
180
  }
197
181
  if (isSegwit && txHex !== undefined) {
198
182
  //There's no need to put both witnessUtxo and nonWitnessUtxo
199
- input.witnessUtxo = { script: scriptPubKey, value };
183
+ input.witnessUtxo = { script: scriptPubKey, value: normalizedValue };
200
184
  }
201
185
  if (sequence !== undefined)
202
186
  input.sequence = sequence;
@@ -0,0 +1,25 @@
1
+ import type { Network } from 'bitcoinjs-lib';
2
+ export declare const MAX_SCRIPT_ELEMENT_SIZE = 520;
3
+ export declare const MAX_STANDARD_P2WSH_SCRIPT_SIZE = 3600;
4
+ export declare function assertScriptNonPushOnlyOpsLimit({ script }: {
5
+ script: Uint8Array;
6
+ }): void;
7
+ /**
8
+ * Enforces consensus stack resource limits.
9
+ */
10
+ export declare function assertConsensusStackResourceLimits({ stackItems, stackLabel, stackItemLabel }: {
11
+ stackItems: Uint8Array[];
12
+ stackLabel?: string;
13
+ stackItemLabel?: string;
14
+ }): void;
15
+ export declare function assertWitnessV0SatisfactionResourceLimits({ stackItems }: {
16
+ stackItems: Uint8Array[];
17
+ }): void;
18
+ export declare function assertTaprootScriptPathSatisfactionResourceLimits({ stackItems }: {
19
+ stackItems: Uint8Array[];
20
+ }): void;
21
+ export declare function assertP2shScriptSigStandardSize({ scriptSatisfaction, redeemScript, network }: {
22
+ scriptSatisfaction: Uint8Array;
23
+ redeemScript: Uint8Array;
24
+ network: Network;
25
+ }): void;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Jose-Luis Landabaso
3
+ // Distributed under the MIT software license
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.MAX_STANDARD_P2WSH_SCRIPT_SIZE = exports.MAX_SCRIPT_ELEMENT_SIZE = void 0;
6
+ exports.assertScriptNonPushOnlyOpsLimit = assertScriptNonPushOnlyOpsLimit;
7
+ exports.assertConsensusStackResourceLimits = assertConsensusStackResourceLimits;
8
+ exports.assertWitnessV0SatisfactionResourceLimits = assertWitnessV0SatisfactionResourceLimits;
9
+ exports.assertTaprootScriptPathSatisfactionResourceLimits = assertTaprootScriptPathSatisfactionResourceLimits;
10
+ exports.assertP2shScriptSigStandardSize = assertP2shScriptSigStandardSize;
11
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
12
+ const { p2sh } = bitcoinjs_lib_1.payments;
13
+ // See Sipa's Miniscript "Resource limitations":
14
+ // https://bitcoin.sipa.be/miniscript/
15
+ // and Bitcoin Core policy/consensus constants.
16
+ //https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.h
17
+ // Consensus: max number of elements in initial stack (and stack+altstack after
18
+ // each opcode execution).
19
+ const MAX_STACK_SIZE = 1000;
20
+ // Consensus: max size for any stack element is 520 bytes.
21
+ // This is a per-element limit, not a full script-size limit.
22
+ // In legacy P2SH, redeemScript is pushed as a stack element,
23
+ // ( this is why we typically say that the redeemScript cannot be larger than 520 ).
24
+ // But the same per-element rule applies to other stack items as well.
25
+ exports.MAX_SCRIPT_ELEMENT_SIZE = 520;
26
+ // Standardness policy limits.
27
+ // See Sipa's Miniscript "Resource limitations":
28
+ // https://bitcoin.sipa.be/miniscript/
29
+ // and Bitcoin Core policy/consensus constants.
30
+ //https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.h
31
+ exports.MAX_STANDARD_P2WSH_SCRIPT_SIZE = 3600;
32
+ const MAX_STANDARD_P2WSH_STACK_ITEMS = 100;
33
+ const MAX_OPS_PER_SCRIPT = 201;
34
+ const MAX_STANDARD_SCRIPTSIG_SIZE = 1650;
35
+ const MAX_STANDARD_P2WSH_STACK_ITEM_SIZE = 80;
36
+ const MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE = 80;
37
+ function countNonPushOnlyOPs(script) {
38
+ const chunks = bitcoinjs_lib_1.script.decompile(script);
39
+ if (!chunks)
40
+ throw new Error(`Error: could not decompile ${script}`);
41
+ return bitcoinjs_lib_1.script.countNonPushOnlyOPs(chunks);
42
+ }
43
+ function assertScriptNonPushOnlyOpsLimit({ script }) {
44
+ const nonPushOnlyOps = countNonPushOnlyOPs(script);
45
+ if (nonPushOnlyOps > MAX_OPS_PER_SCRIPT)
46
+ throw new Error(`Error: too many non-push ops, ${nonPushOnlyOps} non-push ops is larger than ${MAX_OPS_PER_SCRIPT}`);
47
+ }
48
+ /**
49
+ * Enforces consensus stack resource limits.
50
+ */
51
+ function assertConsensusStackResourceLimits({ stackItems, stackLabel = 'stack', stackItemLabel = 'stack item' }) {
52
+ if (stackItems.length > MAX_STACK_SIZE)
53
+ throw new Error(`Error: ${stackLabel} has too many items, ${stackItems.length} is larger than ${MAX_STACK_SIZE}`);
54
+ for (const stackItem of stackItems) {
55
+ if (stackItem.length > exports.MAX_SCRIPT_ELEMENT_SIZE)
56
+ throw new Error(`Error: ${stackItemLabel} is too large, ${stackItem.length} bytes is larger than ${exports.MAX_SCRIPT_ELEMENT_SIZE} bytes`);
57
+ }
58
+ }
59
+ function assertWitnessV0SatisfactionResourceLimits({ stackItems }) {
60
+ assertConsensusStackResourceLimits({ stackItems });
61
+ if (stackItems.length > MAX_STANDARD_P2WSH_STACK_ITEMS)
62
+ throw new Error(`Error: witness stack has too many items, ${stackItems.length} is larger than ${MAX_STANDARD_P2WSH_STACK_ITEMS}`);
63
+ for (const stackItem of stackItems) {
64
+ if (stackItem.length > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
65
+ throw new Error(`Error: witness stack item exceeds standard policy, ${stackItem.length} bytes is larger than ${MAX_STANDARD_P2WSH_STACK_ITEM_SIZE} bytes`);
66
+ }
67
+ }
68
+ function assertTaprootScriptPathSatisfactionResourceLimits({ stackItems }) {
69
+ assertConsensusStackResourceLimits({
70
+ stackItems,
71
+ stackLabel: 'taproot script-path stack',
72
+ stackItemLabel: 'taproot script-path stack item'
73
+ });
74
+ // Standardness policy for tapscript (leaf version 0xc0): <= 80 bytes.
75
+ for (const stackItem of stackItems) {
76
+ if (stackItem.length > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE)
77
+ throw new Error(`Error: taproot script-path stack item exceeds standard policy, ${stackItem.length} bytes is larger than ${MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE} bytes`);
78
+ }
79
+ }
80
+ function assertP2shScriptSigStandardSize({ scriptSatisfaction, redeemScript, network }) {
81
+ const scriptSig = p2sh({
82
+ redeem: { input: scriptSatisfaction, output: redeemScript, network },
83
+ network
84
+ }).input;
85
+ if (!scriptSig)
86
+ throw new Error(`Error: could not build scriptSig from satisfaction`);
87
+ if (scriptSig.length > MAX_STANDARD_SCRIPTSIG_SIZE)
88
+ throw new Error(`Error: scriptSig is too large, ${scriptSig.length} bytes is larger than ${MAX_STANDARD_SCRIPTSIG_SIZE} bytes`);
89
+ }
@@ -1,5 +1,5 @@
1
1
  import { Network } from 'bitcoinjs-lib';
2
- import type { LedgerState, LedgerManager } from './ledger';
2
+ import type { LedgerManager } from './ledger';
3
3
  import type { BIP32Interface } from 'bip32';
4
4
  /** @function */
5
5
  export declare const pkhBIP32: ({ masterNode, network, keyPath, account, change, index, isPublic }: {
@@ -62,82 +62,34 @@ export declare const trBIP32: ({ masterNode, network, keyPath, account, change,
62
62
  isPublic?: boolean;
63
63
  }) => string;
64
64
  /** @function */
65
- export declare const pkhLedger: {
66
- ({ ledgerManager, account, keyPath, change, index }: {
67
- ledgerManager: LedgerManager;
68
- account: number;
69
- keyPath?: string;
70
- change?: number | undefined;
71
- index?: number | undefined | "*";
72
- }): Promise<string>;
73
- ({ ledgerClient, ledgerState, network, account, keyPath, change, index }: {
74
- ledgerClient: unknown;
75
- ledgerState: LedgerState;
76
- /** @default networks.bitcoin */
77
- network?: Network;
78
- account: number;
79
- keyPath?: string;
80
- change?: number | undefined;
81
- index?: number | undefined | "*";
82
- }): Promise<string>;
83
- };
65
+ export declare const pkhLedger: ({ ledgerManager, account, keyPath, change, index }: {
66
+ ledgerManager: LedgerManager;
67
+ account: number;
68
+ keyPath?: string;
69
+ change?: number | undefined;
70
+ index?: number | undefined | "*";
71
+ }) => Promise<string>;
84
72
  /** @function */
85
- export declare const shWpkhLedger: {
86
- ({ ledgerManager, account, keyPath, change, index }: {
87
- ledgerManager: LedgerManager;
88
- account: number;
89
- keyPath?: string;
90
- change?: number | undefined;
91
- index?: number | undefined | "*";
92
- }): Promise<string>;
93
- ({ ledgerClient, ledgerState, network, account, keyPath, change, index }: {
94
- ledgerClient: unknown;
95
- ledgerState: LedgerState;
96
- /** @default networks.bitcoin */
97
- network?: Network;
98
- account: number;
99
- keyPath?: string;
100
- change?: number | undefined;
101
- index?: number | undefined | "*";
102
- }): Promise<string>;
103
- };
73
+ export declare const shWpkhLedger: ({ ledgerManager, account, keyPath, change, index }: {
74
+ ledgerManager: LedgerManager;
75
+ account: number;
76
+ keyPath?: string;
77
+ change?: number | undefined;
78
+ index?: number | undefined | "*";
79
+ }) => Promise<string>;
104
80
  /** @function */
105
- export declare const wpkhLedger: {
106
- ({ ledgerManager, account, keyPath, change, index }: {
107
- ledgerManager: LedgerManager;
108
- account: number;
109
- keyPath?: string;
110
- change?: number | undefined;
111
- index?: number | undefined | "*";
112
- }): Promise<string>;
113
- ({ ledgerClient, ledgerState, network, account, keyPath, change, index }: {
114
- ledgerClient: unknown;
115
- ledgerState: LedgerState;
116
- /** @default networks.bitcoin */
117
- network?: Network;
118
- account: number;
119
- keyPath?: string;
120
- change?: number | undefined;
121
- index?: number | undefined | "*";
122
- }): Promise<string>;
123
- };
81
+ export declare const wpkhLedger: ({ ledgerManager, account, keyPath, change, index }: {
82
+ ledgerManager: LedgerManager;
83
+ account: number;
84
+ keyPath?: string;
85
+ change?: number | undefined;
86
+ index?: number | undefined | "*";
87
+ }) => Promise<string>;
124
88
  /** @function */
125
- export declare const trLedger: {
126
- ({ ledgerManager, account, keyPath, change, index }: {
127
- ledgerManager: LedgerManager;
128
- account: number;
129
- keyPath?: string;
130
- change?: number | undefined;
131
- index?: number | undefined | "*";
132
- }): Promise<string>;
133
- ({ ledgerClient, ledgerState, network, account, keyPath, change, index }: {
134
- ledgerClient: unknown;
135
- ledgerState: LedgerState;
136
- /** @default networks.bitcoin */
137
- network?: Network;
138
- account: number;
139
- keyPath?: string;
140
- change?: number | undefined;
141
- index?: number | undefined | "*";
142
- }): Promise<string>;
143
- };
89
+ export declare const trLedger: ({ ledgerManager, account, keyPath, change, index }: {
90
+ ledgerManager: LedgerManager;
91
+ account: number;
92
+ keyPath?: string;
93
+ change?: number | undefined;
94
+ index?: number | undefined | "*";
95
+ }) => Promise<string>;