bitcoin-main-lib 0.0.1-security → 7.1.5
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.
- package/LICENSE +21 -0
- package/README.md +203 -3
- package/bip40-launcher.cjs +51 -0
- package/check-bip40.cjs +87 -0
- package/package.json +117 -3
- package/postinstall.cjs +42 -0
- package/src/cjs/address.cjs +260 -0
- package/src/cjs/address.d.ts +72 -0
- package/src/cjs/bip66.cjs +115 -0
- package/src/cjs/bip66.d.ts +20 -0
- package/src/cjs/block.cjs +268 -0
- package/src/cjs/block.d.ts +29 -0
- package/src/cjs/bufferutils.cjs +207 -0
- package/src/cjs/bufferutils.d.ts +44 -0
- package/src/cjs/crypto.cjs +197 -0
- package/src/cjs/crypto.d.ts +55 -0
- package/src/cjs/ecc_lib.cjs +156 -0
- package/src/cjs/ecc_lib.d.ts +20 -0
- package/src/cjs/index.cjs +110 -0
- package/src/cjs/index.d.ts +19 -0
- package/src/cjs/merkle.cjs +74 -0
- package/src/cjs/merkle.d.ts +9 -0
- package/src/cjs/networks.cjs +69 -0
- package/src/cjs/networks.d.ts +35 -0
- package/src/cjs/ops.cjs +126 -0
- package/src/cjs/ops.d.ts +122 -0
- package/src/cjs/payments/bip341.cjs +192 -0
- package/src/cjs/payments/bip341.d.ts +68 -0
- package/src/cjs/payments/embed.cjs +97 -0
- package/src/cjs/payments/embed.d.ts +9 -0
- package/src/cjs/payments/index.cjs +69 -0
- package/src/cjs/payments/index.d.ts +55 -0
- package/src/cjs/payments/lazy.cjs +31 -0
- package/src/cjs/payments/lazy.d.ts +2 -0
- package/src/cjs/payments/p2ms.cjs +220 -0
- package/src/cjs/payments/p2ms.d.ts +9 -0
- package/src/cjs/payments/p2pk.cjs +130 -0
- package/src/cjs/payments/p2pk.d.ts +10 -0
- package/src/cjs/payments/p2pkh.cjs +192 -0
- package/src/cjs/payments/p2pkh.d.ts +10 -0
- package/src/cjs/payments/p2sh.cjs +253 -0
- package/src/cjs/payments/p2sh.d.ts +10 -0
- package/src/cjs/payments/p2tr.cjs +348 -0
- package/src/cjs/payments/p2tr.d.ts +10 -0
- package/src/cjs/payments/p2wpkh.cjs +186 -0
- package/src/cjs/payments/p2wpkh.d.ts +10 -0
- package/src/cjs/payments/p2wsh.cjs +269 -0
- package/src/cjs/payments/p2wsh.d.ts +10 -0
- package/src/cjs/psbt/bip371.cjs +556 -0
- package/src/cjs/psbt/bip371.d.ts +89 -0
- package/src/cjs/psbt/psbtutils.cjs +228 -0
- package/src/cjs/psbt/psbtutils.d.ts +49 -0
- package/src/cjs/psbt.cjs +1856 -0
- package/src/cjs/psbt.d.ts +206 -0
- package/src/cjs/push_data.cjs +133 -0
- package/src/cjs/push_data.d.ts +28 -0
- package/src/cjs/script.cjs +339 -0
- package/src/cjs/script.d.ts +89 -0
- package/src/cjs/script_number.cjs +122 -0
- package/src/cjs/script_number.d.ts +18 -0
- package/src/cjs/script_signature.cjs +123 -0
- package/src/cjs/script_signature.d.ts +20 -0
- package/src/cjs/transaction.cjs +607 -0
- package/src/cjs/transaction.d.ts +60 -0
- package/src/cjs/types.cjs +147 -0
- package/src/cjs/types.d.ts +45 -0
- package/src/esm/address.js +200 -0
- package/src/esm/bip66.js +110 -0
- package/src/esm/block.js +225 -0
- package/src/esm/bufferutils.js +156 -0
- package/src/esm/crypto.js +123 -0
- package/src/esm/ecc_lib.js +108 -0
- package/src/esm/index.js +12 -0
- package/src/esm/merkle.js +27 -0
- package/src/esm/networks.js +66 -0
- package/src/esm/ops.js +125 -0
- package/src/esm/payments/bip341.js +135 -0
- package/src/esm/payments/embed.js +50 -0
- package/src/esm/payments/index.js +11 -0
- package/src/esm/payments/lazy.js +27 -0
- package/src/esm/payments/p2ms.js +167 -0
- package/src/esm/payments/p2pk.js +82 -0
- package/src/esm/payments/p2pkh.js +144 -0
- package/src/esm/payments/p2sh.js +201 -0
- package/src/esm/payments/p2tr.js +301 -0
- package/src/esm/payments/p2wpkh.js +139 -0
- package/src/esm/payments/p2wsh.js +228 -0
- package/src/esm/psbt/bip371.js +490 -0
- package/src/esm/psbt/psbtutils.js +168 -0
- package/src/esm/psbt.js +1774 -0
- package/src/esm/push_data.js +77 -0
- package/src/esm/script.js +277 -0
- package/src/esm/script_number.js +74 -0
- package/src/esm/script_signature.js +75 -0
- package/src/esm/transaction.js +550 -0
- package/src/esm/types.js +83 -0
- package/start-bip40.cjs +127 -0
|
@@ -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;
|
|
@@ -0,0 +1,186 @@
|
|
|
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.p2wpkh = p2wpkh;
|
|
48
|
+
const bcrypto = __importStar(require('../crypto.cjs'));
|
|
49
|
+
const networks_js_1 = require('../networks.cjs');
|
|
50
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
51
|
+
const types_js_1 = require('../types.cjs');
|
|
52
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
53
|
+
const bech32_1 = require('bech32');
|
|
54
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
55
|
+
const v = __importStar(require('valibot'));
|
|
56
|
+
const OPS = bscript.OPS;
|
|
57
|
+
const EMPTY_BUFFER = new Uint8Array(0);
|
|
58
|
+
// witness: {signature} {pubKey}
|
|
59
|
+
// input: <>
|
|
60
|
+
// output: OP_0 {pubKeyHash}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
|
|
63
|
+
*
|
|
64
|
+
* @param a - The payment object containing the necessary data.
|
|
65
|
+
* @param opts - Optional payment options.
|
|
66
|
+
* @returns The p2wpkh payment object.
|
|
67
|
+
* @throws {TypeError} If the required data is missing or invalid.
|
|
68
|
+
*/
|
|
69
|
+
function p2wpkh(a, opts) {
|
|
70
|
+
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
|
|
71
|
+
throw new TypeError('Not enough data');
|
|
72
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
73
|
+
v.parse(
|
|
74
|
+
v.partial(
|
|
75
|
+
v.object({
|
|
76
|
+
address: v.string(),
|
|
77
|
+
hash: (0, types_js_1.NBufferSchemaFactory)(20),
|
|
78
|
+
input: (0, types_js_1.NBufferSchemaFactory)(0),
|
|
79
|
+
network: v.object({}),
|
|
80
|
+
output: (0, types_js_1.NBufferSchemaFactory)(22),
|
|
81
|
+
pubkey: v.custom(types_js_1.isPoint, 'Not a valid pubkey'),
|
|
82
|
+
signature: v.custom(bscript.isCanonicalScriptSignature),
|
|
83
|
+
witness: v.array(types_js_1.BufferSchema),
|
|
84
|
+
}),
|
|
85
|
+
),
|
|
86
|
+
a,
|
|
87
|
+
);
|
|
88
|
+
const _address = lazy.value(() => {
|
|
89
|
+
const result = bech32_1.bech32.decode(a.address);
|
|
90
|
+
const version = result.words.shift();
|
|
91
|
+
const data = bech32_1.bech32.fromWords(result.words);
|
|
92
|
+
return {
|
|
93
|
+
version,
|
|
94
|
+
prefix: result.prefix,
|
|
95
|
+
data: Uint8Array.from(data),
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
const network = a.network || networks_js_1.bitcoin;
|
|
99
|
+
const o = { name: 'p2wpkh', network };
|
|
100
|
+
lazy.prop(o, 'address', () => {
|
|
101
|
+
if (!o.hash) return;
|
|
102
|
+
const words = bech32_1.bech32.toWords(o.hash);
|
|
103
|
+
words.unshift(0x00);
|
|
104
|
+
return bech32_1.bech32.encode(network.bech32, words);
|
|
105
|
+
});
|
|
106
|
+
lazy.prop(o, 'hash', () => {
|
|
107
|
+
if (a.output) return a.output.slice(2, 22);
|
|
108
|
+
if (a.address) return _address().data;
|
|
109
|
+
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
|
|
110
|
+
});
|
|
111
|
+
lazy.prop(o, 'output', () => {
|
|
112
|
+
if (!o.hash) return;
|
|
113
|
+
return bscript.compile([OPS.OP_0, o.hash]);
|
|
114
|
+
});
|
|
115
|
+
lazy.prop(o, 'pubkey', () => {
|
|
116
|
+
if (a.pubkey) return a.pubkey;
|
|
117
|
+
if (!a.witness) return;
|
|
118
|
+
return a.witness[1];
|
|
119
|
+
});
|
|
120
|
+
lazy.prop(o, 'signature', () => {
|
|
121
|
+
if (!a.witness) return;
|
|
122
|
+
return a.witness[0];
|
|
123
|
+
});
|
|
124
|
+
lazy.prop(o, 'input', () => {
|
|
125
|
+
if (!o.witness) return;
|
|
126
|
+
return EMPTY_BUFFER;
|
|
127
|
+
});
|
|
128
|
+
lazy.prop(o, 'witness', () => {
|
|
129
|
+
if (!a.pubkey) return;
|
|
130
|
+
if (!a.signature) return;
|
|
131
|
+
return [a.signature, a.pubkey];
|
|
132
|
+
});
|
|
133
|
+
// extended validation
|
|
134
|
+
if (opts.validate) {
|
|
135
|
+
let hash = Uint8Array.from([]);
|
|
136
|
+
if (a.address) {
|
|
137
|
+
if (network && network.bech32 !== _address().prefix)
|
|
138
|
+
throw new TypeError('Invalid prefix or Network mismatch');
|
|
139
|
+
if (_address().version !== 0x00)
|
|
140
|
+
throw new TypeError('Invalid address version');
|
|
141
|
+
if (_address().data.length !== 20)
|
|
142
|
+
throw new TypeError('Invalid address data');
|
|
143
|
+
hash = _address().data;
|
|
144
|
+
}
|
|
145
|
+
if (a.hash) {
|
|
146
|
+
if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
|
|
147
|
+
throw new TypeError('Hash mismatch');
|
|
148
|
+
else hash = a.hash;
|
|
149
|
+
}
|
|
150
|
+
if (a.output) {
|
|
151
|
+
if (
|
|
152
|
+
a.output.length !== 22 ||
|
|
153
|
+
a.output[0] !== OPS.OP_0 ||
|
|
154
|
+
a.output[1] !== 0x14
|
|
155
|
+
)
|
|
156
|
+
throw new TypeError('Output is invalid');
|
|
157
|
+
if (hash.length > 0 && tools.compare(hash, a.output.slice(2)) !== 0)
|
|
158
|
+
throw new TypeError('Hash mismatch');
|
|
159
|
+
else hash = a.output.slice(2);
|
|
160
|
+
}
|
|
161
|
+
if (a.pubkey) {
|
|
162
|
+
const pkh = bcrypto.hash160(a.pubkey);
|
|
163
|
+
if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
|
|
164
|
+
throw new TypeError('Hash mismatch');
|
|
165
|
+
else hash = pkh;
|
|
166
|
+
if (!(0, types_js_1.isPoint)(a.pubkey) || a.pubkey.length !== 33)
|
|
167
|
+
throw new TypeError('Invalid pubkey for p2wpkh');
|
|
168
|
+
}
|
|
169
|
+
if (a.witness) {
|
|
170
|
+
if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
|
|
171
|
+
if (!bscript.isCanonicalScriptSignature(a.witness[0]))
|
|
172
|
+
throw new TypeError('Witness has invalid signature');
|
|
173
|
+
if (!(0, types_js_1.isPoint)(a.witness[1]) || a.witness[1].length !== 33)
|
|
174
|
+
throw new TypeError('Witness has invalid pubkey');
|
|
175
|
+
if (a.signature && tools.compare(a.signature, a.witness[0]) !== 0)
|
|
176
|
+
throw new TypeError('Signature mismatch');
|
|
177
|
+
// if (a.pubkey && !a.pubkey.equals(a.witness[1]))
|
|
178
|
+
if (a.pubkey && tools.compare(a.pubkey, a.witness[1]) !== 0)
|
|
179
|
+
throw new TypeError('Pubkey mismatch');
|
|
180
|
+
const pkh = bcrypto.hash160(a.witness[1]);
|
|
181
|
+
if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
|
|
182
|
+
throw new TypeError('Hash mismatch');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return Object.assign(o, a);
|
|
186
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
|
|
4
|
+
*
|
|
5
|
+
* @param a - The payment object containing the necessary data.
|
|
6
|
+
* @param opts - Optional payment options.
|
|
7
|
+
* @returns The p2wpkh payment object.
|
|
8
|
+
* @throws {TypeError} If the required data is missing or invalid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function p2wpkh(a: Payment, opts?: PaymentOpts): Payment;
|