@btc-vision/transaction 1.7.27 → 1.7.29
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.
- package/browser/_version.d.ts +1 -1
- package/browser/btc-vision-bitcoin.js +1610 -1609
- package/browser/consensus/ConsensusConfig.d.ts +1 -5
- package/browser/consensus/IConsensusConfig.d.ts +6 -0
- package/browser/consensus/metadata/RoswellConsensus.d.ts +1 -1
- package/browser/epoch/interfaces/IChallengeSolution.d.ts +15 -1
- package/browser/generators/builders/CalldataGenerator.d.ts +2 -2
- package/browser/generators/builders/DeploymentGenerator.d.ts +2 -2
- package/browser/generators/builders/P2WDAGenerator.d.ts +2 -2
- package/browser/index.js +876 -1055
- package/browser/opnet.d.ts +7 -2
- package/browser/signer/AddressRotation.d.ts +2 -3
- package/browser/signer/IRotationSigner.d.ts +8 -0
- package/browser/transaction/TransactionFactory.d.ts +4 -27
- package/browser/transaction/browser/Web3Provider.d.ts +2 -25
- package/browser/transaction/builders/CancelTransaction.d.ts +1 -4
- package/browser/transaction/builders/ConsolidatedInteractionTransaction.d.ts +3 -3
- package/browser/transaction/builders/CustomScriptTransaction.d.ts +2 -8
- package/browser/transaction/builders/DeploymentTransaction.d.ts +3 -3
- package/browser/transaction/builders/InteractionTransactionP2WDA.d.ts +2 -2
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +3 -3
- package/browser/transaction/interfaces/ICancelTransactionParameters.d.ts +4 -0
- package/browser/transaction/interfaces/ICustomTransactionParameters.d.ts +8 -0
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +6 -6
- package/browser/transaction/interfaces/ITransactionResponses.d.ts +26 -0
- package/browser/transaction/interfaces/ITweakedTransactionData.d.ts +17 -0
- package/browser/transaction/interfaces/IWeb3ProviderTypes.d.ts +24 -0
- package/browser/transaction/shared/TweakedTransaction.d.ts +2 -14
- package/browser/transaction/utils/WitnessUtils.d.ts +1 -0
- package/browser/utxo/OPNetLimitedProvider.d.ts +1 -1
- package/browser/utxo/interfaces/IUTXO.d.ts +2 -2
- package/browser/vendors.js +90 -90
- package/browser/verification/TapscriptVerificator.d.ts +2 -2
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/consensus/ConsensusConfig.d.ts +1 -5
- package/build/consensus/IConsensusConfig.d.ts +6 -0
- package/build/consensus/IConsensusConfig.js +1 -0
- package/build/consensus/metadata/RoswellConsensus.d.ts +1 -1
- package/build/epoch/interfaces/IChallengeSolution.d.ts +15 -1
- package/build/epoch/validator/EpochValidator.js +35 -3
- package/build/generators/builders/CalldataGenerator.d.ts +2 -2
- package/build/generators/builders/DeploymentGenerator.d.ts +2 -2
- package/build/generators/builders/P2WDAGenerator.d.ts +2 -2
- package/build/opnet.d.ts +7 -2
- package/build/opnet.js +7 -2
- package/build/signer/AddressRotation.d.ts +2 -3
- package/build/signer/IRotationSigner.d.ts +8 -0
- package/build/signer/IRotationSigner.js +1 -0
- package/build/transaction/TransactionFactory.d.ts +4 -27
- package/build/transaction/TransactionFactory.js +1 -1
- package/build/transaction/browser/Web3Provider.d.ts +2 -25
- package/build/transaction/builders/CancelTransaction.d.ts +1 -4
- package/build/transaction/builders/ConsolidatedInteractionTransaction.d.ts +3 -3
- package/build/transaction/builders/CustomScriptTransaction.d.ts +2 -8
- package/build/transaction/builders/DeploymentTransaction.d.ts +3 -3
- package/build/transaction/builders/InteractionTransactionP2WDA.d.ts +2 -2
- package/build/transaction/builders/SharedInteractionTransaction.d.ts +3 -3
- package/build/transaction/builders/TransactionBuilder.js +3 -21
- package/build/transaction/interfaces/ICancelTransactionParameters.d.ts +4 -0
- package/build/transaction/interfaces/ICancelTransactionParameters.js +1 -0
- package/build/transaction/interfaces/ICustomTransactionParameters.d.ts +8 -0
- package/build/transaction/interfaces/ICustomTransactionParameters.js +1 -0
- package/build/transaction/interfaces/ITransactionParameters.d.ts +6 -6
- package/build/transaction/interfaces/ITransactionResponses.d.ts +26 -0
- package/build/transaction/interfaces/ITransactionResponses.js +1 -0
- package/build/transaction/interfaces/ITweakedTransactionData.d.ts +17 -0
- package/build/transaction/interfaces/ITweakedTransactionData.js +1 -0
- package/build/transaction/interfaces/IWeb3ProviderTypes.d.ts +24 -0
- package/build/transaction/interfaces/IWeb3ProviderTypes.js +1 -0
- package/build/transaction/shared/TweakedTransaction.d.ts +2 -14
- package/build/transaction/shared/TweakedTransaction.js +3 -3
- package/build/transaction/utils/WitnessUtils.d.ts +1 -0
- package/build/transaction/utils/WitnessUtils.js +23 -0
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/utxo/OPNetLimitedProvider.d.ts +1 -1
- package/build/utxo/OPNetLimitedProvider.js +1 -1
- package/build/utxo/interfaces/IUTXO.d.ts +2 -2
- package/build/verification/TapscriptVerificator.d.ts +2 -2
- package/package.json +40 -20
- package/src/_version.ts +1 -1
- package/src/consensus/ConsensusConfig.ts +1 -28
- package/src/consensus/IConsensusConfig.ts +29 -0
- package/src/consensus/metadata/RoswellConsensus.ts +1 -1
- package/src/epoch/interfaces/IChallengeSolution.ts +13 -1
- package/src/epoch/validator/EpochValidator.ts +54 -3
- package/src/generators/builders/CalldataGenerator.ts +3 -3
- package/src/generators/builders/DeploymentGenerator.ts +3 -3
- package/src/generators/builders/P2WDAGenerator.ts +2 -2
- package/src/keypair/MessageSigner.ts +1 -1
- package/src/opnet.ts +7 -4
- package/src/signer/AddressRotation.ts +3 -4
- package/src/signer/IRotationSigner.ts +35 -0
- package/src/transaction/TransactionFactory.ts +14 -38
- package/src/transaction/browser/Web3Provider.ts +14 -78
- package/src/transaction/builders/CancelTransaction.ts +2 -11
- package/src/transaction/builders/ConsolidatedInteractionTransaction.ts +3 -3
- package/src/transaction/builders/CustomScriptTransaction.ts +1 -15
- package/src/transaction/builders/DeploymentTransaction.ts +3 -3
- package/src/transaction/builders/InteractionTransactionP2WDA.ts +3 -3
- package/src/transaction/builders/SharedInteractionTransaction.ts +3 -3
- package/src/transaction/builders/TransactionBuilder.ts +2 -28
- package/src/transaction/interfaces/ICancelTransactionParameters.ts +8 -0
- package/src/transaction/interfaces/ICustomTransactionParameters.ts +15 -0
- package/src/transaction/interfaces/ITransactionParameters.ts +6 -6
- package/src/transaction/interfaces/ITransactionResponses.ts +29 -0
- package/src/transaction/interfaces/ITweakedTransactionData.ts +24 -0
- package/src/transaction/interfaces/IWeb3ProviderTypes.ts +74 -0
- package/src/transaction/offline/TransactionReconstructor.ts +1 -1
- package/src/transaction/shared/TweakedTransaction.ts +6 -30
- package/src/transaction/utils/WitnessUtils.ts +36 -0
- package/src/utxo/OPNetLimitedProvider.ts +3 -1
- package/src/utxo/interfaces/IUTXO.ts +2 -2
- package/src/verification/TapscriptVerificator.ts +2 -2
- package/browser/metadata/tokens.d.ts +0 -39
- package/browser/transaction/processor/PsbtTransaction.d.ts +0 -25
- package/build/metadata/tokens.d.ts +0 -39
- package/build/metadata/tokens.js +0 -100
- package/build/transaction/processor/PsbtTransaction.d.ts +0 -25
- package/build/transaction/processor/PsbtTransaction.js +0 -80
- package/src/metadata/tokens.ts +0 -139
- package/src/transaction/processor/PsbtTransaction.ts +0 -179
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as h, a as U, t as c,
|
|
1
|
+
import { d as I, B as h, a as U, t as c, i as Ut, b as Kn, e as Br, c as kt, v as Se, p as qe, u as N } from "./vendors.js";
|
|
2
2
|
const M = {
|
|
3
3
|
messagePrefix: `Bitcoin Signed Message:
|
|
4
4
|
`,
|
|
@@ -36,7 +36,25 @@ const M = {
|
|
|
36
36
|
scriptHash: 196,
|
|
37
37
|
wif: 239
|
|
38
38
|
};
|
|
39
|
-
function
|
|
39
|
+
function Be(t) {
|
|
40
|
+
let e, r;
|
|
41
|
+
try {
|
|
42
|
+
e = I.bech32.decode(t);
|
|
43
|
+
} catch {
|
|
44
|
+
}
|
|
45
|
+
if (e) {
|
|
46
|
+
if (r = e.words[0], r !== 0)
|
|
47
|
+
throw new TypeError(t + " uses wrong encoding");
|
|
48
|
+
} else if (e = I.bech32m.decode(t), r = e.words[0], r === 0)
|
|
49
|
+
throw new TypeError(t + " uses wrong encoding");
|
|
50
|
+
const n = I.bech32.fromWords(e.words.slice(1));
|
|
51
|
+
return {
|
|
52
|
+
version: r,
|
|
53
|
+
prefix: e.prefix,
|
|
54
|
+
data: h.from(n)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function Mn(t) {
|
|
40
58
|
if (t.length < 8 || t.length > 72 || t[0] !== 48 || t[1] !== t.length - 2 || t[2] !== 2)
|
|
41
59
|
return !1;
|
|
42
60
|
const e = t[3];
|
|
@@ -45,7 +63,7 @@ function Dn(t) {
|
|
|
45
63
|
const r = t[5 + e];
|
|
46
64
|
return !(r === 0 || 6 + e + r !== t.length || t[4] & 128 || e > 1 && t[4] === 0 && !(t[5] & 128) || t[e + 6] & 128 || r > 1 && t[e + 6] === 0 && !(t[e + 7] & 128));
|
|
47
65
|
}
|
|
48
|
-
function
|
|
66
|
+
function Dn(t) {
|
|
49
67
|
if (t.length < 8)
|
|
50
68
|
throw new Error("DER sequence length is too short");
|
|
51
69
|
if (t.length > 72)
|
|
@@ -81,7 +99,7 @@ function $n(t) {
|
|
|
81
99
|
s: t.subarray(6 + e)
|
|
82
100
|
};
|
|
83
101
|
}
|
|
84
|
-
function
|
|
102
|
+
function $n(t, e) {
|
|
85
103
|
const r = t.length, n = e.length;
|
|
86
104
|
if (r === 0)
|
|
87
105
|
throw new Error("R length is zero");
|
|
@@ -230,11 +248,11 @@ for (const t of Object.keys(E)) {
|
|
|
230
248
|
function Fr(t) {
|
|
231
249
|
return t < E.OP_PUSHDATA1 ? 1 : t <= 255 ? 2 : t <= 65535 ? 3 : 5;
|
|
232
250
|
}
|
|
233
|
-
function
|
|
251
|
+
function qn(t, e, r) {
|
|
234
252
|
const n = Fr(e);
|
|
235
253
|
return n === 1 ? t.writeUInt8(e, r) : n === 2 ? (t.writeUInt8(E.OP_PUSHDATA1, r), t.writeUInt8(e, r + 1)) : n === 3 ? (t.writeUInt8(E.OP_PUSHDATA2, r), t.writeUInt16LE(e, r + 1)) : (t.writeUInt8(E.OP_PUSHDATA4, r), t.writeUInt32LE(e, r + 1)), n;
|
|
236
254
|
}
|
|
237
|
-
function
|
|
255
|
+
function Xn(t, e) {
|
|
238
256
|
const r = t.readUInt8(e);
|
|
239
257
|
let n, s;
|
|
240
258
|
if (r < E.OP_PUSHDATA1)
|
|
@@ -260,7 +278,7 @@ function jn(t, e) {
|
|
|
260
278
|
size: s
|
|
261
279
|
};
|
|
262
280
|
}
|
|
263
|
-
function
|
|
281
|
+
function jn(t, e, r) {
|
|
264
282
|
e = e || 4, r = r === void 0 ? !0 : r;
|
|
265
283
|
const n = t.length;
|
|
266
284
|
if (n === 0)
|
|
@@ -278,19 +296,19 @@ function zn(t, e, r) {
|
|
|
278
296
|
s |= t[i] << 8 * i;
|
|
279
297
|
return t[n - 1] & 128 ? -(s & ~(128 << 8 * (n - 1))) : s;
|
|
280
298
|
}
|
|
281
|
-
function
|
|
299
|
+
function zn(t) {
|
|
282
300
|
return t > 2147483647 ? 5 : t > 8388607 ? 4 : t > 32767 ? 3 : t > 127 ? 2 : t > 0 ? 1 : 0;
|
|
283
301
|
}
|
|
284
302
|
function Lr(t) {
|
|
285
303
|
let e = Math.abs(t);
|
|
286
|
-
const r =
|
|
304
|
+
const r = zn(e), n = h.allocUnsafe(r), s = t < 0;
|
|
287
305
|
for (let i = 0; i < r; ++i)
|
|
288
306
|
n.writeUInt8(e & 255, i), e >>= 8;
|
|
289
307
|
return n[r - 1] & 128 ? n.writeUInt8(s ? 128 : 0, r - 1) : s && (n[r - 1] |= 128), n;
|
|
290
308
|
}
|
|
291
|
-
const
|
|
309
|
+
const Yn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
292
310
|
__proto__: null,
|
|
293
|
-
decode:
|
|
311
|
+
decode: jn,
|
|
294
312
|
encode: Lr
|
|
295
313
|
}, Symbol.toStringTag, { value: "Module" })), Xe = U.alloc(32, 0), je = U.from("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "hex");
|
|
296
314
|
function at(t, e) {
|
|
@@ -309,9 +327,9 @@ function W(t) {
|
|
|
309
327
|
const n = t.subarray(33);
|
|
310
328
|
return n.compare(Xe) === 0 || n.compare(je) >= 0 ? !1 : e === 4 || e === 6 || e === 7;
|
|
311
329
|
}
|
|
312
|
-
const
|
|
330
|
+
const Zn = 21 * 1e14;
|
|
313
331
|
function qt(t) {
|
|
314
|
-
return c.UInt53(t) && t <=
|
|
332
|
+
return c.UInt53(t) && t <= Zn;
|
|
315
333
|
}
|
|
316
334
|
const Pt = 254;
|
|
317
335
|
function ne(t) {
|
|
@@ -320,18 +338,18 @@ function ne(t) {
|
|
|
320
338
|
const e = t;
|
|
321
339
|
return U.isBuffer(e.output) ? e.version !== void 0 ? (e.version & Pt) === e.version : !0 : !1;
|
|
322
340
|
}
|
|
323
|
-
function
|
|
324
|
-
return globalThis.Array.isArray(t) ? t.length !== 2 ? !1 : t.every((e) =>
|
|
341
|
+
function Re(t) {
|
|
342
|
+
return globalThis.Array.isArray(t) ? t.length !== 2 ? !1 : t.every((e) => Re(e)) : ne(t);
|
|
325
343
|
}
|
|
326
|
-
const
|
|
344
|
+
const Qn = c.BufferN(32), Jn = c.BufferN(20), _e = c.BufferN(32), It = c.Number, se = c.Array, ts = c.Boolean, Vr = c.String, V = c.Buffer, Wr = c.Hex, Te = c.maybe, z = c.tuple, Gr = c.UInt8, Y = c.UInt32, es = c.Function, Kr = c.BufferN, Mr = c.Null, rs = c.oneOf, ie = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
327
345
|
__proto__: null,
|
|
328
346
|
Array: se,
|
|
329
|
-
Boolean:
|
|
347
|
+
Boolean: ts,
|
|
330
348
|
Buffer: V,
|
|
331
|
-
Buffer256bit:
|
|
349
|
+
Buffer256bit: Qn,
|
|
332
350
|
BufferN: Kr,
|
|
333
|
-
Function:
|
|
334
|
-
Hash160bit:
|
|
351
|
+
Function: es,
|
|
352
|
+
Hash160bit: Jn,
|
|
335
353
|
Hash256bit: _e,
|
|
336
354
|
Hex: Wr,
|
|
337
355
|
Null: Mr,
|
|
@@ -343,13 +361,17 @@ const Jn = c.BufferN(32), ts = c.BufferN(20), _e = c.BufferN(32), It = c.Number,
|
|
|
343
361
|
UInt8: Gr,
|
|
344
362
|
isPoint: W,
|
|
345
363
|
isTapleaf: ne,
|
|
346
|
-
isTaptree:
|
|
364
|
+
isTaptree: Re,
|
|
347
365
|
maybe: Te,
|
|
348
|
-
oneOf:
|
|
366
|
+
oneOf: rs,
|
|
349
367
|
stacksEqual: at,
|
|
350
368
|
tuple: z,
|
|
351
369
|
typeforce: c
|
|
352
|
-
}, Symbol.toStringTag, { value: "Module" })), { typeforce:
|
|
370
|
+
}, Symbol.toStringTag, { value: "Module" })), { typeforce: ns } = ie, ze = h.alloc(1, 0);
|
|
371
|
+
function oe(t) {
|
|
372
|
+
const e = t & -129;
|
|
373
|
+
return e > 0 && e < 4;
|
|
374
|
+
}
|
|
353
375
|
function Ye(t) {
|
|
354
376
|
let e = 0;
|
|
355
377
|
for (; t[e] === 0; )
|
|
@@ -361,39 +383,40 @@ function Ze(t) {
|
|
|
361
383
|
const e = h.alloc(32, 0), r = Math.max(0, 32 - t.length);
|
|
362
384
|
return t.copy(e, r), e;
|
|
363
385
|
}
|
|
364
|
-
function
|
|
386
|
+
function ss(t) {
|
|
365
387
|
const e = t.readUInt8(t.length - 1);
|
|
366
|
-
if (!
|
|
388
|
+
if (!oe(e))
|
|
367
389
|
throw new Error(`Invalid hashType ${e}`);
|
|
368
|
-
const r =
|
|
390
|
+
const r = Dn(t.subarray(0, -1)), n = Ze(r.r), s = Ze(r.s);
|
|
369
391
|
return { signature: h.concat([n, s], 64), hashType: e };
|
|
370
392
|
}
|
|
371
|
-
function
|
|
372
|
-
if (
|
|
393
|
+
function is(t, e) {
|
|
394
|
+
if (ns({
|
|
373
395
|
signature: Kr(64),
|
|
374
396
|
hashType: Gr
|
|
375
|
-
}, { signature: t, hashType: e }), !
|
|
397
|
+
}, { signature: t, hashType: e }), !oe(e))
|
|
376
398
|
throw new Error(`Invalid hashType ${e}`);
|
|
377
399
|
const r = h.allocUnsafe(1);
|
|
378
400
|
r.writeUInt8(e, 0);
|
|
379
401
|
const n = Ye(t.subarray(0, 32)), s = Ye(t.subarray(32, 64));
|
|
380
|
-
return h.concat([
|
|
402
|
+
return h.concat([$n(n, s), r]);
|
|
381
403
|
}
|
|
382
|
-
const
|
|
404
|
+
const os = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
383
405
|
__proto__: null,
|
|
384
|
-
decode:
|
|
385
|
-
encode:
|
|
406
|
+
decode: ss,
|
|
407
|
+
encode: is,
|
|
408
|
+
isDefinedHashType: oe
|
|
386
409
|
}, Symbol.toStringTag, { value: "Module" })), { typeforce: Nt } = ie, Dr = E.OP_RESERVED;
|
|
387
|
-
function
|
|
410
|
+
function us(t) {
|
|
388
411
|
return It(t) && (t === E.OP_0 || t >= E.OP_1 && t <= E.OP_16 || t === E.OP_1NEGATE);
|
|
389
412
|
}
|
|
390
413
|
function $r(t) {
|
|
391
|
-
return V(t) ||
|
|
414
|
+
return V(t) || us(t);
|
|
392
415
|
}
|
|
393
|
-
function
|
|
416
|
+
function ue(t) {
|
|
394
417
|
return se(t) && t.every($r);
|
|
395
418
|
}
|
|
396
|
-
function
|
|
419
|
+
function Fe(t) {
|
|
397
420
|
return t.length - t.filter($r).length;
|
|
398
421
|
}
|
|
399
422
|
function zt(t) {
|
|
@@ -409,7 +432,7 @@ function zt(t) {
|
|
|
409
432
|
function qr(t) {
|
|
410
433
|
return h.isBuffer(t);
|
|
411
434
|
}
|
|
412
|
-
function
|
|
435
|
+
function fs(t) {
|
|
413
436
|
return se(t);
|
|
414
437
|
}
|
|
415
438
|
function Yt(t) {
|
|
@@ -428,7 +451,7 @@ function v(t) {
|
|
|
428
451
|
r.writeUInt8(i, n), n += 1;
|
|
429
452
|
return;
|
|
430
453
|
}
|
|
431
|
-
n +=
|
|
454
|
+
n += qn(r, s.length, n), s.copy(r, n), n += s.length;
|
|
432
455
|
} else
|
|
433
456
|
r.writeUInt8(s, n), n += 1;
|
|
434
457
|
}), n !== r.length)
|
|
@@ -436,7 +459,7 @@ function v(t) {
|
|
|
436
459
|
return r;
|
|
437
460
|
}
|
|
438
461
|
function A(t) {
|
|
439
|
-
if (
|
|
462
|
+
if (fs(t))
|
|
440
463
|
return t;
|
|
441
464
|
Nt(V, t);
|
|
442
465
|
const e = [];
|
|
@@ -444,7 +467,7 @@ function A(t) {
|
|
|
444
467
|
for (; r < t.length; ) {
|
|
445
468
|
const n = t[r];
|
|
446
469
|
if (n > E.OP_0 && n <= E.OP_PUSHDATA4) {
|
|
447
|
-
const s =
|
|
470
|
+
const s = Xn(t, r);
|
|
448
471
|
if (s === null || (r += s.size, r + s.number > t.length))
|
|
449
472
|
return null;
|
|
450
473
|
const i = t.subarray(r, r + s.number);
|
|
@@ -469,211 +492,171 @@ function Xr(t) {
|
|
|
469
492
|
return Rr[e];
|
|
470
493
|
}).join(" ");
|
|
471
494
|
}
|
|
472
|
-
function
|
|
495
|
+
function cs(t) {
|
|
473
496
|
return Nt(Vr, t), v(t.split(" ").map((e) => E[e] !== void 0 ? E[e] : (Nt(Wr, e), h.from(e, "hex"))));
|
|
474
497
|
}
|
|
475
498
|
function jr(t) {
|
|
476
|
-
return t = A(t), Nt(
|
|
499
|
+
return t = A(t), Nt(ue, t), t.map((e) => Yt(e) ? e : e === E.OP_0 ? h.allocUnsafe(0) : Lr(e - Dr));
|
|
477
500
|
}
|
|
478
501
|
function zr(t) {
|
|
479
502
|
return W(t);
|
|
480
503
|
}
|
|
481
|
-
function ue(t) {
|
|
482
|
-
const e = t & -129;
|
|
483
|
-
return e > 0 && e < 4;
|
|
484
|
-
}
|
|
485
504
|
function Q(t) {
|
|
486
|
-
return !h.isBuffer(t) || !
|
|
505
|
+
return !h.isBuffer(t) || !oe(t[t.length - 1]) ? !1 : Mn(t.subarray(0, -1));
|
|
487
506
|
}
|
|
488
|
-
const
|
|
507
|
+
const as = Yn, dt = os, hs = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
489
508
|
__proto__: null,
|
|
490
509
|
compile: v,
|
|
491
|
-
countNonPushOnlyOPs:
|
|
510
|
+
countNonPushOnlyOPs: Fe,
|
|
492
511
|
decompile: A,
|
|
493
|
-
fromASM:
|
|
512
|
+
fromASM: cs,
|
|
494
513
|
isCanonicalPubKey: zr,
|
|
495
514
|
isCanonicalScriptSignature: Q,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
number: hs,
|
|
515
|
+
isPushOnly: ue,
|
|
516
|
+
number: as,
|
|
499
517
|
opcodes: E,
|
|
500
518
|
signature: dt,
|
|
501
519
|
toASM: Xr,
|
|
502
520
|
toStack: jr
|
|
503
|
-
}, Symbol.toStringTag, { value: "Module" }))
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (t[1] < 76)
|
|
526
|
-
s = t[1], n = 2;
|
|
527
|
-
else if (t[1] === 76)
|
|
528
|
-
s = t[2], n = 3;
|
|
529
|
-
else
|
|
530
|
-
throw new TypeError("Unsupported push opcode in script");
|
|
531
|
-
const i = h.from(t.subarray(n, n + s));
|
|
532
|
-
if (i.length < ce || i.length > fe)
|
|
533
|
-
throw new TypeError("Invalid program length for segwit address");
|
|
534
|
-
const o = r === E.OP_0 ? 0 : r >= E.OP_1 && r <= E.OP_16 ? r - (E.OP_1 - 1) : -1;
|
|
535
|
-
if (o < ae || o > ls)
|
|
536
|
-
throw new TypeError(`Invalid segwit version ${o}`);
|
|
537
|
-
const u = [o, ...I.bech32m.toWords(i)];
|
|
538
|
-
return I.bech32m.encode(e.bech32Opnet, u);
|
|
539
|
-
}
|
|
540
|
-
function ys(t, e) {
|
|
541
|
-
const r = h.from(t.subarray(2));
|
|
542
|
-
if (r.length < ce || r.length > fe)
|
|
543
|
-
throw new TypeError("Invalid program length for segwit address");
|
|
544
|
-
const n = t[0] - Le;
|
|
545
|
-
if (n < Fe || n > ae)
|
|
546
|
-
throw new TypeError("Invalid version for segwit address");
|
|
547
|
-
if (t[1] !== r.length)
|
|
548
|
-
throw new TypeError(`Invalid script for segwit address ${t[1]} !== ${r.length}`);
|
|
549
|
-
return bs(r, n, e.bech32, e.bech32Opnet);
|
|
521
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
522
|
+
var K;
|
|
523
|
+
(function(t) {
|
|
524
|
+
t.P2PK = "p2pk", t.P2PKH = "p2pkh", t.P2SH = "p2sh", t.P2MS = "p2ms", t.P2WPKH = "p2wpkh", t.P2WSH = "p2wsh", t.P2TR = "p2tr", t.P2OP = "p2op", t.Embed = "embed", t.ScriptRedeem = "scriptRedeem";
|
|
525
|
+
})(K || (K = {}));
|
|
526
|
+
function y(t, e, r) {
|
|
527
|
+
Object.defineProperty(t, e, {
|
|
528
|
+
configurable: !0,
|
|
529
|
+
enumerable: !0,
|
|
530
|
+
get() {
|
|
531
|
+
const n = r.call(this);
|
|
532
|
+
return this[e] = n, n;
|
|
533
|
+
},
|
|
534
|
+
set(n) {
|
|
535
|
+
Object.defineProperty(this, e, {
|
|
536
|
+
configurable: !0,
|
|
537
|
+
enumerable: !0,
|
|
538
|
+
value: n,
|
|
539
|
+
writable: !0
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
});
|
|
550
543
|
}
|
|
551
|
-
function
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
throw new TypeError(t + " is too short");
|
|
555
|
-
if (e.length > 21)
|
|
556
|
-
throw new TypeError(t + " is too long");
|
|
557
|
-
const r = e.readUInt8(0), n = h.from(e.subarray(1));
|
|
558
|
-
return { version: r, hash: n };
|
|
544
|
+
function B(t) {
|
|
545
|
+
let e;
|
|
546
|
+
return () => (e !== void 0 || (e = t()), e);
|
|
559
547
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
548
|
+
const Qe = E, Je = 16, tr = 2, er = 40;
|
|
549
|
+
function Yr(t, e) {
|
|
550
|
+
if (!t.address && !t.output && !t.program && (typeof t.deploymentVersion > "u" || !t.hash160))
|
|
551
|
+
throw new TypeError("At least one of address, output or program must be provided");
|
|
552
|
+
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
553
|
+
address: c.maybe(c.String),
|
|
554
|
+
output: c.maybe(c.Buffer),
|
|
555
|
+
program: c.maybe(c.Buffer),
|
|
556
|
+
network: c.maybe(c.Object),
|
|
557
|
+
deploymentVersion: c.maybe(c.Number),
|
|
558
|
+
hash160: c.maybe(c.BufferN(20))
|
|
559
|
+
}, t);
|
|
560
|
+
const r = () => {
|
|
561
|
+
if (typeof t.deploymentVersion < "u" && typeof t.hash160 < "u") {
|
|
562
|
+
if (t.hash160.length !== 20)
|
|
563
|
+
throw new TypeError("hash160 must be exactly 20 bytes");
|
|
564
|
+
if (t.deploymentVersion < 0 || t.deploymentVersion > 255)
|
|
565
|
+
throw new TypeError("deploymentVersion must fit in one byte");
|
|
566
|
+
return h.concat([h.of(t.deploymentVersion), t.hash160]);
|
|
567
|
+
}
|
|
568
|
+
}, n = B(() => Be(t.address)), s = t.network || M, i = {
|
|
569
|
+
name: K.P2OP,
|
|
570
|
+
network: s,
|
|
571
|
+
deploymentVersion: 0
|
|
576
572
|
};
|
|
573
|
+
if (y(i, "program", () => {
|
|
574
|
+
if (t.program)
|
|
575
|
+
return t.program;
|
|
576
|
+
const o = r();
|
|
577
|
+
if (o)
|
|
578
|
+
return o;
|
|
579
|
+
if (t.output) {
|
|
580
|
+
if (t.output[0] !== Qe.OP_16)
|
|
581
|
+
throw new TypeError("Invalid P2OP script");
|
|
582
|
+
let u = 1, f;
|
|
583
|
+
if (t.output[1] < 76)
|
|
584
|
+
f = t.output[1], u = 2;
|
|
585
|
+
else if (t.output[1] === 76)
|
|
586
|
+
f = t.output[2], u = 3;
|
|
587
|
+
else
|
|
588
|
+
throw new TypeError("Unsupported push opcode in P2OP script");
|
|
589
|
+
return t.output.subarray(u, u + f);
|
|
590
|
+
}
|
|
591
|
+
if (t.address)
|
|
592
|
+
return n().data;
|
|
593
|
+
}), y(i, "deploymentVersion", () => {
|
|
594
|
+
if (i.program)
|
|
595
|
+
return i.program[0];
|
|
596
|
+
}), y(i, "hash160", () => {
|
|
597
|
+
if (i.program)
|
|
598
|
+
return i.program.subarray(1);
|
|
599
|
+
}), y(i, "output", () => {
|
|
600
|
+
if (i.program)
|
|
601
|
+
return v([Qe.OP_16, i.program]);
|
|
602
|
+
}), y(i, "address", () => {
|
|
603
|
+
if (!i.program)
|
|
604
|
+
return;
|
|
605
|
+
if (!s.bech32Opnet)
|
|
606
|
+
throw new TypeError("Network does not support opnet");
|
|
607
|
+
const o = I.bech32m.toWords(i.program);
|
|
608
|
+
return o.unshift(Je), I.bech32m.encode(s.bech32Opnet, o);
|
|
609
|
+
}), e.validate) {
|
|
610
|
+
let o = U.alloc(0);
|
|
611
|
+
if (t.address) {
|
|
612
|
+
const u = n();
|
|
613
|
+
if (s.bech32Opnet !== u.prefix)
|
|
614
|
+
throw new TypeError("Invalid prefix or network mismatch");
|
|
615
|
+
if (u.version !== Je)
|
|
616
|
+
throw new TypeError("Invalid witness version for p2op");
|
|
617
|
+
if (u.data.length < tr || u.data.length > er)
|
|
618
|
+
throw new TypeError("Invalid witness program length");
|
|
619
|
+
o = u.data;
|
|
620
|
+
}
|
|
621
|
+
if (t.program) {
|
|
622
|
+
if (o.length && !o.equals(t.program))
|
|
623
|
+
throw new TypeError("Program mismatch");
|
|
624
|
+
o = t.program;
|
|
625
|
+
}
|
|
626
|
+
if (!o.length && t.deploymentVersion !== void 0 && t.hash160 && (o = r()), t.output) {
|
|
627
|
+
const u = i.program;
|
|
628
|
+
if (o.length && !o.equals(u))
|
|
629
|
+
throw new TypeError("Program mismatch (output vs other source)");
|
|
630
|
+
o = u;
|
|
631
|
+
}
|
|
632
|
+
if (o.length < tr || o.length > er)
|
|
633
|
+
throw new TypeError(`Witness program must be 2–40 bytes. Was ${o.length} bytes`);
|
|
634
|
+
if (t.deploymentVersion !== void 0 && t.deploymentVersion !== o[0])
|
|
635
|
+
throw new TypeError("deploymentVersion mismatch");
|
|
636
|
+
if (t.hash160 && !t.hash160.equals(o.subarray(1)))
|
|
637
|
+
throw new TypeError("hash160 mismatch");
|
|
638
|
+
}
|
|
639
|
+
return Object.assign(i, t);
|
|
577
640
|
}
|
|
578
|
-
function
|
|
579
|
-
|
|
580
|
-
return s.unshift(e), e === Oe && n ? I.bech32m.encode(n, s) : e === 0 ? I.bech32.encode(r, s) : I.bech32m.encode(r, s);
|
|
641
|
+
function ps(t) {
|
|
642
|
+
return t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array";
|
|
581
643
|
}
|
|
582
|
-
function
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
}
|
|
588
|
-
try {
|
|
589
|
-
return yt({ output: t, network: e }).address;
|
|
590
|
-
} catch {
|
|
591
|
-
}
|
|
592
|
-
try {
|
|
593
|
-
return Vt({ output: t, network: e }).address;
|
|
594
|
-
} catch {
|
|
595
|
-
}
|
|
596
|
-
try {
|
|
597
|
-
return bt({ output: t, network: e }).address;
|
|
598
|
-
} catch {
|
|
599
|
-
}
|
|
600
|
-
try {
|
|
601
|
-
return mt({ output: t, network: e }).address;
|
|
602
|
-
} catch {
|
|
603
|
-
}
|
|
604
|
-
try {
|
|
605
|
-
return gs(t, e);
|
|
606
|
-
} catch {
|
|
607
|
-
}
|
|
608
|
-
try {
|
|
609
|
-
return ys(t, e);
|
|
610
|
-
} catch {
|
|
644
|
+
function Zr(t, e, r = "") {
|
|
645
|
+
const n = ps(t), s = t?.length;
|
|
646
|
+
if (!n || e !== void 0) {
|
|
647
|
+
const o = r && `"${r}" `, u = "", f = n ? `length=${s}` : `type=${typeof t}`;
|
|
648
|
+
throw new Error(o + "expected Uint8Array" + u + ", got " + f);
|
|
611
649
|
}
|
|
612
|
-
|
|
650
|
+
return t;
|
|
613
651
|
}
|
|
614
|
-
function
|
|
615
|
-
e = e || M;
|
|
616
|
-
let r, n;
|
|
617
|
-
try {
|
|
618
|
-
r = ms(t);
|
|
619
|
-
} catch {
|
|
620
|
-
}
|
|
621
|
-
if (r) {
|
|
622
|
-
if (r.version === e.pubKeyHash)
|
|
623
|
-
return Et({ hash: r.hash }).output;
|
|
624
|
-
if (r.version === e.scriptHash)
|
|
625
|
-
return yt({ hash: r.hash }).output;
|
|
626
|
-
} else {
|
|
627
|
-
try {
|
|
628
|
-
n = Ve(t);
|
|
629
|
-
} catch {
|
|
630
|
-
}
|
|
631
|
-
if (n) {
|
|
632
|
-
if (n.prefix !== e.bech32 && e.bech32Opnet && n.prefix !== e.bech32Opnet)
|
|
633
|
-
throw new Error(t + " has an invalid prefix");
|
|
634
|
-
if (n.version === 0) {
|
|
635
|
-
if (n.data.length === 20)
|
|
636
|
-
return Vt({ hash: n.data }).output;
|
|
637
|
-
if (n.data.length === 32)
|
|
638
|
-
return bt({ hash: n.data }).output;
|
|
639
|
-
} else if (n.version === 1) {
|
|
640
|
-
if (n.data.length === 32)
|
|
641
|
-
return mt({ pubkey: n.data }).output;
|
|
642
|
-
} else if (n.version === Oe) {
|
|
643
|
-
if (!e.bech32Opnet)
|
|
644
|
-
throw new Error(t + " has an invalid prefix");
|
|
645
|
-
return On({
|
|
646
|
-
program: n.data,
|
|
647
|
-
network: e
|
|
648
|
-
}).output;
|
|
649
|
-
} else if (n.version >= Fe && n.version <= ae && n.data.length >= ce && n.data.length <= fe)
|
|
650
|
-
return n.version !== Oe && console.warn(ds), v([
|
|
651
|
-
n.version + Le,
|
|
652
|
-
n.data
|
|
653
|
-
]);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
throw new TypeError(t + " has no matching Script");
|
|
657
|
-
}
|
|
658
|
-
function _s(t) {
|
|
659
|
-
return t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array";
|
|
660
|
-
}
|
|
661
|
-
function Yr(t, e, r = "") {
|
|
662
|
-
const n = _s(t), s = t?.length;
|
|
663
|
-
if (!n || e !== void 0) {
|
|
664
|
-
const o = r && `"${r}" `, u = "", f = n ? `length=${s}` : `type=${typeof t}`;
|
|
665
|
-
throw new Error(o + "expected Uint8Array" + u + ", got " + f);
|
|
666
|
-
}
|
|
667
|
-
return t;
|
|
668
|
-
}
|
|
669
|
-
function Qe(t, e = !0) {
|
|
652
|
+
function rr(t, e = !0) {
|
|
670
653
|
if (t.destroyed)
|
|
671
654
|
throw new Error("Hash instance has been destroyed");
|
|
672
655
|
if (e && t.finished)
|
|
673
656
|
throw new Error("Hash#digest() has already been called");
|
|
674
657
|
}
|
|
675
|
-
function
|
|
676
|
-
|
|
658
|
+
function ls(t, e) {
|
|
659
|
+
Zr(t, void 0, "digestInto() output");
|
|
677
660
|
const r = e.outputLen;
|
|
678
661
|
if (t.length < r)
|
|
679
662
|
throw new Error('"digestInto() output" expected to be of length >=' + r);
|
|
@@ -691,20 +674,20 @@ function q(t, e) {
|
|
|
691
674
|
function ft(t, e) {
|
|
692
675
|
return t << e | t >>> 32 - e >>> 0;
|
|
693
676
|
}
|
|
694
|
-
function
|
|
677
|
+
function Le(t, e = {}) {
|
|
695
678
|
const r = (s, i) => t(i).update(s).digest(), n = t(void 0);
|
|
696
679
|
return r.outputLen = n.outputLen, r.blockLen = n.blockLen, r.create = (s) => t(s), Object.assign(r, e), Object.freeze(r);
|
|
697
680
|
}
|
|
698
|
-
const
|
|
681
|
+
const ds = (t) => ({
|
|
699
682
|
oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, t])
|
|
700
683
|
});
|
|
701
|
-
function
|
|
684
|
+
function Qr(t, e, r) {
|
|
702
685
|
return t & e ^ ~t & r;
|
|
703
686
|
}
|
|
704
|
-
function
|
|
687
|
+
function Jr(t, e, r) {
|
|
705
688
|
return t & e ^ t & r ^ e & r;
|
|
706
689
|
}
|
|
707
|
-
class
|
|
690
|
+
class Ve {
|
|
708
691
|
blockLen;
|
|
709
692
|
outputLen;
|
|
710
693
|
padOffset;
|
|
@@ -720,7 +703,7 @@ class Ge {
|
|
|
720
703
|
this.blockLen = e, this.outputLen = r, this.padOffset = n, this.isLE = s, this.buffer = new Uint8Array(e), this.view = we(this.buffer);
|
|
721
704
|
}
|
|
722
705
|
update(e) {
|
|
723
|
-
|
|
706
|
+
rr(this), Zr(e);
|
|
724
707
|
const { view: r, buffer: n, blockLen: s } = this, i = e.length;
|
|
725
708
|
for (let o = 0; o < i; ) {
|
|
726
709
|
const u = Math.min(s - this.pos, i - o);
|
|
@@ -735,7 +718,7 @@ class Ge {
|
|
|
735
718
|
return this.length += e.length, this.roundClean(), this;
|
|
736
719
|
}
|
|
737
720
|
digestInto(e) {
|
|
738
|
-
|
|
721
|
+
rr(this), ls(e, this), this.finished = !0;
|
|
739
722
|
const { buffer: r, view: n, blockLen: s, isLE: i } = this;
|
|
740
723
|
let { pos: o } = this;
|
|
741
724
|
r[o++] = 128, ht(this.buffer.subarray(o)), this.padOffset > s - o && (this.process(n, 0), o = 0);
|
|
@@ -782,7 +765,7 @@ const et = /* @__PURE__ */ Uint32Array.from([
|
|
|
782
765
|
271733878,
|
|
783
766
|
3285377520
|
|
784
767
|
]), rt = /* @__PURE__ */ new Uint32Array(80);
|
|
785
|
-
class
|
|
768
|
+
class ws extends Ve {
|
|
786
769
|
A = St[0] | 0;
|
|
787
770
|
B = St[1] | 0;
|
|
788
771
|
C = St[2] | 0;
|
|
@@ -806,7 +789,7 @@ class As extends Ge {
|
|
|
806
789
|
let { A: n, B: s, C: i, D: o, E: u } = this;
|
|
807
790
|
for (let f = 0; f < 80; f++) {
|
|
808
791
|
let a, p;
|
|
809
|
-
f < 20 ? (a =
|
|
792
|
+
f < 20 ? (a = Qr(s, i, o), p = 1518500249) : f < 40 ? (a = s ^ i ^ o, p = 1859775393) : f < 60 ? (a = Jr(s, i, o), p = 2400959708) : (a = s ^ i ^ o, p = 3395469782);
|
|
810
793
|
const l = ft(n, 5) + a + u + p + rt[f] | 0;
|
|
811
794
|
u = o, o = i, i = ft(s, 30), s = n, n = l;
|
|
812
795
|
}
|
|
@@ -819,7 +802,7 @@ class As extends Ge {
|
|
|
819
802
|
this.set(0, 0, 0, 0, 0), ht(this.buffer);
|
|
820
803
|
}
|
|
821
804
|
}
|
|
822
|
-
const
|
|
805
|
+
const gs = /* @__PURE__ */ Le(() => new ws()), ys = /* @__PURE__ */ Uint8Array.from([
|
|
823
806
|
7,
|
|
824
807
|
4,
|
|
825
808
|
13,
|
|
@@ -836,36 +819,36 @@ const Ps = /* @__PURE__ */ We(() => new As()), Is = /* @__PURE__ */ Uint8Array.f
|
|
|
836
819
|
14,
|
|
837
820
|
11,
|
|
838
821
|
8
|
|
839
|
-
]),
|
|
840
|
-
const r = [[
|
|
822
|
+
]), tn = Uint8Array.from(new Array(16).fill(0).map((t, e) => e)), ms = tn.map((t) => (9 * t + 5) % 16), en = /* @__PURE__ */ (() => {
|
|
823
|
+
const r = [[tn], [ms]];
|
|
841
824
|
for (let n = 0; n < 4; n++)
|
|
842
825
|
for (let s of r)
|
|
843
|
-
s.push(s[n].map((i) =>
|
|
826
|
+
s.push(s[n].map((i) => ys[i]));
|
|
844
827
|
return r;
|
|
845
|
-
})(),
|
|
828
|
+
})(), rn = en[0], nn = en[1], sn = /* @__PURE__ */ [
|
|
846
829
|
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
847
830
|
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
848
831
|
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
849
832
|
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
850
833
|
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
|
|
851
|
-
].map((t) => Uint8Array.from(t)),
|
|
834
|
+
].map((t) => Uint8Array.from(t)), bs = /* @__PURE__ */ rn.map((t, e) => t.map((r) => sn[e][r])), Es = /* @__PURE__ */ nn.map((t, e) => t.map((r) => sn[e][r])), Ss = /* @__PURE__ */ Uint32Array.from([
|
|
852
835
|
0,
|
|
853
836
|
1518500249,
|
|
854
837
|
1859775393,
|
|
855
838
|
2400959708,
|
|
856
839
|
2840853838
|
|
857
|
-
]),
|
|
840
|
+
]), _s = /* @__PURE__ */ Uint32Array.from([
|
|
858
841
|
1352829926,
|
|
859
842
|
1548603684,
|
|
860
843
|
1836072691,
|
|
861
844
|
2053994217,
|
|
862
845
|
0
|
|
863
846
|
]);
|
|
864
|
-
function
|
|
847
|
+
function nr(t, e, r, n) {
|
|
865
848
|
return t === 0 ? e ^ r ^ n : t === 1 ? e & r | ~e & n : t === 2 ? (e | ~r) ^ n : t === 3 ? e & n | r & ~n : e ^ (r | ~n);
|
|
866
849
|
}
|
|
867
850
|
const Wt = /* @__PURE__ */ new Uint32Array(16);
|
|
868
|
-
class
|
|
851
|
+
class Ts extends Ve {
|
|
869
852
|
h0 = 1732584193;
|
|
870
853
|
h1 = -271733879;
|
|
871
854
|
h2 = -1732584194;
|
|
@@ -886,13 +869,13 @@ class Ns extends Ge {
|
|
|
886
869
|
Wt[b] = e.getUint32(r, !0);
|
|
887
870
|
let n = this.h0 | 0, s = n, i = this.h1 | 0, o = i, u = this.h2 | 0, f = u, a = this.h3 | 0, p = a, l = this.h4 | 0, d = l;
|
|
888
871
|
for (let b = 0; b < 5; b++) {
|
|
889
|
-
const w = 4 - b, S =
|
|
872
|
+
const w = 4 - b, S = Ss[b], P = _s[b], k = rn[b], H = nn[b], _ = bs[b], O = Es[b];
|
|
890
873
|
for (let T = 0; T < 16; T++) {
|
|
891
|
-
const F = ft(n +
|
|
874
|
+
const F = ft(n + nr(b, i, u, a) + Wt[k[T]] + S, _[T]) + l | 0;
|
|
892
875
|
n = l, l = a, a = ft(u, 10) | 0, u = i, i = F;
|
|
893
876
|
}
|
|
894
877
|
for (let T = 0; T < 16; T++) {
|
|
895
|
-
const F = ft(s +
|
|
878
|
+
const F = ft(s + nr(w, o, f, p) + Wt[H[T]] + P, O[T]) + d | 0;
|
|
896
879
|
s = d, d = p, p = ft(f, 10) | 0, f = o, o = F;
|
|
897
880
|
}
|
|
898
881
|
}
|
|
@@ -905,7 +888,7 @@ class Ns extends Ge {
|
|
|
905
888
|
this.destroyed = !0, ht(this.buffer), this.set(0, 0, 0, 0, 0);
|
|
906
889
|
}
|
|
907
890
|
}
|
|
908
|
-
const
|
|
891
|
+
const on = /* @__PURE__ */ Le(() => new Ts()), Os = /* @__PURE__ */ Uint32Array.from([
|
|
909
892
|
1116352408,
|
|
910
893
|
1899447441,
|
|
911
894
|
3049323471,
|
|
@@ -971,7 +954,7 @@ const sn = /* @__PURE__ */ We(() => new Ns()), Cs = /* @__PURE__ */ Uint32Array.
|
|
|
971
954
|
3204031479,
|
|
972
955
|
3329325298
|
|
973
956
|
]), nt = /* @__PURE__ */ new Uint32Array(64);
|
|
974
|
-
class
|
|
957
|
+
class As extends Ve {
|
|
975
958
|
constructor(e) {
|
|
976
959
|
super(64, e, 8, !1);
|
|
977
960
|
}
|
|
@@ -992,7 +975,7 @@ class Bs extends Ge {
|
|
|
992
975
|
}
|
|
993
976
|
let { A: n, B: s, C: i, D: o, E: u, F: f, G: a, H: p } = this;
|
|
994
977
|
for (let l = 0; l < 64; l++) {
|
|
995
|
-
const d = q(u, 6) ^ q(u, 11) ^ q(u, 25), b = p + d +
|
|
978
|
+
const d = q(u, 6) ^ q(u, 11) ^ q(u, 25), b = p + d + Qr(u, f, a) + Os[l] + nt[l] | 0, S = (q(n, 2) ^ q(n, 13) ^ q(n, 22)) + Jr(n, s, i) | 0;
|
|
996
979
|
p = a, a = f, f = u, u = o + b | 0, o = i, i = s, s = n, n = b + S | 0;
|
|
997
980
|
}
|
|
998
981
|
n = n + this.A | 0, s = s + this.B | 0, i = i + this.C | 0, o = o + this.D | 0, u = u + this.E | 0, f = f + this.F | 0, a = a + this.G | 0, p = p + this.H | 0, this.set(n, s, i, o, u, f, a, p);
|
|
@@ -1004,7 +987,7 @@ class Bs extends Ge {
|
|
|
1004
987
|
this.set(0, 0, 0, 0, 0, 0, 0, 0), ht(this.buffer);
|
|
1005
988
|
}
|
|
1006
989
|
}
|
|
1007
|
-
class
|
|
990
|
+
class Ps extends As {
|
|
1008
991
|
// We cannot use array here since array allows indexing by variable
|
|
1009
992
|
// which means optimizer/compiler cannot use registers.
|
|
1010
993
|
A = et[0] | 0;
|
|
@@ -1019,26 +1002,26 @@ class Rs extends Bs {
|
|
|
1019
1002
|
super(32);
|
|
1020
1003
|
}
|
|
1021
1004
|
}
|
|
1022
|
-
const Zt = /* @__PURE__ */
|
|
1023
|
-
() => new
|
|
1024
|
-
/* @__PURE__ */
|
|
1005
|
+
const Zt = /* @__PURE__ */ Le(
|
|
1006
|
+
() => new Ps(),
|
|
1007
|
+
/* @__PURE__ */ ds(1)
|
|
1025
1008
|
);
|
|
1026
|
-
function
|
|
1027
|
-
return h.from(
|
|
1009
|
+
function Is(t) {
|
|
1010
|
+
return h.from(on(Uint8Array.from(t)));
|
|
1028
1011
|
}
|
|
1029
|
-
function
|
|
1030
|
-
return h.from(
|
|
1012
|
+
function Hs(t) {
|
|
1013
|
+
return h.from(gs(Uint8Array.from(t)));
|
|
1031
1014
|
}
|
|
1032
1015
|
function D(t) {
|
|
1033
1016
|
return h.from(Zt(Uint8Array.from(t)));
|
|
1034
1017
|
}
|
|
1035
1018
|
function C(t) {
|
|
1036
|
-
return h.from(
|
|
1019
|
+
return h.from(on(Zt(Uint8Array.from(t))));
|
|
1037
1020
|
}
|
|
1038
1021
|
function it(t) {
|
|
1039
1022
|
return h.from(Zt(Zt(Uint8Array.from(t))));
|
|
1040
1023
|
}
|
|
1041
|
-
const
|
|
1024
|
+
const vs = [
|
|
1042
1025
|
"BIP0340/challenge",
|
|
1043
1026
|
"BIP0340/aux",
|
|
1044
1027
|
"BIP0340/nonce",
|
|
@@ -1048,7 +1031,7 @@ const Vs = [
|
|
|
1048
1031
|
"TapTweak",
|
|
1049
1032
|
"KeyAgg list",
|
|
1050
1033
|
"KeyAgg coefficient"
|
|
1051
|
-
],
|
|
1034
|
+
], un = {
|
|
1052
1035
|
"BIP0340/challenge": h.from([
|
|
1053
1036
|
123,
|
|
1054
1037
|
181,
|
|
@@ -1645,628 +1628,380 @@ const Vs = [
|
|
|
1645
1628
|
])
|
|
1646
1629
|
};
|
|
1647
1630
|
function Rt(t, e) {
|
|
1648
|
-
return D(h.concat([
|
|
1631
|
+
return D(h.concat([un[t], e]));
|
|
1649
1632
|
}
|
|
1650
|
-
const
|
|
1633
|
+
const xs = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1651
1634
|
__proto__: null,
|
|
1652
|
-
TAGGED_HASH_PREFIXES:
|
|
1653
|
-
TAGS:
|
|
1635
|
+
TAGGED_HASH_PREFIXES: un,
|
|
1636
|
+
TAGS: vs,
|
|
1654
1637
|
hash160: C,
|
|
1655
1638
|
hash256: it,
|
|
1656
|
-
ripemd160:
|
|
1657
|
-
sha1:
|
|
1639
|
+
ripemd160: Is,
|
|
1640
|
+
sha1: Hs,
|
|
1658
1641
|
sha256: D,
|
|
1659
1642
|
taggedHash: Rt
|
|
1660
|
-
}, Symbol.toStringTag, { value: "Module" }))
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
const
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
tweak: "a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac",
|
|
1690
|
-
parity: 1,
|
|
1691
|
-
result: "e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf"
|
|
1692
|
-
},
|
|
1693
|
-
{
|
|
1694
|
-
pubkey: "2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
|
|
1695
|
-
tweak: "823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47",
|
|
1696
|
-
parity: 0,
|
|
1697
|
-
result: "9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c"
|
|
1698
|
-
}
|
|
1699
|
-
], { typeforce: un } = ie;
|
|
1700
|
-
function fn(t, e) {
|
|
1701
|
-
if (typeof t != "number")
|
|
1702
|
-
throw new Error("cannot write a non-number as a number");
|
|
1703
|
-
if (t < 0)
|
|
1704
|
-
throw new Error("specified a negative value for writing an unsigned value");
|
|
1705
|
-
if (t > e)
|
|
1706
|
-
throw new Error("RangeError: value out of range");
|
|
1707
|
-
if (Math.floor(t) !== t)
|
|
1708
|
-
throw new Error("value has a fractional component");
|
|
1709
|
-
}
|
|
1710
|
-
function Ms(t, e) {
|
|
1711
|
-
const r = t.readUInt32LE(e);
|
|
1712
|
-
let n = t.readUInt32LE(e + 4);
|
|
1713
|
-
return n *= 4294967296, fn(n + r, 9007199254740991), n + r;
|
|
1714
|
-
}
|
|
1715
|
-
function Ds(t, e, r) {
|
|
1716
|
-
return fn(e, 9007199254740991), t.writeInt32LE(e & -1, r), t.writeUInt32LE(Math.floor(e / 4294967296), r + 4), r + 8;
|
|
1717
|
-
}
|
|
1718
|
-
function Ke(t) {
|
|
1719
|
-
if (t.length < 1)
|
|
1720
|
-
return t;
|
|
1721
|
-
let e = t.length - 1, r = 0;
|
|
1722
|
-
for (let n = 0; n < t.length / 2; n++)
|
|
1723
|
-
r = t[n], t[n] = t[e], t[e] = r, e--;
|
|
1724
|
-
return t;
|
|
1725
|
-
}
|
|
1726
|
-
function tr(t) {
|
|
1727
|
-
const e = h.allocUnsafe(t.length);
|
|
1728
|
-
return t.copy(e), e;
|
|
1729
|
-
}
|
|
1730
|
-
class x {
|
|
1731
|
-
constructor(e, r = 0) {
|
|
1732
|
-
this.buffer = e, this.offset = r, un(z(V, Y), [e, r]);
|
|
1643
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
1644
|
+
const Us = {
|
|
1645
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
|
|
1646
|
+
n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
|
|
1647
|
+
b: 7n,
|
|
1648
|
+
Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
|
|
1649
|
+
Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n
|
|
1650
|
+
}, { p: ct, n: fn, Gx: ks, Gy: Ns, b: cn } = Us, Xt = 32, Oe = 64, G = (t = "") => {
|
|
1651
|
+
throw new Error(t);
|
|
1652
|
+
}, an = (t) => typeof t == "bigint", hn = (t) => typeof t == "string", Cs = (t) => t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array", Ct = (t, e) => !Cs(t) || typeof e == "number" && e > 0 && t.length !== e ? G("Uint8Array expected") : t, pn = (t) => new Uint8Array(t), Bs = (t) => Uint8Array.from(t), ln = (t, e) => t.toString(16).padStart(e, "0"), dn = (t) => Array.from(Ct(t)).map((e) => ln(e, 2)).join(""), J = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, sr = (t) => {
|
|
1653
|
+
if (t >= J._0 && t <= J._9)
|
|
1654
|
+
return t - J._0;
|
|
1655
|
+
if (t >= J.A && t <= J.F)
|
|
1656
|
+
return t - (J.A - 10);
|
|
1657
|
+
if (t >= J.a && t <= J.f)
|
|
1658
|
+
return t - (J.a - 10);
|
|
1659
|
+
}, wn = (t) => {
|
|
1660
|
+
const e = "hex invalid";
|
|
1661
|
+
if (!hn(t))
|
|
1662
|
+
return G(e);
|
|
1663
|
+
const r = t.length, n = r / 2;
|
|
1664
|
+
if (r % 2)
|
|
1665
|
+
return G(e);
|
|
1666
|
+
const s = pn(n);
|
|
1667
|
+
for (let i = 0, o = 0; i < n; i++, o += 2) {
|
|
1668
|
+
const u = sr(t.charCodeAt(o)), f = sr(t.charCodeAt(o + 1));
|
|
1669
|
+
if (u === void 0 || f === void 0)
|
|
1670
|
+
return G(e);
|
|
1671
|
+
s[i] = u * 16 + f;
|
|
1733
1672
|
}
|
|
1734
|
-
|
|
1735
|
-
|
|
1673
|
+
return s;
|
|
1674
|
+
}, gn = (t, e) => Ct(hn(t) ? wn(t) : Bs(Ct(t)), e), ir = (...t) => {
|
|
1675
|
+
const e = pn(t.reduce((n, s) => n + Ct(s).length, 0));
|
|
1676
|
+
let r = 0;
|
|
1677
|
+
return t.forEach((n) => {
|
|
1678
|
+
e.set(n, r), r += n.length;
|
|
1679
|
+
}), e;
|
|
1680
|
+
}, Qt = BigInt, Ft = (t, e, r, n = "bad number: out of range") => an(t) && e <= t && t < r ? t : G(n), m = (t, e = ct) => {
|
|
1681
|
+
const r = t % e;
|
|
1682
|
+
return r >= 0n ? r : e + r;
|
|
1683
|
+
}, Rs = (t, e) => {
|
|
1684
|
+
(t === 0n || e <= 0n) && G("no inverse n=" + t + " mod=" + e);
|
|
1685
|
+
let r = m(t, e), n = e, s = 0n, i = 1n;
|
|
1686
|
+
for (; r !== 0n; ) {
|
|
1687
|
+
const o = n / r, u = n % r, f = s - i * o;
|
|
1688
|
+
n = r, r = u, s = i, i = f;
|
|
1736
1689
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1690
|
+
return n === 1n ? m(s, e) : G("no inverse");
|
|
1691
|
+
}, or = (t) => t instanceof R ? t : G("Point expected"), yn = (t) => m(m(t * t) * t + cn), ur = (t) => Ft(t, 0n, ct), jt = (t) => Ft(t, 1n, ct), Fs = (t) => Ft(t, 1n, fn), Ae = (t) => (t & 1n) === 0n, mn = (t) => Uint8Array.of(t), Ls = (t) => mn(Ae(t) ? 2 : 3), Vs = (t) => {
|
|
1692
|
+
const e = yn(jt(t));
|
|
1693
|
+
let r = 1n;
|
|
1694
|
+
for (let n = e, s = (ct + 1n) / 4n; s > 0n; s >>= 1n)
|
|
1695
|
+
s & 1n && (r = r * n % ct), n = n * n % ct;
|
|
1696
|
+
return m(r * r) === e ? r : G("sqrt invalid");
|
|
1697
|
+
};
|
|
1698
|
+
class R {
|
|
1699
|
+
static BASE;
|
|
1700
|
+
static ZERO;
|
|
1701
|
+
px;
|
|
1702
|
+
py;
|
|
1703
|
+
pz;
|
|
1704
|
+
constructor(e, r, n) {
|
|
1705
|
+
this.px = ur(e), this.py = jt(r), this.pz = ur(n), Object.freeze(this);
|
|
1739
1706
|
}
|
|
1740
|
-
|
|
1741
|
-
|
|
1707
|
+
/** Convert Uint8Array or hex string to Point. */
|
|
1708
|
+
static fromBytes(e) {
|
|
1709
|
+
Ct(e);
|
|
1710
|
+
let r;
|
|
1711
|
+
const n = e[0], s = e.subarray(1), i = fr(s, 0, Xt), o = e.length;
|
|
1712
|
+
if (o === Xt + 1 && [2, 3].includes(n)) {
|
|
1713
|
+
let u = Vs(i);
|
|
1714
|
+
const f = Ae(u);
|
|
1715
|
+
Ae(Qt(n)) !== f && (u = m(-u)), r = new R(i, u, 1n);
|
|
1716
|
+
}
|
|
1717
|
+
return o === Oe + 1 && n === 4 && (r = new R(i, fr(s, Xt, Oe), 1n)), r ? r.assertValidity() : G("bad point: not on curve");
|
|
1742
1718
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1719
|
+
/** Equality check: compare points P&Q. */
|
|
1720
|
+
equals(e) {
|
|
1721
|
+
const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = or(e), f = m(r * u), a = m(i * s), p = m(n * u), l = m(o * s);
|
|
1722
|
+
return f === a && p === l;
|
|
1745
1723
|
}
|
|
1746
|
-
|
|
1747
|
-
this.
|
|
1724
|
+
is0() {
|
|
1725
|
+
return this.equals(ut);
|
|
1748
1726
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
this.
|
|
1727
|
+
/** Flip point over y coordinate. */
|
|
1728
|
+
negate() {
|
|
1729
|
+
return new R(this.px, m(-this.py), this.pz);
|
|
1752
1730
|
}
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
this.offset += e.copy(this.buffer, this.offset);
|
|
1731
|
+
/** Point doubling: P+P, complete formula. */
|
|
1732
|
+
double() {
|
|
1733
|
+
return this.add(this);
|
|
1757
1734
|
}
|
|
1758
|
-
|
|
1759
|
-
|
|
1735
|
+
/**
|
|
1736
|
+
* Point addition: P+Q, complete, exception-free formula
|
|
1737
|
+
* (Renes-Costello-Batina, algo 1 of [2015/1060](https://eprint.iacr.org/2015/1060)).
|
|
1738
|
+
* Cost: `12M + 0S + 3*a + 3*b3 + 23add`.
|
|
1739
|
+
*/
|
|
1740
|
+
// prettier-ignore
|
|
1741
|
+
add(e) {
|
|
1742
|
+
const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = or(e), f = 0n, a = cn;
|
|
1743
|
+
let p = 0n, l = 0n, d = 0n;
|
|
1744
|
+
const b = m(a * 3n);
|
|
1745
|
+
let w = m(r * i), S = m(n * o), P = m(s * u), k = m(r + n), H = m(i + o);
|
|
1746
|
+
k = m(k * H), H = m(w + S), k = m(k - H), H = m(r + s);
|
|
1747
|
+
let _ = m(i + u);
|
|
1748
|
+
return H = m(H * _), _ = m(w + P), H = m(H - _), _ = m(n + s), p = m(o + u), _ = m(_ * p), p = m(S + P), _ = m(_ - p), d = m(f * H), p = m(b * P), d = m(p + d), p = m(S - d), d = m(S + d), l = m(p * d), S = m(w + w), S = m(S + w), P = m(f * P), H = m(b * H), S = m(S + P), P = m(w - P), P = m(f * P), H = m(H + P), w = m(S * H), l = m(l + w), w = m(_ * H), p = m(k * p), p = m(p - w), w = m(k * S), d = m(_ * d), d = m(d + w), new R(p, l, d);
|
|
1760
1749
|
}
|
|
1761
|
-
|
|
1762
|
-
|
|
1750
|
+
/**
|
|
1751
|
+
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
|
|
1752
|
+
* Uses {@link wNAF} for base point.
|
|
1753
|
+
* Uses fake point to mitigate side-channel leakage.
|
|
1754
|
+
* @param n scalar by which point is multiplied
|
|
1755
|
+
* @param safe safe mode guards against timing attacks; unsafe mode is faster
|
|
1756
|
+
*/
|
|
1757
|
+
multiply(e, r = !0) {
|
|
1758
|
+
if (!r && e === 0n)
|
|
1759
|
+
return ut;
|
|
1760
|
+
if (Fs(e), e === 1n)
|
|
1761
|
+
return this;
|
|
1762
|
+
if (this.equals(wt))
|
|
1763
|
+
return Ds(e).p;
|
|
1764
|
+
let n = ut, s = wt;
|
|
1765
|
+
for (let i = this; e > 0n; i = i.double(), e >>= 1n)
|
|
1766
|
+
e & 1n ? n = n.add(i) : r && (s = s.add(i));
|
|
1767
|
+
return n;
|
|
1763
1768
|
}
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1769
|
+
/** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
|
|
1770
|
+
toAffine() {
|
|
1771
|
+
const { px: e, py: r, pz: n } = this;
|
|
1772
|
+
if (this.equals(ut))
|
|
1773
|
+
return { x: 0n, y: 0n };
|
|
1774
|
+
if (n === 1n)
|
|
1775
|
+
return { x: e, y: r };
|
|
1776
|
+
const s = Rs(n, ct);
|
|
1777
|
+
return m(n * s) !== 1n && G("inverse invalid"), { x: m(e * s), y: m(r * s) };
|
|
1768
1778
|
}
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1779
|
+
/** Checks if the point is valid and on-curve. */
|
|
1780
|
+
assertValidity() {
|
|
1781
|
+
const { x: e, y: r } = this.toAffine();
|
|
1782
|
+
return jt(e), jt(r), m(r * r) === yn(e) ? this : G("bad point: not on curve");
|
|
1773
1783
|
}
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1784
|
+
/** Converts point to 33/65-byte Uint8Array. */
|
|
1785
|
+
toBytes(e = !0) {
|
|
1786
|
+
const { x: r, y: n } = this.assertValidity().toAffine(), s = cr(r);
|
|
1787
|
+
return e ? ir(Ls(n), s) : ir(mn(4), s, cr(n));
|
|
1777
1788
|
}
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1789
|
+
/** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */
|
|
1790
|
+
static fromAffine(e) {
|
|
1791
|
+
const { x: r, y: n } = e;
|
|
1792
|
+
return r === 0n && n === 0n ? ut : new R(r, n, 1n);
|
|
1781
1793
|
}
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
return this.offset += 4, e;
|
|
1794
|
+
toHex(e) {
|
|
1795
|
+
return dn(this.toBytes(e));
|
|
1785
1796
|
}
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
return this.offset += 8, e;
|
|
1797
|
+
static fromPrivateKey(e) {
|
|
1798
|
+
return wt.multiply(Gs(e));
|
|
1789
1799
|
}
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
return this.offset += e.bytes, e.numberValue || 0;
|
|
1800
|
+
static fromHex(e) {
|
|
1801
|
+
return R.fromBytes(gn(e));
|
|
1793
1802
|
}
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
throw new Error("Cannot read slice out of bounds");
|
|
1797
|
-
const r = h.from(this.buffer.subarray(this.offset, this.offset + e));
|
|
1798
|
-
return this.offset += e, r;
|
|
1803
|
+
get x() {
|
|
1804
|
+
return this.toAffine().x;
|
|
1799
1805
|
}
|
|
1800
|
-
|
|
1801
|
-
return this.
|
|
1806
|
+
get y() {
|
|
1807
|
+
return this.toAffine().y;
|
|
1802
1808
|
}
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
for (let n = 0; n < e; n++)
|
|
1806
|
-
r.push(this.readVarSlice());
|
|
1807
|
-
return r;
|
|
1809
|
+
toRawBytes(e) {
|
|
1810
|
+
return this.toBytes(e);
|
|
1808
1811
|
}
|
|
1809
1812
|
}
|
|
1810
|
-
const
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
const
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1813
|
+
const wt = new R(ks, Ns, 1n), ut = new R(0n, 1n, 0n);
|
|
1814
|
+
R.BASE = wt;
|
|
1815
|
+
R.ZERO = ut;
|
|
1816
|
+
const bn = (t) => Qt("0x" + (dn(t) || "0")), fr = (t, e, r) => bn(t.subarray(e, r)), Ws = 2n ** 256n, cr = (t) => wn(ln(Ft(t, 0n, Ws), Oe)), Gs = (t) => {
|
|
1817
|
+
const e = an(t) ? t : bn(gn(t, Xt));
|
|
1818
|
+
return Ft(e, 1n, fn, "private key invalid 3");
|
|
1819
|
+
}, Jt = 8, Ks = 256, En = Math.ceil(Ks / Jt) + 1, Pe = 2 ** (Jt - 1), Ms = () => {
|
|
1820
|
+
const t = [];
|
|
1821
|
+
let e = wt, r = e;
|
|
1822
|
+
for (let n = 0; n < En; n++) {
|
|
1823
|
+
r = e, t.push(r);
|
|
1824
|
+
for (let s = 1; s < Pe; s++)
|
|
1825
|
+
r = r.add(e), t.push(r);
|
|
1826
|
+
e = r.double();
|
|
1819
1827
|
}
|
|
1820
|
-
return
|
|
1821
|
-
}
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
const
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
}
|
|
1845
|
-
function Z(t) {
|
|
1846
|
-
const e = t.version || Me;
|
|
1847
|
-
return Rt("TapLeaf", U.concat([U.from([e]), Xs(t.output)]));
|
|
1848
|
-
}
|
|
1849
|
-
function an(t, e) {
|
|
1850
|
-
return Rt("TapTweak", U.concat(e ? [t, e] : [t]));
|
|
1851
|
-
}
|
|
1852
|
-
function At(t, e) {
|
|
1853
|
-
if (!U.isBuffer(t) || t.length !== 32 || e && e.length !== 32)
|
|
1854
|
-
return null;
|
|
1855
|
-
const r = an(t, e), n = Ae().xOnlyPointAddTweak(t, r);
|
|
1856
|
-
return !n || n.xOnlyPubkey === null ? null : {
|
|
1857
|
-
parity: n.parity,
|
|
1858
|
-
x: U.from(n.xOnlyPubkey)
|
|
1859
|
-
};
|
|
1860
|
-
}
|
|
1861
|
-
function Pe(t, e) {
|
|
1862
|
-
return Rt("TapBranch", U.concat([t, e]));
|
|
1863
|
-
}
|
|
1864
|
-
function Xs(t) {
|
|
1865
|
-
const e = kt(t.length), r = U.allocUnsafe(e);
|
|
1866
|
-
return Br(t.length, r), U.concat([r, t]);
|
|
1867
|
-
}
|
|
1868
|
-
function y(t, e, r) {
|
|
1869
|
-
Object.defineProperty(t, e, {
|
|
1870
|
-
configurable: !0,
|
|
1871
|
-
enumerable: !0,
|
|
1872
|
-
get() {
|
|
1873
|
-
const n = r.call(this);
|
|
1874
|
-
return this[e] = n, n;
|
|
1875
|
-
},
|
|
1876
|
-
set(n) {
|
|
1877
|
-
Object.defineProperty(this, e, {
|
|
1878
|
-
configurable: !0,
|
|
1879
|
-
enumerable: !0,
|
|
1880
|
-
value: n,
|
|
1881
|
-
writable: !0
|
|
1882
|
-
});
|
|
1883
|
-
}
|
|
1884
|
-
});
|
|
1828
|
+
return t;
|
|
1829
|
+
};
|
|
1830
|
+
let ar;
|
|
1831
|
+
const hr = (t, e) => {
|
|
1832
|
+
const r = e.negate();
|
|
1833
|
+
return t ? r : e;
|
|
1834
|
+
}, Ds = (t) => {
|
|
1835
|
+
const e = ar || (ar = Ms());
|
|
1836
|
+
let r = ut, n = wt;
|
|
1837
|
+
const s = 2 ** Jt, i = s, o = Qt(s - 1), u = Qt(Jt);
|
|
1838
|
+
for (let f = 0; f < En; f++) {
|
|
1839
|
+
let a = Number(t & o);
|
|
1840
|
+
t >>= u, a > Pe && (a -= i, t += 1n);
|
|
1841
|
+
const p = f * Pe, l = p, d = p + Math.abs(a) - 1, b = f % 2 !== 0, w = a < 0;
|
|
1842
|
+
a === 0 ? n = n.add(hr(b, e[l])) : r = r.add(hr(w, e[d]));
|
|
1843
|
+
}
|
|
1844
|
+
return { p: r, f: n };
|
|
1845
|
+
}, Ht = (t) => {
|
|
1846
|
+
const e = t.length === 32 ? t : t.subarray(1, 33);
|
|
1847
|
+
return h.isBuffer(e) ? e : h.from(e);
|
|
1848
|
+
};
|
|
1849
|
+
function pr(t) {
|
|
1850
|
+
let e = t.toString(16);
|
|
1851
|
+
return e = e.padStart(64, "0"), e.length > 64 && (e = e.slice(-64)), h.from(e, "hex");
|
|
1885
1852
|
}
|
|
1886
|
-
function
|
|
1853
|
+
function Ie(t) {
|
|
1854
|
+
if (t.length === 32)
|
|
1855
|
+
return;
|
|
1856
|
+
if (![33, 65].includes(t.length)) {
|
|
1857
|
+
console.warn(`Unsupported key length=${t.length}. Must be 33 (compressed) or 65 (uncompressed).`);
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1887
1860
|
let e;
|
|
1888
|
-
|
|
1861
|
+
try {
|
|
1862
|
+
e = R.fromHex(t);
|
|
1863
|
+
} catch {
|
|
1864
|
+
throw new Error("Invalid secp256k1 public key bytes. Cannot parse.");
|
|
1865
|
+
}
|
|
1866
|
+
const r = pr(e.x), n = pr(e.y), i = e.y % 2n === 0n ? 6 : 7, o = h.alloc(65);
|
|
1867
|
+
o[0] = i, r.copy(o, 1), n.copy(o, 33);
|
|
1868
|
+
const u = h.concat([h.from([4]), r, n]);
|
|
1869
|
+
return {
|
|
1870
|
+
hybrid: o,
|
|
1871
|
+
uncompressed: u
|
|
1872
|
+
};
|
|
1889
1873
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
output: c.maybe(c.Buffer),
|
|
1897
|
-
data: c.maybe(c.arrayOf(c.Buffer))
|
|
1898
|
-
}, t);
|
|
1899
|
-
const r = t.network || M, n = { name: K.Embed, network: r, data: [] };
|
|
1900
|
-
if (y(n, "output", () => {
|
|
1901
|
-
if (t.data)
|
|
1902
|
-
return v([er.OP_RETURN].concat(t.data));
|
|
1903
|
-
}), y(n, "data", () => {
|
|
1904
|
-
if (!t.output)
|
|
1905
|
-
return;
|
|
1906
|
-
const s = A(t.output);
|
|
1907
|
-
if (s != null)
|
|
1908
|
-
return s.slice(1);
|
|
1909
|
-
}), e.validate && t.output) {
|
|
1910
|
-
const s = A(t.output);
|
|
1911
|
-
if (s[0] !== er.OP_RETURN)
|
|
1912
|
-
throw new TypeError("Output is invalid");
|
|
1913
|
-
if (!s.slice(1).every(c.Buffer))
|
|
1914
|
-
throw new TypeError("Output is invalid");
|
|
1915
|
-
if (t.data && !at(t.data, n.data))
|
|
1916
|
-
throw new TypeError("Data mismatch");
|
|
1874
|
+
function Gt(t, e) {
|
|
1875
|
+
if (t.equals(e))
|
|
1876
|
+
return !0;
|
|
1877
|
+
if (t.length === 65 && e.length === 65) {
|
|
1878
|
+
const r = h.from(t), n = h.from(e);
|
|
1879
|
+
return (r[0] === 6 || r[0] === 7) && (r[0] = 4), (n[0] === 6 || n[0] === 7) && (n[0] = 4), r.equals(n);
|
|
1917
1880
|
}
|
|
1918
|
-
return
|
|
1881
|
+
return !1;
|
|
1919
1882
|
}
|
|
1920
|
-
const
|
|
1921
|
-
function
|
|
1922
|
-
if (!t.
|
|
1883
|
+
const st = E;
|
|
1884
|
+
function yt(t, e) {
|
|
1885
|
+
if (!t.address && !t.hash && !t.output && !t.pubkey && !t.input)
|
|
1923
1886
|
throw new TypeError("Not enough data");
|
|
1924
|
-
e = Object.assign({ validate: !0 }, e || {})
|
|
1925
|
-
function r(f) {
|
|
1926
|
-
return Q(f) || (e.allowIncomplete && f === lt.OP_0) !== void 0;
|
|
1927
|
-
}
|
|
1928
|
-
c({
|
|
1887
|
+
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
1929
1888
|
network: c.maybe(c.Object),
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
output: c.maybe(c.
|
|
1933
|
-
|
|
1934
|
-
|
|
1889
|
+
address: c.maybe(c.String),
|
|
1890
|
+
hash: c.maybe(c.BufferN(20)),
|
|
1891
|
+
output: c.maybe(c.BufferN(25)),
|
|
1892
|
+
pubkey: c.maybe(W),
|
|
1893
|
+
signature: c.maybe(Q),
|
|
1935
1894
|
input: c.maybe(c.Buffer)
|
|
1936
1895
|
}, t);
|
|
1937
|
-
const
|
|
1938
|
-
|
|
1939
|
-
|
|
1896
|
+
const r = B(() => {
|
|
1897
|
+
const o = h.from(Ut.decode(t.address)), u = o.readUInt8(0), f = o.subarray(1);
|
|
1898
|
+
return { version: u, hash: f };
|
|
1899
|
+
}), n = B(() => A(t.input)), s = t.network || M, i = {
|
|
1900
|
+
name: K.P2PKH,
|
|
1901
|
+
network: s,
|
|
1902
|
+
hash: void 0
|
|
1940
1903
|
};
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
return v([].concat(Gt + t.m, t.pubkeys, Gt + s.n, lt.OP_CHECKMULTISIG));
|
|
1948
|
-
}), y(s, "m", () => {
|
|
1949
|
-
if (s.output)
|
|
1950
|
-
return u(s.output), s.m;
|
|
1951
|
-
}), y(s, "n", () => {
|
|
1952
|
-
if (s.pubkeys)
|
|
1953
|
-
return s.pubkeys.length;
|
|
1954
|
-
}), y(s, "pubkeys", () => {
|
|
1904
|
+
if (y(i, "address", () => {
|
|
1905
|
+
if (!i.hash)
|
|
1906
|
+
return;
|
|
1907
|
+
const o = h.allocUnsafe(21);
|
|
1908
|
+
return o.writeUInt8(s.pubKeyHash, 0), i.hash.copy(o, 1), Ut.encode(o);
|
|
1909
|
+
}), y(i, "hash", () => {
|
|
1955
1910
|
if (t.output)
|
|
1956
|
-
return
|
|
1957
|
-
|
|
1958
|
-
|
|
1911
|
+
return t.output.subarray(3, 23);
|
|
1912
|
+
if (t.address)
|
|
1913
|
+
return r().hash;
|
|
1914
|
+
if (t.pubkey || i.pubkey)
|
|
1915
|
+
return C(t.pubkey || i.pubkey);
|
|
1916
|
+
}), y(i, "output", () => {
|
|
1917
|
+
if (i.hash)
|
|
1918
|
+
return v([
|
|
1919
|
+
st.OP_DUP,
|
|
1920
|
+
st.OP_HASH160,
|
|
1921
|
+
i.hash,
|
|
1922
|
+
st.OP_EQUALVERIFY,
|
|
1923
|
+
st.OP_CHECKSIG
|
|
1924
|
+
]);
|
|
1925
|
+
}), y(i, "pubkey", () => {
|
|
1926
|
+
if (t.input)
|
|
1927
|
+
return n()[1];
|
|
1928
|
+
}), y(i, "signature", () => {
|
|
1929
|
+
if (t.input)
|
|
1930
|
+
return n()[0];
|
|
1931
|
+
}), y(i, "input", () => {
|
|
1932
|
+
if (!t.pubkey || !t.signature)
|
|
1959
1933
|
return;
|
|
1960
|
-
|
|
1961
|
-
if (
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
}), y(
|
|
1967
|
-
if (
|
|
1934
|
+
let o = t.pubkey;
|
|
1935
|
+
if (t.useHybrid || t.useUncompressed) {
|
|
1936
|
+
const u = Ie(t.pubkey);
|
|
1937
|
+
u && (t.useUncompressed ? o = u.uncompressed : o = u.hybrid);
|
|
1938
|
+
}
|
|
1939
|
+
return v([t.signature, o]);
|
|
1940
|
+
}), y(i, "witness", () => {
|
|
1941
|
+
if (i.input)
|
|
1968
1942
|
return [];
|
|
1969
|
-
}), y(s, "name", () => {
|
|
1970
|
-
if (!(!s.m || !s.n))
|
|
1971
|
-
return `p2ms(${s.m} of ${s.n})`;
|
|
1972
1943
|
}), e.validate) {
|
|
1944
|
+
let o = h.from([]);
|
|
1945
|
+
if (t.address) {
|
|
1946
|
+
if (r().version !== s.pubKeyHash)
|
|
1947
|
+
throw new TypeError("Invalid version or Network mismatch");
|
|
1948
|
+
if (r().hash.length !== 20)
|
|
1949
|
+
throw new TypeError("Invalid address");
|
|
1950
|
+
o = r().hash;
|
|
1951
|
+
}
|
|
1952
|
+
if (t.hash) {
|
|
1953
|
+
if (o.length > 0 && !o.equals(t.hash))
|
|
1954
|
+
throw new TypeError("Hash mismatch");
|
|
1955
|
+
o = t.hash;
|
|
1956
|
+
}
|
|
1973
1957
|
if (t.output) {
|
|
1974
|
-
if (
|
|
1975
|
-
throw new TypeError("Output is invalid");
|
|
1976
|
-
if (!c.Number(i[i.length - 2]))
|
|
1977
|
-
throw new TypeError("Output is invalid");
|
|
1978
|
-
if (i[i.length - 1] !== lt.OP_CHECKMULTISIG)
|
|
1979
|
-
throw new TypeError("Output is invalid");
|
|
1980
|
-
if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== i.length - 3)
|
|
1981
|
-
throw new TypeError("Output is invalid");
|
|
1982
|
-
if (!s.pubkeys.every((f) => W(f)))
|
|
1958
|
+
if (t.output.length !== 25 || t.output[0] !== st.OP_DUP || t.output[1] !== st.OP_HASH160 || t.output[2] !== 20 || t.output[23] !== st.OP_EQUALVERIFY || t.output[24] !== st.OP_CHECKSIG)
|
|
1983
1959
|
throw new TypeError("Output is invalid");
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
if (t.pubkeys && !at(t.pubkeys, s.pubkeys))
|
|
1989
|
-
throw new TypeError("Pubkeys mismatch");
|
|
1960
|
+
const u = t.output.subarray(3, 23);
|
|
1961
|
+
if (o.length > 0 && !o.equals(u))
|
|
1962
|
+
throw new TypeError("Hash mismatch");
|
|
1963
|
+
o = u;
|
|
1990
1964
|
}
|
|
1991
|
-
if (t.
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
if (
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
1965
|
+
if (t.pubkey) {
|
|
1966
|
+
const u = C(t.pubkey);
|
|
1967
|
+
let f = o.length > 0 && !o.equals(u);
|
|
1968
|
+
if (f && (t.pubkey.length === 33 && (t.pubkey[0] === 2 || t.pubkey[0] === 3) || t.pubkey.length === 65 && t.pubkey[0] === 4)) {
|
|
1969
|
+
const a = Ie(t.pubkey);
|
|
1970
|
+
if (a) {
|
|
1971
|
+
const p = C(a.uncompressed);
|
|
1972
|
+
if (o.equals(p))
|
|
1973
|
+
f = !1, t.useUncompressed = !0;
|
|
1974
|
+
else {
|
|
1975
|
+
const l = C(a.hybrid);
|
|
1976
|
+
f = !o.equals(l), f || (t.useHybrid = !0);
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
if (f)
|
|
1981
|
+
throw new TypeError("Hash mismatch");
|
|
1982
|
+
o = u;
|
|
2002
1983
|
}
|
|
2003
1984
|
if (t.input) {
|
|
2004
|
-
|
|
1985
|
+
const u = n();
|
|
1986
|
+
if (u.length !== 2)
|
|
2005
1987
|
throw new TypeError("Input is invalid");
|
|
2006
|
-
if (
|
|
2007
|
-
throw new TypeError("Input has invalid signature
|
|
2008
|
-
if (
|
|
1988
|
+
if (!Q(u[0]))
|
|
1989
|
+
throw new TypeError("Input has invalid signature");
|
|
1990
|
+
if (!W(u[1]))
|
|
1991
|
+
throw new TypeError("Input has invalid pubkey");
|
|
1992
|
+
if (t.signature && !t.signature.equals(u[0]))
|
|
2009
1993
|
throw new TypeError("Signature mismatch");
|
|
2010
|
-
if (t.
|
|
2011
|
-
throw new TypeError("Signature count mismatch");
|
|
2012
|
-
}
|
|
2013
|
-
}
|
|
2014
|
-
return Object.assign(s, t);
|
|
2015
|
-
}
|
|
2016
|
-
const rr = E;
|
|
2017
|
-
function De(t, e) {
|
|
2018
|
-
if (!t.input && !t.output && !t.pubkey && !t.input && !t.signature)
|
|
2019
|
-
throw new TypeError("Not enough data");
|
|
2020
|
-
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
2021
|
-
network: c.maybe(c.Object),
|
|
2022
|
-
output: c.maybe(c.Buffer),
|
|
2023
|
-
pubkey: c.maybe(W),
|
|
2024
|
-
signature: c.maybe(Q),
|
|
2025
|
-
input: c.maybe(c.Buffer)
|
|
2026
|
-
}, t);
|
|
2027
|
-
const r = B(() => A(t.input)), n = t.network || M, s = {
|
|
2028
|
-
name: K.P2PK,
|
|
2029
|
-
network: n,
|
|
2030
|
-
pubkey: void 0
|
|
2031
|
-
};
|
|
2032
|
-
if (y(s, "output", () => {
|
|
2033
|
-
if (t.pubkey)
|
|
2034
|
-
return v([t.pubkey, rr.OP_CHECKSIG]);
|
|
2035
|
-
}), y(s, "pubkey", () => {
|
|
2036
|
-
if (t.output)
|
|
2037
|
-
return t.output.subarray(1, -1);
|
|
2038
|
-
}), y(s, "signature", () => {
|
|
2039
|
-
if (t.input)
|
|
2040
|
-
return r()[0];
|
|
2041
|
-
}), y(s, "input", () => {
|
|
2042
|
-
if (t.signature)
|
|
2043
|
-
return v([t.signature]);
|
|
2044
|
-
}), y(s, "witness", () => {
|
|
2045
|
-
if (s.input)
|
|
2046
|
-
return [];
|
|
2047
|
-
}), e.validate) {
|
|
2048
|
-
if (t.output) {
|
|
2049
|
-
if (t.output[t.output.length - 1] !== rr.OP_CHECKSIG)
|
|
2050
|
-
throw new TypeError("Output is invalid");
|
|
2051
|
-
if (!W(s.pubkey))
|
|
2052
|
-
throw new TypeError("Output pubkey is invalid");
|
|
2053
|
-
if (t.pubkey && !t.pubkey.equals(s.pubkey))
|
|
1994
|
+
if (t.pubkey && !t.pubkey.equals(u[1]))
|
|
2054
1995
|
throw new TypeError("Pubkey mismatch");
|
|
1996
|
+
const f = C(u[1]);
|
|
1997
|
+
if (o.length > 0 && !o.equals(f))
|
|
1998
|
+
throw new TypeError("Hash mismatch (input)");
|
|
2055
1999
|
}
|
|
2056
|
-
if (t.signature && t.input && !t.input.equals(s.input))
|
|
2057
|
-
throw new TypeError("Signature mismatch");
|
|
2058
|
-
if (t.input) {
|
|
2059
|
-
if (r().length !== 1)
|
|
2060
|
-
throw new TypeError("Input is invalid");
|
|
2061
|
-
if (!Q(s.signature))
|
|
2062
|
-
throw new TypeError("Input has invalid signature");
|
|
2063
|
-
}
|
|
2064
|
-
}
|
|
2065
|
-
return Object.assign(s, t);
|
|
2066
|
-
}
|
|
2067
|
-
const zs = {
|
|
2068
|
-
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
|
|
2069
|
-
n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
|
|
2070
|
-
b: 7n,
|
|
2071
|
-
Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
|
|
2072
|
-
Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n
|
|
2073
|
-
}, { p: ct, n: hn, Gx: Ys, Gy: Zs, b: pn } = zs, Xt = 32, Ie = 64, G = (t = "") => {
|
|
2074
|
-
throw new Error(t);
|
|
2075
|
-
}, ln = (t) => typeof t == "bigint", dn = (t) => typeof t == "string", Qs = (t) => t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array", Bt = (t, e) => !Qs(t) || typeof e == "number" && e > 0 && t.length !== e ? G("Uint8Array expected") : t, wn = (t) => new Uint8Array(t), Js = (t) => Uint8Array.from(t), gn = (t, e) => t.toString(16).padStart(e, "0"), yn = (t) => Array.from(Bt(t)).map((e) => gn(e, 2)).join(""), tt = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, nr = (t) => {
|
|
2076
|
-
if (t >= tt._0 && t <= tt._9)
|
|
2077
|
-
return t - tt._0;
|
|
2078
|
-
if (t >= tt.A && t <= tt.F)
|
|
2079
|
-
return t - (tt.A - 10);
|
|
2080
|
-
if (t >= tt.a && t <= tt.f)
|
|
2081
|
-
return t - (tt.a - 10);
|
|
2082
|
-
}, mn = (t) => {
|
|
2083
|
-
const e = "hex invalid";
|
|
2084
|
-
if (!dn(t))
|
|
2085
|
-
return G(e);
|
|
2086
|
-
const r = t.length, n = r / 2;
|
|
2087
|
-
if (r % 2)
|
|
2088
|
-
return G(e);
|
|
2089
|
-
const s = wn(n);
|
|
2090
|
-
for (let i = 0, o = 0; i < n; i++, o += 2) {
|
|
2091
|
-
const u = nr(t.charCodeAt(o)), f = nr(t.charCodeAt(o + 1));
|
|
2092
|
-
if (u === void 0 || f === void 0)
|
|
2093
|
-
return G(e);
|
|
2094
|
-
s[i] = u * 16 + f;
|
|
2095
|
-
}
|
|
2096
|
-
return s;
|
|
2097
|
-
}, bn = (t, e) => Bt(dn(t) ? mn(t) : Js(Bt(t)), e), sr = (...t) => {
|
|
2098
|
-
const e = wn(t.reduce((n, s) => n + Bt(s).length, 0));
|
|
2099
|
-
let r = 0;
|
|
2100
|
-
return t.forEach((n) => {
|
|
2101
|
-
e.set(n, r), r += n.length;
|
|
2102
|
-
}), e;
|
|
2103
|
-
}, te = BigInt, Lt = (t, e, r, n = "bad number: out of range") => ln(t) && e <= t && t < r ? t : G(n), m = (t, e = ct) => {
|
|
2104
|
-
const r = t % e;
|
|
2105
|
-
return r >= 0n ? r : e + r;
|
|
2106
|
-
}, ti = (t, e) => {
|
|
2107
|
-
(t === 0n || e <= 0n) && G("no inverse n=" + t + " mod=" + e);
|
|
2108
|
-
let r = m(t, e), n = e, s = 0n, i = 1n;
|
|
2109
|
-
for (; r !== 0n; ) {
|
|
2110
|
-
const o = n / r, u = n % r, f = s - i * o;
|
|
2111
|
-
n = r, r = u, s = i, i = f;
|
|
2112
|
-
}
|
|
2113
|
-
return n === 1n ? m(s, e) : G("no inverse");
|
|
2114
|
-
}, ir = (t) => t instanceof R ? t : G("Point expected"), En = (t) => m(m(t * t) * t + pn), or = (t) => Lt(t, 0n, ct), jt = (t) => Lt(t, 1n, ct), ei = (t) => Lt(t, 1n, hn), He = (t) => (t & 1n) === 0n, Sn = (t) => Uint8Array.of(t), ri = (t) => Sn(He(t) ? 2 : 3), ni = (t) => {
|
|
2115
|
-
const e = En(jt(t));
|
|
2116
|
-
let r = 1n;
|
|
2117
|
-
for (let n = e, s = (ct + 1n) / 4n; s > 0n; s >>= 1n)
|
|
2118
|
-
s & 1n && (r = r * n % ct), n = n * n % ct;
|
|
2119
|
-
return m(r * r) === e ? r : G("sqrt invalid");
|
|
2120
|
-
};
|
|
2121
|
-
class R {
|
|
2122
|
-
static BASE;
|
|
2123
|
-
static ZERO;
|
|
2124
|
-
px;
|
|
2125
|
-
py;
|
|
2126
|
-
pz;
|
|
2127
|
-
constructor(e, r, n) {
|
|
2128
|
-
this.px = or(e), this.py = jt(r), this.pz = or(n), Object.freeze(this);
|
|
2129
|
-
}
|
|
2130
|
-
/** Convert Uint8Array or hex string to Point. */
|
|
2131
|
-
static fromBytes(e) {
|
|
2132
|
-
Bt(e);
|
|
2133
|
-
let r;
|
|
2134
|
-
const n = e[0], s = e.subarray(1), i = ur(s, 0, Xt), o = e.length;
|
|
2135
|
-
if (o === Xt + 1 && [2, 3].includes(n)) {
|
|
2136
|
-
let u = ni(i);
|
|
2137
|
-
const f = He(u);
|
|
2138
|
-
He(te(n)) !== f && (u = m(-u)), r = new R(i, u, 1n);
|
|
2139
|
-
}
|
|
2140
|
-
return o === Ie + 1 && n === 4 && (r = new R(i, ur(s, Xt, Ie), 1n)), r ? r.assertValidity() : G("bad point: not on curve");
|
|
2141
|
-
}
|
|
2142
|
-
/** Equality check: compare points P&Q. */
|
|
2143
|
-
equals(e) {
|
|
2144
|
-
const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = ir(e), f = m(r * u), a = m(i * s), p = m(n * u), l = m(o * s);
|
|
2145
|
-
return f === a && p === l;
|
|
2146
|
-
}
|
|
2147
|
-
is0() {
|
|
2148
|
-
return this.equals(ut);
|
|
2149
|
-
}
|
|
2150
|
-
/** Flip point over y coordinate. */
|
|
2151
|
-
negate() {
|
|
2152
|
-
return new R(this.px, m(-this.py), this.pz);
|
|
2153
|
-
}
|
|
2154
|
-
/** Point doubling: P+P, complete formula. */
|
|
2155
|
-
double() {
|
|
2156
|
-
return this.add(this);
|
|
2157
|
-
}
|
|
2158
|
-
/**
|
|
2159
|
-
* Point addition: P+Q, complete, exception-free formula
|
|
2160
|
-
* (Renes-Costello-Batina, algo 1 of [2015/1060](https://eprint.iacr.org/2015/1060)).
|
|
2161
|
-
* Cost: `12M + 0S + 3*a + 3*b3 + 23add`.
|
|
2162
|
-
*/
|
|
2163
|
-
// prettier-ignore
|
|
2164
|
-
add(e) {
|
|
2165
|
-
const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = ir(e), f = 0n, a = pn;
|
|
2166
|
-
let p = 0n, l = 0n, d = 0n;
|
|
2167
|
-
const b = m(a * 3n);
|
|
2168
|
-
let w = m(r * i), S = m(n * o), P = m(s * u), k = m(r + n), H = m(i + o);
|
|
2169
|
-
k = m(k * H), H = m(w + S), k = m(k - H), H = m(r + s);
|
|
2170
|
-
let _ = m(i + u);
|
|
2171
|
-
return H = m(H * _), _ = m(w + P), H = m(H - _), _ = m(n + s), p = m(o + u), _ = m(_ * p), p = m(S + P), _ = m(_ - p), d = m(f * H), p = m(b * P), d = m(p + d), p = m(S - d), d = m(S + d), l = m(p * d), S = m(w + w), S = m(S + w), P = m(f * P), H = m(b * H), S = m(S + P), P = m(w - P), P = m(f * P), H = m(H + P), w = m(S * H), l = m(l + w), w = m(_ * H), p = m(k * p), p = m(p - w), w = m(k * S), d = m(_ * d), d = m(d + w), new R(p, l, d);
|
|
2172
|
-
}
|
|
2173
|
-
/**
|
|
2174
|
-
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
|
|
2175
|
-
* Uses {@link wNAF} for base point.
|
|
2176
|
-
* Uses fake point to mitigate side-channel leakage.
|
|
2177
|
-
* @param n scalar by which point is multiplied
|
|
2178
|
-
* @param safe safe mode guards against timing attacks; unsafe mode is faster
|
|
2179
|
-
*/
|
|
2180
|
-
multiply(e, r = !0) {
|
|
2181
|
-
if (!r && e === 0n)
|
|
2182
|
-
return ut;
|
|
2183
|
-
if (ei(e), e === 1n)
|
|
2184
|
-
return this;
|
|
2185
|
-
if (this.equals(wt))
|
|
2186
|
-
return fi(e).p;
|
|
2187
|
-
let n = ut, s = wt;
|
|
2188
|
-
for (let i = this; e > 0n; i = i.double(), e >>= 1n)
|
|
2189
|
-
e & 1n ? n = n.add(i) : r && (s = s.add(i));
|
|
2190
|
-
return n;
|
|
2191
|
-
}
|
|
2192
|
-
/** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
|
|
2193
|
-
toAffine() {
|
|
2194
|
-
const { px: e, py: r, pz: n } = this;
|
|
2195
|
-
if (this.equals(ut))
|
|
2196
|
-
return { x: 0n, y: 0n };
|
|
2197
|
-
if (n === 1n)
|
|
2198
|
-
return { x: e, y: r };
|
|
2199
|
-
const s = ti(n, ct);
|
|
2200
|
-
return m(n * s) !== 1n && G("inverse invalid"), { x: m(e * s), y: m(r * s) };
|
|
2201
|
-
}
|
|
2202
|
-
/** Checks if the point is valid and on-curve. */
|
|
2203
|
-
assertValidity() {
|
|
2204
|
-
const { x: e, y: r } = this.toAffine();
|
|
2205
|
-
return jt(e), jt(r), m(r * r) === En(e) ? this : G("bad point: not on curve");
|
|
2206
|
-
}
|
|
2207
|
-
/** Converts point to 33/65-byte Uint8Array. */
|
|
2208
|
-
toBytes(e = !0) {
|
|
2209
|
-
const { x: r, y: n } = this.assertValidity().toAffine(), s = fr(r);
|
|
2210
|
-
return e ? sr(ri(n), s) : sr(Sn(4), s, fr(n));
|
|
2211
|
-
}
|
|
2212
|
-
/** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */
|
|
2213
|
-
static fromAffine(e) {
|
|
2214
|
-
const { x: r, y: n } = e;
|
|
2215
|
-
return r === 0n && n === 0n ? ut : new R(r, n, 1n);
|
|
2216
|
-
}
|
|
2217
|
-
toHex(e) {
|
|
2218
|
-
return yn(this.toBytes(e));
|
|
2219
|
-
}
|
|
2220
|
-
static fromPrivateKey(e) {
|
|
2221
|
-
return wt.multiply(ii(e));
|
|
2222
|
-
}
|
|
2223
|
-
static fromHex(e) {
|
|
2224
|
-
return R.fromBytes(bn(e));
|
|
2225
|
-
}
|
|
2226
|
-
get x() {
|
|
2227
|
-
return this.toAffine().x;
|
|
2228
|
-
}
|
|
2229
|
-
get y() {
|
|
2230
|
-
return this.toAffine().y;
|
|
2231
|
-
}
|
|
2232
|
-
toRawBytes(e) {
|
|
2233
|
-
return this.toBytes(e);
|
|
2234
2000
|
}
|
|
2001
|
+
return Object.assign(i, t);
|
|
2235
2002
|
}
|
|
2236
|
-
const
|
|
2237
|
-
|
|
2238
|
-
R.ZERO = ut;
|
|
2239
|
-
const _n = (t) => te("0x" + (yn(t) || "0")), ur = (t, e, r) => _n(t.subarray(e, r)), si = 2n ** 256n, fr = (t) => mn(gn(Lt(t, 0n, si), Ie)), ii = (t) => {
|
|
2240
|
-
const e = ln(t) ? t : _n(bn(t, Xt));
|
|
2241
|
-
return Lt(e, 1n, hn, "private key invalid 3");
|
|
2242
|
-
}, ee = 8, oi = 256, Tn = Math.ceil(oi / ee) + 1, ve = 2 ** (ee - 1), ui = () => {
|
|
2243
|
-
const t = [];
|
|
2244
|
-
let e = wt, r = e;
|
|
2245
|
-
for (let n = 0; n < Tn; n++) {
|
|
2246
|
-
r = e, t.push(r);
|
|
2247
|
-
for (let s = 1; s < ve; s++)
|
|
2248
|
-
r = r.add(e), t.push(r);
|
|
2249
|
-
e = r.double();
|
|
2250
|
-
}
|
|
2251
|
-
return t;
|
|
2252
|
-
};
|
|
2253
|
-
let cr;
|
|
2254
|
-
const ar = (t, e) => {
|
|
2255
|
-
const r = e.negate();
|
|
2256
|
-
return t ? r : e;
|
|
2257
|
-
}, fi = (t) => {
|
|
2258
|
-
const e = cr || (cr = ui());
|
|
2259
|
-
let r = ut, n = wt;
|
|
2260
|
-
const s = 2 ** ee, i = s, o = te(s - 1), u = te(ee);
|
|
2261
|
-
for (let f = 0; f < Tn; f++) {
|
|
2262
|
-
let a = Number(t & o);
|
|
2263
|
-
t >>= u, a > ve && (a -= i, t += 1n);
|
|
2264
|
-
const p = f * ve, l = p, d = p + Math.abs(a) - 1, b = f % 2 !== 0, w = a < 0;
|
|
2265
|
-
a === 0 ? n = n.add(ar(b, e[l])) : r = r.add(ar(w, e[d]));
|
|
2266
|
-
}
|
|
2267
|
-
return { p: r, f: n };
|
|
2268
|
-
}, _t = E;
|
|
2269
|
-
function yt(t, e) {
|
|
2003
|
+
const _t = E;
|
|
2004
|
+
function mt(t, e) {
|
|
2270
2005
|
if (!t.address && !t.hash && !t.output && !t.redeem && !t.input)
|
|
2271
2006
|
throw new TypeError("Not enough data");
|
|
2272
2007
|
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
@@ -2358,7 +2093,7 @@ function yt(t, e) {
|
|
|
2358
2093
|
throw new TypeError("Redeem.output too short");
|
|
2359
2094
|
if (a.output.byteLength > 520)
|
|
2360
2095
|
throw new TypeError("Redeem.output unspendable if larger than 520 bytes");
|
|
2361
|
-
if (
|
|
2096
|
+
if (Fe(p) > 201)
|
|
2362
2097
|
throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");
|
|
2363
2098
|
const l = C(a.output);
|
|
2364
2099
|
if (u.length > 0 && !u.equals(l))
|
|
@@ -2373,7 +2108,7 @@ function yt(t, e) {
|
|
|
2373
2108
|
throw new TypeError("Input and witness provided");
|
|
2374
2109
|
if (p) {
|
|
2375
2110
|
const d = A(a.input);
|
|
2376
|
-
if (!
|
|
2111
|
+
if (!ue(d))
|
|
2377
2112
|
throw new TypeError("Non push-only scriptSig");
|
|
2378
2113
|
}
|
|
2379
2114
|
}
|
|
@@ -2403,61 +2138,269 @@ function yt(t, e) {
|
|
|
2403
2138
|
}
|
|
2404
2139
|
return Object.assign(n, t);
|
|
2405
2140
|
}
|
|
2406
|
-
const
|
|
2407
|
-
function
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2141
|
+
const vt = {};
|
|
2142
|
+
function oo(t) {
|
|
2143
|
+
t ? t !== vt.eccLib && ($s(t), vt.eccLib = t) : vt.eccLib = t;
|
|
2144
|
+
}
|
|
2145
|
+
function He() {
|
|
2146
|
+
if (!vt.eccLib)
|
|
2147
|
+
throw new Error("No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance");
|
|
2148
|
+
return vt.eccLib;
|
|
2149
|
+
}
|
|
2150
|
+
const tt = (t) => h.from(t, "hex");
|
|
2151
|
+
function $s(t) {
|
|
2152
|
+
L(typeof t.isXOnlyPoint == "function"), L(t.isXOnlyPoint(tt("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))), L(t.isXOnlyPoint(tt("fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e"))), L(t.isXOnlyPoint(tt("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"))), L(t.isXOnlyPoint(tt("0000000000000000000000000000000000000000000000000000000000000001"))), L(!t.isXOnlyPoint(tt("0000000000000000000000000000000000000000000000000000000000000000"))), L(!t.isXOnlyPoint(tt("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"))), L(typeof t.xOnlyPointAddTweak == "function"), qs.forEach((e) => {
|
|
2153
|
+
const r = t.xOnlyPointAddTweak(tt(e.pubkey), tt(e.tweak));
|
|
2154
|
+
e.result === null ? L(r === null) : (L(r !== null), L(r.parity === e.parity), L(h.from(r.xOnlyPubkey).equals(tt(e.result))));
|
|
2155
|
+
});
|
|
2156
|
+
}
|
|
2157
|
+
function L(t) {
|
|
2158
|
+
if (!t)
|
|
2159
|
+
throw new Error("ecc library invalid");
|
|
2160
|
+
}
|
|
2161
|
+
const qs = [
|
|
2162
|
+
{
|
|
2163
|
+
pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
|
|
2164
|
+
tweak: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
|
|
2165
|
+
parity: -1,
|
|
2166
|
+
result: null
|
|
2167
|
+
},
|
|
2168
|
+
{
|
|
2169
|
+
pubkey: "1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b",
|
|
2170
|
+
tweak: "a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac",
|
|
2171
|
+
parity: 1,
|
|
2172
|
+
result: "e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf"
|
|
2173
|
+
},
|
|
2174
|
+
{
|
|
2175
|
+
pubkey: "2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
|
|
2176
|
+
tweak: "823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47",
|
|
2177
|
+
parity: 0,
|
|
2178
|
+
result: "9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c"
|
|
2179
|
+
}
|
|
2180
|
+
], { typeforce: Sn } = ie;
|
|
2181
|
+
function _n(t, e) {
|
|
2182
|
+
if (typeof t != "number")
|
|
2183
|
+
throw new Error("cannot write a non-number as a number");
|
|
2184
|
+
if (t < 0)
|
|
2185
|
+
throw new Error("specified a negative value for writing an unsigned value");
|
|
2186
|
+
if (t > e)
|
|
2187
|
+
throw new Error("RangeError: value out of range");
|
|
2188
|
+
if (Math.floor(t) !== t)
|
|
2189
|
+
throw new Error("value has a fractional component");
|
|
2190
|
+
}
|
|
2191
|
+
function Xs(t, e) {
|
|
2192
|
+
const r = t.readUInt32LE(e);
|
|
2193
|
+
let n = t.readUInt32LE(e + 4);
|
|
2194
|
+
return n *= 4294967296, _n(n + r, 9007199254740991), n + r;
|
|
2195
|
+
}
|
|
2196
|
+
function js(t, e, r) {
|
|
2197
|
+
return _n(e, 9007199254740991), t.writeInt32LE(e & -1, r), t.writeUInt32LE(Math.floor(e / 4294967296), r + 4), r + 8;
|
|
2198
|
+
}
|
|
2199
|
+
function We(t) {
|
|
2200
|
+
if (t.length < 1)
|
|
2201
|
+
return t;
|
|
2202
|
+
let e = t.length - 1, r = 0;
|
|
2203
|
+
for (let n = 0; n < t.length / 2; n++)
|
|
2204
|
+
r = t[n], t[n] = t[e], t[e] = r, e--;
|
|
2205
|
+
return t;
|
|
2206
|
+
}
|
|
2207
|
+
function lr(t) {
|
|
2208
|
+
const e = h.allocUnsafe(t.length);
|
|
2209
|
+
return t.copy(e), e;
|
|
2210
|
+
}
|
|
2211
|
+
class x {
|
|
2212
|
+
constructor(e, r = 0) {
|
|
2213
|
+
this.buffer = e, this.offset = r, Sn(z(V, Y), [e, r]);
|
|
2214
|
+
}
|
|
2215
|
+
static withCapacity(e) {
|
|
2216
|
+
return new x(h.alloc(e));
|
|
2217
|
+
}
|
|
2218
|
+
writeUInt8(e) {
|
|
2219
|
+
this.offset = this.buffer.writeUInt8(e, this.offset);
|
|
2220
|
+
}
|
|
2221
|
+
writeInt32(e) {
|
|
2222
|
+
this.offset = this.buffer.writeInt32LE(e, this.offset);
|
|
2223
|
+
}
|
|
2224
|
+
writeUInt32(e) {
|
|
2225
|
+
this.offset = this.buffer.writeUInt32LE(e, this.offset);
|
|
2226
|
+
}
|
|
2227
|
+
writeUInt64(e) {
|
|
2228
|
+
this.offset = js(this.buffer, e, this.offset);
|
|
2229
|
+
}
|
|
2230
|
+
writeVarInt(e) {
|
|
2231
|
+
const r = Br(e, this.buffer, this.offset);
|
|
2232
|
+
this.offset += r.bytes;
|
|
2233
|
+
}
|
|
2234
|
+
writeSlice(e) {
|
|
2235
|
+
if (this.buffer.length < this.offset + e.length)
|
|
2236
|
+
throw new Error("Cannot write slice out of bounds");
|
|
2237
|
+
this.offset += e.copy(this.buffer, this.offset);
|
|
2238
|
+
}
|
|
2239
|
+
writeVarSlice(e) {
|
|
2240
|
+
this.writeVarInt(e.length), this.writeSlice(e);
|
|
2241
|
+
}
|
|
2242
|
+
writeVector(e) {
|
|
2243
|
+
this.writeVarInt(e.length), e.forEach((r) => this.writeVarSlice(r));
|
|
2244
|
+
}
|
|
2245
|
+
end() {
|
|
2246
|
+
if (this.buffer.length === this.offset)
|
|
2247
|
+
return this.buffer;
|
|
2248
|
+
throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
class zs {
|
|
2252
|
+
constructor(e, r = 0) {
|
|
2253
|
+
this.buffer = e, this.offset = r, Sn(z(V, Y), [e, r]);
|
|
2254
|
+
}
|
|
2255
|
+
readUInt8() {
|
|
2256
|
+
const e = this.buffer.readUInt8(this.offset);
|
|
2257
|
+
return this.offset++, e;
|
|
2258
|
+
}
|
|
2259
|
+
readInt32() {
|
|
2260
|
+
const e = this.buffer.readInt32LE(this.offset);
|
|
2261
|
+
return this.offset += 4, e;
|
|
2262
|
+
}
|
|
2263
|
+
readUInt32() {
|
|
2264
|
+
const e = this.buffer.readUInt32LE(this.offset);
|
|
2265
|
+
return this.offset += 4, e;
|
|
2266
|
+
}
|
|
2267
|
+
readUInt64() {
|
|
2268
|
+
const e = Xs(this.buffer, this.offset);
|
|
2269
|
+
return this.offset += 8, e;
|
|
2270
|
+
}
|
|
2271
|
+
readVarInt() {
|
|
2272
|
+
const e = Kn(this.buffer, this.offset);
|
|
2273
|
+
return this.offset += e.bytes, e.numberValue || 0;
|
|
2274
|
+
}
|
|
2275
|
+
readSlice(e) {
|
|
2276
|
+
if (this.buffer.length < this.offset + e)
|
|
2277
|
+
throw new Error("Cannot read slice out of bounds");
|
|
2278
|
+
const r = h.from(this.buffer.subarray(this.offset, this.offset + e));
|
|
2279
|
+
return this.offset += e, r;
|
|
2280
|
+
}
|
|
2281
|
+
readVarSlice() {
|
|
2282
|
+
return this.readSlice(this.readVarInt());
|
|
2283
|
+
}
|
|
2284
|
+
readVector() {
|
|
2285
|
+
const e = this.readVarInt(), r = [];
|
|
2286
|
+
for (let n = 0; n < e; n++)
|
|
2287
|
+
r.push(this.readVarSlice());
|
|
2288
|
+
return r;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
const Ge = 192, Tn = 128, Ys = (t) => "left" in t && "right" in t;
|
|
2292
|
+
function te(t, e) {
|
|
2293
|
+
if (t.length < 33)
|
|
2294
|
+
throw new TypeError(`The control-block length is too small. Got ${t.length}, expected min 33.`);
|
|
2295
|
+
const r = (t.length - 33) / 32;
|
|
2296
|
+
let n = e;
|
|
2297
|
+
for (let s = 0; s < r; s++) {
|
|
2298
|
+
const i = t.subarray(33 + 32 * s, 65 + 32 * s);
|
|
2299
|
+
n.compare(i) < 0 ? n = ve(n, i) : n = ve(i, n);
|
|
2300
|
+
}
|
|
2301
|
+
return n;
|
|
2302
|
+
}
|
|
2303
|
+
function ee(t) {
|
|
2304
|
+
if (ne(t))
|
|
2305
|
+
return { hash: Z(t) };
|
|
2306
|
+
const e = [ee(t[0]), ee(t[1])];
|
|
2307
|
+
e.sort((s, i) => s.hash.compare(i.hash));
|
|
2308
|
+
const [r, n] = e;
|
|
2309
|
+
return {
|
|
2310
|
+
hash: ve(r.hash, n.hash),
|
|
2311
|
+
left: r,
|
|
2312
|
+
right: n
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
function Bt(t, e) {
|
|
2316
|
+
if (Ys(t)) {
|
|
2317
|
+
const r = Bt(t.left, e);
|
|
2318
|
+
if (r !== void 0)
|
|
2319
|
+
return [...r, t.right.hash];
|
|
2320
|
+
const n = Bt(t.right, e);
|
|
2321
|
+
if (n !== void 0)
|
|
2322
|
+
return [...n, t.left.hash];
|
|
2323
|
+
} else if (t.hash.equals(e))
|
|
2324
|
+
return [];
|
|
2325
|
+
}
|
|
2326
|
+
function Z(t) {
|
|
2327
|
+
const e = t.version || Ge;
|
|
2328
|
+
return Rt("TapLeaf", U.concat([U.from([e]), Qs(t.output)]));
|
|
2329
|
+
}
|
|
2330
|
+
function Zs(t, e) {
|
|
2331
|
+
return Rt("TapTweak", U.concat(e ? [t, e] : [t]));
|
|
2332
|
+
}
|
|
2333
|
+
function At(t, e) {
|
|
2334
|
+
if (!U.isBuffer(t) || t.length !== 32 || e && e.length !== 32)
|
|
2335
|
+
return null;
|
|
2336
|
+
const r = Zs(t, e), n = He().xOnlyPointAddTweak(t, r);
|
|
2337
|
+
return !n || n.xOnlyPubkey === null ? null : {
|
|
2338
|
+
parity: n.parity,
|
|
2339
|
+
x: U.from(n.xOnlyPubkey)
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
function ve(t, e) {
|
|
2343
|
+
return Rt("TapBranch", U.concat([t, e]));
|
|
2344
|
+
}
|
|
2345
|
+
function Qs(t) {
|
|
2346
|
+
const e = kt(t.length), r = U.allocUnsafe(e);
|
|
2347
|
+
return Br(t.length, r), U.concat([r, t]);
|
|
2348
|
+
}
|
|
2349
|
+
const dr = E, wr = 1, Js = 80;
|
|
2350
|
+
function bt(t, e) {
|
|
2351
|
+
if (!t.address && !t.output && !t.pubkey && !t.internalPubkey && !(t.witness && t.witness.length > 1))
|
|
2352
|
+
throw new TypeError("Not enough data");
|
|
2353
|
+
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
2354
|
+
address: c.maybe(c.String),
|
|
2355
|
+
input: c.maybe(c.BufferN(0)),
|
|
2356
|
+
network: c.maybe(c.Object),
|
|
2357
|
+
output: c.maybe(c.BufferN(34)),
|
|
2358
|
+
internalPubkey: c.maybe(c.BufferN(32)),
|
|
2359
|
+
hash: c.maybe(c.BufferN(32)),
|
|
2360
|
+
pubkey: c.maybe(c.BufferN(32)),
|
|
2361
|
+
signature: c.maybe(c.anyOf(c.BufferN(64), c.BufferN(65))),
|
|
2362
|
+
witness: c.maybe(c.arrayOf(c.Buffer)),
|
|
2363
|
+
scriptTree: c.maybe(Re),
|
|
2364
|
+
redeem: c.maybe({
|
|
2365
|
+
output: c.maybe(c.Buffer),
|
|
2366
|
+
redeemVersion: c.maybe(c.Number),
|
|
2367
|
+
witness: c.maybe(c.arrayOf(c.Buffer))
|
|
2368
|
+
}),
|
|
2369
|
+
redeemVersion: c.maybe(c.Number)
|
|
2370
|
+
}, t);
|
|
2371
|
+
const r = B(() => Be(t.address)), n = B(() => {
|
|
2372
|
+
if (!(!t.witness || !t.witness.length))
|
|
2373
|
+
return t.witness.length >= 2 && t.witness[t.witness.length - 1][0] === Js ? t.witness.slice(0, -1) : t.witness.slice();
|
|
2374
|
+
}), s = B(() => {
|
|
2375
|
+
if (t.scriptTree)
|
|
2376
|
+
return ee(t.scriptTree);
|
|
2377
|
+
if (t.hash)
|
|
2378
|
+
return { hash: t.hash };
|
|
2379
|
+
}), i = t.network || M, o = {
|
|
2380
|
+
name: K.P2TR,
|
|
2381
|
+
network: i
|
|
2382
|
+
};
|
|
2383
|
+
if (y(o, "address", () => {
|
|
2384
|
+
if (!o.pubkey)
|
|
2385
|
+
return;
|
|
2386
|
+
const u = I.bech32m.toWords(o.pubkey);
|
|
2387
|
+
return u.unshift(wr), I.bech32m.encode(i.bech32, u);
|
|
2388
|
+
}), y(o, "hash", () => {
|
|
2389
|
+
const u = s();
|
|
2390
|
+
if (u)
|
|
2391
|
+
return u.hash;
|
|
2449
2392
|
const f = n();
|
|
2450
2393
|
if (f && f.length > 1) {
|
|
2451
2394
|
const a = f[f.length - 1], p = a[0] & Pt, l = f[f.length - 2], d = Z({
|
|
2452
2395
|
output: l,
|
|
2453
2396
|
version: p
|
|
2454
2397
|
});
|
|
2455
|
-
return
|
|
2398
|
+
return te(a, d);
|
|
2456
2399
|
}
|
|
2457
2400
|
}), y(o, "output", () => {
|
|
2458
2401
|
if (o.pubkey)
|
|
2459
|
-
return v([
|
|
2460
|
-
}), y(o, "redeemVersion", () => t.redeemVersion ? t.redeemVersion : t.redeem && t.redeem.redeemVersion !== void 0 && t.redeem.redeemVersion !== null ? t.redeem.redeemVersion :
|
|
2402
|
+
return v([dr.OP_1, o.pubkey]);
|
|
2403
|
+
}), y(o, "redeemVersion", () => t.redeemVersion ? t.redeemVersion : t.redeem && t.redeem.redeemVersion !== void 0 && t.redeem.redeemVersion !== null ? t.redeem.redeemVersion : Ge), y(o, "redeem", () => {
|
|
2461
2404
|
const u = n();
|
|
2462
2405
|
if (!(!u || u.length < 2))
|
|
2463
2406
|
return {
|
|
@@ -2497,7 +2440,7 @@ function mt(t, e) {
|
|
|
2497
2440
|
const f = Z({
|
|
2498
2441
|
output: t.redeem.output,
|
|
2499
2442
|
version: o.redeemVersion
|
|
2500
|
-
}), a =
|
|
2443
|
+
}), a = Bt(u, f);
|
|
2501
2444
|
if (!a)
|
|
2502
2445
|
return;
|
|
2503
2446
|
const p = At(t.internalPubkey, u.hash);
|
|
@@ -2513,7 +2456,7 @@ function mt(t, e) {
|
|
|
2513
2456
|
if (t.address) {
|
|
2514
2457
|
if (i && i.bech32 !== r().prefix)
|
|
2515
2458
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
2516
|
-
if (r().version !==
|
|
2459
|
+
if (r().version !== wr)
|
|
2517
2460
|
throw new TypeError("Invalid address version");
|
|
2518
2461
|
if (r().data.length !== 32)
|
|
2519
2462
|
throw new TypeError("Invalid address data");
|
|
@@ -2525,7 +2468,7 @@ function mt(t, e) {
|
|
|
2525
2468
|
u = t.pubkey;
|
|
2526
2469
|
}
|
|
2527
2470
|
if (t.output) {
|
|
2528
|
-
if (t.output.length !== 34 || t.output[0] !==
|
|
2471
|
+
if (t.output.length !== 34 || t.output[0] !== dr.OP_1 || t.output[1] !== 32)
|
|
2529
2472
|
throw new TypeError("Output is invalid");
|
|
2530
2473
|
if (u.length > 0 && !u.equals(t.output.subarray(2)))
|
|
2531
2474
|
throw new TypeError("Pubkey mismatch");
|
|
@@ -2537,7 +2480,7 @@ function mt(t, e) {
|
|
|
2537
2480
|
throw new TypeError("Pubkey mismatch");
|
|
2538
2481
|
u = p.x;
|
|
2539
2482
|
}
|
|
2540
|
-
if (u && u.length && !
|
|
2483
|
+
if (u && u.length && !He().isXOnlyPoint(u))
|
|
2541
2484
|
throw new TypeError("Invalid pubkey for p2tr");
|
|
2542
2485
|
const f = s();
|
|
2543
2486
|
if (t.hash && f && !t.hash.equals(f.hash))
|
|
@@ -2547,7 +2490,7 @@ function mt(t, e) {
|
|
|
2547
2490
|
output: t.redeem.output,
|
|
2548
2491
|
version: o.redeemVersion
|
|
2549
2492
|
});
|
|
2550
|
-
if (!
|
|
2493
|
+
if (!Bt(f, p))
|
|
2551
2494
|
throw new TypeError("Redeem script not in tree");
|
|
2552
2495
|
}
|
|
2553
2496
|
const a = n();
|
|
@@ -2579,12 +2522,12 @@ function mt(t, e) {
|
|
|
2579
2522
|
const d = p.subarray(1, 33);
|
|
2580
2523
|
if (t.internalPubkey && !t.internalPubkey.equals(d))
|
|
2581
2524
|
throw new TypeError("Internal pubkey mismatch");
|
|
2582
|
-
if (!
|
|
2525
|
+
if (!He().isXOnlyPoint(d))
|
|
2583
2526
|
throw new TypeError("Invalid internalPubkey for p2tr witness");
|
|
2584
2527
|
const b = p[0] & Pt, w = a[a.length - 2], S = Z({
|
|
2585
2528
|
output: w,
|
|
2586
2529
|
version: b
|
|
2587
|
-
}), P =
|
|
2530
|
+
}), P = te(p, S), k = At(d, P);
|
|
2588
2531
|
if (!k)
|
|
2589
2532
|
throw new TypeError("Invalid outputKey for p2tr witness");
|
|
2590
2533
|
if (u.length && !u.equals(k.x))
|
|
@@ -2595,8 +2538,8 @@ function mt(t, e) {
|
|
|
2595
2538
|
}
|
|
2596
2539
|
return Object.assign(o, t);
|
|
2597
2540
|
}
|
|
2598
|
-
const
|
|
2599
|
-
function
|
|
2541
|
+
const gr = E, ti = h.alloc(0);
|
|
2542
|
+
function Lt(t, e) {
|
|
2600
2543
|
if (!t.address && !t.hash && !t.output && !t.pubkey && !t.witness)
|
|
2601
2544
|
throw new TypeError("Not enough data");
|
|
2602
2545
|
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
@@ -2627,219 +2570,556 @@ function Vt(t, e) {
|
|
|
2627
2570
|
return i.unshift(0), I.bech32.encode(n.bech32, i);
|
|
2628
2571
|
}), y(s, "hash", () => {
|
|
2629
2572
|
if (t.output)
|
|
2630
|
-
return t.output.subarray(2, 22);
|
|
2631
|
-
if (t.address)
|
|
2632
|
-
return r().data;
|
|
2633
|
-
if (t.pubkey || s.pubkey)
|
|
2634
|
-
return C(t.pubkey || s.pubkey);
|
|
2635
|
-
}), y(s, "output", () => {
|
|
2636
|
-
if (s.hash)
|
|
2637
|
-
return v([
|
|
2638
|
-
}), y(s, "pubkey", () => {
|
|
2639
|
-
if (t.pubkey)
|
|
2640
|
-
return t.pubkey;
|
|
2641
|
-
if (t.witness)
|
|
2642
|
-
return t.witness[1];
|
|
2643
|
-
}), y(s, "signature", () => {
|
|
2644
|
-
if (t.witness)
|
|
2645
|
-
return t.witness[0];
|
|
2573
|
+
return t.output.subarray(2, 22);
|
|
2574
|
+
if (t.address)
|
|
2575
|
+
return r().data;
|
|
2576
|
+
if (t.pubkey || s.pubkey)
|
|
2577
|
+
return C(t.pubkey || s.pubkey);
|
|
2578
|
+
}), y(s, "output", () => {
|
|
2579
|
+
if (s.hash)
|
|
2580
|
+
return v([gr.OP_0, s.hash]);
|
|
2581
|
+
}), y(s, "pubkey", () => {
|
|
2582
|
+
if (t.pubkey)
|
|
2583
|
+
return t.pubkey;
|
|
2584
|
+
if (t.witness)
|
|
2585
|
+
return t.witness[1];
|
|
2586
|
+
}), y(s, "signature", () => {
|
|
2587
|
+
if (t.witness)
|
|
2588
|
+
return t.witness[0];
|
|
2589
|
+
}), y(s, "input", () => {
|
|
2590
|
+
if (s.witness)
|
|
2591
|
+
return ti;
|
|
2592
|
+
}), y(s, "witness", () => {
|
|
2593
|
+
if (t.pubkey && t.signature)
|
|
2594
|
+
return [t.signature, t.pubkey];
|
|
2595
|
+
}), e.validate) {
|
|
2596
|
+
let i = h.from([]);
|
|
2597
|
+
if (t.address) {
|
|
2598
|
+
if (n && n.bech32 !== r().prefix)
|
|
2599
|
+
throw new TypeError("Invalid prefix or Network mismatch");
|
|
2600
|
+
if (r().version !== 0)
|
|
2601
|
+
throw new TypeError("Invalid address version");
|
|
2602
|
+
if (r().data.length !== 20)
|
|
2603
|
+
throw new TypeError("Invalid address data");
|
|
2604
|
+
i = r().data;
|
|
2605
|
+
}
|
|
2606
|
+
if (t.hash) {
|
|
2607
|
+
if (i.length > 0 && !i.equals(t.hash))
|
|
2608
|
+
throw new TypeError("Hash mismatch");
|
|
2609
|
+
i = t.hash;
|
|
2610
|
+
}
|
|
2611
|
+
if (t.output) {
|
|
2612
|
+
if (t.output.length !== 22 || t.output[0] !== gr.OP_0 || t.output[1] !== 20)
|
|
2613
|
+
throw new TypeError("Output is invalid");
|
|
2614
|
+
if (i.length > 0 && !i.equals(t.output.subarray(2)))
|
|
2615
|
+
throw new TypeError("Hash mismatch");
|
|
2616
|
+
i = t.output.subarray(2);
|
|
2617
|
+
}
|
|
2618
|
+
if (t.pubkey) {
|
|
2619
|
+
const o = C(t.pubkey);
|
|
2620
|
+
if (i.length > 0 && !i.equals(o))
|
|
2621
|
+
throw new TypeError("Hash mismatch");
|
|
2622
|
+
if (i = o, !W(t.pubkey) || t.pubkey.length !== 33)
|
|
2623
|
+
throw new TypeError("Invalid pubkey for p2wpkh");
|
|
2624
|
+
}
|
|
2625
|
+
if (t.witness) {
|
|
2626
|
+
if (t.witness.length !== 2)
|
|
2627
|
+
throw new TypeError("Witness is invalid");
|
|
2628
|
+
if (!Q(t.witness[0]))
|
|
2629
|
+
throw new TypeError("Witness has invalid signature");
|
|
2630
|
+
if (!W(t.witness[1]) || t.witness[1].length !== 33)
|
|
2631
|
+
throw new TypeError("Witness has invalid pubkey");
|
|
2632
|
+
if (t.signature && !t.signature.equals(t.witness[0]))
|
|
2633
|
+
throw new TypeError("Signature mismatch");
|
|
2634
|
+
if (t.pubkey && !t.pubkey.equals(t.witness[1]))
|
|
2635
|
+
throw new TypeError("Pubkey mismatch");
|
|
2636
|
+
const o = C(t.witness[1]);
|
|
2637
|
+
if (i.length > 0 && !i.equals(o))
|
|
2638
|
+
throw new TypeError("Hash mismatch");
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
return Object.assign(s, t);
|
|
2642
|
+
}
|
|
2643
|
+
const yr = E, ge = h.alloc(0);
|
|
2644
|
+
function Kt(t) {
|
|
2645
|
+
return !!(h.isBuffer(t) && t.length === 65 && t[0] === 4 && W(t));
|
|
2646
|
+
}
|
|
2647
|
+
function Et(t, e) {
|
|
2648
|
+
if (!t.address && !t.hash && !t.output && !t.redeem && !t.witness)
|
|
2649
|
+
throw new TypeError("Not enough data");
|
|
2650
|
+
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
2651
|
+
network: c.maybe(c.Object),
|
|
2652
|
+
address: c.maybe(c.String),
|
|
2653
|
+
hash: c.maybe(c.BufferN(32)),
|
|
2654
|
+
output: c.maybe(c.BufferN(34)),
|
|
2655
|
+
redeem: c.maybe({
|
|
2656
|
+
input: c.maybe(c.Buffer),
|
|
2657
|
+
network: c.maybe(c.Object),
|
|
2658
|
+
output: c.maybe(c.Buffer),
|
|
2659
|
+
witness: c.maybe(c.arrayOf(c.Buffer))
|
|
2660
|
+
}),
|
|
2661
|
+
input: c.maybe(c.BufferN(0)),
|
|
2662
|
+
witness: c.maybe(c.arrayOf(c.Buffer))
|
|
2663
|
+
}, t);
|
|
2664
|
+
const r = B(() => {
|
|
2665
|
+
const o = I.bech32.decode(t.address), u = o.words.shift(), f = I.bech32.fromWords(o.words);
|
|
2666
|
+
return {
|
|
2667
|
+
version: u,
|
|
2668
|
+
prefix: o.prefix,
|
|
2669
|
+
data: h.from(f)
|
|
2670
|
+
};
|
|
2671
|
+
}), n = B(() => A(t.redeem.input));
|
|
2672
|
+
let s = t.network;
|
|
2673
|
+
s || (s = t.redeem && t.redeem.network || M);
|
|
2674
|
+
const i = {
|
|
2675
|
+
network: s,
|
|
2676
|
+
name: K.P2WSH
|
|
2677
|
+
};
|
|
2678
|
+
if (y(i, "address", () => {
|
|
2679
|
+
if (!i.hash)
|
|
2680
|
+
return;
|
|
2681
|
+
const o = I.bech32.toWords(i.hash);
|
|
2682
|
+
return o.unshift(0), I.bech32.encode(s.bech32, o);
|
|
2683
|
+
}), y(i, "hash", () => {
|
|
2684
|
+
if (t.output)
|
|
2685
|
+
return t.output.subarray(2);
|
|
2686
|
+
if (t.address)
|
|
2687
|
+
return r().data;
|
|
2688
|
+
if (i.redeem && i.redeem.output)
|
|
2689
|
+
return D(i.redeem.output);
|
|
2690
|
+
}), y(i, "output", () => {
|
|
2691
|
+
if (i.hash)
|
|
2692
|
+
return v([yr.OP_0, i.hash]);
|
|
2693
|
+
}), y(i, "redeem", () => {
|
|
2694
|
+
if (t.witness)
|
|
2695
|
+
return {
|
|
2696
|
+
output: t.witness[t.witness.length - 1],
|
|
2697
|
+
input: ge,
|
|
2698
|
+
witness: t.witness.slice(0, -1)
|
|
2699
|
+
};
|
|
2700
|
+
}), y(i, "input", () => {
|
|
2701
|
+
if (i.witness)
|
|
2702
|
+
return ge;
|
|
2703
|
+
}), y(i, "witness", () => {
|
|
2704
|
+
if (t.redeem && t.redeem.input && t.redeem.input.length > 0 && t.redeem.output && t.redeem.output.length > 0) {
|
|
2705
|
+
const o = jr(n());
|
|
2706
|
+
return i.redeem = Object.assign({ witness: o }, t.redeem), i.redeem.input = ge, [].concat(o, t.redeem.output);
|
|
2707
|
+
}
|
|
2708
|
+
if (t.redeem && t.redeem.output && t.redeem.witness)
|
|
2709
|
+
return [].concat(t.redeem.witness, t.redeem.output);
|
|
2710
|
+
}), y(i, "name", () => {
|
|
2711
|
+
const o = ["p2wsh"];
|
|
2712
|
+
return i.redeem !== void 0 && i.redeem.name !== void 0 && o.push(i.redeem.name), o.join("-");
|
|
2713
|
+
}), e.validate) {
|
|
2714
|
+
let o = h.from([]);
|
|
2715
|
+
if (t.address) {
|
|
2716
|
+
if (r().prefix !== s.bech32)
|
|
2717
|
+
throw new TypeError("Invalid prefix or Network mismatch");
|
|
2718
|
+
if (r().version !== 0)
|
|
2719
|
+
throw new TypeError("Invalid address version");
|
|
2720
|
+
if (r().data.length !== 32)
|
|
2721
|
+
throw new TypeError("Invalid address data");
|
|
2722
|
+
o = r().data;
|
|
2723
|
+
}
|
|
2724
|
+
if (t.hash) {
|
|
2725
|
+
if (o.length > 0 && !o.equals(t.hash))
|
|
2726
|
+
throw new TypeError("Hash mismatch");
|
|
2727
|
+
o = t.hash;
|
|
2728
|
+
}
|
|
2729
|
+
if (t.output) {
|
|
2730
|
+
if (t.output.length !== 34 || t.output[0] !== yr.OP_0 || t.output[1] !== 32)
|
|
2731
|
+
throw new TypeError("Output is invalid");
|
|
2732
|
+
const u = t.output.subarray(2);
|
|
2733
|
+
if (o.length > 0 && !o.equals(u))
|
|
2734
|
+
throw new TypeError("Hash mismatch");
|
|
2735
|
+
o = u;
|
|
2736
|
+
}
|
|
2737
|
+
if (t.redeem) {
|
|
2738
|
+
if (t.redeem.network && t.redeem.network !== s)
|
|
2739
|
+
throw new TypeError("Network mismatch");
|
|
2740
|
+
if (t.redeem.input && t.redeem.input.length > 0 && t.redeem.witness && t.redeem.witness.length > 0)
|
|
2741
|
+
throw new TypeError("Ambiguous witness source");
|
|
2742
|
+
if (t.redeem.output) {
|
|
2743
|
+
const u = A(t.redeem.output);
|
|
2744
|
+
if (!u || u.length < 1)
|
|
2745
|
+
throw new TypeError("Redeem.output is invalid");
|
|
2746
|
+
if (t.redeem.output.byteLength > 3600)
|
|
2747
|
+
throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");
|
|
2748
|
+
if (Fe(u) > 201)
|
|
2749
|
+
throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");
|
|
2750
|
+
const f = D(t.redeem.output);
|
|
2751
|
+
if (o.length > 0 && !o.equals(f))
|
|
2752
|
+
throw new TypeError("Hash mismatch");
|
|
2753
|
+
o = f;
|
|
2754
|
+
}
|
|
2755
|
+
if (t.redeem.input && !ue(n()))
|
|
2756
|
+
throw new TypeError("Non push-only scriptSig");
|
|
2757
|
+
if (t.witness && t.redeem.witness && !at(t.witness, t.redeem.witness))
|
|
2758
|
+
throw new TypeError("Witness and redeem.witness mismatch");
|
|
2759
|
+
if (t.redeem.input && n().some(Kt) || t.redeem.output && (A(t.redeem.output) || []).some(Kt))
|
|
2760
|
+
throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey");
|
|
2761
|
+
}
|
|
2762
|
+
if (t.witness && t.witness.length > 0) {
|
|
2763
|
+
const u = t.witness[t.witness.length - 1];
|
|
2764
|
+
if (t.redeem && t.redeem.output && !t.redeem.output.equals(u))
|
|
2765
|
+
throw new TypeError("Witness and redeem.output mismatch");
|
|
2766
|
+
if (t.witness.some(Kt) || (A(u) || []).some(Kt))
|
|
2767
|
+
throw new TypeError("Witness contains uncompressed pubkey");
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
return Object.assign(i, t);
|
|
2771
|
+
}
|
|
2772
|
+
const fe = 40, ce = 2, ae = 15, ei = 16, xe = 16, Ke = 2, Me = 80, ri = "WARNING: Sending to a future segwit version address can lead to loss of funds. End users MUST be warned carefully in the GUI and asked if they wish to proceed with caution. Wallets should verify the segwit version from the output of fromBech32, then decide when it is safe to use which version of segwit.", ni = (t) => {
|
|
2773
|
+
try {
|
|
2774
|
+
const e = h.from(t.subarray(2));
|
|
2775
|
+
if (e.length < ce || e.length > fe)
|
|
2776
|
+
throw new TypeError("Invalid program length for segwit address");
|
|
2777
|
+
const r = t[0] - Me;
|
|
2778
|
+
if (r < Ke || r > ae + 1)
|
|
2779
|
+
throw new TypeError("Invalid version for segwit address");
|
|
2780
|
+
if (r === 1)
|
|
2781
|
+
throw new TypeError("taproot");
|
|
2782
|
+
return !0;
|
|
2783
|
+
} catch {
|
|
2784
|
+
}
|
|
2785
|
+
return !1;
|
|
2786
|
+
};
|
|
2787
|
+
function si(t, e) {
|
|
2788
|
+
if (!h.isBuffer(t))
|
|
2789
|
+
throw new TypeError("output must be a Buffer");
|
|
2790
|
+
if (!e.bech32Opnet)
|
|
2791
|
+
throw new Error("Network does not support opnet");
|
|
2792
|
+
const r = t[0];
|
|
2793
|
+
let n = 1, s;
|
|
2794
|
+
if (t[1] < 76)
|
|
2795
|
+
s = t[1], n = 2;
|
|
2796
|
+
else if (t[1] === 76)
|
|
2797
|
+
s = t[2], n = 3;
|
|
2798
|
+
else
|
|
2799
|
+
throw new TypeError("Unsupported push opcode in script");
|
|
2800
|
+
const i = h.from(t.subarray(n, n + s));
|
|
2801
|
+
if (i.length < ce || i.length > fe)
|
|
2802
|
+
throw new TypeError("Invalid program length for segwit address");
|
|
2803
|
+
const o = r === E.OP_0 ? 0 : r >= E.OP_1 && r <= E.OP_16 ? r - (E.OP_1 - 1) : -1;
|
|
2804
|
+
if (o < ae || o > ei)
|
|
2805
|
+
throw new TypeError(`Invalid segwit version ${o}`);
|
|
2806
|
+
const u = [o, ...I.bech32m.toWords(i)];
|
|
2807
|
+
return I.bech32m.encode(e.bech32Opnet, u);
|
|
2808
|
+
}
|
|
2809
|
+
function ii(t, e) {
|
|
2810
|
+
const r = h.from(t.subarray(2));
|
|
2811
|
+
if (r.length < ce || r.length > fe)
|
|
2812
|
+
throw new TypeError("Invalid program length for segwit address");
|
|
2813
|
+
const n = t[0] - Me;
|
|
2814
|
+
if (n < Ke || n > ae)
|
|
2815
|
+
throw new TypeError("Invalid version for segwit address");
|
|
2816
|
+
if (t[1] !== r.length)
|
|
2817
|
+
throw new TypeError(`Invalid script for segwit address ${t[1]} !== ${r.length}`);
|
|
2818
|
+
return ui(r, n, e.bech32, e.bech32Opnet);
|
|
2819
|
+
}
|
|
2820
|
+
function oi(t) {
|
|
2821
|
+
const e = h.from(Ut.decode(t));
|
|
2822
|
+
if (e.length < 21)
|
|
2823
|
+
throw new TypeError(t + " is too short");
|
|
2824
|
+
if (e.length > 21)
|
|
2825
|
+
throw new TypeError(t + " is too long");
|
|
2826
|
+
const r = e.readUInt8(0), n = h.from(e.subarray(1));
|
|
2827
|
+
return { version: r, hash: n };
|
|
2828
|
+
}
|
|
2829
|
+
function ui(t, e, r, n) {
|
|
2830
|
+
const s = I.bech32.toWords(t);
|
|
2831
|
+
return s.unshift(e), e === xe && n ? I.bech32m.encode(n, s) : e === 0 ? I.bech32.encode(r, s) : I.bech32m.encode(r, s);
|
|
2832
|
+
}
|
|
2833
|
+
function fi(t, e) {
|
|
2834
|
+
e = e || M;
|
|
2835
|
+
try {
|
|
2836
|
+
return yt({ output: t, network: e }).address;
|
|
2837
|
+
} catch {
|
|
2838
|
+
}
|
|
2839
|
+
try {
|
|
2840
|
+
return mt({ output: t, network: e }).address;
|
|
2841
|
+
} catch {
|
|
2842
|
+
}
|
|
2843
|
+
try {
|
|
2844
|
+
return Lt({ output: t, network: e }).address;
|
|
2845
|
+
} catch {
|
|
2846
|
+
}
|
|
2847
|
+
try {
|
|
2848
|
+
return Et({ output: t, network: e }).address;
|
|
2849
|
+
} catch {
|
|
2850
|
+
}
|
|
2851
|
+
try {
|
|
2852
|
+
return bt({ output: t, network: e }).address;
|
|
2853
|
+
} catch {
|
|
2854
|
+
}
|
|
2855
|
+
try {
|
|
2856
|
+
return si(t, e);
|
|
2857
|
+
} catch {
|
|
2858
|
+
}
|
|
2859
|
+
try {
|
|
2860
|
+
return ii(t, e);
|
|
2861
|
+
} catch {
|
|
2862
|
+
}
|
|
2863
|
+
throw new Error(Xr(t) + " has no matching Address");
|
|
2864
|
+
}
|
|
2865
|
+
function ci(t, e) {
|
|
2866
|
+
e = e || M;
|
|
2867
|
+
let r, n;
|
|
2868
|
+
try {
|
|
2869
|
+
r = oi(t);
|
|
2870
|
+
} catch {
|
|
2871
|
+
}
|
|
2872
|
+
if (r) {
|
|
2873
|
+
if (r.version === e.pubKeyHash)
|
|
2874
|
+
return yt({ hash: r.hash }).output;
|
|
2875
|
+
if (r.version === e.scriptHash)
|
|
2876
|
+
return mt({ hash: r.hash }).output;
|
|
2877
|
+
} else {
|
|
2878
|
+
try {
|
|
2879
|
+
n = Be(t);
|
|
2880
|
+
} catch {
|
|
2881
|
+
}
|
|
2882
|
+
if (n) {
|
|
2883
|
+
if (n.prefix !== e.bech32 && e.bech32Opnet && n.prefix !== e.bech32Opnet)
|
|
2884
|
+
throw new Error(t + " has an invalid prefix");
|
|
2885
|
+
if (n.version === 0) {
|
|
2886
|
+
if (n.data.length === 20)
|
|
2887
|
+
return Lt({ hash: n.data }).output;
|
|
2888
|
+
if (n.data.length === 32)
|
|
2889
|
+
return Et({ hash: n.data }).output;
|
|
2890
|
+
} else if (n.version === 1) {
|
|
2891
|
+
if (n.data.length === 32)
|
|
2892
|
+
return bt({ pubkey: n.data }).output;
|
|
2893
|
+
} else if (n.version === xe) {
|
|
2894
|
+
if (!e.bech32Opnet)
|
|
2895
|
+
throw new Error(t + " has an invalid prefix");
|
|
2896
|
+
return Yr({
|
|
2897
|
+
program: n.data,
|
|
2898
|
+
network: e
|
|
2899
|
+
}).output;
|
|
2900
|
+
} else if (n.version >= Ke && n.version <= ae && n.data.length >= ce && n.data.length <= fe)
|
|
2901
|
+
return n.version !== xe && console.warn(ri), v([
|
|
2902
|
+
n.version + Me,
|
|
2903
|
+
n.data
|
|
2904
|
+
]);
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
throw new TypeError(t + " has no matching Script");
|
|
2908
|
+
}
|
|
2909
|
+
const mr = E;
|
|
2910
|
+
function ai(t, e) {
|
|
2911
|
+
if (!t.data && !t.output)
|
|
2912
|
+
throw new TypeError("Not enough data");
|
|
2913
|
+
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
2914
|
+
network: c.maybe(c.Object),
|
|
2915
|
+
output: c.maybe(c.Buffer),
|
|
2916
|
+
data: c.maybe(c.arrayOf(c.Buffer))
|
|
2917
|
+
}, t);
|
|
2918
|
+
const r = t.network || M, n = { name: K.Embed, network: r, data: [] };
|
|
2919
|
+
if (y(n, "output", () => {
|
|
2920
|
+
if (t.data)
|
|
2921
|
+
return v([mr.OP_RETURN].concat(t.data));
|
|
2922
|
+
}), y(n, "data", () => {
|
|
2923
|
+
if (!t.output)
|
|
2924
|
+
return;
|
|
2925
|
+
const s = A(t.output);
|
|
2926
|
+
if (s != null)
|
|
2927
|
+
return s.slice(1);
|
|
2928
|
+
}), e.validate && t.output) {
|
|
2929
|
+
const s = A(t.output);
|
|
2930
|
+
if (s[0] !== mr.OP_RETURN)
|
|
2931
|
+
throw new TypeError("Output is invalid");
|
|
2932
|
+
if (!s.slice(1).every(c.Buffer))
|
|
2933
|
+
throw new TypeError("Output is invalid");
|
|
2934
|
+
if (t.data && !at(t.data, n.data))
|
|
2935
|
+
throw new TypeError("Data mismatch");
|
|
2936
|
+
}
|
|
2937
|
+
return Object.assign(n, t);
|
|
2938
|
+
}
|
|
2939
|
+
const lt = E, Mt = lt.OP_RESERVED;
|
|
2940
|
+
function Vt(t, e) {
|
|
2941
|
+
if (!t.input && !t.output && !(t.pubkeys && t.m !== void 0) && !t.signatures)
|
|
2942
|
+
throw new TypeError("Not enough data");
|
|
2943
|
+
e = Object.assign({ validate: !0 }, e || {});
|
|
2944
|
+
function r(f) {
|
|
2945
|
+
return Q(f) || (e.allowIncomplete && f === lt.OP_0) !== void 0;
|
|
2946
|
+
}
|
|
2947
|
+
c({
|
|
2948
|
+
network: c.maybe(c.Object),
|
|
2949
|
+
m: c.maybe(c.Number),
|
|
2950
|
+
n: c.maybe(c.Number),
|
|
2951
|
+
output: c.maybe(c.Buffer),
|
|
2952
|
+
pubkeys: c.maybe(c.arrayOf(W)),
|
|
2953
|
+
signatures: c.maybe(c.arrayOf(r)),
|
|
2954
|
+
input: c.maybe(c.Buffer)
|
|
2955
|
+
}, t);
|
|
2956
|
+
const s = {
|
|
2957
|
+
network: t.network || M,
|
|
2958
|
+
name: K.P2MS
|
|
2959
|
+
};
|
|
2960
|
+
let i = [], o = !1;
|
|
2961
|
+
function u(f) {
|
|
2962
|
+
o || (o = !0, i = A(f), s.m = i[0] - Mt, s.n = i[i.length - 2] - Mt, s.pubkeys = i.slice(1, -2));
|
|
2963
|
+
}
|
|
2964
|
+
if (y(s, "output", () => {
|
|
2965
|
+
if (t.m && s.n && t.pubkeys)
|
|
2966
|
+
return v([].concat(Mt + t.m, t.pubkeys, Mt + s.n, lt.OP_CHECKMULTISIG));
|
|
2967
|
+
}), y(s, "m", () => {
|
|
2968
|
+
if (s.output)
|
|
2969
|
+
return u(s.output), s.m;
|
|
2970
|
+
}), y(s, "n", () => {
|
|
2971
|
+
if (s.pubkeys)
|
|
2972
|
+
return s.pubkeys.length;
|
|
2973
|
+
}), y(s, "pubkeys", () => {
|
|
2974
|
+
if (t.output)
|
|
2975
|
+
return u(t.output), s.pubkeys;
|
|
2976
|
+
}), y(s, "signatures", () => {
|
|
2977
|
+
if (!t.input)
|
|
2978
|
+
return;
|
|
2979
|
+
const f = A(t.input);
|
|
2980
|
+
if (f != null)
|
|
2981
|
+
return f.slice(1);
|
|
2646
2982
|
}), y(s, "input", () => {
|
|
2647
|
-
if (
|
|
2648
|
-
return
|
|
2983
|
+
if (t.signatures)
|
|
2984
|
+
return v([lt.OP_0].concat(t.signatures));
|
|
2649
2985
|
}), y(s, "witness", () => {
|
|
2650
|
-
if (
|
|
2651
|
-
return [
|
|
2986
|
+
if (s.input)
|
|
2987
|
+
return [];
|
|
2988
|
+
}), y(s, "name", () => {
|
|
2989
|
+
if (!(!s.m || !s.n))
|
|
2990
|
+
return `p2ms(${s.m} of ${s.n})`;
|
|
2652
2991
|
}), e.validate) {
|
|
2653
|
-
let i = h.from([]);
|
|
2654
|
-
if (t.address) {
|
|
2655
|
-
if (n && n.bech32 !== r().prefix)
|
|
2656
|
-
throw new TypeError("Invalid prefix or Network mismatch");
|
|
2657
|
-
if (r().version !== 0)
|
|
2658
|
-
throw new TypeError("Invalid address version");
|
|
2659
|
-
if (r().data.length !== 20)
|
|
2660
|
-
throw new TypeError("Invalid address data");
|
|
2661
|
-
i = r().data;
|
|
2662
|
-
}
|
|
2663
|
-
if (t.hash) {
|
|
2664
|
-
if (i.length > 0 && !i.equals(t.hash))
|
|
2665
|
-
throw new TypeError("Hash mismatch");
|
|
2666
|
-
i = t.hash;
|
|
2667
|
-
}
|
|
2668
2992
|
if (t.output) {
|
|
2669
|
-
if (t.output
|
|
2993
|
+
if (u(t.output), !c.Number(i[0]))
|
|
2670
2994
|
throw new TypeError("Output is invalid");
|
|
2671
|
-
if (i.length
|
|
2672
|
-
throw new TypeError("
|
|
2673
|
-
i
|
|
2995
|
+
if (!c.Number(i[i.length - 2]))
|
|
2996
|
+
throw new TypeError("Output is invalid");
|
|
2997
|
+
if (i[i.length - 1] !== lt.OP_CHECKMULTISIG)
|
|
2998
|
+
throw new TypeError("Output is invalid");
|
|
2999
|
+
if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== i.length - 3)
|
|
3000
|
+
throw new TypeError("Output is invalid");
|
|
3001
|
+
if (!s.pubkeys.every((f) => W(f)))
|
|
3002
|
+
throw new TypeError("Output is invalid");
|
|
3003
|
+
if (t.m !== void 0 && t.m !== s.m)
|
|
3004
|
+
throw new TypeError("m mismatch");
|
|
3005
|
+
if (t.n !== void 0 && t.n !== s.n)
|
|
3006
|
+
throw new TypeError("n mismatch");
|
|
3007
|
+
if (t.pubkeys && !at(t.pubkeys, s.pubkeys))
|
|
3008
|
+
throw new TypeError("Pubkeys mismatch");
|
|
2674
3009
|
}
|
|
2675
|
-
if (t.
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
throw new TypeError("Invalid pubkey for p2wpkh");
|
|
3010
|
+
if (t.pubkeys) {
|
|
3011
|
+
if (t.n !== void 0 && t.n !== t.pubkeys.length)
|
|
3012
|
+
throw new TypeError("Pubkey count mismatch");
|
|
3013
|
+
if (s.n = t.pubkeys.length, s.n < s.m)
|
|
3014
|
+
throw new TypeError("Pubkey count cannot be less than m");
|
|
2681
3015
|
}
|
|
2682
|
-
if (t.
|
|
2683
|
-
if (t.
|
|
2684
|
-
throw new TypeError("
|
|
2685
|
-
if (
|
|
2686
|
-
throw new TypeError("
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
if (t.
|
|
3016
|
+
if (t.signatures) {
|
|
3017
|
+
if (t.signatures.length < s.m)
|
|
3018
|
+
throw new TypeError("Not enough signatures provided");
|
|
3019
|
+
if (t.signatures.length > s.m)
|
|
3020
|
+
throw new TypeError("Too many signatures provided");
|
|
3021
|
+
}
|
|
3022
|
+
if (t.input) {
|
|
3023
|
+
if (t.input[0] !== lt.OP_0)
|
|
3024
|
+
throw new TypeError("Input is invalid");
|
|
3025
|
+
if (s.signatures.length === 0 || !s.signatures.every(r))
|
|
3026
|
+
throw new TypeError("Input has invalid signature(s)");
|
|
3027
|
+
if (t.signatures && !at(t.signatures, s.signatures))
|
|
2690
3028
|
throw new TypeError("Signature mismatch");
|
|
2691
|
-
if (t.
|
|
2692
|
-
throw new TypeError("
|
|
2693
|
-
const o = C(t.witness[1]);
|
|
2694
|
-
if (i.length > 0 && !i.equals(o))
|
|
2695
|
-
throw new TypeError("Hash mismatch");
|
|
3029
|
+
if (t.m !== void 0 && t.m !== t.signatures.length)
|
|
3030
|
+
throw new TypeError("Signature count mismatch");
|
|
2696
3031
|
}
|
|
2697
3032
|
}
|
|
2698
3033
|
return Object.assign(s, t);
|
|
2699
3034
|
}
|
|
2700
|
-
const
|
|
2701
|
-
function
|
|
2702
|
-
|
|
2703
|
-
}
|
|
2704
|
-
function bt(t, e) {
|
|
2705
|
-
if (!t.address && !t.hash && !t.output && !t.redeem && !t.witness)
|
|
3035
|
+
const br = E;
|
|
3036
|
+
function De(t, e) {
|
|
3037
|
+
if (!t.input && !t.output && !t.pubkey && !t.input && !t.signature)
|
|
2706
3038
|
throw new TypeError("Not enough data");
|
|
2707
3039
|
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
2708
3040
|
network: c.maybe(c.Object),
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
input: c.maybe(c.Buffer),
|
|
2714
|
-
network: c.maybe(c.Object),
|
|
2715
|
-
output: c.maybe(c.Buffer),
|
|
2716
|
-
witness: c.maybe(c.arrayOf(c.Buffer))
|
|
2717
|
-
}),
|
|
2718
|
-
input: c.maybe(c.BufferN(0)),
|
|
2719
|
-
witness: c.maybe(c.arrayOf(c.Buffer))
|
|
3041
|
+
output: c.maybe(c.Buffer),
|
|
3042
|
+
pubkey: c.maybe(W),
|
|
3043
|
+
signature: c.maybe(Q),
|
|
3044
|
+
input: c.maybe(c.Buffer)
|
|
2720
3045
|
}, t);
|
|
2721
|
-
const r = B(() => {
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
prefix: o.prefix,
|
|
2726
|
-
data: h.from(f)
|
|
2727
|
-
};
|
|
2728
|
-
}), n = B(() => A(t.redeem.input));
|
|
2729
|
-
let s = t.network;
|
|
2730
|
-
s || (s = t.redeem && t.redeem.network || M);
|
|
2731
|
-
const i = {
|
|
2732
|
-
network: s,
|
|
2733
|
-
name: K.P2WSH
|
|
3046
|
+
const r = B(() => A(t.input)), n = t.network || M, s = {
|
|
3047
|
+
name: K.P2PK,
|
|
3048
|
+
network: n,
|
|
3049
|
+
pubkey: void 0
|
|
2734
3050
|
};
|
|
2735
|
-
if (y(
|
|
2736
|
-
if (
|
|
2737
|
-
return;
|
|
2738
|
-
|
|
2739
|
-
return o.unshift(0), I.bech32.encode(s.bech32, o);
|
|
2740
|
-
}), y(i, "hash", () => {
|
|
3051
|
+
if (y(s, "output", () => {
|
|
3052
|
+
if (t.pubkey)
|
|
3053
|
+
return v([t.pubkey, br.OP_CHECKSIG]);
|
|
3054
|
+
}), y(s, "pubkey", () => {
|
|
2741
3055
|
if (t.output)
|
|
2742
|
-
return t.output.subarray(
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
return {
|
|
2753
|
-
output: t.witness[t.witness.length - 1],
|
|
2754
|
-
input: ge,
|
|
2755
|
-
witness: t.witness.slice(0, -1)
|
|
2756
|
-
};
|
|
2757
|
-
}), y(i, "input", () => {
|
|
2758
|
-
if (i.witness)
|
|
2759
|
-
return ge;
|
|
2760
|
-
}), y(i, "witness", () => {
|
|
2761
|
-
if (t.redeem && t.redeem.input && t.redeem.input.length > 0 && t.redeem.output && t.redeem.output.length > 0) {
|
|
2762
|
-
const o = jr(n());
|
|
2763
|
-
return i.redeem = Object.assign({ witness: o }, t.redeem), i.redeem.input = ge, [].concat(o, t.redeem.output);
|
|
2764
|
-
}
|
|
2765
|
-
if (t.redeem && t.redeem.output && t.redeem.witness)
|
|
2766
|
-
return [].concat(t.redeem.witness, t.redeem.output);
|
|
2767
|
-
}), y(i, "name", () => {
|
|
2768
|
-
const o = ["p2wsh"];
|
|
2769
|
-
return i.redeem !== void 0 && i.redeem.name !== void 0 && o.push(i.redeem.name), o.join("-");
|
|
3056
|
+
return t.output.subarray(1, -1);
|
|
3057
|
+
}), y(s, "signature", () => {
|
|
3058
|
+
if (t.input)
|
|
3059
|
+
return r()[0];
|
|
3060
|
+
}), y(s, "input", () => {
|
|
3061
|
+
if (t.signature)
|
|
3062
|
+
return v([t.signature]);
|
|
3063
|
+
}), y(s, "witness", () => {
|
|
3064
|
+
if (s.input)
|
|
3065
|
+
return [];
|
|
2770
3066
|
}), e.validate) {
|
|
2771
|
-
let o = h.from([]);
|
|
2772
|
-
if (t.address) {
|
|
2773
|
-
if (r().prefix !== s.bech32)
|
|
2774
|
-
throw new TypeError("Invalid prefix or Network mismatch");
|
|
2775
|
-
if (r().version !== 0)
|
|
2776
|
-
throw new TypeError("Invalid address version");
|
|
2777
|
-
if (r().data.length !== 32)
|
|
2778
|
-
throw new TypeError("Invalid address data");
|
|
2779
|
-
o = r().data;
|
|
2780
|
-
}
|
|
2781
|
-
if (t.hash) {
|
|
2782
|
-
if (o.length > 0 && !o.equals(t.hash))
|
|
2783
|
-
throw new TypeError("Hash mismatch");
|
|
2784
|
-
o = t.hash;
|
|
2785
|
-
}
|
|
2786
3067
|
if (t.output) {
|
|
2787
|
-
if (t.output
|
|
3068
|
+
if (t.output[t.output.length - 1] !== br.OP_CHECKSIG)
|
|
2788
3069
|
throw new TypeError("Output is invalid");
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
}
|
|
2794
|
-
if (t.redeem) {
|
|
2795
|
-
if (t.redeem.network && t.redeem.network !== s)
|
|
2796
|
-
throw new TypeError("Network mismatch");
|
|
2797
|
-
if (t.redeem.input && t.redeem.input.length > 0 && t.redeem.witness && t.redeem.witness.length > 0)
|
|
2798
|
-
throw new TypeError("Ambiguous witness source");
|
|
2799
|
-
if (t.redeem.output) {
|
|
2800
|
-
const u = A(t.redeem.output);
|
|
2801
|
-
if (!u || u.length < 1)
|
|
2802
|
-
throw new TypeError("Redeem.output is invalid");
|
|
2803
|
-
if (t.redeem.output.byteLength > 3600)
|
|
2804
|
-
throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");
|
|
2805
|
-
if (Re(u) > 201)
|
|
2806
|
-
throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");
|
|
2807
|
-
const f = D(t.redeem.output);
|
|
2808
|
-
if (o.length > 0 && !o.equals(f))
|
|
2809
|
-
throw new TypeError("Hash mismatch");
|
|
2810
|
-
o = f;
|
|
2811
|
-
}
|
|
2812
|
-
if (t.redeem.input && !oe(n()))
|
|
2813
|
-
throw new TypeError("Non push-only scriptSig");
|
|
2814
|
-
if (t.witness && t.redeem.witness && !at(t.witness, t.redeem.witness))
|
|
2815
|
-
throw new TypeError("Witness and redeem.witness mismatch");
|
|
2816
|
-
if (t.redeem.input && n().some(Kt) || t.redeem.output && (A(t.redeem.output) || []).some(Kt))
|
|
2817
|
-
throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey");
|
|
3070
|
+
if (!W(s.pubkey))
|
|
3071
|
+
throw new TypeError("Output pubkey is invalid");
|
|
3072
|
+
if (t.pubkey && !t.pubkey.equals(s.pubkey))
|
|
3073
|
+
throw new TypeError("Pubkey mismatch");
|
|
2818
3074
|
}
|
|
2819
|
-
if (t.
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
3075
|
+
if (t.signature && t.input && !t.input.equals(s.input))
|
|
3076
|
+
throw new TypeError("Signature mismatch");
|
|
3077
|
+
if (t.input) {
|
|
3078
|
+
if (r().length !== 1)
|
|
3079
|
+
throw new TypeError("Input is invalid");
|
|
3080
|
+
if (!Q(s.signature))
|
|
3081
|
+
throw new TypeError("Input has invalid signature");
|
|
2825
3082
|
}
|
|
2826
3083
|
}
|
|
2827
|
-
return Object.assign(
|
|
3084
|
+
return Object.assign(s, t);
|
|
2828
3085
|
}
|
|
2829
|
-
const
|
|
3086
|
+
const hi = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
3087
|
+
__proto__: null,
|
|
3088
|
+
LEAF_VERSION_TAPSCRIPT: Ge,
|
|
3089
|
+
MAX_TAPTREE_DEPTH: Tn,
|
|
3090
|
+
get PaymentType() {
|
|
3091
|
+
return K;
|
|
3092
|
+
},
|
|
3093
|
+
findScriptPath: Bt,
|
|
3094
|
+
p2data: ai,
|
|
3095
|
+
p2ms: Vt,
|
|
3096
|
+
p2op: Yr,
|
|
3097
|
+
p2pk: De,
|
|
3098
|
+
p2pkh: yt,
|
|
3099
|
+
p2sh: mt,
|
|
3100
|
+
p2tr: bt,
|
|
3101
|
+
p2wpkh: Lt,
|
|
3102
|
+
p2wsh: Et,
|
|
3103
|
+
prop: y,
|
|
3104
|
+
rootHashFromPath: te,
|
|
3105
|
+
tapleafHash: Z,
|
|
3106
|
+
toHashTree: ee,
|
|
3107
|
+
tweakKey: At,
|
|
3108
|
+
value: B
|
|
3109
|
+
}, Symbol.toStringTag, { value: "Module" })), { typeforce: X } = ie;
|
|
2830
3110
|
function j(t) {
|
|
2831
3111
|
const e = t.length;
|
|
2832
3112
|
return kt(e) + e;
|
|
2833
3113
|
}
|
|
2834
|
-
function
|
|
3114
|
+
function pi(t) {
|
|
2835
3115
|
const e = t.length;
|
|
2836
3116
|
return kt(e) + t.reduce((r, n) => r + j(n), 0);
|
|
2837
3117
|
}
|
|
2838
|
-
const ot = h.allocUnsafe(0),
|
|
3118
|
+
const ot = h.allocUnsafe(0), Er = [], ye = h.from("0000000000000000000000000000000000000000000000000000000000000000", "hex"), Sr = h.from("0000000000000000000000000000000000000000000000000000000000000001", "hex"), li = h.from("ffffffffffffffff", "hex"), di = {
|
|
2839
3119
|
script: ot,
|
|
2840
|
-
valueBuffer:
|
|
3120
|
+
valueBuffer: li
|
|
2841
3121
|
};
|
|
2842
|
-
function
|
|
3122
|
+
function wi(t) {
|
|
2843
3123
|
return "value" in t;
|
|
2844
3124
|
}
|
|
2845
3125
|
class g {
|
|
@@ -2847,7 +3127,7 @@ class g {
|
|
|
2847
3127
|
this.version = 1, this.locktime = 0, this.ins = [], this.outs = [];
|
|
2848
3128
|
}
|
|
2849
3129
|
static fromBuffer(e, r) {
|
|
2850
|
-
const n = new
|
|
3130
|
+
const n = new zs(e), s = new g();
|
|
2851
3131
|
s.version = n.readInt32();
|
|
2852
3132
|
const i = n.readUInt8(), o = n.readUInt8();
|
|
2853
3133
|
let u = !1;
|
|
@@ -2860,7 +3140,7 @@ class g {
|
|
|
2860
3140
|
index: d,
|
|
2861
3141
|
script: b,
|
|
2862
3142
|
sequence: w,
|
|
2863
|
-
witness:
|
|
3143
|
+
witness: Er
|
|
2864
3144
|
});
|
|
2865
3145
|
}
|
|
2866
3146
|
const a = n.readVarInt();
|
|
@@ -2900,7 +3180,7 @@ class g {
|
|
|
2900
3180
|
index: r,
|
|
2901
3181
|
script: s || ot,
|
|
2902
3182
|
sequence: n,
|
|
2903
|
-
witness:
|
|
3183
|
+
witness: Er
|
|
2904
3184
|
}) - 1;
|
|
2905
3185
|
}
|
|
2906
3186
|
addOutput(e, r) {
|
|
@@ -2921,7 +3201,7 @@ class g {
|
|
|
2921
3201
|
}
|
|
2922
3202
|
byteLength(e = !0) {
|
|
2923
3203
|
const r = e && this.hasWitnesses();
|
|
2924
|
-
return (r ? 10 : 8) + kt(this.ins.length) + kt(this.outs.length) + this.ins.reduce((n, s) => n + 40 + j(s.script), 0) + this.outs.reduce((n, s) => n + 8 + j(s.script), 0) + (r ? this.ins.reduce((n, s) => n +
|
|
3204
|
+
return (r ? 10 : 8) + kt(this.ins.length) + kt(this.outs.length) + this.ins.reduce((n, s) => n + 40 + j(s.script), 0) + this.outs.reduce((n, s) => n + 8 + j(s.script), 0) + (r ? this.ins.reduce((n, s) => n + pi(s.witness), 0) : 0);
|
|
2925
3205
|
}
|
|
2926
3206
|
clone() {
|
|
2927
3207
|
const e = new g();
|
|
@@ -2942,7 +3222,7 @@ class g {
|
|
|
2942
3222
|
r,
|
|
2943
3223
|
n
|
|
2944
3224
|
]), e >= this.ins.length)
|
|
2945
|
-
return
|
|
3225
|
+
return Sr;
|
|
2946
3226
|
const s = A(r);
|
|
2947
3227
|
if (!s)
|
|
2948
3228
|
throw new Error("Could not decompile prevOutScript");
|
|
@@ -2953,10 +3233,10 @@ class g {
|
|
|
2953
3233
|
});
|
|
2954
3234
|
else if ((n & 31) === g.SIGHASH_SINGLE) {
|
|
2955
3235
|
if (e >= this.outs.length)
|
|
2956
|
-
return
|
|
3236
|
+
return Sr;
|
|
2957
3237
|
o.outs.length = e + 1;
|
|
2958
3238
|
for (let f = 0; f < e; f++)
|
|
2959
|
-
o.outs[f] =
|
|
3239
|
+
o.outs[f] = di;
|
|
2960
3240
|
o.ins.forEach((f, a) => {
|
|
2961
3241
|
a !== e && (f.sequence = 0);
|
|
2962
3242
|
});
|
|
@@ -2986,7 +3266,7 @@ class g {
|
|
|
2986
3266
|
} else {
|
|
2987
3267
|
if (!this.outs.length)
|
|
2988
3268
|
throw new Error("Add outputs to the transaction before signing.");
|
|
2989
|
-
const O = this.outs.map((F) => 8 + j(F.script)).reduce((F,
|
|
3269
|
+
const O = this.outs.map((F) => 8 + j(F.script)).reduce((F, Gn) => F + Gn), T = x.withCapacity(O);
|
|
2990
3270
|
this.outs.forEach((F) => {
|
|
2991
3271
|
T.writeUInt64(F.value), T.writeVarSlice(F.script);
|
|
2992
3272
|
}), P = D(T.end());
|
|
@@ -3032,7 +3312,7 @@ class g {
|
|
|
3032
3312
|
return e && this.isCoinbase() ? h.alloc(32, 0) : it(this.__toBuffer(void 0, void 0, e));
|
|
3033
3313
|
}
|
|
3034
3314
|
getId() {
|
|
3035
|
-
return
|
|
3315
|
+
return We(this.getHash(!1)).toString("hex");
|
|
3036
3316
|
}
|
|
3037
3317
|
toBuffer(e, r) {
|
|
3038
3318
|
return this.__toBuffer(e, r, !0);
|
|
@@ -3054,7 +3334,7 @@ class g {
|
|
|
3054
3334
|
return i && (s.writeUInt8(g.ADVANCED_TRANSACTION_MARKER), s.writeUInt8(g.ADVANCED_TRANSACTION_FLAG)), s.writeVarInt(this.ins.length), this.ins.forEach((o) => {
|
|
3055
3335
|
s.writeSlice(o.hash), s.writeUInt32(o.index), s.writeVarSlice(o.script), s.writeUInt32(o.sequence);
|
|
3056
3336
|
}), s.writeVarInt(this.outs.length), this.outs.forEach((o) => {
|
|
3057
|
-
|
|
3337
|
+
wi(o) ? s.writeUInt64(o.value) : s.writeSlice(o.valueBuffer), s.writeVarSlice(o.script);
|
|
3058
3338
|
}), i && this.ins.forEach((o) => {
|
|
3059
3339
|
s.writeVector(o.witness);
|
|
3060
3340
|
}), s.writeUInt32(this.locktime), r !== void 0 ? e.subarray(r, s.offset) : e;
|
|
@@ -3073,259 +3353,6 @@ g.ADVANCED_TRANSACTION_FLAG = 1;
|
|
|
3073
3353
|
g.TRUC_VERSION = 3;
|
|
3074
3354
|
g.TRUC_MAX_VSIZE = 1e4;
|
|
3075
3355
|
g.TRUC_CHILD_MAX_VSIZE = 1e3;
|
|
3076
|
-
const vt = (t) => {
|
|
3077
|
-
const e = t.length === 32 ? t : t.subarray(1, 33);
|
|
3078
|
-
return h.isBuffer(e) ? e : h.from(e);
|
|
3079
|
-
};
|
|
3080
|
-
function yr(t, e, r) {
|
|
3081
|
-
const n = Hi(e, t, r);
|
|
3082
|
-
try {
|
|
3083
|
-
const i = Pi(e, n).concat(n.script).concat(n.controlBlock);
|
|
3084
|
-
return { finalScriptWitness: xt(i) };
|
|
3085
|
-
} catch (s) {
|
|
3086
|
-
throw new Error(`Can not finalize taproot input #${t}: ${s}`);
|
|
3087
|
-
}
|
|
3088
|
-
}
|
|
3089
|
-
function Mt(t, e) {
|
|
3090
|
-
const r = e ? h.from([e]) : h.from([]);
|
|
3091
|
-
return h.concat([t, r]);
|
|
3092
|
-
}
|
|
3093
|
-
function $(t) {
|
|
3094
|
-
return t && !!(t.tapInternalKey || t.tapMerkleRoot || t.tapLeafScript && t.tapLeafScript.length || t.tapBip32Derivation && t.tapBip32Derivation.length || t.witnessUtxo && Pn(t.witnessUtxo.script));
|
|
3095
|
-
}
|
|
3096
|
-
function me(t, e) {
|
|
3097
|
-
return t && !!(t.tapInternalKey || t.tapTree || t.tapBip32Derivation && t.tapBip32Derivation.length || e);
|
|
3098
|
-
}
|
|
3099
|
-
function mr(t, e, r) {
|
|
3100
|
-
Ti(t, e, r), Ai(t, e, r);
|
|
3101
|
-
}
|
|
3102
|
-
function br(t, e, r) {
|
|
3103
|
-
Oi(t, e, r), wi(t, e);
|
|
3104
|
-
}
|
|
3105
|
-
function wi(t, e) {
|
|
3106
|
-
if (!e.tapTree && !e.tapInternalKey)
|
|
3107
|
-
return;
|
|
3108
|
-
const r = e.tapInternalKey || t.tapInternalKey, n = e.tapTree || t.tapTree;
|
|
3109
|
-
if (r) {
|
|
3110
|
-
const s = t.script, i = gi(r, n);
|
|
3111
|
-
if (s && !s.equals(i))
|
|
3112
|
-
throw new Error("Error adding output. Script or address missmatch.");
|
|
3113
|
-
}
|
|
3114
|
-
}
|
|
3115
|
-
function gi(t, e) {
|
|
3116
|
-
const r = e && yi(e.leaves), { output: n } = mt({
|
|
3117
|
-
internalPubkey: t,
|
|
3118
|
-
scriptTree: r
|
|
3119
|
-
});
|
|
3120
|
-
if (!n)
|
|
3121
|
-
throw new Error("Failed to generate taproot script pubkey");
|
|
3122
|
-
return n;
|
|
3123
|
-
}
|
|
3124
|
-
function yi(t = []) {
|
|
3125
|
-
return t.length === 1 && t[0].depth === 0 ? {
|
|
3126
|
-
output: t[0].script,
|
|
3127
|
-
version: t[0].leafVersion
|
|
3128
|
-
} : _i(t);
|
|
3129
|
-
}
|
|
3130
|
-
function mi(t, e) {
|
|
3131
|
-
return Ei(t).some((n) => Hn(n, bi, e));
|
|
3132
|
-
}
|
|
3133
|
-
function bi(t) {
|
|
3134
|
-
return {
|
|
3135
|
-
signature: t.subarray(0, 64),
|
|
3136
|
-
hashType: t.subarray(64)[0] || g.SIGHASH_DEFAULT
|
|
3137
|
-
};
|
|
3138
|
-
}
|
|
3139
|
-
function Ei(t) {
|
|
3140
|
-
const e = [];
|
|
3141
|
-
if (t.tapKeySig && e.push(t.tapKeySig), t.tapScriptSig && e.push(...t.tapScriptSig.map((r) => r.signature)), !e.length) {
|
|
3142
|
-
const r = Si(t.finalScriptWitness);
|
|
3143
|
-
r && e.push(r);
|
|
3144
|
-
}
|
|
3145
|
-
return e;
|
|
3146
|
-
}
|
|
3147
|
-
function Si(t) {
|
|
3148
|
-
if (!t)
|
|
3149
|
-
return;
|
|
3150
|
-
const e = t.subarray(2);
|
|
3151
|
-
if (e.length === 64 || e.length === 65)
|
|
3152
|
-
return e;
|
|
3153
|
-
}
|
|
3154
|
-
function _i(t) {
|
|
3155
|
-
let e;
|
|
3156
|
-
for (const r of t)
|
|
3157
|
-
if (e = xe(r, e), !e)
|
|
3158
|
-
throw new Error("No room left to insert tapleaf in tree");
|
|
3159
|
-
return e;
|
|
3160
|
-
}
|
|
3161
|
-
function xe(t, e, r = 0) {
|
|
3162
|
-
if (r > cn)
|
|
3163
|
-
throw new Error("Max taptree depth exceeded.");
|
|
3164
|
-
if (t.depth === r)
|
|
3165
|
-
return e ? void 0 : {
|
|
3166
|
-
output: t.script,
|
|
3167
|
-
version: t.leafVersion
|
|
3168
|
-
};
|
|
3169
|
-
if (ne(e))
|
|
3170
|
-
return;
|
|
3171
|
-
const n = xe(t, e && e[0], r + 1);
|
|
3172
|
-
if (n)
|
|
3173
|
-
return [n, e && e[1]];
|
|
3174
|
-
const s = xe(t, e && e[1], r + 1);
|
|
3175
|
-
if (s)
|
|
3176
|
-
return [e && e[0], s];
|
|
3177
|
-
}
|
|
3178
|
-
function Ti(t, e, r) {
|
|
3179
|
-
const n = $(t) && gt(e), s = gt(t) && $(e), i = t === e && $(e) && gt(e);
|
|
3180
|
-
if (n || s || i)
|
|
3181
|
-
throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
|
|
3182
|
-
}
|
|
3183
|
-
function Oi(t, e, r) {
|
|
3184
|
-
const n = me(t) && gt(e), s = gt(t) && me(e), i = t === e && me(e) && gt(e);
|
|
3185
|
-
if (n || s || i)
|
|
3186
|
-
throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
|
|
3187
|
-
}
|
|
3188
|
-
function Ai(t, e, r) {
|
|
3189
|
-
if (e.tapMerkleRoot) {
|
|
3190
|
-
const n = (e.tapLeafScript || []).every((i) => be(i, e.tapMerkleRoot)), s = (t.tapLeafScript || []).every((i) => be(i, e.tapMerkleRoot));
|
|
3191
|
-
if (!n || !s)
|
|
3192
|
-
throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
|
|
3193
|
-
} else if (t.tapMerkleRoot && !(e.tapLeafScript || []).every((s) => be(s, t.tapMerkleRoot)))
|
|
3194
|
-
throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
|
|
3195
|
-
}
|
|
3196
|
-
function be(t, e) {
|
|
3197
|
-
if (!e)
|
|
3198
|
-
return !0;
|
|
3199
|
-
const r = Z({
|
|
3200
|
-
output: t.script,
|
|
3201
|
-
version: t.leafVersion
|
|
3202
|
-
});
|
|
3203
|
-
return Qt(t.controlBlock, r).equals(e);
|
|
3204
|
-
}
|
|
3205
|
-
function Pi(t, e) {
|
|
3206
|
-
const r = Z({
|
|
3207
|
-
output: e.script,
|
|
3208
|
-
version: e.leafVersion
|
|
3209
|
-
});
|
|
3210
|
-
return (t.tapScriptSig || []).filter((n) => n.leafHash.equals(r)).map((n) => Ii(e.script, n)).sort((n, s) => s.positionInScript - n.positionInScript).map((n) => n.signature);
|
|
3211
|
-
}
|
|
3212
|
-
function Ii(t, e) {
|
|
3213
|
-
return Object.assign({
|
|
3214
|
-
positionInScript: In(e.pubkey, t)
|
|
3215
|
-
}, e);
|
|
3216
|
-
}
|
|
3217
|
-
function Hi(t, e, r) {
|
|
3218
|
-
const { tapScriptSig: n } = t;
|
|
3219
|
-
if (!n || !n.length)
|
|
3220
|
-
throw new Error(`Can not finalize taproot input #${e}. No tapleaf script signature provided.`);
|
|
3221
|
-
const s = (t.tapLeafScript || []).sort((i, o) => i.controlBlock.length - o.controlBlock.length).find((i) => vi(i, n, r));
|
|
3222
|
-
if (!s)
|
|
3223
|
-
throw new Error(`Can not finalize taproot input #${e}. Signature for tapleaf script not found.`);
|
|
3224
|
-
return s;
|
|
3225
|
-
}
|
|
3226
|
-
function vi(t, e, r) {
|
|
3227
|
-
const n = Z({
|
|
3228
|
-
output: t.script,
|
|
3229
|
-
version: t.leafVersion
|
|
3230
|
-
});
|
|
3231
|
-
return (!r || r.equals(n)) && e.find((i) => i.leafHash.equals(n)) !== void 0;
|
|
3232
|
-
}
|
|
3233
|
-
function gt(t) {
|
|
3234
|
-
return t && !!(t.redeemScript || t.witnessScript || t.bip32Derivation && t.bip32Derivation.length);
|
|
3235
|
-
}
|
|
3236
|
-
const Er = E, Sr = 16, _r = 2, Tr = 40;
|
|
3237
|
-
function On(t, e) {
|
|
3238
|
-
if (!t.address && !t.output && !t.program && (typeof t.deploymentVersion > "u" || !t.hash160))
|
|
3239
|
-
throw new TypeError("At least one of address, output or program must be provided");
|
|
3240
|
-
e = Object.assign({ validate: !0 }, e || {}), c({
|
|
3241
|
-
address: c.maybe(c.String),
|
|
3242
|
-
output: c.maybe(c.Buffer),
|
|
3243
|
-
program: c.maybe(c.Buffer),
|
|
3244
|
-
network: c.maybe(c.Object),
|
|
3245
|
-
deploymentVersion: c.maybe(c.Number),
|
|
3246
|
-
hash160: c.maybe(c.BufferN(20))
|
|
3247
|
-
}, t);
|
|
3248
|
-
const r = () => {
|
|
3249
|
-
if (typeof t.deploymentVersion < "u" && typeof t.hash160 < "u") {
|
|
3250
|
-
if (t.hash160.length !== 20)
|
|
3251
|
-
throw new TypeError("hash160 must be exactly 20 bytes");
|
|
3252
|
-
if (t.deploymentVersion < 0 || t.deploymentVersion > 255)
|
|
3253
|
-
throw new TypeError("deploymentVersion must fit in one byte");
|
|
3254
|
-
return h.concat([h.of(t.deploymentVersion), t.hash160]);
|
|
3255
|
-
}
|
|
3256
|
-
}, n = B(() => Ve(t.address)), s = t.network || M, i = {
|
|
3257
|
-
name: K.P2OP,
|
|
3258
|
-
network: s,
|
|
3259
|
-
deploymentVersion: 0
|
|
3260
|
-
};
|
|
3261
|
-
if (y(i, "program", () => {
|
|
3262
|
-
if (t.program)
|
|
3263
|
-
return t.program;
|
|
3264
|
-
const o = r();
|
|
3265
|
-
if (o)
|
|
3266
|
-
return o;
|
|
3267
|
-
if (t.output) {
|
|
3268
|
-
if (t.output[0] !== Er.OP_16)
|
|
3269
|
-
throw new TypeError("Invalid P2OP script");
|
|
3270
|
-
let u = 1, f;
|
|
3271
|
-
if (t.output[1] < 76)
|
|
3272
|
-
f = t.output[1], u = 2;
|
|
3273
|
-
else if (t.output[1] === 76)
|
|
3274
|
-
f = t.output[2], u = 3;
|
|
3275
|
-
else
|
|
3276
|
-
throw new TypeError("Unsupported push opcode in P2OP script");
|
|
3277
|
-
return t.output.subarray(u, u + f);
|
|
3278
|
-
}
|
|
3279
|
-
if (t.address)
|
|
3280
|
-
return n().data;
|
|
3281
|
-
}), y(i, "deploymentVersion", () => {
|
|
3282
|
-
if (i.program)
|
|
3283
|
-
return i.program[0];
|
|
3284
|
-
}), y(i, "hash160", () => {
|
|
3285
|
-
if (i.program)
|
|
3286
|
-
return i.program.subarray(1);
|
|
3287
|
-
}), y(i, "output", () => {
|
|
3288
|
-
if (i.program)
|
|
3289
|
-
return v([Er.OP_16, i.program]);
|
|
3290
|
-
}), y(i, "address", () => {
|
|
3291
|
-
if (!i.program)
|
|
3292
|
-
return;
|
|
3293
|
-
if (!s.bech32Opnet)
|
|
3294
|
-
throw new TypeError("Network does not support opnet");
|
|
3295
|
-
const o = I.bech32m.toWords(i.program);
|
|
3296
|
-
return o.unshift(Sr), I.bech32m.encode(s.bech32Opnet, o);
|
|
3297
|
-
}), e.validate) {
|
|
3298
|
-
let o = U.alloc(0);
|
|
3299
|
-
if (t.address) {
|
|
3300
|
-
const u = n();
|
|
3301
|
-
if (s.bech32Opnet !== u.prefix)
|
|
3302
|
-
throw new TypeError("Invalid prefix or network mismatch");
|
|
3303
|
-
if (u.version !== Sr)
|
|
3304
|
-
throw new TypeError("Invalid witness version for p2op");
|
|
3305
|
-
if (u.data.length < _r || u.data.length > Tr)
|
|
3306
|
-
throw new TypeError("Invalid witness program length");
|
|
3307
|
-
o = u.data;
|
|
3308
|
-
}
|
|
3309
|
-
if (t.program) {
|
|
3310
|
-
if (o.length && !o.equals(t.program))
|
|
3311
|
-
throw new TypeError("Program mismatch");
|
|
3312
|
-
o = t.program;
|
|
3313
|
-
}
|
|
3314
|
-
if (!o.length && t.deploymentVersion !== void 0 && t.hash160 && (o = r()), t.output) {
|
|
3315
|
-
const u = i.program;
|
|
3316
|
-
if (o.length && !o.equals(u))
|
|
3317
|
-
throw new TypeError("Program mismatch (output vs other source)");
|
|
3318
|
-
o = u;
|
|
3319
|
-
}
|
|
3320
|
-
if (o.length < _r || o.length > Tr)
|
|
3321
|
-
throw new TypeError(`Witness program must be 2–40 bytes. Was ${o.length} bytes`);
|
|
3322
|
-
if (t.deploymentVersion !== void 0 && t.deploymentVersion !== o[0])
|
|
3323
|
-
throw new TypeError("deploymentVersion mismatch");
|
|
3324
|
-
if (t.hash160 && !t.hash160.equals(o.subarray(1)))
|
|
3325
|
-
throw new TypeError("hash160 mismatch");
|
|
3326
|
-
}
|
|
3327
|
-
return Object.assign(i, t);
|
|
3328
|
-
}
|
|
3329
3356
|
function pt(t) {
|
|
3330
3357
|
return (e) => {
|
|
3331
3358
|
try {
|
|
@@ -3335,7 +3362,7 @@ function pt(t) {
|
|
|
3335
3362
|
}
|
|
3336
3363
|
};
|
|
3337
3364
|
}
|
|
3338
|
-
const
|
|
3365
|
+
const gi = pt(Vt), yi = pt(De), mi = pt(yt), he = pt(Lt), _r = pt(Et), On = pt(mt), An = pt(bt), uo = (t) => t.length === 4 && t[0] === 81 && t[1] === 2 && t[2] === 78 && t[3] === 115;
|
|
3339
3366
|
function xt(t) {
|
|
3340
3367
|
let e = h.allocUnsafe(0);
|
|
3341
3368
|
function r(o) {
|
|
@@ -3351,61 +3378,27 @@ function xt(t) {
|
|
|
3351
3378
|
function i(o) {
|
|
3352
3379
|
n(o.length), o.forEach(s);
|
|
3353
3380
|
}
|
|
3354
|
-
return i(t), e;
|
|
3355
|
-
}
|
|
3356
|
-
function Ue(t) {
|
|
3357
|
-
if (t.length === 32)
|
|
3358
|
-
return;
|
|
3359
|
-
if (![33, 65].includes(t.length)) {
|
|
3360
|
-
console.warn(`Unsupported key length=${t.length}. Must be 33 (compressed) or 65 (uncompressed).`);
|
|
3361
|
-
return;
|
|
3362
|
-
}
|
|
3363
|
-
let e;
|
|
3364
|
-
try {
|
|
3365
|
-
e = R.fromHex(t);
|
|
3366
|
-
} catch {
|
|
3367
|
-
throw new Error("Invalid secp256k1 public key bytes. Cannot parse.");
|
|
3368
|
-
}
|
|
3369
|
-
const r = Ar(e.x), n = Ar(e.y), i = e.y % 2n === 0n ? 6 : 7, o = h.alloc(65);
|
|
3370
|
-
o[0] = i, r.copy(o, 1), n.copy(o, 33);
|
|
3371
|
-
const u = h.concat([h.from([4]), r, n]);
|
|
3372
|
-
return {
|
|
3373
|
-
hybrid: o,
|
|
3374
|
-
uncompressed: u
|
|
3375
|
-
};
|
|
3376
|
-
}
|
|
3377
|
-
function Ar(t) {
|
|
3378
|
-
let e = t.toString(16);
|
|
3379
|
-
return e = e.padStart(64, "0"), e.length > 64 && (e = e.slice(-64)), h.from(e, "hex");
|
|
3380
|
-
}
|
|
3381
|
-
function Dt(t, e) {
|
|
3382
|
-
if (t.equals(e))
|
|
3383
|
-
return !0;
|
|
3384
|
-
if (t.length === 65 && e.length === 65) {
|
|
3385
|
-
const r = h.from(t), n = h.from(e);
|
|
3386
|
-
return (r[0] === 6 || r[0] === 7) && (r[0] = 4), (n[0] === 6 || n[0] === 7) && (n[0] = 4), r.equals(n);
|
|
3387
|
-
}
|
|
3388
|
-
return !1;
|
|
3381
|
+
return i(t), e;
|
|
3389
3382
|
}
|
|
3390
|
-
function
|
|
3383
|
+
function Pn(t, e) {
|
|
3391
3384
|
const r = A(e);
|
|
3392
3385
|
if (r === null)
|
|
3393
3386
|
throw new Error("Unknown script error");
|
|
3394
|
-
const n = C(t), s =
|
|
3387
|
+
const n = C(t), s = Ht(t), i = Ie(t), o = i?.hybrid ? C(i.hybrid) : void 0, u = i?.uncompressed ? C(i.uncompressed) : void 0;
|
|
3395
3388
|
return r.findIndex((f) => {
|
|
3396
3389
|
if (typeof f == "number")
|
|
3397
3390
|
return !1;
|
|
3398
|
-
if (
|
|
3391
|
+
if (Gt(f, t) || Gt(f, s) || f.equals(n) || i && (Gt(f, i.uncompressed) || Gt(f, i.hybrid) || o && f.equals(o) || u && f.equals(u)))
|
|
3399
3392
|
return !0;
|
|
3400
3393
|
});
|
|
3401
3394
|
}
|
|
3402
3395
|
function pe(t, e) {
|
|
3403
|
-
return
|
|
3396
|
+
return Pn(t, e) !== -1;
|
|
3404
3397
|
}
|
|
3405
|
-
function
|
|
3406
|
-
return
|
|
3398
|
+
function bi(t, e) {
|
|
3399
|
+
return Ei(t).some((n) => In(n, dt.decode, e));
|
|
3407
3400
|
}
|
|
3408
|
-
function
|
|
3401
|
+
function In(t, e, r) {
|
|
3409
3402
|
const { hashType: n } = e(t), s = [];
|
|
3410
3403
|
switch (n & g.SIGHASH_ANYONECANPAY && s.push("addInput"), n & 31) {
|
|
3411
3404
|
case g.SIGHASH_ALL:
|
|
@@ -3417,175 +3410,183 @@ function Hn(t, e, r) {
|
|
|
3417
3410
|
}
|
|
3418
3411
|
return s.indexOf(r) === -1;
|
|
3419
3412
|
}
|
|
3420
|
-
function
|
|
3413
|
+
function Ei(t) {
|
|
3421
3414
|
const { partialSig: e } = t;
|
|
3422
3415
|
let r;
|
|
3423
3416
|
if (!e || e.length === 0) {
|
|
3424
3417
|
if (!t.finalScriptSig && !t.finalScriptWitness)
|
|
3425
3418
|
return [];
|
|
3426
|
-
r =
|
|
3419
|
+
r = Si(t);
|
|
3427
3420
|
} else
|
|
3428
3421
|
r = e;
|
|
3429
3422
|
return r.map((n) => n.signature);
|
|
3430
3423
|
}
|
|
3431
|
-
function
|
|
3424
|
+
function Si(t) {
|
|
3432
3425
|
const e = t.finalScriptSig ? A(t.finalScriptSig) || [] : [], r = t.finalScriptWitness ? A(t.finalScriptWitness) || [] : [];
|
|
3433
3426
|
return e.concat(r).filter((n) => h.isBuffer(n) && Q(n)).map((n) => ({ signature: n }));
|
|
3434
3427
|
}
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3428
|
+
function Tr(t, e, r) {
|
|
3429
|
+
const n = Bi(e, t, r);
|
|
3430
|
+
try {
|
|
3431
|
+
const i = Ni(e, n).concat(n.script).concat(n.controlBlock);
|
|
3432
|
+
return { finalScriptWitness: xt(i) };
|
|
3433
|
+
} catch (s) {
|
|
3434
|
+
throw new Error(`Can not finalize taproot input #${t}: ${s}`);
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
function Dt(t, e) {
|
|
3438
|
+
const r = e ? h.from([e]) : h.from([]);
|
|
3439
|
+
return h.concat([t, r]);
|
|
3440
|
+
}
|
|
3441
|
+
function $(t) {
|
|
3442
|
+
return t && !!(t.tapInternalKey || t.tapMerkleRoot || t.tapLeafScript && t.tapLeafScript.length || t.tapBip32Derivation && t.tapBip32Derivation.length || t.witnessUtxo && An(t.witnessUtxo.script));
|
|
3443
|
+
}
|
|
3444
|
+
function me(t, e) {
|
|
3445
|
+
return t && !!(t.tapInternalKey || t.tapTree || t.tapBip32Derivation && t.tapBip32Derivation.length || e);
|
|
3446
|
+
}
|
|
3447
|
+
function Or(t, e, r) {
|
|
3448
|
+
xi(t, e, r), ki(t, e, r);
|
|
3449
|
+
}
|
|
3450
|
+
function Ar(t, e, r) {
|
|
3451
|
+
Ui(t, e, r), _i(t, e);
|
|
3452
|
+
}
|
|
3453
|
+
function _i(t, e) {
|
|
3454
|
+
if (!e.tapTree && !e.tapInternalKey)
|
|
3455
|
+
return;
|
|
3456
|
+
const r = e.tapInternalKey || t.tapInternalKey, n = e.tapTree || t.tapTree;
|
|
3457
|
+
if (r) {
|
|
3458
|
+
const s = t.script, i = Ti(r, n);
|
|
3459
|
+
if (s && !s.equals(i))
|
|
3460
|
+
throw new Error("Error adding output. Script or address missmatch.");
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
3463
|
+
function Ti(t, e) {
|
|
3464
|
+
const r = e && Oi(e.leaves), { output: n } = bt({
|
|
3465
|
+
internalPubkey: t,
|
|
3466
|
+
scriptTree: r
|
|
3467
|
+
});
|
|
3468
|
+
if (!n)
|
|
3469
|
+
throw new Error("Failed to generate taproot script pubkey");
|
|
3470
|
+
return n;
|
|
3471
|
+
}
|
|
3472
|
+
function Oi(t = []) {
|
|
3473
|
+
return t.length === 1 && t[0].depth === 0 ? {
|
|
3474
|
+
output: t[0].script,
|
|
3475
|
+
version: t[0].leafVersion
|
|
3476
|
+
} : vi(t);
|
|
3477
|
+
}
|
|
3478
|
+
function Ai(t, e) {
|
|
3479
|
+
return Ii(t).some((n) => In(n, Pi, e));
|
|
3480
|
+
}
|
|
3481
|
+
function Pi(t) {
|
|
3482
|
+
return {
|
|
3483
|
+
signature: t.subarray(0, 64),
|
|
3484
|
+
hashType: t.subarray(64)[0] || g.SIGHASH_DEFAULT
|
|
3455
3485
|
};
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
if (t.output)
|
|
3463
|
-
return t.output.subarray(3, 23);
|
|
3464
|
-
if (t.address)
|
|
3465
|
-
return r().hash;
|
|
3466
|
-
if (t.pubkey || i.pubkey)
|
|
3467
|
-
return C(t.pubkey || i.pubkey);
|
|
3468
|
-
}), y(i, "output", () => {
|
|
3469
|
-
if (i.hash)
|
|
3470
|
-
return v([
|
|
3471
|
-
st.OP_DUP,
|
|
3472
|
-
st.OP_HASH160,
|
|
3473
|
-
i.hash,
|
|
3474
|
-
st.OP_EQUALVERIFY,
|
|
3475
|
-
st.OP_CHECKSIG
|
|
3476
|
-
]);
|
|
3477
|
-
}), y(i, "pubkey", () => {
|
|
3478
|
-
if (t.input)
|
|
3479
|
-
return n()[1];
|
|
3480
|
-
}), y(i, "signature", () => {
|
|
3481
|
-
if (t.input)
|
|
3482
|
-
return n()[0];
|
|
3483
|
-
}), y(i, "input", () => {
|
|
3484
|
-
if (!t.pubkey || !t.signature)
|
|
3485
|
-
return;
|
|
3486
|
-
let o = t.pubkey;
|
|
3487
|
-
if (t.useHybrid || t.useUncompressed) {
|
|
3488
|
-
const u = Ue(t.pubkey);
|
|
3489
|
-
u && (t.useUncompressed ? o = u.uncompressed : o = u.hybrid);
|
|
3490
|
-
}
|
|
3491
|
-
return v([t.signature, o]);
|
|
3492
|
-
}), y(i, "witness", () => {
|
|
3493
|
-
if (i.input)
|
|
3494
|
-
return [];
|
|
3495
|
-
}), e.validate) {
|
|
3496
|
-
let o = h.from([]);
|
|
3497
|
-
if (t.address) {
|
|
3498
|
-
if (r().version !== s.pubKeyHash)
|
|
3499
|
-
throw new TypeError("Invalid version or Network mismatch");
|
|
3500
|
-
if (r().hash.length !== 20)
|
|
3501
|
-
throw new TypeError("Invalid address");
|
|
3502
|
-
o = r().hash;
|
|
3503
|
-
}
|
|
3504
|
-
if (t.hash) {
|
|
3505
|
-
if (o.length > 0 && !o.equals(t.hash))
|
|
3506
|
-
throw new TypeError("Hash mismatch");
|
|
3507
|
-
o = t.hash;
|
|
3508
|
-
}
|
|
3509
|
-
if (t.output) {
|
|
3510
|
-
if (t.output.length !== 25 || t.output[0] !== st.OP_DUP || t.output[1] !== st.OP_HASH160 || t.output[2] !== 20 || t.output[23] !== st.OP_EQUALVERIFY || t.output[24] !== st.OP_CHECKSIG)
|
|
3511
|
-
throw new TypeError("Output is invalid");
|
|
3512
|
-
const u = t.output.subarray(3, 23);
|
|
3513
|
-
if (o.length > 0 && !o.equals(u))
|
|
3514
|
-
throw new TypeError("Hash mismatch");
|
|
3515
|
-
o = u;
|
|
3516
|
-
}
|
|
3517
|
-
if (t.pubkey) {
|
|
3518
|
-
const u = C(t.pubkey);
|
|
3519
|
-
let f = o.length > 0 && !o.equals(u);
|
|
3520
|
-
if (f && (t.pubkey.length === 33 && (t.pubkey[0] === 2 || t.pubkey[0] === 3) || t.pubkey.length === 65 && t.pubkey[0] === 4)) {
|
|
3521
|
-
const a = Ue(t.pubkey);
|
|
3522
|
-
if (a) {
|
|
3523
|
-
const p = C(a.uncompressed);
|
|
3524
|
-
if (o.equals(p))
|
|
3525
|
-
f = !1, t.useUncompressed = !0;
|
|
3526
|
-
else {
|
|
3527
|
-
const l = C(a.hybrid);
|
|
3528
|
-
f = !o.equals(l), f || (t.useHybrid = !0);
|
|
3529
|
-
}
|
|
3530
|
-
}
|
|
3531
|
-
}
|
|
3532
|
-
if (f)
|
|
3533
|
-
throw new TypeError("Hash mismatch");
|
|
3534
|
-
o = u;
|
|
3535
|
-
}
|
|
3536
|
-
if (t.input) {
|
|
3537
|
-
const u = n();
|
|
3538
|
-
if (u.length !== 2)
|
|
3539
|
-
throw new TypeError("Input is invalid");
|
|
3540
|
-
if (!Q(u[0]))
|
|
3541
|
-
throw new TypeError("Input has invalid signature");
|
|
3542
|
-
if (!W(u[1]))
|
|
3543
|
-
throw new TypeError("Input has invalid pubkey");
|
|
3544
|
-
if (t.signature && !t.signature.equals(u[0]))
|
|
3545
|
-
throw new TypeError("Signature mismatch");
|
|
3546
|
-
if (t.pubkey && !t.pubkey.equals(u[1]))
|
|
3547
|
-
throw new TypeError("Pubkey mismatch");
|
|
3548
|
-
const f = C(u[1]);
|
|
3549
|
-
if (o.length > 0 && !o.equals(f))
|
|
3550
|
-
throw new TypeError("Hash mismatch (input)");
|
|
3551
|
-
}
|
|
3486
|
+
}
|
|
3487
|
+
function Ii(t) {
|
|
3488
|
+
const e = [];
|
|
3489
|
+
if (t.tapKeySig && e.push(t.tapKeySig), t.tapScriptSig && e.push(...t.tapScriptSig.map((r) => r.signature)), !e.length) {
|
|
3490
|
+
const r = Hi(t.finalScriptWitness);
|
|
3491
|
+
r && e.push(r);
|
|
3552
3492
|
}
|
|
3553
|
-
return
|
|
3493
|
+
return e;
|
|
3554
3494
|
}
|
|
3555
|
-
|
|
3556
|
-
(
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3495
|
+
function Hi(t) {
|
|
3496
|
+
if (!t)
|
|
3497
|
+
return;
|
|
3498
|
+
const e = t.subarray(2);
|
|
3499
|
+
if (e.length === 64 || e.length === 65)
|
|
3500
|
+
return e;
|
|
3501
|
+
}
|
|
3502
|
+
function vi(t) {
|
|
3503
|
+
let e;
|
|
3504
|
+
for (const r of t)
|
|
3505
|
+
if (e = Ue(r, e), !e)
|
|
3506
|
+
throw new Error("No room left to insert tapleaf in tree");
|
|
3507
|
+
return e;
|
|
3508
|
+
}
|
|
3509
|
+
function Ue(t, e, r = 0) {
|
|
3510
|
+
if (r > Tn)
|
|
3511
|
+
throw new Error("Max taptree depth exceeded.");
|
|
3512
|
+
if (t.depth === r)
|
|
3513
|
+
return e ? void 0 : {
|
|
3514
|
+
output: t.script,
|
|
3515
|
+
version: t.leafVersion
|
|
3516
|
+
};
|
|
3517
|
+
if (ne(e))
|
|
3518
|
+
return;
|
|
3519
|
+
const n = Ue(t, e && e[0], r + 1);
|
|
3520
|
+
if (n)
|
|
3521
|
+
return [n, e && e[1]];
|
|
3522
|
+
const s = Ue(t, e && e[1], r + 1);
|
|
3523
|
+
if (s)
|
|
3524
|
+
return [e && e[0], s];
|
|
3525
|
+
}
|
|
3526
|
+
function xi(t, e, r) {
|
|
3527
|
+
const n = $(t) && gt(e), s = gt(t) && $(e), i = t === e && $(e) && gt(e);
|
|
3528
|
+
if (n || s || i)
|
|
3529
|
+
throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
|
|
3530
|
+
}
|
|
3531
|
+
function Ui(t, e, r) {
|
|
3532
|
+
const n = me(t) && gt(e), s = gt(t) && me(e), i = t === e && me(e) && gt(e);
|
|
3533
|
+
if (n || s || i)
|
|
3534
|
+
throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
|
|
3535
|
+
}
|
|
3536
|
+
function ki(t, e, r) {
|
|
3537
|
+
if (e.tapMerkleRoot) {
|
|
3538
|
+
const n = (e.tapLeafScript || []).every((i) => be(i, e.tapMerkleRoot)), s = (t.tapLeafScript || []).every((i) => be(i, e.tapMerkleRoot));
|
|
3539
|
+
if (!n || !s)
|
|
3540
|
+
throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
|
|
3541
|
+
} else if (t.tapMerkleRoot && !(e.tapLeafScript || []).every((s) => be(s, t.tapMerkleRoot)))
|
|
3542
|
+
throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
|
|
3543
|
+
}
|
|
3544
|
+
function be(t, e) {
|
|
3545
|
+
if (!e)
|
|
3546
|
+
return !0;
|
|
3547
|
+
const r = Z({
|
|
3548
|
+
output: t.script,
|
|
3549
|
+
version: t.leafVersion
|
|
3550
|
+
});
|
|
3551
|
+
return te(t.controlBlock, r).equals(e);
|
|
3552
|
+
}
|
|
3553
|
+
function Ni(t, e) {
|
|
3554
|
+
const r = Z({
|
|
3555
|
+
output: e.script,
|
|
3556
|
+
version: e.leafVersion
|
|
3557
|
+
});
|
|
3558
|
+
return (t.tapScriptSig || []).filter((n) => n.leafHash.equals(r)).map((n) => Ci(e.script, n)).sort((n, s) => s.positionInScript - n.positionInScript).map((n) => n.signature);
|
|
3559
|
+
}
|
|
3560
|
+
function Ci(t, e) {
|
|
3561
|
+
return Object.assign({
|
|
3562
|
+
positionInScript: Pn(e.pubkey, t)
|
|
3563
|
+
}, e);
|
|
3564
|
+
}
|
|
3565
|
+
function Bi(t, e, r) {
|
|
3566
|
+
const { tapScriptSig: n } = t;
|
|
3567
|
+
if (!n || !n.length)
|
|
3568
|
+
throw new Error(`Can not finalize taproot input #${e}. No tapleaf script signature provided.`);
|
|
3569
|
+
const s = (t.tapLeafScript || []).sort((i, o) => i.controlBlock.length - o.controlBlock.length).find((i) => Ri(i, n, r));
|
|
3570
|
+
if (!s)
|
|
3571
|
+
throw new Error(`Can not finalize taproot input #${e}. Signature for tapleaf script not found.`);
|
|
3572
|
+
return s;
|
|
3573
|
+
}
|
|
3574
|
+
function Ri(t, e, r) {
|
|
3575
|
+
const n = Z({
|
|
3576
|
+
output: t.script,
|
|
3577
|
+
version: t.leafVersion
|
|
3578
|
+
});
|
|
3579
|
+
return (!r || r.equals(n)) && e.find((i) => i.leafHash.equals(n)) !== void 0;
|
|
3580
|
+
}
|
|
3581
|
+
function gt(t) {
|
|
3582
|
+
return t && !!(t.redeemScript || t.witnessScript || t.bip32Derivation && t.bip32Derivation.length);
|
|
3583
|
+
}
|
|
3584
|
+
const Pr = Se.decode, Fi = {
|
|
3584
3585
|
network: M,
|
|
3585
3586
|
maximumFeeRate: 5e3
|
|
3586
3587
|
};
|
|
3587
3588
|
class ke {
|
|
3588
|
-
constructor(e = {}, r = new qe.Psbt(new
|
|
3589
|
+
constructor(e = {}, r = new qe.Psbt(new Hn())) {
|
|
3589
3590
|
this.data = r, this.opts = Object.assign({}, Fi, e), this.__CACHE = {
|
|
3590
3591
|
__NON_WITNESS_UTXO_TX_CACHE: [],
|
|
3591
3592
|
__NON_WITNESS_UTXO_BUF_CACHE: [],
|
|
@@ -3618,7 +3619,7 @@ class ke {
|
|
|
3618
3619
|
}
|
|
3619
3620
|
get txInputs() {
|
|
3620
3621
|
return this.__CACHE.__TX.ins.map((e) => ({
|
|
3621
|
-
hash:
|
|
3622
|
+
hash: lr(e.hash),
|
|
3622
3623
|
index: e.index,
|
|
3623
3624
|
sequence: e.sequence
|
|
3624
3625
|
}));
|
|
@@ -3627,11 +3628,11 @@ class ke {
|
|
|
3627
3628
|
return this.__CACHE.__TX.outs.map((e) => {
|
|
3628
3629
|
let r;
|
|
3629
3630
|
try {
|
|
3630
|
-
r =
|
|
3631
|
+
r = fi(e.script, this.opts.network);
|
|
3631
3632
|
} catch {
|
|
3632
3633
|
}
|
|
3633
3634
|
return {
|
|
3634
|
-
script:
|
|
3635
|
+
script: lr(e.script),
|
|
3635
3636
|
value: e.value,
|
|
3636
3637
|
address: r
|
|
3637
3638
|
};
|
|
@@ -3685,11 +3686,11 @@ class ke {
|
|
|
3685
3686
|
addInput(e, r = !0) {
|
|
3686
3687
|
if (!e || e.hash === void 0 || e.index === void 0)
|
|
3687
3688
|
throw new Error("Invalid arguments for Psbt.addInput. Requires single object with at least [hash] and [index]");
|
|
3688
|
-
|
|
3689
|
+
Or(e, e, "addInput"), r && Tt(this.data.inputs, "addInput"), e.witnessScript && re(e.witnessScript);
|
|
3689
3690
|
const n = this.__CACHE;
|
|
3690
3691
|
this.data.addInput(e);
|
|
3691
3692
|
const s = n.__TX.ins[n.__TX.ins.length - 1];
|
|
3692
|
-
|
|
3693
|
+
Un(n, s);
|
|
3693
3694
|
const i = this.data.inputs.length - 1, o = this.data.inputs[i];
|
|
3694
3695
|
return o.nonWitnessUtxo && Ce(this.__CACHE, o, i), n.__FEE = void 0, n.__FEE_RATE = void 0, n.__EXTRACTED_TX = void 0, this;
|
|
3695
3696
|
}
|
|
@@ -3701,21 +3702,21 @@ class ke {
|
|
|
3701
3702
|
if (arguments.length > 1 || !e || e.value === void 0 || !r && !n)
|
|
3702
3703
|
throw new Error("Invalid arguments for Psbt.addOutput. Requires single object with at least [script or address] and [value]");
|
|
3703
3704
|
if (Tt(this.data.inputs, "addOutput"), r) {
|
|
3704
|
-
const { address: i } = e, { network: o } = this.opts, u =
|
|
3705
|
+
const { address: i } = e, { network: o } = this.opts, u = ci(i, o);
|
|
3705
3706
|
e = Object.assign({}, e, { script: u });
|
|
3706
3707
|
}
|
|
3707
|
-
|
|
3708
|
+
Ar(e, e, "addOutput");
|
|
3708
3709
|
const s = this.__CACHE;
|
|
3709
3710
|
return this.data.addOutput(e), s.__FEE = void 0, s.__FEE_RATE = void 0, s.__EXTRACTED_TX = void 0, this;
|
|
3710
3711
|
}
|
|
3711
3712
|
extractTransaction(e, r) {
|
|
3712
|
-
if (r && (this.data.inputs = this.data.inputs.filter((i) => !i.partialSig)), !this.data.inputs.every(
|
|
3713
|
+
if (r && (this.data.inputs = this.data.inputs.filter((i) => !i.partialSig)), !this.data.inputs.every(vn))
|
|
3713
3714
|
throw new Error("Not finalized");
|
|
3714
3715
|
const n = this.__CACHE;
|
|
3715
3716
|
if (e || Wi(this, n, this.opts), n.__EXTRACTED_TX)
|
|
3716
3717
|
return n.__EXTRACTED_TX;
|
|
3717
3718
|
const s = n.__TX.clone();
|
|
3718
|
-
return
|
|
3719
|
+
return Fn(this.data.inputs, s, n, !0, r), s;
|
|
3719
3720
|
}
|
|
3720
3721
|
getFeeRate(e = !1) {
|
|
3721
3722
|
return Ur("__FEE_RATE", "fee rate", this.data.inputs, this.__CACHE, e);
|
|
@@ -3730,14 +3731,14 @@ class ke {
|
|
|
3730
3731
|
const s = N.checkForInput(this.data.inputs, e);
|
|
3731
3732
|
return $(s) ? this._finalizeTaprootInput(e, s, void 0, r) : this._finalizeInput(e, s, r, n ?? !0);
|
|
3732
3733
|
}
|
|
3733
|
-
finalizeTaprootInput(e, r, n =
|
|
3734
|
+
finalizeTaprootInput(e, r, n = Tr) {
|
|
3734
3735
|
const s = N.checkForInput(this.data.inputs, e);
|
|
3735
3736
|
if ($(s))
|
|
3736
3737
|
return this._finalizeTaprootInput(e, s, r, n);
|
|
3737
3738
|
throw new Error(`Cannot finalize input #${e}. Not Taproot.`);
|
|
3738
3739
|
}
|
|
3739
3740
|
getInputType(e) {
|
|
3740
|
-
const r = N.checkForInput(this.data.inputs, e), n =
|
|
3741
|
+
const r = N.checkForInput(this.data.inputs, e), n = Ln(e, r, this.__CACHE), s = de(n, e, "input", r.redeemScript || Ji(r.finalScriptSig), r.witnessScript || to(r.finalScriptWitness)), i = s.type === "raw" ? "" : s.type + "-", o = Wn(s.meaningfulScript);
|
|
3741
3742
|
return i + o;
|
|
3742
3743
|
}
|
|
3743
3744
|
inputHasPubkey(e, r) {
|
|
@@ -3887,11 +3888,11 @@ class ke {
|
|
|
3887
3888
|
return this.data.updateGlobal(e), this;
|
|
3888
3889
|
}
|
|
3889
3890
|
updateInput(e, r) {
|
|
3890
|
-
return r.witnessScript && re(r.witnessScript),
|
|
3891
|
+
return r.witnessScript && re(r.witnessScript), Or(this.data.inputs[e], r, "updateInput"), this.data.updateInput(e, r), r.nonWitnessUtxo && Ce(this.__CACHE, this.data.inputs[e], e), this;
|
|
3891
3892
|
}
|
|
3892
3893
|
updateOutput(e, r) {
|
|
3893
3894
|
const n = this.data.outputs[e];
|
|
3894
|
-
return
|
|
3895
|
+
return Ar(n, r, "updateOutput"), this.data.updateOutput(e, r), this;
|
|
3895
3896
|
}
|
|
3896
3897
|
addUnknownKeyValToGlobal(e) {
|
|
3897
3898
|
return this.data.addUnknownKeyValToGlobal(e), this;
|
|
@@ -3923,11 +3924,11 @@ class ke {
|
|
|
3923
3924
|
throw new Error(`Unknown error finalizing input #${e}`);
|
|
3924
3925
|
return this.data.clearFinalizedInput(e), this;
|
|
3925
3926
|
}
|
|
3926
|
-
_finalizeTaprootInput(e, r, n, s =
|
|
3927
|
+
_finalizeTaprootInput(e, r, n, s = Tr) {
|
|
3927
3928
|
if (!r.witnessUtxo)
|
|
3928
3929
|
throw new Error(`Cannot finalize input #${e}. Missing witness utxo.`);
|
|
3929
3930
|
if (r.tapKeySig) {
|
|
3930
|
-
const i =
|
|
3931
|
+
const i = bt({
|
|
3931
3932
|
output: r.witnessUtxo.script,
|
|
3932
3933
|
signature: r.tapKeySig
|
|
3933
3934
|
});
|
|
@@ -3953,10 +3954,10 @@ class ke {
|
|
|
3953
3954
|
const u = [];
|
|
3954
3955
|
let f, a, p;
|
|
3955
3956
|
for (const l of o) {
|
|
3956
|
-
const d = dt.decode(l.signature), { hash: b, script: w } = p !== d.hashType || !f || !a ?
|
|
3957
|
+
const d = dt.decode(l.signature), { hash: b, script: w } = p !== d.hashType || !f || !a ? Nn(e, Object.assign({}, s, {
|
|
3957
3958
|
sighashType: d.hashType
|
|
3958
3959
|
}), this.__CACHE, !0) : { hash: f, script: a };
|
|
3959
|
-
p = d.hashType, f = b, a = w,
|
|
3960
|
+
p = d.hashType, f = b, a = w, xn(l.pubkey, w, "verify"), u.push(r(l.pubkey, b, d.signature));
|
|
3960
3961
|
}
|
|
3961
3962
|
return u.every((l) => l);
|
|
3962
3963
|
}
|
|
@@ -3966,7 +3967,7 @@ class ke {
|
|
|
3966
3967
|
throw new Error("No signatures to validate");
|
|
3967
3968
|
if (typeof r != "function")
|
|
3968
3969
|
throw new Error("Need validator function to validate signatures");
|
|
3969
|
-
n = n &&
|
|
3970
|
+
n = n && Ht(n);
|
|
3970
3971
|
const u = n ? Ne(e, s, this.data.inputs, n, this.__CACHE) : qi(e, s, this.data.inputs, this.__CACHE);
|
|
3971
3972
|
if (!u.length)
|
|
3972
3973
|
throw new Error("No signatures for this pubkey");
|
|
@@ -3998,9 +3999,9 @@ class ke {
|
|
|
3998
3999
|
return this.data.updateInput(e, { partialSig: f }), this;
|
|
3999
4000
|
}
|
|
4000
4001
|
_signTaprootInput(e, r, n, s, i = [g.SIGHASH_DEFAULT]) {
|
|
4001
|
-
const o = h.isBuffer(n.publicKey) ? n.publicKey : h.from(n.publicKey), u = this.checkTaprootHashesForSig(e, r, n, s, i), f = n.signSchnorr.bind(n), a = (d) => h.isBuffer(d) ? d : h.from(d), p = u.filter((d) => !d.leafHash).map((d) =>
|
|
4002
|
-
pubkey:
|
|
4003
|
-
signature:
|
|
4002
|
+
const o = h.isBuffer(n.publicKey) ? n.publicKey : h.from(n.publicKey), u = this.checkTaprootHashesForSig(e, r, n, s, i), f = n.signSchnorr.bind(n), a = (d) => h.isBuffer(d) ? d : h.from(d), p = u.filter((d) => !d.leafHash).map((d) => Dt(a(f(d.hash)), r.sighashType))[0], l = u.filter((d) => !!d.leafHash).map((d) => ({
|
|
4003
|
+
pubkey: Ht(o),
|
|
4004
|
+
signature: Dt(a(f(d.hash)), r.sighashType),
|
|
4004
4005
|
leafHash: d.leafHash
|
|
4005
4006
|
}));
|
|
4006
4007
|
return p && this.data.updateInput(e, { tapKeySig: p }), l.length && this.data.updateInput(e, { tapScriptSig: l }), this;
|
|
@@ -4021,7 +4022,7 @@ class ke {
|
|
|
4021
4022
|
const o = h.isBuffer(n.publicKey) ? n.publicKey : h.from(n.publicKey), u = this.checkTaprootHashesForSig(e, r, n, s, i), f = n.signSchnorr.bind(n), a = (w) => h.isBuffer(w) ? w : h.from(w), p = [], l = u.filter((w) => !w.leafHash)[0];
|
|
4022
4023
|
if (l) {
|
|
4023
4024
|
const w = Promise.resolve(f(l.hash)).then((S) => ({
|
|
4024
|
-
tapKeySig:
|
|
4025
|
+
tapKeySig: Dt(a(S), r.sighashType)
|
|
4025
4026
|
}));
|
|
4026
4027
|
p.push(w);
|
|
4027
4028
|
}
|
|
@@ -4031,8 +4032,8 @@ class ke {
|
|
|
4031
4032
|
const P = await f(S.hash);
|
|
4032
4033
|
return { tapScriptSig: [
|
|
4033
4034
|
{
|
|
4034
|
-
pubkey:
|
|
4035
|
-
signature:
|
|
4035
|
+
pubkey: Ht(o),
|
|
4036
|
+
signature: Dt(a(P), r.sighashType),
|
|
4036
4037
|
leafHash: S.leafHash
|
|
4037
4038
|
}
|
|
4038
4039
|
] };
|
|
@@ -4044,8 +4045,8 @@ class ke {
|
|
|
4044
4045
|
this.data.updateInput(e, w);
|
|
4045
4046
|
}
|
|
4046
4047
|
}
|
|
4047
|
-
const Li = (t) => new
|
|
4048
|
-
class
|
|
4048
|
+
const Li = (t) => new Hn(t);
|
|
4049
|
+
class Hn {
|
|
4049
4050
|
constructor(e = h.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
|
|
4050
4051
|
this.tx = g.fromBuffer(e), Ki(this.tx), Object.defineProperty(this, "tx", {
|
|
4051
4052
|
enumerable: !1,
|
|
@@ -4061,7 +4062,7 @@ class vn {
|
|
|
4061
4062
|
addInput(e) {
|
|
4062
4063
|
if (e.hash === void 0 || e.index === void 0 || !h.isBuffer(e.hash) && typeof e.hash != "string" || typeof e.index != "number")
|
|
4063
4064
|
throw new Error("Error adding input.");
|
|
4064
|
-
const r = typeof e.hash == "string" ?
|
|
4065
|
+
const r = typeof e.hash == "string" ? We(h.from(e.hash, "hex")) : e.hash;
|
|
4065
4066
|
this.tx.addInput(r, e.index, e.sequence);
|
|
4066
4067
|
}
|
|
4067
4068
|
addOutput(e) {
|
|
@@ -4080,7 +4081,7 @@ function Vi(t, e, r) {
|
|
|
4080
4081
|
case "witnesspubkeyhash":
|
|
4081
4082
|
return Ir(1, t.partialSig);
|
|
4082
4083
|
case "multisig": {
|
|
4083
|
-
const n =
|
|
4084
|
+
const n = Vt({
|
|
4084
4085
|
output: e
|
|
4085
4086
|
});
|
|
4086
4087
|
return Ir(n.m, t.partialSig, n.pubkeys);
|
|
@@ -4106,7 +4107,7 @@ function Ir(t, e, r) {
|
|
|
4106
4107
|
throw new Error("Too many signatures");
|
|
4107
4108
|
return n.length === t;
|
|
4108
4109
|
}
|
|
4109
|
-
function
|
|
4110
|
+
function vn(t) {
|
|
4110
4111
|
return !!t.finalScriptSig || !!t.finalScriptWitness;
|
|
4111
4112
|
}
|
|
4112
4113
|
function Hr(t) {
|
|
@@ -4129,7 +4130,7 @@ function Wi(t, e, r) {
|
|
|
4129
4130
|
}
|
|
4130
4131
|
function Tt(t, e) {
|
|
4131
4132
|
t.forEach((r) => {
|
|
4132
|
-
if ($(r) ?
|
|
4133
|
+
if ($(r) ? Ai(r, e) : bi(r, e))
|
|
4133
4134
|
throw new Error("Can not modify transaction, signatures exist.");
|
|
4134
4135
|
});
|
|
4135
4136
|
}
|
|
@@ -4143,7 +4144,7 @@ function Gi(t) {
|
|
|
4143
4144
|
throw new Error("Signature sighash does not match input sighash type");
|
|
4144
4145
|
});
|
|
4145
4146
|
}
|
|
4146
|
-
function
|
|
4147
|
+
function xn(t, e, r) {
|
|
4147
4148
|
if (!pe(t, e))
|
|
4148
4149
|
throw new Error(`Can not ${r} for this input with the key ${t.toString("hex")}`);
|
|
4149
4150
|
}
|
|
@@ -4153,16 +4154,16 @@ function Ki(t) {
|
|
|
4153
4154
|
}
|
|
4154
4155
|
function Mi(t, e) {
|
|
4155
4156
|
t.ins.forEach((r) => {
|
|
4156
|
-
|
|
4157
|
+
Un(e, r);
|
|
4157
4158
|
});
|
|
4158
4159
|
}
|
|
4159
|
-
function
|
|
4160
|
-
const r = `${
|
|
4160
|
+
function Un(t, e) {
|
|
4161
|
+
const r = `${We(h.from(e.hash)).toString("hex")}:${e.index}`;
|
|
4161
4162
|
if (t.__TX_IN_CACHE[r])
|
|
4162
4163
|
throw new Error("Duplicate input detected.");
|
|
4163
4164
|
t.__TX_IN_CACHE[r] = 1;
|
|
4164
4165
|
}
|
|
4165
|
-
function
|
|
4166
|
+
function kn(t, e) {
|
|
4166
4167
|
return (r, n, s, i) => {
|
|
4167
4168
|
const o = t({
|
|
4168
4169
|
redeem: { output: s }
|
|
@@ -4171,45 +4172,45 @@ function Nn(t, e) {
|
|
|
4171
4172
|
throw new Error(`${e} for ${i} #${r} doesn't match the scriptPubKey in the prevout`);
|
|
4172
4173
|
};
|
|
4173
4174
|
}
|
|
4174
|
-
const vr =
|
|
4175
|
+
const vr = kn(mt, "Redeem script"), xr = kn(Et, "Witness script");
|
|
4175
4176
|
function Ur(t, e, r, n, s = !1) {
|
|
4176
|
-
if (!r.every(
|
|
4177
|
+
if (!r.every(vn))
|
|
4177
4178
|
throw new Error(`PSBT must be finalized to calculate ${e}`);
|
|
4178
4179
|
if (t === "__FEE_RATE" && n.__FEE_RATE)
|
|
4179
4180
|
return n.__FEE_RATE;
|
|
4180
4181
|
if (t === "__FEE" && n.__FEE)
|
|
4181
4182
|
return n.__FEE;
|
|
4182
4183
|
let i, o = !0;
|
|
4183
|
-
n.__EXTRACTED_TX ? (i = n.__EXTRACTED_TX, o = !1) : i = n.__TX.clone(),
|
|
4184
|
+
n.__EXTRACTED_TX ? (i = n.__EXTRACTED_TX, o = !1) : i = n.__TX.clone(), Fn(r, i, n, o, s);
|
|
4184
4185
|
const u = t === "__FEE_RATE" ? n.__FEE_RATE : n.__FEE;
|
|
4185
4186
|
if (u === void 0)
|
|
4186
4187
|
throw new Error(`Failed to calculate ${e}`);
|
|
4187
4188
|
return u;
|
|
4188
4189
|
}
|
|
4189
4190
|
function Di(t, e, r, n, s, i, o = !0, u) {
|
|
4190
|
-
const f =
|
|
4191
|
+
const f = Wn(r);
|
|
4191
4192
|
if (!Vi(e, r, f) && o)
|
|
4192
4193
|
throw new Error(`Can not finalize input #${t}`);
|
|
4193
4194
|
return $i(r, f, e.partialSig, n, s, i, u);
|
|
4194
4195
|
}
|
|
4195
4196
|
function $i(t, e, r, n, s, i, o) {
|
|
4196
4197
|
let u, f;
|
|
4197
|
-
const a = Xi(t, e, r), p = i ?
|
|
4198
|
+
const a = Xi(t, e, r), p = i ? Et({ redeem: a }) : null, l = s ? mt({ redeem: p || a }) : null;
|
|
4198
4199
|
return n ? (p ? f = xt(p.witness) : a ? f = xt(a.witness) : f = xt(o ?? [h.from([0])]), l && (u = l?.input)) : l ? u = l?.input : a ? u = a.input : u = Array.isArray(o) && o[0] ? o[0] : h.from([1]), {
|
|
4199
4200
|
finalScriptSig: u,
|
|
4200
4201
|
finalScriptWitness: f
|
|
4201
4202
|
};
|
|
4202
4203
|
}
|
|
4203
4204
|
function kr(t, e, r, n, s) {
|
|
4204
|
-
const i = N.checkForInput(t, e), { hash: o, sighashType: u, script: f } =
|
|
4205
|
-
return
|
|
4205
|
+
const i = N.checkForInput(t, e), { hash: o, sighashType: u, script: f } = Nn(e, i, n, !1, s);
|
|
4206
|
+
return xn(r, f, "sign"), {
|
|
4206
4207
|
hash: o,
|
|
4207
4208
|
sighashType: u
|
|
4208
4209
|
};
|
|
4209
4210
|
}
|
|
4210
|
-
function
|
|
4211
|
+
function Nn(t, e, r, n, s) {
|
|
4211
4212
|
const i = r.__TX, o = e.sighashType || g.SIGHASH_ALL;
|
|
4212
|
-
|
|
4213
|
+
Bn(o, s);
|
|
4213
4214
|
let u, f;
|
|
4214
4215
|
if (e.nonWitnessUtxo) {
|
|
4215
4216
|
const l = le(r, e, t), d = i.ins[t].hash, b = l.getHash();
|
|
@@ -4225,7 +4226,7 @@ function Cn(t, e, r, n, s) {
|
|
|
4225
4226
|
if (["p2sh-p2wsh", "p2wsh"].indexOf(p) >= 0)
|
|
4226
4227
|
u = i.hashForWitnessV0(t, a, f.value, o);
|
|
4227
4228
|
else if (he(a)) {
|
|
4228
|
-
const l =
|
|
4229
|
+
const l = yt({
|
|
4229
4230
|
hash: a.subarray(2)
|
|
4230
4231
|
}).output;
|
|
4231
4232
|
u = i.hashForWitnessV0(t, l, f.value, o);
|
|
@@ -4246,7 +4247,7 @@ PROCEED WITH CAUTION!
|
|
|
4246
4247
|
function qi(t, e, r, n) {
|
|
4247
4248
|
const s = [];
|
|
4248
4249
|
if (e.tapInternalKey) {
|
|
4249
|
-
const o =
|
|
4250
|
+
const o = Cn(t, e, n);
|
|
4250
4251
|
o && s.push(o);
|
|
4251
4252
|
}
|
|
4252
4253
|
if (e.tapScriptSig) {
|
|
@@ -4255,20 +4256,20 @@ function qi(t, e, r, n) {
|
|
|
4255
4256
|
}
|
|
4256
4257
|
return s.map((o) => Ne(t, e, r, o, n)).flat();
|
|
4257
4258
|
}
|
|
4258
|
-
function
|
|
4259
|
+
function Cn(t, e, r) {
|
|
4259
4260
|
const { script: n } = $e(t, e, r);
|
|
4260
|
-
return
|
|
4261
|
+
return An(n) ? h.from(n.subarray(2, 34)) : null;
|
|
4261
4262
|
}
|
|
4262
4263
|
function Nr(t) {
|
|
4263
4264
|
return t.length === 64 ? t : h.from(t.subarray(0, 64));
|
|
4264
4265
|
}
|
|
4265
4266
|
function Ne(t, e, r, n, s, i, o) {
|
|
4266
4267
|
const u = s.__TX, f = e.sighashType || g.SIGHASH_DEFAULT;
|
|
4267
|
-
|
|
4268
|
+
Bn(f, o);
|
|
4268
4269
|
const a = r.map((w, S) => $e(S, w, s)), p = a.map((w) => w.script), l = a.map((w) => w.value), d = [];
|
|
4269
4270
|
if (e.tapInternalKey && !i) {
|
|
4270
|
-
const w =
|
|
4271
|
-
if (
|
|
4271
|
+
const w = Cn(t, e, s) || h.from([]);
|
|
4272
|
+
if (Ht(n).equals(w)) {
|
|
4272
4273
|
const S = u.hashForWitnessV1(t, p, l, f);
|
|
4273
4274
|
d.push({ pubkey: n, hash: S });
|
|
4274
4275
|
}
|
|
@@ -4289,7 +4290,7 @@ function Ne(t, e, r, n, s, i, o) {
|
|
|
4289
4290
|
});
|
|
4290
4291
|
return d.concat(b);
|
|
4291
4292
|
}
|
|
4292
|
-
function
|
|
4293
|
+
function Bn(t, e) {
|
|
4293
4294
|
if (e && e.indexOf(t) < 0) {
|
|
4294
4295
|
const r = Yi(t);
|
|
4295
4296
|
throw new Error(`Sighash type is not allowed. Retry the sign method passing the sighashTypes array of whitelisted types. Sighash type: ${r}`);
|
|
@@ -4300,7 +4301,7 @@ function Xi(t, e, r) {
|
|
|
4300
4301
|
switch (e) {
|
|
4301
4302
|
case "multisig": {
|
|
4302
4303
|
const s = zi(t, r);
|
|
4303
|
-
n =
|
|
4304
|
+
n = Vt({
|
|
4304
4305
|
output: t,
|
|
4305
4306
|
signatures: s
|
|
4306
4307
|
});
|
|
@@ -4313,14 +4314,14 @@ function Xi(t, e, r) {
|
|
|
4313
4314
|
});
|
|
4314
4315
|
break;
|
|
4315
4316
|
case "pubkeyhash":
|
|
4316
|
-
n =
|
|
4317
|
+
n = yt({
|
|
4317
4318
|
output: t,
|
|
4318
4319
|
pubkey: r[0].pubkey,
|
|
4319
4320
|
signature: r[0].signature
|
|
4320
4321
|
});
|
|
4321
4322
|
break;
|
|
4322
4323
|
case "witnesspubkeyhash":
|
|
4323
|
-
n =
|
|
4324
|
+
n = Lt({
|
|
4324
4325
|
output: t,
|
|
4325
4326
|
pubkey: r[0].pubkey,
|
|
4326
4327
|
signature: r[0].signature
|
|
@@ -4351,7 +4352,7 @@ function ji(t, e, r) {
|
|
|
4351
4352
|
const i = s.script;
|
|
4352
4353
|
if (!i)
|
|
4353
4354
|
throw new TypeError("Invalid script for segwit address");
|
|
4354
|
-
s.isSegwit =
|
|
4355
|
+
s.isSegwit = ni(i);
|
|
4355
4356
|
} catch {
|
|
4356
4357
|
}
|
|
4357
4358
|
return s;
|
|
@@ -4374,9 +4375,9 @@ function Cr(t, e, r) {
|
|
|
4374
4375
|
});
|
|
4375
4376
|
}
|
|
4376
4377
|
function zi(t, e) {
|
|
4377
|
-
return
|
|
4378
|
+
return Vt({ output: t }).pubkeys.map((n) => (e.filter((s) => s.pubkey.equals(n))[0] || {}).signature).filter((n) => !!n);
|
|
4378
4379
|
}
|
|
4379
|
-
function
|
|
4380
|
+
function Rn(t) {
|
|
4380
4381
|
let e = 0;
|
|
4381
4382
|
function r(o) {
|
|
4382
4383
|
return e += o, t.subarray(e - o, e);
|
|
@@ -4430,10 +4431,10 @@ function Ce(t, e, r) {
|
|
|
4430
4431
|
}
|
|
4431
4432
|
});
|
|
4432
4433
|
}
|
|
4433
|
-
function
|
|
4434
|
+
function Fn(t, e, r, n, s) {
|
|
4434
4435
|
let i = 0;
|
|
4435
4436
|
t.forEach((a, p) => {
|
|
4436
|
-
if (n && a.finalScriptSig && (e.ins[p].script = a.finalScriptSig), n && a.finalScriptWitness && (e.ins[p].witness =
|
|
4437
|
+
if (n && a.finalScriptSig && (e.ins[p].script = a.finalScriptSig), n && a.finalScriptWitness && (e.ins[p].witness = Rn(a.finalScriptWitness)), a.witnessUtxo)
|
|
4437
4438
|
i += a.witnessUtxo.value;
|
|
4438
4439
|
else if (a.nonWitnessUtxo) {
|
|
4439
4440
|
const l = le(r, a, p), d = e.ins[p].index, b = l.outs[d];
|
|
@@ -4450,7 +4451,7 @@ function le(t, e, r) {
|
|
|
4450
4451
|
const n = t.__NON_WITNESS_UTXO_TX_CACHE;
|
|
4451
4452
|
return n[r] || Ce(t, e, r), n[r];
|
|
4452
4453
|
}
|
|
4453
|
-
function
|
|
4454
|
+
function Ln(t, e, r) {
|
|
4454
4455
|
const { script: n } = $e(t, e, r);
|
|
4455
4456
|
return n;
|
|
4456
4457
|
}
|
|
@@ -4467,7 +4468,7 @@ function $e(t, e, r) {
|
|
|
4467
4468
|
throw new Error("Can't find pubkey in input without Utxo data");
|
|
4468
4469
|
}
|
|
4469
4470
|
function Zi(t, e, r, n) {
|
|
4470
|
-
const s =
|
|
4471
|
+
const s = Ln(r, e, n), { meaningfulScript: i } = de(s, r, "input", e.redeemScript, e.witnessScript);
|
|
4471
4472
|
return pe(t, i);
|
|
4472
4473
|
}
|
|
4473
4474
|
function Qi(t, e, r, n) {
|
|
@@ -4481,14 +4482,14 @@ function Ji(t) {
|
|
|
4481
4482
|
if (!e)
|
|
4482
4483
|
return;
|
|
4483
4484
|
const r = e[e.length - 1];
|
|
4484
|
-
if (!(!h.isBuffer(r) ||
|
|
4485
|
+
if (!(!h.isBuffer(r) || Vn(r) || ro(r) || !A(r)))
|
|
4485
4486
|
return r;
|
|
4486
4487
|
}
|
|
4487
4488
|
function to(t) {
|
|
4488
4489
|
if (!t)
|
|
4489
4490
|
return;
|
|
4490
|
-
const e =
|
|
4491
|
-
if (!(
|
|
4491
|
+
const e = Rn(t), r = e[e.length - 1];
|
|
4492
|
+
if (!(Vn(r) || !A(r)))
|
|
4492
4493
|
return r;
|
|
4493
4494
|
}
|
|
4494
4495
|
function eo(t) {
|
|
@@ -4498,14 +4499,14 @@ function eo(t) {
|
|
|
4498
4499
|
}
|
|
4499
4500
|
return h.from(t);
|
|
4500
4501
|
}
|
|
4501
|
-
function
|
|
4502
|
+
function Vn(t) {
|
|
4502
4503
|
return t.length === 33 && zr(t);
|
|
4503
4504
|
}
|
|
4504
4505
|
function ro(t) {
|
|
4505
4506
|
return Q(t);
|
|
4506
4507
|
}
|
|
4507
4508
|
function de(t, e, r, n, s) {
|
|
4508
|
-
const i =
|
|
4509
|
+
const i = On(t), o = i && n && _r(n), u = _r(t);
|
|
4509
4510
|
if (i && n === void 0)
|
|
4510
4511
|
throw new Error("scriptPubkey is P2SH but redeemScript missing");
|
|
4511
4512
|
if ((u || o) && s === void 0)
|
|
@@ -4517,61 +4518,61 @@ function de(t, e, r, n, s) {
|
|
|
4517
4518
|
};
|
|
4518
4519
|
}
|
|
4519
4520
|
function re(t) {
|
|
4520
|
-
if (he(t) ||
|
|
4521
|
+
if (he(t) || On(t))
|
|
4521
4522
|
throw new Error("P2WPKH or P2SH can not be contained within P2WSH");
|
|
4522
4523
|
}
|
|
4523
|
-
function
|
|
4524
|
-
return he(t) ? "witnesspubkeyhash" :
|
|
4524
|
+
function Wn(t) {
|
|
4525
|
+
return he(t) ? "witnesspubkeyhash" : mi(t) ? "pubkeyhash" : gi(t) ? "multisig" : yi(t) ? "pubkey" : "nonstandard";
|
|
4525
4526
|
}
|
|
4526
4527
|
function Ot(t) {
|
|
4527
4528
|
return [...Array(t).keys()];
|
|
4528
4529
|
}
|
|
4529
4530
|
const fo = {
|
|
4530
|
-
payments:
|
|
4531
|
-
script:
|
|
4532
|
-
crypto:
|
|
4531
|
+
payments: hi,
|
|
4532
|
+
script: hs,
|
|
4533
|
+
crypto: xs
|
|
4533
4534
|
};
|
|
4534
4535
|
export {
|
|
4535
4536
|
so as A,
|
|
4536
|
-
|
|
4537
|
-
|
|
4537
|
+
An as B,
|
|
4538
|
+
Pn as C,
|
|
4538
4539
|
Di as D,
|
|
4539
|
-
|
|
4540
|
+
mi as E,
|
|
4540
4541
|
he as F,
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4542
|
+
ni as G,
|
|
4543
|
+
_r as H,
|
|
4544
|
+
On as I,
|
|
4544
4545
|
uo as J,
|
|
4545
|
-
|
|
4546
|
-
|
|
4546
|
+
yi as K,
|
|
4547
|
+
gi as L,
|
|
4547
4548
|
ke as M,
|
|
4548
|
-
|
|
4549
|
+
Hs as N,
|
|
4549
4550
|
K as P,
|
|
4550
4551
|
g as T,
|
|
4551
|
-
|
|
4552
|
+
Et as a,
|
|
4552
4553
|
M as b,
|
|
4553
|
-
|
|
4554
|
-
|
|
4554
|
+
Lt as c,
|
|
4555
|
+
bt as d,
|
|
4555
4556
|
fo as e,
|
|
4556
4557
|
v as f,
|
|
4557
|
-
|
|
4558
|
-
|
|
4558
|
+
fi as g,
|
|
4559
|
+
ci as h,
|
|
4559
4560
|
oo as i,
|
|
4560
|
-
|
|
4561
|
-
|
|
4561
|
+
mt as j,
|
|
4562
|
+
yt as k,
|
|
4562
4563
|
De as l,
|
|
4563
4564
|
A as m,
|
|
4564
|
-
|
|
4565
|
+
Be as n,
|
|
4565
4566
|
E as o,
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4569
|
-
|
|
4570
|
-
|
|
4567
|
+
Vt as p,
|
|
4568
|
+
oi as q,
|
|
4569
|
+
as as r,
|
|
4570
|
+
Ie as s,
|
|
4571
|
+
Ht as t,
|
|
4571
4572
|
it as u,
|
|
4572
4573
|
C as v,
|
|
4573
|
-
|
|
4574
|
-
|
|
4574
|
+
Is as w,
|
|
4575
|
+
Zs as x,
|
|
4575
4576
|
D as y,
|
|
4576
4577
|
io as z
|
|
4577
4578
|
};
|