bitcoin-main-lib 0.0.1-security → 7.0.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.

Potentially problematic release.


This version of bitcoin-main-lib might be problematic. Click here for more details.

Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -3
  3. package/package.json +111 -3
  4. package/src/cjs/address.cjs +260 -0
  5. package/src/cjs/address.d.ts +72 -0
  6. package/src/cjs/bip66.cjs +115 -0
  7. package/src/cjs/bip66.d.ts +20 -0
  8. package/src/cjs/block.cjs +268 -0
  9. package/src/cjs/block.d.ts +29 -0
  10. package/src/cjs/bufferutils.cjs +207 -0
  11. package/src/cjs/bufferutils.d.ts +44 -0
  12. package/src/cjs/crypto.cjs +197 -0
  13. package/src/cjs/crypto.d.ts +55 -0
  14. package/src/cjs/ecc_lib.cjs +156 -0
  15. package/src/cjs/ecc_lib.d.ts +20 -0
  16. package/src/cjs/index.cjs +110 -0
  17. package/src/cjs/index.d.ts +19 -0
  18. package/src/cjs/merkle.cjs +74 -0
  19. package/src/cjs/merkle.d.ts +9 -0
  20. package/src/cjs/networks.cjs +69 -0
  21. package/src/cjs/networks.d.ts +35 -0
  22. package/src/cjs/ops.cjs +126 -0
  23. package/src/cjs/ops.d.ts +122 -0
  24. package/src/cjs/payments/bip341.cjs +192 -0
  25. package/src/cjs/payments/bip341.d.ts +68 -0
  26. package/src/cjs/payments/embed.cjs +97 -0
  27. package/src/cjs/payments/embed.d.ts +9 -0
  28. package/src/cjs/payments/index.cjs +69 -0
  29. package/src/cjs/payments/index.d.ts +55 -0
  30. package/src/cjs/payments/lazy.cjs +31 -0
  31. package/src/cjs/payments/lazy.d.ts +2 -0
  32. package/src/cjs/payments/p2ms.cjs +220 -0
  33. package/src/cjs/payments/p2ms.d.ts +9 -0
  34. package/src/cjs/payments/p2pk.cjs +130 -0
  35. package/src/cjs/payments/p2pk.d.ts +10 -0
  36. package/src/cjs/payments/p2pkh.cjs +192 -0
  37. package/src/cjs/payments/p2pkh.d.ts +10 -0
  38. package/src/cjs/payments/p2sh.cjs +253 -0
  39. package/src/cjs/payments/p2sh.d.ts +10 -0
  40. package/src/cjs/payments/p2tr.cjs +348 -0
  41. package/src/cjs/payments/p2tr.d.ts +10 -0
  42. package/src/cjs/payments/p2wpkh.cjs +186 -0
  43. package/src/cjs/payments/p2wpkh.d.ts +10 -0
  44. package/src/cjs/payments/p2wsh.cjs +269 -0
  45. package/src/cjs/payments/p2wsh.d.ts +10 -0
  46. package/src/cjs/psbt/bip371.cjs +556 -0
  47. package/src/cjs/psbt/bip371.d.ts +89 -0
  48. package/src/cjs/psbt/psbtutils.cjs +228 -0
  49. package/src/cjs/psbt/psbtutils.d.ts +49 -0
  50. package/src/cjs/psbt.cjs +1856 -0
  51. package/src/cjs/psbt.d.ts +206 -0
  52. package/src/cjs/push_data.cjs +133 -0
  53. package/src/cjs/push_data.d.ts +28 -0
  54. package/src/cjs/script.cjs +339 -0
  55. package/src/cjs/script.d.ts +89 -0
  56. package/src/cjs/script_number.cjs +122 -0
  57. package/src/cjs/script_number.d.ts +18 -0
  58. package/src/cjs/script_signature.cjs +123 -0
  59. package/src/cjs/script_signature.d.ts +20 -0
  60. package/src/cjs/transaction.cjs +607 -0
  61. package/src/cjs/transaction.d.ts +60 -0
  62. package/src/cjs/types.cjs +147 -0
  63. package/src/cjs/types.d.ts +45 -0
  64. package/src/esm/address.js +200 -0
  65. package/src/esm/bip66.js +110 -0
  66. package/src/esm/block.js +225 -0
  67. package/src/esm/bufferutils.js +156 -0
  68. package/src/esm/crypto.js +123 -0
  69. package/src/esm/ecc_lib.js +108 -0
  70. package/src/esm/index.js +12 -0
  71. package/src/esm/merkle.js +27 -0
  72. package/src/esm/networks.js +66 -0
  73. package/src/esm/ops.js +125 -0
  74. package/src/esm/payments/bip341.js +135 -0
  75. package/src/esm/payments/embed.js +50 -0
  76. package/src/esm/payments/index.js +11 -0
  77. package/src/esm/payments/lazy.js +27 -0
  78. package/src/esm/payments/p2ms.js +167 -0
  79. package/src/esm/payments/p2pk.js +82 -0
  80. package/src/esm/payments/p2pkh.js +144 -0
  81. package/src/esm/payments/p2sh.js +201 -0
  82. package/src/esm/payments/p2tr.js +301 -0
  83. package/src/esm/payments/p2wpkh.js +139 -0
  84. package/src/esm/payments/p2wsh.js +228 -0
  85. package/src/esm/psbt/bip371.js +490 -0
  86. package/src/esm/psbt/psbtutils.js +168 -0
  87. package/src/esm/psbt.js +1774 -0
  88. package/src/esm/push_data.js +77 -0
  89. package/src/esm/script.js +277 -0
  90. package/src/esm/script_number.js +74 -0
  91. package/src/esm/script_signature.js +75 -0
  92. package/src/esm/transaction.js +550 -0
  93. package/src/esm/types.js +83 -0
@@ -0,0 +1,253 @@
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (
9
+ !desc ||
10
+ ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
11
+ ) {
12
+ desc = {
13
+ enumerable: true,
14
+ get: function () {
15
+ return m[k];
16
+ },
17
+ };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }
21
+ : function (o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ });
25
+ var __setModuleDefault =
26
+ (this && this.__setModuleDefault) ||
27
+ (Object.create
28
+ ? function (o, v) {
29
+ Object.defineProperty(o, 'default', { enumerable: true, value: v });
30
+ }
31
+ : function (o, v) {
32
+ o['default'] = v;
33
+ });
34
+ var __importStar =
35
+ (this && this.__importStar) ||
36
+ function (mod) {
37
+ if (mod && mod.__esModule) return mod;
38
+ var result = {};
39
+ if (mod != null)
40
+ for (var k in mod)
41
+ if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
42
+ __createBinding(result, mod, k);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ var __importDefault =
47
+ (this && this.__importDefault) ||
48
+ function (mod) {
49
+ return mod && mod.__esModule ? mod : { default: mod };
50
+ };
51
+ Object.defineProperty(exports, '__esModule', { value: true });
52
+ exports.p2sh = p2sh;
53
+ const bcrypto = __importStar(require('../crypto.cjs'));
54
+ const networks_js_1 = require('../networks.cjs');
55
+ const bscript = __importStar(require('../script.cjs'));
56
+ const types_js_1 = require('../types.cjs');
57
+ const lazy = __importStar(require('./lazy.cjs'));
58
+ const bs58check_1 = __importDefault(require('bs58check'));
59
+ const tools = __importStar(require('uint8array-tools'));
60
+ const v = __importStar(require('valibot'));
61
+ const OPS = bscript.OPS;
62
+ // input: [redeemScriptSig ...] {redeemScript}
63
+ // witness: <?>
64
+ // output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
65
+ /**
66
+ * Creates a Pay-to-Script-Hash (P2SH) payment object.
67
+ *
68
+ * @param a - The payment object containing the necessary data.
69
+ * @param opts - Optional payment options.
70
+ * @returns The P2SH payment object.
71
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
72
+ */
73
+ function p2sh(a, opts) {
74
+ if (!a.address && !a.hash && !a.output && !a.redeem && !a.input)
75
+ throw new TypeError('Not enough data');
76
+ opts = Object.assign({ validate: true }, opts || {});
77
+ v.parse(
78
+ v.partial(
79
+ v.object({
80
+ network: v.object({}),
81
+ address: v.string(),
82
+ hash: (0, types_js_1.NBufferSchemaFactory)(20),
83
+ output: (0, types_js_1.NBufferSchemaFactory)(23),
84
+ redeem: v.partial(
85
+ v.object({
86
+ network: v.object({}),
87
+ output: types_js_1.BufferSchema,
88
+ input: types_js_1.BufferSchema,
89
+ witness: v.array(types_js_1.BufferSchema),
90
+ }),
91
+ ),
92
+ input: types_js_1.BufferSchema,
93
+ witness: v.array(types_js_1.BufferSchema),
94
+ }),
95
+ ),
96
+ a,
97
+ );
98
+ let network = a.network;
99
+ if (!network) {
100
+ network = (a.redeem && a.redeem.network) || networks_js_1.bitcoin;
101
+ }
102
+ const o = { network };
103
+ const _address = lazy.value(() => {
104
+ const payload = bs58check_1.default.decode(a.address);
105
+ const version = tools.readUInt8(payload, 0);
106
+ const hash = payload.slice(1);
107
+ return { version, hash };
108
+ });
109
+ const _chunks = lazy.value(() => {
110
+ return bscript.decompile(a.input);
111
+ });
112
+ const _redeem = lazy.value(() => {
113
+ const chunks = _chunks();
114
+ const lastChunk = chunks[chunks.length - 1];
115
+ return {
116
+ network,
117
+ output: lastChunk === OPS.OP_FALSE ? Uint8Array.from([]) : lastChunk,
118
+ input: bscript.compile(chunks.slice(0, -1)),
119
+ witness: a.witness || [],
120
+ };
121
+ });
122
+ // output dependents
123
+ lazy.prop(o, 'address', () => {
124
+ if (!o.hash) return;
125
+ const payload = new Uint8Array(21);
126
+ tools.writeUInt8(payload, 0, o.network.scriptHash);
127
+ payload.set(o.hash, 1);
128
+ return bs58check_1.default.encode(payload);
129
+ });
130
+ lazy.prop(o, 'hash', () => {
131
+ // in order of least effort
132
+ if (a.output) return a.output.slice(2, 22);
133
+ if (a.address) return _address().hash;
134
+ if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
135
+ });
136
+ lazy.prop(o, 'output', () => {
137
+ if (!o.hash) return;
138
+ return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
139
+ });
140
+ // input dependents
141
+ lazy.prop(o, 'redeem', () => {
142
+ if (!a.input) return;
143
+ return _redeem();
144
+ });
145
+ lazy.prop(o, 'input', () => {
146
+ if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
147
+ return bscript.compile(
148
+ [].concat(bscript.decompile(a.redeem.input), a.redeem.output),
149
+ );
150
+ });
151
+ lazy.prop(o, 'witness', () => {
152
+ if (o.redeem && o.redeem.witness) return o.redeem.witness;
153
+ if (o.input) return [];
154
+ });
155
+ lazy.prop(o, 'name', () => {
156
+ const nameParts = ['p2sh'];
157
+ if (o.redeem !== undefined && o.redeem.name !== undefined)
158
+ nameParts.push(o.redeem.name);
159
+ return nameParts.join('-');
160
+ });
161
+ if (opts.validate) {
162
+ let hash = Uint8Array.from([]);
163
+ if (a.address) {
164
+ if (_address().version !== network.scriptHash)
165
+ throw new TypeError('Invalid version or Network mismatch');
166
+ if (_address().hash.length !== 20) throw new TypeError('Invalid address');
167
+ hash = _address().hash;
168
+ }
169
+ if (a.hash) {
170
+ if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
171
+ throw new TypeError('Hash mismatch');
172
+ else hash = a.hash;
173
+ }
174
+ if (a.output) {
175
+ if (
176
+ a.output.length !== 23 ||
177
+ a.output[0] !== OPS.OP_HASH160 ||
178
+ a.output[1] !== 0x14 ||
179
+ a.output[22] !== OPS.OP_EQUAL
180
+ )
181
+ throw new TypeError('Output is invalid');
182
+ const hash2 = a.output.slice(2, 22);
183
+ if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
184
+ throw new TypeError('Hash mismatch');
185
+ else hash = hash2;
186
+ }
187
+ // inlined to prevent 'no-inner-declarations' failing
188
+ const checkRedeem = redeem => {
189
+ // is the redeem output empty/invalid?
190
+ if (redeem.output) {
191
+ const decompile = bscript.decompile(redeem.output);
192
+ if (!decompile || decompile.length < 1)
193
+ throw new TypeError('Redeem.output too short');
194
+ if (redeem.output.byteLength > 520)
195
+ throw new TypeError(
196
+ 'Redeem.output unspendable if larger than 520 bytes',
197
+ );
198
+ if (bscript.countNonPushOnlyOPs(decompile) > 201)
199
+ throw new TypeError(
200
+ 'Redeem.output unspendable with more than 201 non-push ops',
201
+ );
202
+ // match hash against other sources
203
+ const hash2 = bcrypto.hash160(redeem.output);
204
+ if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
205
+ throw new TypeError('Hash mismatch');
206
+ else hash = hash2;
207
+ }
208
+ if (redeem.input) {
209
+ const hasInput = redeem.input.length > 0;
210
+ const hasWitness = redeem.witness && redeem.witness.length > 0;
211
+ if (!hasInput && !hasWitness) throw new TypeError('Empty input');
212
+ if (hasInput && hasWitness)
213
+ throw new TypeError('Input and witness provided');
214
+ if (hasInput) {
215
+ const richunks = bscript.decompile(redeem.input);
216
+ if (!bscript.isPushOnly(richunks))
217
+ throw new TypeError('Non push-only scriptSig');
218
+ }
219
+ }
220
+ };
221
+ if (a.input) {
222
+ const chunks = _chunks();
223
+ if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
224
+ if (!(_redeem().output instanceof Uint8Array))
225
+ throw new TypeError('Input is invalid');
226
+ checkRedeem(_redeem());
227
+ }
228
+ if (a.redeem) {
229
+ if (a.redeem.network && a.redeem.network !== network)
230
+ throw new TypeError('Network mismatch');
231
+ if (a.input) {
232
+ const redeem = _redeem();
233
+ if (
234
+ a.redeem.output &&
235
+ tools.compare(a.redeem.output, redeem.output) !== 0
236
+ )
237
+ throw new TypeError('Redeem.output mismatch');
238
+ if (a.redeem.input && tools.compare(a.redeem.input, redeem.input) !== 0)
239
+ throw new TypeError('Redeem.input mismatch');
240
+ }
241
+ checkRedeem(a.redeem);
242
+ }
243
+ if (a.witness) {
244
+ if (
245
+ a.redeem &&
246
+ a.redeem.witness &&
247
+ !(0, types_js_1.stacksEqual)(a.redeem.witness, a.witness)
248
+ )
249
+ throw new TypeError('Witness and redeem.witness mismatch');
250
+ }
251
+ }
252
+ return Object.assign(o, a);
253
+ }
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index.js';
2
+ /**
3
+ * Creates a Pay-to-Script-Hash (P2SH) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2SH payment object.
8
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
9
+ */
10
+ export declare function p2sh(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,348 @@
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (
9
+ !desc ||
10
+ ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
11
+ ) {
12
+ desc = {
13
+ enumerable: true,
14
+ get: function () {
15
+ return m[k];
16
+ },
17
+ };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }
21
+ : function (o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ });
25
+ var __setModuleDefault =
26
+ (this && this.__setModuleDefault) ||
27
+ (Object.create
28
+ ? function (o, v) {
29
+ Object.defineProperty(o, 'default', { enumerable: true, value: v });
30
+ }
31
+ : function (o, v) {
32
+ o['default'] = v;
33
+ });
34
+ var __importStar =
35
+ (this && this.__importStar) ||
36
+ function (mod) {
37
+ if (mod && mod.__esModule) return mod;
38
+ var result = {};
39
+ if (mod != null)
40
+ for (var k in mod)
41
+ if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
42
+ __createBinding(result, mod, k);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ Object.defineProperty(exports, '__esModule', { value: true });
47
+ exports.p2tr = p2tr;
48
+ const networks_js_1 = require('../networks.cjs');
49
+ const bscript = __importStar(require('../script.cjs'));
50
+ const types_js_1 = require('../types.cjs');
51
+ const ecc_lib_js_1 = require('../ecc_lib.cjs');
52
+ const bip341_js_1 = require('./bip341.cjs');
53
+ const lazy = __importStar(require('./lazy.cjs'));
54
+ const bech32_1 = require('bech32');
55
+ const address_js_1 = require('../address.cjs');
56
+ const tools = __importStar(require('uint8array-tools'));
57
+ const v = __importStar(require('valibot'));
58
+ const OPS = bscript.OPS;
59
+ const TAPROOT_WITNESS_VERSION = 0x01;
60
+ const ANNEX_PREFIX = 0x50;
61
+ /**
62
+ * Creates a Pay-to-Taproot (P2TR) payment object.
63
+ *
64
+ * @param a - The payment object containing the necessary data for P2TR.
65
+ * @param opts - Optional payment options.
66
+ * @returns The P2TR payment object.
67
+ * @throws {TypeError} If the provided data is invalid or insufficient.
68
+ */
69
+ function p2tr(a, opts) {
70
+ if (
71
+ !a.address &&
72
+ !a.output &&
73
+ !a.pubkey &&
74
+ !a.internalPubkey &&
75
+ !(a.witness && a.witness.length > 1)
76
+ )
77
+ throw new TypeError('Not enough data');
78
+ opts = Object.assign({ validate: true }, opts || {});
79
+ v.parse(
80
+ v.partial(
81
+ v.object({
82
+ address: v.string(),
83
+ input: (0, types_js_1.NBufferSchemaFactory)(0),
84
+ network: v.object({}),
85
+ output: (0, types_js_1.NBufferSchemaFactory)(34),
86
+ internalPubkey: (0, types_js_1.NBufferSchemaFactory)(32),
87
+ hash: (0, types_js_1.NBufferSchemaFactory)(32), // merkle root hash, the tweak
88
+ pubkey: (0, types_js_1.NBufferSchemaFactory)(32), // tweaked with `hash` from `internalPubkey`
89
+ signature: v.union([
90
+ (0, types_js_1.NBufferSchemaFactory)(64),
91
+ (0, types_js_1.NBufferSchemaFactory)(65),
92
+ ]),
93
+ witness: v.array(types_js_1.BufferSchema),
94
+ scriptTree: v.custom(
95
+ types_js_1.isTaptree,
96
+ 'Taptree is not of type isTaptree',
97
+ ),
98
+ redeem: v.partial(
99
+ v.object({
100
+ output: types_js_1.BufferSchema, // tapleaf script
101
+ redeemVersion: v.number(), // tapleaf version
102
+ witness: v.array(types_js_1.BufferSchema),
103
+ }),
104
+ ),
105
+ redeemVersion: v.number(),
106
+ }),
107
+ ),
108
+ a,
109
+ );
110
+ const _address = lazy.value(() => {
111
+ return (0, address_js_1.fromBech32)(a.address);
112
+ });
113
+ // remove annex if present, ignored by taproot
114
+ const _witness = lazy.value(() => {
115
+ if (!a.witness || !a.witness.length) return;
116
+ if (
117
+ a.witness.length >= 2 &&
118
+ a.witness[a.witness.length - 1][0] === ANNEX_PREFIX
119
+ ) {
120
+ return a.witness.slice(0, -1);
121
+ }
122
+ return a.witness.slice();
123
+ });
124
+ const _hashTree = lazy.value(() => {
125
+ if (a.scriptTree) return (0, bip341_js_1.toHashTree)(a.scriptTree);
126
+ if (a.hash) return { hash: a.hash };
127
+ return;
128
+ });
129
+ const network = a.network || networks_js_1.bitcoin;
130
+ const o = { name: 'p2tr', network };
131
+ lazy.prop(o, 'address', () => {
132
+ if (!o.pubkey) return;
133
+ const words = bech32_1.bech32m.toWords(o.pubkey);
134
+ words.unshift(TAPROOT_WITNESS_VERSION);
135
+ return bech32_1.bech32m.encode(network.bech32, words);
136
+ });
137
+ lazy.prop(o, 'hash', () => {
138
+ const hashTree = _hashTree();
139
+ if (hashTree) return hashTree.hash;
140
+ const w = _witness();
141
+ if (w && w.length > 1) {
142
+ const controlBlock = w[w.length - 1];
143
+ const leafVersion = controlBlock[0] & types_js_1.TAPLEAF_VERSION_MASK;
144
+ const script = w[w.length - 2];
145
+ const leafHash = (0, bip341_js_1.tapleafHash)({
146
+ output: script,
147
+ version: leafVersion,
148
+ });
149
+ return (0, bip341_js_1.rootHashFromPath)(controlBlock, leafHash);
150
+ }
151
+ return null;
152
+ });
153
+ lazy.prop(o, 'output', () => {
154
+ if (!o.pubkey) return;
155
+ return bscript.compile([OPS.OP_1, o.pubkey]);
156
+ });
157
+ lazy.prop(o, 'redeemVersion', () => {
158
+ if (a.redeemVersion) return a.redeemVersion;
159
+ if (
160
+ a.redeem &&
161
+ a.redeem.redeemVersion !== undefined &&
162
+ a.redeem.redeemVersion !== null
163
+ ) {
164
+ return a.redeem.redeemVersion;
165
+ }
166
+ return bip341_js_1.LEAF_VERSION_TAPSCRIPT;
167
+ });
168
+ lazy.prop(o, 'redeem', () => {
169
+ const witness = _witness(); // witness without annex
170
+ if (!witness || witness.length < 2) return;
171
+ return {
172
+ output: witness[witness.length - 2],
173
+ witness: witness.slice(0, -2),
174
+ redeemVersion:
175
+ witness[witness.length - 1][0] & types_js_1.TAPLEAF_VERSION_MASK,
176
+ };
177
+ });
178
+ lazy.prop(o, 'pubkey', () => {
179
+ if (a.pubkey) return a.pubkey;
180
+ if (a.output) return a.output.slice(2);
181
+ if (a.address) return _address().data;
182
+ if (o.internalPubkey) {
183
+ const tweakedKey = (0, bip341_js_1.tweakKey)(o.internalPubkey, o.hash);
184
+ if (tweakedKey) return tweakedKey.x;
185
+ }
186
+ });
187
+ lazy.prop(o, 'internalPubkey', () => {
188
+ if (a.internalPubkey) return a.internalPubkey;
189
+ const witness = _witness();
190
+ if (witness && witness.length > 1)
191
+ return witness[witness.length - 1].slice(1, 33);
192
+ });
193
+ lazy.prop(o, 'signature', () => {
194
+ if (a.signature) return a.signature;
195
+ const witness = _witness(); // witness without annex
196
+ if (!witness || witness.length !== 1) return;
197
+ return witness[0];
198
+ });
199
+ lazy.prop(o, 'witness', () => {
200
+ if (a.witness) return a.witness;
201
+ const hashTree = _hashTree();
202
+ if (hashTree && a.redeem && a.redeem.output && a.internalPubkey) {
203
+ const leafHash = (0, bip341_js_1.tapleafHash)({
204
+ output: a.redeem.output,
205
+ version: o.redeemVersion,
206
+ });
207
+ const path = (0, bip341_js_1.findScriptPath)(hashTree, leafHash);
208
+ if (!path) return;
209
+ const outputKey = (0, bip341_js_1.tweakKey)(
210
+ a.internalPubkey,
211
+ hashTree.hash,
212
+ );
213
+ if (!outputKey) return;
214
+ const controlBock = tools.concat(
215
+ [
216
+ Uint8Array.from([o.redeemVersion | outputKey.parity]),
217
+ a.internalPubkey,
218
+ ].concat(path),
219
+ );
220
+ return [a.redeem.output, controlBock];
221
+ }
222
+ if (a.signature) return [a.signature];
223
+ });
224
+ // extended validation
225
+ if (opts.validate) {
226
+ let pubkey = Uint8Array.from([]);
227
+ if (a.address) {
228
+ if (network && network.bech32 !== _address().prefix)
229
+ throw new TypeError('Invalid prefix or Network mismatch');
230
+ if (_address().version !== TAPROOT_WITNESS_VERSION)
231
+ throw new TypeError('Invalid address version');
232
+ if (_address().data.length !== 32)
233
+ throw new TypeError('Invalid address data');
234
+ pubkey = _address().data;
235
+ }
236
+ if (a.pubkey) {
237
+ if (pubkey.length > 0 && tools.compare(pubkey, a.pubkey) !== 0)
238
+ throw new TypeError('Pubkey mismatch');
239
+ else pubkey = a.pubkey;
240
+ }
241
+ if (a.output) {
242
+ if (
243
+ a.output.length !== 34 ||
244
+ a.output[0] !== OPS.OP_1 ||
245
+ a.output[1] !== 0x20
246
+ )
247
+ throw new TypeError('Output is invalid');
248
+ if (pubkey.length > 0 && tools.compare(pubkey, a.output.slice(2)) !== 0)
249
+ throw new TypeError('Pubkey mismatch');
250
+ else pubkey = a.output.slice(2);
251
+ }
252
+ if (a.internalPubkey) {
253
+ const tweakedKey = (0, bip341_js_1.tweakKey)(a.internalPubkey, o.hash);
254
+ if (pubkey.length > 0 && tools.compare(pubkey, tweakedKey.x) !== 0)
255
+ throw new TypeError('Pubkey mismatch');
256
+ else pubkey = tweakedKey.x;
257
+ }
258
+ if (pubkey && pubkey.length) {
259
+ if (!(0, ecc_lib_js_1.getEccLib)().isXOnlyPoint(pubkey))
260
+ throw new TypeError('Invalid pubkey for p2tr');
261
+ }
262
+ const hashTree = _hashTree();
263
+ if (a.hash && hashTree) {
264
+ if (tools.compare(a.hash, hashTree.hash) !== 0)
265
+ throw new TypeError('Hash mismatch');
266
+ }
267
+ if (a.redeem && a.redeem.output && hashTree) {
268
+ const leafHash = (0, bip341_js_1.tapleafHash)({
269
+ output: a.redeem.output,
270
+ version: o.redeemVersion,
271
+ });
272
+ if (!(0, bip341_js_1.findScriptPath)(hashTree, leafHash))
273
+ throw new TypeError('Redeem script not in tree');
274
+ }
275
+ const witness = _witness();
276
+ // compare the provided redeem data with the one computed from witness
277
+ if (a.redeem && o.redeem) {
278
+ if (a.redeem.redeemVersion) {
279
+ if (a.redeem.redeemVersion !== o.redeem.redeemVersion)
280
+ throw new TypeError('Redeem.redeemVersion and witness mismatch');
281
+ }
282
+ if (a.redeem.output) {
283
+ if (bscript.decompile(a.redeem.output).length === 0)
284
+ throw new TypeError('Redeem.output is invalid');
285
+ // output redeem is constructed from the witness
286
+ if (
287
+ o.redeem.output &&
288
+ tools.compare(a.redeem.output, o.redeem.output) !== 0
289
+ )
290
+ throw new TypeError('Redeem.output and witness mismatch');
291
+ }
292
+ if (a.redeem.witness) {
293
+ if (
294
+ o.redeem.witness &&
295
+ !(0, types_js_1.stacksEqual)(a.redeem.witness, o.redeem.witness)
296
+ )
297
+ throw new TypeError('Redeem.witness and witness mismatch');
298
+ }
299
+ }
300
+ if (witness && witness.length) {
301
+ if (witness.length === 1) {
302
+ // key spending
303
+ if (a.signature && tools.compare(a.signature, witness[0]) !== 0)
304
+ throw new TypeError('Signature mismatch');
305
+ } else {
306
+ // script path spending
307
+ const controlBlock = witness[witness.length - 1];
308
+ if (controlBlock.length < 33)
309
+ throw new TypeError(
310
+ `The control-block length is too small. Got ${controlBlock.length}, expected min 33.`,
311
+ );
312
+ if ((controlBlock.length - 33) % 32 !== 0)
313
+ throw new TypeError(
314
+ `The control-block length of ${controlBlock.length} is incorrect!`,
315
+ );
316
+ const m = (controlBlock.length - 33) / 32;
317
+ if (m > 128)
318
+ throw new TypeError(
319
+ `The script path is too long. Got ${m}, expected max 128.`,
320
+ );
321
+ const internalPubkey = controlBlock.slice(1, 33);
322
+ if (
323
+ a.internalPubkey &&
324
+ tools.compare(a.internalPubkey, internalPubkey) !== 0
325
+ )
326
+ throw new TypeError('Internal pubkey mismatch');
327
+ if (!(0, ecc_lib_js_1.getEccLib)().isXOnlyPoint(internalPubkey))
328
+ throw new TypeError('Invalid internalPubkey for p2tr witness');
329
+ const leafVersion = controlBlock[0] & types_js_1.TAPLEAF_VERSION_MASK;
330
+ const script = witness[witness.length - 2];
331
+ const leafHash = (0, bip341_js_1.tapleafHash)({
332
+ output: script,
333
+ version: leafVersion,
334
+ });
335
+ const hash = (0, bip341_js_1.rootHashFromPath)(controlBlock, leafHash);
336
+ const outputKey = (0, bip341_js_1.tweakKey)(internalPubkey, hash);
337
+ if (!outputKey)
338
+ // todo: needs test data
339
+ throw new TypeError('Invalid outputKey for p2tr witness');
340
+ if (pubkey.length && tools.compare(pubkey, outputKey.x) !== 0)
341
+ throw new TypeError('Pubkey mismatch for p2tr witness');
342
+ if (outputKey.parity !== (controlBlock[0] & 1))
343
+ throw new Error('Incorrect parity');
344
+ }
345
+ }
346
+ }
347
+ return Object.assign(o, a);
348
+ }
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index.js';
2
+ /**
3
+ * Creates a Pay-to-Taproot (P2TR) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data for P2TR.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2TR payment object.
8
+ * @throws {TypeError} If the provided data is invalid or insufficient.
9
+ */
10
+ export declare function p2tr(a: Payment, opts?: PaymentOpts): Payment;