@btc-vision/transaction 1.7.19 → 1.7.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/.babelrc +10 -1
  2. package/LICENSE +190 -21
  3. package/README.md +1 -1
  4. package/browser/_version.d.ts +1 -1
  5. package/browser/bip39.js +204 -0
  6. package/browser/bitcoin-utils.js +3172 -0
  7. package/browser/btc-vision-bip32.js +805 -0
  8. package/browser/btc-vision-bitcoin.js +4179 -0
  9. package/browser/btc-vision-logger.js +273 -0
  10. package/browser/btc-vision-post-quantum.js +542 -0
  11. package/browser/chain/ChainData.d.ts +1 -1
  12. package/browser/crypto/crypto.d.ts +1 -1
  13. package/browser/generators/AddressGenerator.d.ts +1 -1
  14. package/browser/generators/Generator.d.ts +1 -1
  15. package/browser/generators/MLDSAData.d.ts +1 -1
  16. package/browser/generators/builders/CalldataGenerator.d.ts +1 -1
  17. package/browser/generators/builders/CustomGenerator.d.ts +1 -1
  18. package/browser/generators/builders/DeploymentGenerator.d.ts +1 -1
  19. package/browser/generators/builders/HashCommitmentGenerator.d.ts +49 -0
  20. package/browser/generators/builders/LegacyCalldataGenerator.d.ts +1 -1
  21. package/browser/generators/builders/P2WDAGenerator.d.ts +1 -1
  22. package/browser/index.js +10775 -2
  23. package/browser/keypair/Address.d.ts +5 -3
  24. package/browser/keypair/AddressVerificator.d.ts +2 -2
  25. package/browser/keypair/EcKeyPair.d.ts +2 -2
  26. package/browser/keypair/MessageSigner.d.ts +2 -2
  27. package/browser/keypair/Wallet.d.ts +2 -2
  28. package/browser/metadata/ContractBaseMetadata.d.ts +1 -1
  29. package/browser/mnemonic/Mnemonic.d.ts +2 -2
  30. package/browser/noble-curves.js +3316 -0
  31. package/browser/noble-hashes.js +1608 -0
  32. package/browser/opnet.d.ts +15 -2
  33. package/browser/p2wda/P2WDADetector.d.ts +2 -2
  34. package/browser/polyfills.js +4590 -0
  35. package/browser/scure-base.js +410 -0
  36. package/browser/signer/AddressRotation.d.ts +12 -0
  37. package/browser/signer/SignerUtils.d.ts +1 -1
  38. package/browser/signer/TweakedSigner.d.ts +1 -1
  39. package/browser/transaction/TransactionFactory.d.ts +15 -1
  40. package/browser/transaction/browser/BrowserSignerBase.d.ts +1 -1
  41. package/browser/transaction/browser/Web3Provider.d.ts +1 -1
  42. package/browser/transaction/browser/extensions/UnisatSigner.d.ts +1 -1
  43. package/browser/transaction/browser/extensions/XverseSigner.d.ts +1 -1
  44. package/browser/transaction/builders/CancelTransaction.d.ts +1 -1
  45. package/browser/transaction/builders/ConsolidatedInteractionTransaction.d.ts +44 -0
  46. package/browser/transaction/builders/CustomScriptTransaction.d.ts +1 -1
  47. package/browser/transaction/builders/DeploymentTransaction.d.ts +1 -1
  48. package/browser/transaction/builders/FundingTransaction.d.ts +1 -1
  49. package/browser/transaction/builders/InteractionTransaction.d.ts +1 -1
  50. package/browser/transaction/builders/InteractionTransactionP2WDA.d.ts +2 -2
  51. package/browser/transaction/builders/MultiSignTransaction.d.ts +1 -1
  52. package/browser/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
  53. package/browser/transaction/builders/TransactionBuilder.d.ts +1 -1
  54. package/browser/transaction/enums/TransactionType.d.ts +3 -1
  55. package/browser/transaction/interfaces/IConsolidatedTransactionParameters.d.ts +31 -0
  56. package/browser/transaction/interfaces/ITransactionParameters.d.ts +3 -1
  57. package/browser/transaction/interfaces/Tap.d.ts +1 -1
  58. package/browser/transaction/mineable/TimelockGenerator.d.ts +1 -1
  59. package/browser/transaction/offline/OfflineTransactionManager.d.ts +69 -0
  60. package/browser/transaction/offline/TransactionReconstructor.d.ts +28 -0
  61. package/browser/transaction/offline/TransactionSerializer.d.ts +50 -0
  62. package/browser/transaction/offline/TransactionStateCapture.d.ts +52 -0
  63. package/browser/transaction/offline/interfaces/ISerializableState.d.ts +62 -0
  64. package/browser/transaction/offline/interfaces/ITypeSpecificData.d.ts +62 -0
  65. package/browser/transaction/processor/PsbtTransaction.d.ts +1 -1
  66. package/browser/transaction/shared/P2TR_MS.d.ts +1 -1
  67. package/browser/transaction/shared/TweakedTransaction.d.ts +15 -4
  68. package/browser/utxo/OPNetLimitedProvider.d.ts +1 -1
  69. package/browser/utxo/interfaces/IUTXO.d.ts +2 -0
  70. package/browser/valibot.js +4948 -0
  71. package/browser/vendors.js +12913 -0
  72. package/browser/verification/TapscriptVerificator.d.ts +1 -1
  73. package/build/_version.d.ts +1 -1
  74. package/build/_version.js +1 -1
  75. package/build/generators/builders/HashCommitmentGenerator.d.ts +49 -0
  76. package/build/generators/builders/HashCommitmentGenerator.js +229 -0
  77. package/build/keypair/Address.d.ts +3 -1
  78. package/build/keypair/Address.js +87 -54
  79. package/build/opnet.d.ts +14 -1
  80. package/build/opnet.js +11 -1
  81. package/build/signer/AddressRotation.d.ts +12 -0
  82. package/build/signer/AddressRotation.js +16 -0
  83. package/build/transaction/TransactionFactory.d.ts +14 -0
  84. package/build/transaction/TransactionFactory.js +36 -0
  85. package/build/transaction/builders/ConsolidatedInteractionTransaction.d.ts +44 -0
  86. package/build/transaction/builders/ConsolidatedInteractionTransaction.js +259 -0
  87. package/build/transaction/builders/TransactionBuilder.js +2 -0
  88. package/build/transaction/enums/TransactionType.d.ts +3 -1
  89. package/build/transaction/enums/TransactionType.js +2 -0
  90. package/build/transaction/interfaces/IConsolidatedTransactionParameters.d.ts +31 -0
  91. package/build/transaction/interfaces/IConsolidatedTransactionParameters.js +1 -0
  92. package/build/transaction/interfaces/ITransactionParameters.d.ts +2 -0
  93. package/build/transaction/offline/OfflineTransactionManager.d.ts +69 -0
  94. package/build/transaction/offline/OfflineTransactionManager.js +255 -0
  95. package/build/transaction/offline/TransactionReconstructor.d.ts +28 -0
  96. package/build/transaction/offline/TransactionReconstructor.js +243 -0
  97. package/build/transaction/offline/TransactionSerializer.d.ts +50 -0
  98. package/build/transaction/offline/TransactionSerializer.js +700 -0
  99. package/build/transaction/offline/TransactionStateCapture.d.ts +52 -0
  100. package/build/transaction/offline/TransactionStateCapture.js +275 -0
  101. package/build/transaction/offline/interfaces/ISerializableState.d.ts +62 -0
  102. package/build/transaction/offline/interfaces/ISerializableState.js +2 -0
  103. package/build/transaction/offline/interfaces/ITypeSpecificData.d.ts +62 -0
  104. package/build/transaction/offline/interfaces/ITypeSpecificData.js +19 -0
  105. package/build/transaction/shared/TweakedTransaction.d.ts +12 -1
  106. package/build/transaction/shared/TweakedTransaction.js +75 -8
  107. package/build/utxo/interfaces/IUTXO.d.ts +2 -0
  108. package/documentation/README.md +5 -0
  109. package/documentation/offline-transaction-signing.md +650 -0
  110. package/documentation/transaction-building.md +603 -0
  111. package/package.json +62 -4
  112. package/src/_version.ts +1 -1
  113. package/src/generators/builders/HashCommitmentGenerator.ts +495 -0
  114. package/src/keypair/Address.ts +123 -70
  115. package/src/opnet.ts +16 -1
  116. package/src/signer/AddressRotation.ts +72 -0
  117. package/src/transaction/TransactionFactory.ts +87 -0
  118. package/src/transaction/builders/CancelTransaction.ts +4 -2
  119. package/src/transaction/builders/ConsolidatedInteractionTransaction.ts +561 -0
  120. package/src/transaction/builders/CustomScriptTransaction.ts +4 -2
  121. package/src/transaction/builders/MultiSignTransaction.ts +4 -2
  122. package/src/transaction/builders/TransactionBuilder.ts +8 -2
  123. package/src/transaction/enums/TransactionType.ts +2 -0
  124. package/src/transaction/interfaces/IConsolidatedTransactionParameters.ts +78 -0
  125. package/src/transaction/interfaces/ITransactionParameters.ts +8 -0
  126. package/src/transaction/offline/OfflineTransactionManager.ts +630 -0
  127. package/src/transaction/offline/TransactionReconstructor.ts +402 -0
  128. package/src/transaction/offline/TransactionSerializer.ts +920 -0
  129. package/src/transaction/offline/TransactionStateCapture.ts +469 -0
  130. package/src/transaction/offline/interfaces/ISerializableState.ts +141 -0
  131. package/src/transaction/offline/interfaces/ITypeSpecificData.ts +172 -0
  132. package/src/transaction/shared/TweakedTransaction.ts +156 -9
  133. package/src/utxo/interfaces/IUTXO.ts +8 -0
  134. package/test/address-rotation.test.ts +553 -0
  135. package/test/offline-transaction.test.ts +2065 -0
  136. package/vite.config.browser.ts +92 -0
  137. package/webpack.config.js +143 -2
  138. package/browser/crypto/crypto-browser.d.ts +0 -11
  139. package/browser/index.js.LICENSE.txt +0 -29
@@ -0,0 +1,4179 @@
1
+ import { B as h, a as N } from "./polyfills.js";
2
+ import { i as _n, d as P, a as Oe, b as bn, e as Sr, c as Pe, v as Ke, p as Ct, u as U } from "./bitcoin-utils.js";
3
+ import { f as _r, g as Tn, s as Me } from "./noble-hashes.js";
4
+ const K = {
5
+ messagePrefix: `Bitcoin Signed Message:
6
+ `,
7
+ bech32: "bc",
8
+ bech32Opnet: "op",
9
+ bip32: {
10
+ public: 76067358,
11
+ private: 76066276
12
+ },
13
+ pubKeyHash: 0,
14
+ scriptHash: 5,
15
+ wif: 128
16
+ }, Pi = {
17
+ messagePrefix: `Bitcoin Signed Message:
18
+ `,
19
+ bech32: "bcrt",
20
+ bech32Opnet: "opr",
21
+ bip32: {
22
+ public: 70617039,
23
+ private: 70615956
24
+ },
25
+ pubKeyHash: 111,
26
+ scriptHash: 196,
27
+ wif: 239
28
+ }, Ii = {
29
+ messagePrefix: `Bitcoin Signed Message:
30
+ `,
31
+ bech32: "tb",
32
+ bech32Opnet: "opt",
33
+ bip32: {
34
+ public: 70617039,
35
+ private: 70615956
36
+ },
37
+ pubKeyHash: 111,
38
+ scriptHash: 196,
39
+ wif: 239
40
+ };
41
+ function On(e) {
42
+ if (e.length < 8 || e.length > 72 || e[0] !== 48 || e[1] !== e.length - 2 || e[2] !== 2)
43
+ return !1;
44
+ const t = e[3];
45
+ if (t === 0 || 5 + t >= e.length || e[4 + t] !== 2)
46
+ return !1;
47
+ const r = e[5 + t];
48
+ return !(r === 0 || 6 + t + r !== e.length || e[4] & 128 || t > 1 && e[4] === 0 && !(e[5] & 128) || e[t + 6] & 128 || r > 1 && e[t + 6] === 0 && !(e[t + 7] & 128));
49
+ }
50
+ function Pn(e) {
51
+ if (e.length < 8)
52
+ throw new Error("DER sequence length is too short");
53
+ if (e.length > 72)
54
+ throw new Error("DER sequence length is too long");
55
+ if (e[0] !== 48)
56
+ throw new Error("Expected DER sequence");
57
+ if (e[1] !== e.length - 2)
58
+ throw new Error("DER sequence length is invalid");
59
+ if (e[2] !== 2)
60
+ throw new Error("Expected DER integer");
61
+ const t = e[3];
62
+ if (t === 0)
63
+ throw new Error("R length is zero");
64
+ if (5 + t >= e.length)
65
+ throw new Error("R length is too long");
66
+ if (e[4 + t] !== 2)
67
+ throw new Error("Expected DER integer (2)");
68
+ const r = e[5 + t];
69
+ if (r === 0)
70
+ throw new Error("S length is zero");
71
+ if (6 + t + r !== e.length)
72
+ throw new Error("S length is invalid");
73
+ if (e[4] & 128)
74
+ throw new Error("R value is negative");
75
+ if (t > 1 && e[4] === 0 && !(e[5] & 128))
76
+ throw new Error("R value excessively padded");
77
+ if (e[t + 6] & 128)
78
+ throw new Error("S value is negative");
79
+ if (r > 1 && e[t + 6] === 0 && !(e[t + 7] & 128))
80
+ throw new Error("S value excessively padded");
81
+ return {
82
+ r: e.slice(4, 4 + t),
83
+ s: e.slice(6 + t)
84
+ };
85
+ }
86
+ function In(e, t) {
87
+ const r = e.length, n = t.length;
88
+ if (r === 0)
89
+ throw new Error("R length is zero");
90
+ if (n === 0)
91
+ throw new Error("S length is zero");
92
+ if (r > 33)
93
+ throw new Error("R length is too long");
94
+ if (n > 33)
95
+ throw new Error("S length is too long");
96
+ if (e[0] & 128)
97
+ throw new Error("R value is negative");
98
+ if (t[0] & 128)
99
+ throw new Error("S value is negative");
100
+ if (r > 1 && e[0] === 0 && !(e[1] & 128))
101
+ throw new Error("R value excessively padded");
102
+ if (n > 1 && t[0] === 0 && !(t[1] & 128))
103
+ throw new Error("S value excessively padded");
104
+ const s = h.allocUnsafe(6 + r + n);
105
+ return s[0] = 48, s[1] = s.length - 2, s[2] = 2, s[3] = e.length, e.copy(s, 4), s[4 + r] = 2, s[5 + r] = t.length, t.copy(s, 6 + r), s;
106
+ }
107
+ const E = {
108
+ OP_FALSE: 0,
109
+ OP_0: 0,
110
+ OP_PUSHDATA1: 76,
111
+ OP_PUSHDATA2: 77,
112
+ OP_PUSHDATA4: 78,
113
+ OP_1NEGATE: 79,
114
+ OP_RESERVED: 80,
115
+ OP_TRUE: 81,
116
+ OP_1: 81,
117
+ OP_2: 82,
118
+ OP_3: 83,
119
+ OP_4: 84,
120
+ OP_5: 85,
121
+ OP_6: 86,
122
+ OP_7: 87,
123
+ OP_8: 88,
124
+ OP_9: 89,
125
+ OP_10: 90,
126
+ OP_11: 91,
127
+ OP_12: 92,
128
+ OP_13: 93,
129
+ OP_14: 94,
130
+ OP_15: 95,
131
+ OP_16: 96,
132
+ OP_NOP: 97,
133
+ OP_VER: 98,
134
+ OP_IF: 99,
135
+ OP_NOTIF: 100,
136
+ OP_VERIF: 101,
137
+ OP_VERNOTIF: 102,
138
+ OP_ELSE: 103,
139
+ OP_ENDIF: 104,
140
+ OP_VERIFY: 105,
141
+ OP_RETURN: 106,
142
+ OP_TOALTSTACK: 107,
143
+ OP_FROMALTSTACK: 108,
144
+ OP_2DROP: 109,
145
+ OP_2DUP: 110,
146
+ OP_3DUP: 111,
147
+ OP_2OVER: 112,
148
+ OP_2ROT: 113,
149
+ OP_2SWAP: 114,
150
+ OP_IFDUP: 115,
151
+ OP_DEPTH: 116,
152
+ OP_DROP: 117,
153
+ OP_DUP: 118,
154
+ OP_NIP: 119,
155
+ OP_OVER: 120,
156
+ OP_PICK: 121,
157
+ OP_ROLL: 122,
158
+ OP_ROT: 123,
159
+ OP_SWAP: 124,
160
+ OP_TUCK: 125,
161
+ OP_CAT: 126,
162
+ OP_SUBSTR: 127,
163
+ OP_LEFT: 128,
164
+ OP_RIGHT: 129,
165
+ OP_SIZE: 130,
166
+ OP_INVERT: 131,
167
+ OP_AND: 132,
168
+ OP_OR: 133,
169
+ OP_XOR: 134,
170
+ OP_EQUAL: 135,
171
+ OP_EQUALVERIFY: 136,
172
+ OP_RESERVED1: 137,
173
+ OP_RESERVED2: 138,
174
+ OP_1ADD: 139,
175
+ OP_1SUB: 140,
176
+ OP_2MUL: 141,
177
+ OP_2DIV: 142,
178
+ OP_NEGATE: 143,
179
+ OP_ABS: 144,
180
+ OP_NOT: 145,
181
+ OP_0NOTEQUAL: 146,
182
+ OP_ADD: 147,
183
+ OP_SUB: 148,
184
+ OP_MUL: 149,
185
+ OP_DIV: 150,
186
+ OP_MOD: 151,
187
+ OP_LSHIFT: 152,
188
+ OP_RSHIFT: 153,
189
+ OP_BOOLAND: 154,
190
+ OP_BOOLOR: 155,
191
+ OP_NUMEQUAL: 156,
192
+ OP_NUMEQUALVERIFY: 157,
193
+ OP_NUMNOTEQUAL: 158,
194
+ OP_LESSTHAN: 159,
195
+ OP_GREATERTHAN: 160,
196
+ OP_LESSTHANOREQUAL: 161,
197
+ OP_GREATERTHANOREQUAL: 162,
198
+ OP_MIN: 163,
199
+ OP_MAX: 164,
200
+ OP_WITHIN: 165,
201
+ OP_RIPEMD160: 166,
202
+ OP_SHA1: 167,
203
+ OP_SHA256: 168,
204
+ OP_HASH160: 169,
205
+ OP_HASH256: 170,
206
+ OP_CODESEPARATOR: 171,
207
+ OP_CHECKSIG: 172,
208
+ OP_CHECKSIGVERIFY: 173,
209
+ OP_CHECKMULTISIG: 174,
210
+ OP_CHECKMULTISIGVERIFY: 175,
211
+ OP_NOP1: 176,
212
+ OP_NOP2: 177,
213
+ OP_CHECKLOCKTIMEVERIFY: 177,
214
+ OP_NOP3: 178,
215
+ OP_CHECKSEQUENCEVERIFY: 178,
216
+ OP_NOP4: 179,
217
+ OP_NOP5: 180,
218
+ OP_NOP6: 181,
219
+ OP_NOP7: 182,
220
+ OP_NOP8: 183,
221
+ OP_NOP9: 184,
222
+ OP_NOP10: 185,
223
+ OP_CHECKSIGADD: 186,
224
+ OP_PUBKEYHASH: 253,
225
+ OP_PUBKEY: 254,
226
+ OP_INVALIDOPCODE: 255
227
+ }, br = {};
228
+ for (const e of Object.keys(E)) {
229
+ const t = E[e];
230
+ br[t] = e;
231
+ }
232
+ function Tr(e) {
233
+ return e < E.OP_PUSHDATA1 ? 1 : e <= 255 ? 2 : e <= 65535 ? 3 : 5;
234
+ }
235
+ function An(e, t, r) {
236
+ const n = Tr(t);
237
+ return n === 1 ? e.writeUInt8(t, r) : n === 2 ? (e.writeUInt8(E.OP_PUSHDATA1, r), e.writeUInt8(t, r + 1)) : n === 3 ? (e.writeUInt8(E.OP_PUSHDATA2, r), e.writeUInt16LE(t, r + 1)) : (e.writeUInt8(E.OP_PUSHDATA4, r), e.writeUInt32LE(t, r + 1)), n;
238
+ }
239
+ function Hn(e, t) {
240
+ const r = e.readUInt8(t);
241
+ let n, s;
242
+ if (r < E.OP_PUSHDATA1)
243
+ n = r, s = 1;
244
+ else if (r === E.OP_PUSHDATA1) {
245
+ if (t + 2 > e.length)
246
+ return null;
247
+ n = e.readUInt8(t + 1), s = 2;
248
+ } else if (r === E.OP_PUSHDATA2) {
249
+ if (t + 3 > e.length)
250
+ return null;
251
+ n = e.readUInt16LE(t + 1), s = 3;
252
+ } else {
253
+ if (t + 5 > e.length)
254
+ return null;
255
+ if (r !== E.OP_PUSHDATA4)
256
+ throw new Error("Unexpected opcode");
257
+ n = e.readUInt32LE(t + 1), s = 5;
258
+ }
259
+ return {
260
+ opcode: r,
261
+ number: n,
262
+ size: s
263
+ };
264
+ }
265
+ function vn(e, t, r) {
266
+ t = t || 4, r = r === void 0 ? !0 : r;
267
+ const n = e.length;
268
+ if (n === 0)
269
+ return 0;
270
+ if (n > t)
271
+ throw new TypeError("Script number overflow");
272
+ if (r && (e[n - 1] & 127) === 0 && (n <= 1 || (e[n - 2] & 128) === 0))
273
+ throw new Error("Non-minimally encoded script number");
274
+ if (n === 5) {
275
+ const i = e.readUInt32LE(0), o = e.readUInt8(4);
276
+ return o & 128 ? -((o & -129) * 4294967296 + i) : o * 4294967296 + i;
277
+ }
278
+ let s = 0;
279
+ for (let i = 0; i < n; ++i)
280
+ s |= e[i] << 8 * i;
281
+ return e[n - 1] & 128 ? -(s & ~(128 << 8 * (n - 1))) : s;
282
+ }
283
+ function kn(e) {
284
+ return e > 2147483647 ? 5 : e > 8388607 ? 4 : e > 32767 ? 3 : e > 127 ? 2 : e > 0 ? 1 : 0;
285
+ }
286
+ function Or(e) {
287
+ let t = Math.abs(e);
288
+ const r = kn(t), n = h.allocUnsafe(r), s = e < 0;
289
+ for (let i = 0; i < r; ++i)
290
+ n.writeUInt8(t & 255, i), t >>= 8;
291
+ return n[r - 1] & 128 ? n.writeUInt8(s ? 128 : 0, r - 1) : s && (n[r - 1] |= 128), n;
292
+ }
293
+ const Nn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
294
+ __proto__: null,
295
+ decode: vn,
296
+ encode: Or
297
+ }, Symbol.toStringTag, { value: "Module" })), f = _n, Rt = N.alloc(32, 0), Bt = N.from("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "hex");
298
+ function ie(e, t) {
299
+ return e.length !== t.length ? !1 : e.every((r, n) => r.equals(t[n]));
300
+ }
301
+ function L(e) {
302
+ if (!N.isBuffer(e) || e.length < 33)
303
+ return !1;
304
+ const t = e[0], r = e.slice(1, 33);
305
+ if (r.compare(Rt) === 0 || r.compare(Bt) >= 0)
306
+ return !1;
307
+ if ((t === 2 || t === 3) && e.length === 33)
308
+ return !0;
309
+ if (e.length !== 65)
310
+ return !1;
311
+ const n = e.slice(33);
312
+ return n.compare(Rt) === 0 || n.compare(Bt) >= 0 ? !1 : t === 4 || t === 6 || t === 7;
313
+ }
314
+ const Un = 21 * 1e14;
315
+ function Le(e) {
316
+ return f.UInt53(e) && e <= Un;
317
+ }
318
+ const Ee = 254;
319
+ function Ze(e) {
320
+ return !e || !("output" in e) || !N.isBuffer(e.output) ? !1 : e.version !== void 0 ? (e.version & Ee) === e.version : !0;
321
+ }
322
+ function Pt(e) {
323
+ return ve(e) ? e.length !== 2 ? !1 : e.every((t) => Pt(t)) : Ze(e);
324
+ }
325
+ const xn = f.BufferN(32), Cn = f.BufferN(20), lt = f.BufferN(32), Se = f.Number, ve = f.Array, Rn = f.Boolean, Pr = f.String, V = f.Buffer, Ir = f.Hex, dt = f.maybe, D = f.tuple, Ar = f.UInt8, z = f.UInt32, Bn = f.Function, Hr = f.BufferN, vr = f.Null, Fn = f.oneOf, Qe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
326
+ __proto__: null,
327
+ Array: ve,
328
+ Boolean: Rn,
329
+ Buffer: V,
330
+ Buffer256bit: xn,
331
+ BufferN: Hr,
332
+ Function: Bn,
333
+ Hash160bit: Cn,
334
+ Hash256bit: lt,
335
+ Hex: Ir,
336
+ Null: vr,
337
+ Number: Se,
338
+ Satoshi: Le,
339
+ String: Pr,
340
+ TAPLEAF_VERSION_MASK: Ee,
341
+ UInt32: z,
342
+ UInt8: Ar,
343
+ isPoint: L,
344
+ isTapleaf: Ze,
345
+ isTaptree: Pt,
346
+ maybe: dt,
347
+ oneOf: Fn,
348
+ stacksEqual: ie,
349
+ tuple: D,
350
+ typeforce: f
351
+ }, Symbol.toStringTag, { value: "Module" })), { typeforce: Vn } = Qe, Ft = h.alloc(1, 0);
352
+ function Vt(e) {
353
+ let t = 0;
354
+ for (; e[t] === 0; )
355
+ ++t;
356
+ return t === e.length ? Ft : (e = e.slice(t), e[0] & 128 ? h.concat([Ft, e], 1 + e.length) : e);
357
+ }
358
+ function Lt(e) {
359
+ e[0] === 0 && (e = e.slice(1));
360
+ const t = h.alloc(32, 0), r = Math.max(0, 32 - e.length);
361
+ return e.copy(t, r), t;
362
+ }
363
+ function Ln(e) {
364
+ const t = e.readUInt8(e.length - 1);
365
+ if (!et(t))
366
+ throw new Error("Invalid hashType " + t);
367
+ const r = Pn(e.slice(0, -1)), n = Lt(r.r), s = Lt(r.s);
368
+ return { signature: h.concat([n, s], 64), hashType: t };
369
+ }
370
+ function Wn(e, t) {
371
+ if (Vn({
372
+ signature: Hr(64),
373
+ hashType: Ar
374
+ }, { signature: e, hashType: t }), !et(t))
375
+ throw new Error("Invalid hashType " + t);
376
+ const r = h.allocUnsafe(1);
377
+ r.writeUInt8(t, 0);
378
+ const n = Vt(e.slice(0, 32)), s = Vt(e.slice(32, 64));
379
+ return h.concat([In(n, s), r]);
380
+ }
381
+ const Gn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
382
+ __proto__: null,
383
+ decode: Ln,
384
+ encode: Wn
385
+ }, Symbol.toStringTag, { value: "Module" })), { typeforce: Ie } = Qe, kr = E.OP_RESERVED;
386
+ function Kn(e) {
387
+ return Se(e) && (e === E.OP_0 || e >= E.OP_1 && e <= E.OP_16 || e === E.OP_1NEGATE);
388
+ }
389
+ function Nr(e) {
390
+ return V(e) || Kn(e);
391
+ }
392
+ function Je(e) {
393
+ return ve(e) && e.every(Nr);
394
+ }
395
+ function It(e) {
396
+ return e.length - e.filter(Nr).length;
397
+ }
398
+ function qe(e) {
399
+ if (e.length === 0)
400
+ return E.OP_0;
401
+ if (e.length === 1) {
402
+ if (e[0] >= 1 && e[0] <= 16)
403
+ return kr + e[0];
404
+ if (e[0] === 129)
405
+ return E.OP_1NEGATE;
406
+ }
407
+ }
408
+ function Ur(e) {
409
+ return h.isBuffer(e);
410
+ }
411
+ function Mn(e) {
412
+ return ve(e);
413
+ }
414
+ function $e(e) {
415
+ return h.isBuffer(e);
416
+ }
417
+ function H(e) {
418
+ if (Ur(e))
419
+ return e;
420
+ Ie(ve, e);
421
+ const t = e.reduce((s, i) => $e(i) ? i.length === 1 && qe(i) !== void 0 ? s + 1 : s + Tr(i.length) + i.length : s + 1, 0), r = h.allocUnsafe(t);
422
+ let n = 0;
423
+ if (e.forEach((s) => {
424
+ if ($e(s)) {
425
+ const i = qe(s);
426
+ if (i !== void 0) {
427
+ r.writeUInt8(i, n), n += 1;
428
+ return;
429
+ }
430
+ n += An(r, s.length, n), s.copy(r, n), n += s.length;
431
+ } else
432
+ r.writeUInt8(s, n), n += 1;
433
+ }), n !== r.length)
434
+ throw new Error("Could not decode chunks");
435
+ return r;
436
+ }
437
+ function b(e) {
438
+ if (Mn(e))
439
+ return e;
440
+ Ie(V, e);
441
+ const t = [];
442
+ let r = 0;
443
+ for (; r < e.length; ) {
444
+ const n = e[r];
445
+ if (n > E.OP_0 && n <= E.OP_PUSHDATA4) {
446
+ const s = Hn(e, r);
447
+ if (s === null || (r += s.size, r + s.number > e.length))
448
+ return null;
449
+ const i = e.slice(r, r + s.number);
450
+ r += s.number;
451
+ const o = qe(i);
452
+ o !== void 0 ? t.push(o) : t.push(i);
453
+ } else
454
+ t.push(n), r += 1;
455
+ }
456
+ return t;
457
+ }
458
+ function xr(e) {
459
+ if (Ur(e) && (e = b(e)), !e)
460
+ throw new Error("Could not convert invalid chunks to ASM");
461
+ return e.map((t) => {
462
+ if ($e(t)) {
463
+ const r = qe(t);
464
+ if (r === void 0)
465
+ return t.toString("hex");
466
+ t = r;
467
+ }
468
+ return br[t];
469
+ }).join(" ");
470
+ }
471
+ function qn(e) {
472
+ return Ie(Pr, e), H(e.split(" ").map((t) => E[t] !== void 0 ? E[t] : (Ie(Ir, t), h.from(t, "hex"))));
473
+ }
474
+ function Cr(e) {
475
+ return e = b(e), Ie(Je, e), e.map((t) => $e(t) ? t : t === E.OP_0 ? h.allocUnsafe(0) : Or(t - kr));
476
+ }
477
+ function Rr(e) {
478
+ return L(e);
479
+ }
480
+ function et(e) {
481
+ const t = e & -129;
482
+ return t > 0 && t < 4;
483
+ }
484
+ function Y(e) {
485
+ return !h.isBuffer(e) || !et(e[e.length - 1]) ? !1 : On(e.slice(0, -1));
486
+ }
487
+ const $n = Nn, fe = Gn, Xn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
488
+ __proto__: null,
489
+ compile: H,
490
+ countNonPushOnlyOPs: It,
491
+ decompile: b,
492
+ fromASM: qn,
493
+ isCanonicalPubKey: Rr,
494
+ isCanonicalScriptSignature: Y,
495
+ isDefinedHashType: et,
496
+ isPushOnly: Je,
497
+ number: $n,
498
+ opcodes: E,
499
+ signature: fe,
500
+ toASM: xr,
501
+ toStack: Cr
502
+ }, Symbol.toStringTag, { value: "Module" })), tt = 40, rt = 2, nt = 15, Dn = 16, wt = 16, At = 2, Ht = 80, zn = "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.", jn = (e) => {
503
+ try {
504
+ const t = h.from(e.subarray(2));
505
+ if (t.length < rt || t.length > tt)
506
+ throw new TypeError("Invalid program length for segwit address");
507
+ const r = e[0] - Ht;
508
+ if (r < At || r > nt + 1)
509
+ throw new TypeError("Invalid version for segwit address");
510
+ if (r === 1)
511
+ throw new TypeError("taproot");
512
+ return !0;
513
+ } catch {
514
+ }
515
+ return !1;
516
+ };
517
+ function Yn(e, t) {
518
+ if (!h.isBuffer(e))
519
+ throw new TypeError("output must be a Buffer");
520
+ if (!t.bech32Opnet)
521
+ throw new Error("Network does not support opnet");
522
+ const r = e[0];
523
+ let n = 1, s;
524
+ if (e[1] < 76)
525
+ s = e[1], n = 2;
526
+ else if (e[1] === 76)
527
+ s = e[2], n = 3;
528
+ else
529
+ throw new TypeError("Unsupported push opcode in script");
530
+ const i = h.from(e.subarray(n, n + s));
531
+ if (i.length < rt || i.length > tt)
532
+ throw new TypeError("Invalid program length for segwit address");
533
+ const o = r === E.OP_0 ? 0 : r >= E.OP_1 && r <= E.OP_16 ? r - (E.OP_1 - 1) : -1;
534
+ if (o < nt || o > Dn)
535
+ throw new TypeError(`Invalid segwit version ${o}`);
536
+ const u = [o, ...P.bech32m.toWords(i)];
537
+ return P.bech32m.encode(t.bech32Opnet, u);
538
+ }
539
+ function Zn(e, t) {
540
+ const r = h.from(e.subarray(2));
541
+ if (r.length < rt || r.length > tt)
542
+ throw new TypeError("Invalid program length for segwit address");
543
+ const n = e[0] - Ht;
544
+ if (n < At || n > nt)
545
+ throw new TypeError("Invalid version for segwit address");
546
+ if (e[1] !== r.length)
547
+ throw new TypeError(`Invalid script for segwit address ${e[1]} !== ${r.length}`);
548
+ return Jn(r, n, t.bech32, t.bech32Opnet);
549
+ }
550
+ function Qn(e) {
551
+ const t = h.from(Oe.decode(e));
552
+ if (t.length < 21)
553
+ throw new TypeError(e + " is too short");
554
+ if (t.length > 21)
555
+ throw new TypeError(e + " is too long");
556
+ const r = t.readUInt8(0), n = h.from(t.subarray(1));
557
+ return { version: r, hash: n };
558
+ }
559
+ function vt(e) {
560
+ let t, r;
561
+ try {
562
+ t = P.bech32.decode(e);
563
+ } catch {
564
+ }
565
+ if (t) {
566
+ if (r = t.words[0], r !== 0)
567
+ throw new TypeError(e + " uses wrong encoding");
568
+ } else if (t = P.bech32m.decode(e), r = t.words[0], r === 0)
569
+ throw new TypeError(e + " uses wrong encoding");
570
+ const n = P.bech32.fromWords(t.words.slice(1));
571
+ return {
572
+ version: r,
573
+ prefix: t.prefix,
574
+ data: h.from(n)
575
+ };
576
+ }
577
+ function Jn(e, t, r, n) {
578
+ const s = P.bech32.toWords(e);
579
+ return s.unshift(t), t === wt && n ? P.bech32m.encode(n, s) : t === 0 ? P.bech32.encode(r, s) : P.bech32m.encode(r, s);
580
+ }
581
+ function es(e, t) {
582
+ t = t || K;
583
+ try {
584
+ return de({ output: e, network: t }).address;
585
+ } catch {
586
+ }
587
+ try {
588
+ return he({ output: e, network: t }).address;
589
+ } catch {
590
+ }
591
+ try {
592
+ return xe({ output: e, network: t }).address;
593
+ } catch {
594
+ }
595
+ try {
596
+ return le({ output: e, network: t }).address;
597
+ } catch {
598
+ }
599
+ try {
600
+ return pe({ output: e, network: t }).address;
601
+ } catch {
602
+ }
603
+ try {
604
+ return Yn(e, t);
605
+ } catch {
606
+ }
607
+ try {
608
+ return Zn(e, t);
609
+ } catch {
610
+ }
611
+ throw new Error(xr(e) + " has no matching Address");
612
+ }
613
+ function ts(e, t) {
614
+ t = t || K;
615
+ let r, n;
616
+ try {
617
+ r = Qn(e);
618
+ } catch {
619
+ }
620
+ if (r) {
621
+ if (r.version === t.pubKeyHash)
622
+ return de({ hash: r.hash }).output;
623
+ if (r.version === t.scriptHash)
624
+ return he({ hash: r.hash }).output;
625
+ } else {
626
+ try {
627
+ n = vt(e);
628
+ } catch {
629
+ }
630
+ if (n) {
631
+ if (n.prefix !== t.bech32 && t.bech32Opnet && n.prefix !== t.bech32Opnet)
632
+ throw new Error(e + " has an invalid prefix");
633
+ if (n.version === 0) {
634
+ if (n.data.length === 20)
635
+ return xe({ hash: n.data }).output;
636
+ if (n.data.length === 32)
637
+ return le({ hash: n.data }).output;
638
+ } else if (n.version === 1) {
639
+ if (n.data.length === 32)
640
+ return pe({ pubkey: n.data }).output;
641
+ } else if (n.version === wt) {
642
+ if (!t.bech32Opnet)
643
+ throw new Error(e + " has an invalid prefix");
644
+ return tn({
645
+ program: n.data,
646
+ network: t
647
+ }).output;
648
+ } else if (n.version >= At && n.version <= nt && n.data.length >= rt && n.data.length <= tt)
649
+ return n.version !== wt && console.warn(zn), H([
650
+ n.version + Ht,
651
+ n.data
652
+ ]);
653
+ }
654
+ }
655
+ return h.from(e, "hex");
656
+ }
657
+ function rs(e) {
658
+ return h.from(_r(Uint8Array.from(e)));
659
+ }
660
+ function ns(e) {
661
+ return h.from(Tn(Uint8Array.from(e)));
662
+ }
663
+ function M(e) {
664
+ return h.from(Me(Uint8Array.from(e)));
665
+ }
666
+ function x(e) {
667
+ return h.from(_r(Me(Uint8Array.from(e))));
668
+ }
669
+ function te(e) {
670
+ return h.from(Me(Me(Uint8Array.from(e))));
671
+ }
672
+ const ss = [
673
+ "BIP0340/challenge",
674
+ "BIP0340/aux",
675
+ "BIP0340/nonce",
676
+ "TapLeaf",
677
+ "TapBranch",
678
+ "TapSighash",
679
+ "TapTweak",
680
+ "KeyAgg list",
681
+ "KeyAgg coefficient"
682
+ ], Br = {
683
+ "BIP0340/challenge": h.from([
684
+ 123,
685
+ 181,
686
+ 45,
687
+ 122,
688
+ 159,
689
+ 239,
690
+ 88,
691
+ 50,
692
+ 62,
693
+ 177,
694
+ 191,
695
+ 122,
696
+ 64,
697
+ 125,
698
+ 179,
699
+ 130,
700
+ 210,
701
+ 243,
702
+ 242,
703
+ 216,
704
+ 27,
705
+ 177,
706
+ 34,
707
+ 79,
708
+ 73,
709
+ 254,
710
+ 81,
711
+ 143,
712
+ 109,
713
+ 72,
714
+ 211,
715
+ 124,
716
+ 123,
717
+ 181,
718
+ 45,
719
+ 122,
720
+ 159,
721
+ 239,
722
+ 88,
723
+ 50,
724
+ 62,
725
+ 177,
726
+ 191,
727
+ 122,
728
+ 64,
729
+ 125,
730
+ 179,
731
+ 130,
732
+ 210,
733
+ 243,
734
+ 242,
735
+ 216,
736
+ 27,
737
+ 177,
738
+ 34,
739
+ 79,
740
+ 73,
741
+ 254,
742
+ 81,
743
+ 143,
744
+ 109,
745
+ 72,
746
+ 211,
747
+ 124
748
+ ]),
749
+ "BIP0340/aux": h.from([
750
+ 241,
751
+ 239,
752
+ 78,
753
+ 94,
754
+ 192,
755
+ 99,
756
+ 202,
757
+ 218,
758
+ 109,
759
+ 148,
760
+ 202,
761
+ 250,
762
+ 157,
763
+ 152,
764
+ 126,
765
+ 160,
766
+ 105,
767
+ 38,
768
+ 88,
769
+ 57,
770
+ 236,
771
+ 193,
772
+ 31,
773
+ 151,
774
+ 45,
775
+ 119,
776
+ 165,
777
+ 46,
778
+ 216,
779
+ 193,
780
+ 204,
781
+ 144,
782
+ 241,
783
+ 239,
784
+ 78,
785
+ 94,
786
+ 192,
787
+ 99,
788
+ 202,
789
+ 218,
790
+ 109,
791
+ 148,
792
+ 202,
793
+ 250,
794
+ 157,
795
+ 152,
796
+ 126,
797
+ 160,
798
+ 105,
799
+ 38,
800
+ 88,
801
+ 57,
802
+ 236,
803
+ 193,
804
+ 31,
805
+ 151,
806
+ 45,
807
+ 119,
808
+ 165,
809
+ 46,
810
+ 216,
811
+ 193,
812
+ 204,
813
+ 144
814
+ ]),
815
+ "BIP0340/nonce": h.from([
816
+ 7,
817
+ 73,
818
+ 119,
819
+ 52,
820
+ 167,
821
+ 155,
822
+ 203,
823
+ 53,
824
+ 91,
825
+ 155,
826
+ 140,
827
+ 125,
828
+ 3,
829
+ 79,
830
+ 18,
831
+ 28,
832
+ 244,
833
+ 52,
834
+ 215,
835
+ 62,
836
+ 247,
837
+ 45,
838
+ 218,
839
+ 25,
840
+ 135,
841
+ 0,
842
+ 97,
843
+ 251,
844
+ 82,
845
+ 191,
846
+ 235,
847
+ 47,
848
+ 7,
849
+ 73,
850
+ 119,
851
+ 52,
852
+ 167,
853
+ 155,
854
+ 203,
855
+ 53,
856
+ 91,
857
+ 155,
858
+ 140,
859
+ 125,
860
+ 3,
861
+ 79,
862
+ 18,
863
+ 28,
864
+ 244,
865
+ 52,
866
+ 215,
867
+ 62,
868
+ 247,
869
+ 45,
870
+ 218,
871
+ 25,
872
+ 135,
873
+ 0,
874
+ 97,
875
+ 251,
876
+ 82,
877
+ 191,
878
+ 235,
879
+ 47
880
+ ]),
881
+ TapLeaf: h.from([
882
+ 174,
883
+ 234,
884
+ 143,
885
+ 220,
886
+ 66,
887
+ 8,
888
+ 152,
889
+ 49,
890
+ 5,
891
+ 115,
892
+ 75,
893
+ 88,
894
+ 8,
895
+ 29,
896
+ 30,
897
+ 38,
898
+ 56,
899
+ 211,
900
+ 95,
901
+ 28,
902
+ 181,
903
+ 64,
904
+ 8,
905
+ 212,
906
+ 211,
907
+ 87,
908
+ 202,
909
+ 3,
910
+ 190,
911
+ 120,
912
+ 233,
913
+ 238,
914
+ 174,
915
+ 234,
916
+ 143,
917
+ 220,
918
+ 66,
919
+ 8,
920
+ 152,
921
+ 49,
922
+ 5,
923
+ 115,
924
+ 75,
925
+ 88,
926
+ 8,
927
+ 29,
928
+ 30,
929
+ 38,
930
+ 56,
931
+ 211,
932
+ 95,
933
+ 28,
934
+ 181,
935
+ 64,
936
+ 8,
937
+ 212,
938
+ 211,
939
+ 87,
940
+ 202,
941
+ 3,
942
+ 190,
943
+ 120,
944
+ 233,
945
+ 238
946
+ ]),
947
+ TapBranch: h.from([
948
+ 25,
949
+ 65,
950
+ 161,
951
+ 242,
952
+ 229,
953
+ 110,
954
+ 185,
955
+ 95,
956
+ 162,
957
+ 169,
958
+ 241,
959
+ 148,
960
+ 190,
961
+ 92,
962
+ 1,
963
+ 247,
964
+ 33,
965
+ 111,
966
+ 51,
967
+ 237,
968
+ 130,
969
+ 176,
970
+ 145,
971
+ 70,
972
+ 52,
973
+ 144,
974
+ 208,
975
+ 91,
976
+ 245,
977
+ 22,
978
+ 160,
979
+ 21,
980
+ 25,
981
+ 65,
982
+ 161,
983
+ 242,
984
+ 229,
985
+ 110,
986
+ 185,
987
+ 95,
988
+ 162,
989
+ 169,
990
+ 241,
991
+ 148,
992
+ 190,
993
+ 92,
994
+ 1,
995
+ 247,
996
+ 33,
997
+ 111,
998
+ 51,
999
+ 237,
1000
+ 130,
1001
+ 176,
1002
+ 145,
1003
+ 70,
1004
+ 52,
1005
+ 144,
1006
+ 208,
1007
+ 91,
1008
+ 245,
1009
+ 22,
1010
+ 160,
1011
+ 21
1012
+ ]),
1013
+ TapSighash: h.from([
1014
+ 244,
1015
+ 10,
1016
+ 72,
1017
+ 223,
1018
+ 75,
1019
+ 42,
1020
+ 112,
1021
+ 200,
1022
+ 180,
1023
+ 146,
1024
+ 75,
1025
+ 242,
1026
+ 101,
1027
+ 70,
1028
+ 97,
1029
+ 237,
1030
+ 61,
1031
+ 149,
1032
+ 253,
1033
+ 102,
1034
+ 163,
1035
+ 19,
1036
+ 235,
1037
+ 135,
1038
+ 35,
1039
+ 117,
1040
+ 151,
1041
+ 198,
1042
+ 40,
1043
+ 228,
1044
+ 160,
1045
+ 49,
1046
+ 244,
1047
+ 10,
1048
+ 72,
1049
+ 223,
1050
+ 75,
1051
+ 42,
1052
+ 112,
1053
+ 200,
1054
+ 180,
1055
+ 146,
1056
+ 75,
1057
+ 242,
1058
+ 101,
1059
+ 70,
1060
+ 97,
1061
+ 237,
1062
+ 61,
1063
+ 149,
1064
+ 253,
1065
+ 102,
1066
+ 163,
1067
+ 19,
1068
+ 235,
1069
+ 135,
1070
+ 35,
1071
+ 117,
1072
+ 151,
1073
+ 198,
1074
+ 40,
1075
+ 228,
1076
+ 160,
1077
+ 49
1078
+ ]),
1079
+ TapTweak: h.from([
1080
+ 232,
1081
+ 15,
1082
+ 225,
1083
+ 99,
1084
+ 156,
1085
+ 156,
1086
+ 160,
1087
+ 80,
1088
+ 227,
1089
+ 175,
1090
+ 27,
1091
+ 57,
1092
+ 193,
1093
+ 67,
1094
+ 198,
1095
+ 62,
1096
+ 66,
1097
+ 156,
1098
+ 188,
1099
+ 235,
1100
+ 21,
1101
+ 217,
1102
+ 64,
1103
+ 251,
1104
+ 181,
1105
+ 197,
1106
+ 161,
1107
+ 244,
1108
+ 175,
1109
+ 87,
1110
+ 197,
1111
+ 233,
1112
+ 232,
1113
+ 15,
1114
+ 225,
1115
+ 99,
1116
+ 156,
1117
+ 156,
1118
+ 160,
1119
+ 80,
1120
+ 227,
1121
+ 175,
1122
+ 27,
1123
+ 57,
1124
+ 193,
1125
+ 67,
1126
+ 198,
1127
+ 62,
1128
+ 66,
1129
+ 156,
1130
+ 188,
1131
+ 235,
1132
+ 21,
1133
+ 217,
1134
+ 64,
1135
+ 251,
1136
+ 181,
1137
+ 197,
1138
+ 161,
1139
+ 244,
1140
+ 175,
1141
+ 87,
1142
+ 197,
1143
+ 233
1144
+ ]),
1145
+ "KeyAgg list": h.from([
1146
+ 72,
1147
+ 28,
1148
+ 151,
1149
+ 28,
1150
+ 60,
1151
+ 11,
1152
+ 70,
1153
+ 215,
1154
+ 240,
1155
+ 178,
1156
+ 117,
1157
+ 174,
1158
+ 89,
1159
+ 141,
1160
+ 78,
1161
+ 44,
1162
+ 126,
1163
+ 215,
1164
+ 49,
1165
+ 156,
1166
+ 89,
1167
+ 74,
1168
+ 92,
1169
+ 110,
1170
+ 199,
1171
+ 158,
1172
+ 160,
1173
+ 212,
1174
+ 153,
1175
+ 2,
1176
+ 148,
1177
+ 240,
1178
+ 72,
1179
+ 28,
1180
+ 151,
1181
+ 28,
1182
+ 60,
1183
+ 11,
1184
+ 70,
1185
+ 215,
1186
+ 240,
1187
+ 178,
1188
+ 117,
1189
+ 174,
1190
+ 89,
1191
+ 141,
1192
+ 78,
1193
+ 44,
1194
+ 126,
1195
+ 215,
1196
+ 49,
1197
+ 156,
1198
+ 89,
1199
+ 74,
1200
+ 92,
1201
+ 110,
1202
+ 199,
1203
+ 158,
1204
+ 160,
1205
+ 212,
1206
+ 153,
1207
+ 2,
1208
+ 148,
1209
+ 240
1210
+ ]),
1211
+ "KeyAgg coefficient": h.from([
1212
+ 191,
1213
+ 201,
1214
+ 4,
1215
+ 3,
1216
+ 77,
1217
+ 28,
1218
+ 136,
1219
+ 232,
1220
+ 200,
1221
+ 14,
1222
+ 34,
1223
+ 229,
1224
+ 61,
1225
+ 36,
1226
+ 86,
1227
+ 109,
1228
+ 100,
1229
+ 130,
1230
+ 78,
1231
+ 214,
1232
+ 66,
1233
+ 114,
1234
+ 129,
1235
+ 192,
1236
+ 145,
1237
+ 0,
1238
+ 249,
1239
+ 77,
1240
+ 205,
1241
+ 82,
1242
+ 201,
1243
+ 129,
1244
+ 191,
1245
+ 201,
1246
+ 4,
1247
+ 3,
1248
+ 77,
1249
+ 28,
1250
+ 136,
1251
+ 232,
1252
+ 200,
1253
+ 14,
1254
+ 34,
1255
+ 229,
1256
+ 61,
1257
+ 36,
1258
+ 86,
1259
+ 109,
1260
+ 100,
1261
+ 130,
1262
+ 78,
1263
+ 214,
1264
+ 66,
1265
+ 114,
1266
+ 129,
1267
+ 192,
1268
+ 145,
1269
+ 0,
1270
+ 249,
1271
+ 77,
1272
+ 205,
1273
+ 82,
1274
+ 201,
1275
+ 129
1276
+ ])
1277
+ };
1278
+ function ke(e, t) {
1279
+ return M(h.concat([Br[e], t]));
1280
+ }
1281
+ const is = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1282
+ __proto__: null,
1283
+ TAGGED_HASH_PREFIXES: Br,
1284
+ TAGS: ss,
1285
+ hash160: x,
1286
+ hash256: te,
1287
+ ripemd160: rs,
1288
+ sha1: ns,
1289
+ sha256: M,
1290
+ taggedHash: ke
1291
+ }, Symbol.toStringTag, { value: "Module" })), _e = {};
1292
+ function Ai(e) {
1293
+ e ? e !== _e.eccLib && (os(e), _e.eccLib = e) : _e.eccLib = e;
1294
+ }
1295
+ function Fr() {
1296
+ if (!_e.eccLib)
1297
+ throw new Error("No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance");
1298
+ return _e.eccLib;
1299
+ }
1300
+ const Z = (e) => h.from(e, "hex");
1301
+ function os(e) {
1302
+ F(typeof e.isXOnlyPoint == "function"), F(e.isXOnlyPoint(Z("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))), F(e.isXOnlyPoint(Z("fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e"))), F(e.isXOnlyPoint(Z("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"))), F(e.isXOnlyPoint(Z("0000000000000000000000000000000000000000000000000000000000000001"))), F(!e.isXOnlyPoint(Z("0000000000000000000000000000000000000000000000000000000000000000"))), F(!e.isXOnlyPoint(Z("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"))), F(typeof e.xOnlyPointAddTweak == "function"), us.forEach((t) => {
1303
+ const r = e.xOnlyPointAddTweak(Z(t.pubkey), Z(t.tweak));
1304
+ t.result === null ? F(r === null) : (F(r !== null), F(r.parity === t.parity), F(h.from(r.xOnlyPubkey).equals(Z(t.result))));
1305
+ });
1306
+ }
1307
+ function F(e) {
1308
+ if (!e)
1309
+ throw new Error("ecc library invalid");
1310
+ }
1311
+ const us = [
1312
+ {
1313
+ pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
1314
+ tweak: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
1315
+ parity: -1,
1316
+ result: null
1317
+ },
1318
+ {
1319
+ pubkey: "1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b",
1320
+ tweak: "a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac",
1321
+ parity: 1,
1322
+ result: "e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf"
1323
+ },
1324
+ {
1325
+ pubkey: "2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
1326
+ tweak: "823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47",
1327
+ parity: 0,
1328
+ result: "9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c"
1329
+ }
1330
+ ], { typeforce: Vr } = Qe;
1331
+ function Lr(e, t) {
1332
+ if (typeof e != "number")
1333
+ throw new Error("cannot write a non-number as a number");
1334
+ if (e < 0)
1335
+ throw new Error("specified a negative value for writing an unsigned value");
1336
+ if (e > t)
1337
+ throw new Error("RangeError: value out of range");
1338
+ if (Math.floor(e) !== e)
1339
+ throw new Error("value has a fractional component");
1340
+ }
1341
+ function fs(e, t) {
1342
+ const r = e.readUInt32LE(t);
1343
+ let n = e.readUInt32LE(t + 4);
1344
+ return n *= 4294967296, Lr(n + r, 9007199254740991), n + r;
1345
+ }
1346
+ function cs(e, t, r) {
1347
+ return Lr(t, 9007199254740991), e.writeInt32LE(t & -1, r), e.writeUInt32LE(Math.floor(t / 4294967296), r + 4), r + 8;
1348
+ }
1349
+ function kt(e) {
1350
+ if (e.length < 1)
1351
+ return e;
1352
+ let t = e.length - 1, r = 0;
1353
+ for (let n = 0; n < e.length / 2; n++)
1354
+ r = e[n], e[n] = e[t], e[t] = r, t--;
1355
+ return e;
1356
+ }
1357
+ function Wt(e) {
1358
+ const t = h.allocUnsafe(e.length);
1359
+ return e.copy(t), t;
1360
+ }
1361
+ class k {
1362
+ constructor(t, r = 0) {
1363
+ this.buffer = t, this.offset = r, Vr(D(V, z), [t, r]);
1364
+ }
1365
+ static withCapacity(t) {
1366
+ return new k(h.alloc(t));
1367
+ }
1368
+ writeUInt8(t) {
1369
+ this.offset = this.buffer.writeUInt8(t, this.offset);
1370
+ }
1371
+ writeInt32(t) {
1372
+ this.offset = this.buffer.writeInt32LE(t, this.offset);
1373
+ }
1374
+ writeUInt32(t) {
1375
+ this.offset = this.buffer.writeUInt32LE(t, this.offset);
1376
+ }
1377
+ writeUInt64(t) {
1378
+ this.offset = cs(this.buffer, t, this.offset);
1379
+ }
1380
+ writeVarInt(t) {
1381
+ const r = Sr(t, this.buffer, this.offset);
1382
+ this.offset += r.bytes;
1383
+ }
1384
+ writeSlice(t) {
1385
+ if (this.buffer.length < this.offset + t.length)
1386
+ throw new Error("Cannot write slice out of bounds");
1387
+ this.offset += t.copy(this.buffer, this.offset);
1388
+ }
1389
+ writeVarSlice(t) {
1390
+ this.writeVarInt(t.length), this.writeSlice(t);
1391
+ }
1392
+ writeVector(t) {
1393
+ this.writeVarInt(t.length), t.forEach((r) => this.writeVarSlice(r));
1394
+ }
1395
+ end() {
1396
+ if (this.buffer.length === this.offset)
1397
+ return this.buffer;
1398
+ throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
1399
+ }
1400
+ }
1401
+ class as {
1402
+ constructor(t, r = 0) {
1403
+ this.buffer = t, this.offset = r, Vr(D(V, z), [t, r]);
1404
+ }
1405
+ readUInt8() {
1406
+ const t = this.buffer.readUInt8(this.offset);
1407
+ return this.offset++, t;
1408
+ }
1409
+ readInt32() {
1410
+ const t = this.buffer.readInt32LE(this.offset);
1411
+ return this.offset += 4, t;
1412
+ }
1413
+ readUInt32() {
1414
+ const t = this.buffer.readUInt32LE(this.offset);
1415
+ return this.offset += 4, t;
1416
+ }
1417
+ readUInt64() {
1418
+ const t = fs(this.buffer, this.offset);
1419
+ return this.offset += 8, t;
1420
+ }
1421
+ readVarInt() {
1422
+ const t = bn(this.buffer, this.offset);
1423
+ return this.offset += t.bytes, t.numberValue || 0;
1424
+ }
1425
+ readSlice(t) {
1426
+ if (this.buffer.length < this.offset + t)
1427
+ throw new Error("Cannot read slice out of bounds");
1428
+ const r = h.from(this.buffer.subarray(this.offset, this.offset + t));
1429
+ return this.offset += t, r;
1430
+ }
1431
+ readVarSlice() {
1432
+ return this.readSlice(this.readVarInt());
1433
+ }
1434
+ readVector() {
1435
+ const t = this.readVarInt(), r = [];
1436
+ for (let n = 0; n < t; n++)
1437
+ r.push(this.readVarSlice());
1438
+ return r;
1439
+ }
1440
+ }
1441
+ const Nt = 192, Wr = 128, hs = (e) => "left" in e && "right" in e;
1442
+ function Xe(e, t) {
1443
+ if (e.length < 33)
1444
+ throw new TypeError(`The control-block length is too small. Got ${e.length}, expected min 33.`);
1445
+ const r = (e.length - 33) / 32;
1446
+ let n = t;
1447
+ for (let s = 0; s < r; s++) {
1448
+ const i = e.slice(33 + 32 * s, 65 + 32 * s);
1449
+ n.compare(i) < 0 ? n = gt(n, i) : n = gt(i, n);
1450
+ }
1451
+ return n;
1452
+ }
1453
+ function De(e) {
1454
+ if (Ze(e))
1455
+ return { hash: j(e) };
1456
+ const t = [De(e[0]), De(e[1])];
1457
+ t.sort((s, i) => s.hash.compare(i.hash));
1458
+ const [r, n] = t;
1459
+ return {
1460
+ hash: gt(r.hash, n.hash),
1461
+ left: r,
1462
+ right: n
1463
+ };
1464
+ }
1465
+ function Ae(e, t) {
1466
+ if (hs(e)) {
1467
+ const r = Ae(e.left, t);
1468
+ if (r !== void 0)
1469
+ return [...r, e.right.hash];
1470
+ const n = Ae(e.right, t);
1471
+ if (n !== void 0)
1472
+ return [...n, e.left.hash];
1473
+ } else if (e.hash.equals(t))
1474
+ return [];
1475
+ }
1476
+ function j(e) {
1477
+ const t = e.version || Nt;
1478
+ return ke("TapLeaf", N.concat([N.from([t]), ps(e.output)]));
1479
+ }
1480
+ function Gr(e, t) {
1481
+ return ke("TapTweak", N.concat(t ? [e, t] : [e]));
1482
+ }
1483
+ function ye(e, t) {
1484
+ if (!N.isBuffer(e) || e.length !== 32 || t && t.length !== 32)
1485
+ return null;
1486
+ const r = Gr(e, t), n = Fr().xOnlyPointAddTweak(e, r);
1487
+ return !n || n.xOnlyPubkey === null ? null : {
1488
+ parity: n.parity,
1489
+ x: N.from(n.xOnlyPubkey)
1490
+ };
1491
+ }
1492
+ function gt(e, t) {
1493
+ return ke("TapBranch", N.concat([e, t]));
1494
+ }
1495
+ function ps(e) {
1496
+ const t = Pe(e.length), r = N.allocUnsafe(t);
1497
+ return Sr(e.length, r), N.concat([r, e]);
1498
+ }
1499
+ function w(e, t, r) {
1500
+ Object.defineProperty(e, t, {
1501
+ configurable: !0,
1502
+ enumerable: !0,
1503
+ get() {
1504
+ const n = r.call(this);
1505
+ return this[t] = n, n;
1506
+ },
1507
+ set(n) {
1508
+ Object.defineProperty(this, t, {
1509
+ configurable: !0,
1510
+ enumerable: !0,
1511
+ value: n,
1512
+ writable: !0
1513
+ });
1514
+ }
1515
+ });
1516
+ }
1517
+ function C(e) {
1518
+ let t;
1519
+ return () => (t !== void 0 || (t = e()), t);
1520
+ }
1521
+ const Gt = E;
1522
+ function ls(e, t) {
1523
+ if (!e.data && !e.output)
1524
+ throw new TypeError("Not enough data");
1525
+ t = Object.assign({ validate: !0 }, t || {}), f({
1526
+ network: f.maybe(f.Object),
1527
+ output: f.maybe(f.Buffer),
1528
+ data: f.maybe(f.arrayOf(f.Buffer))
1529
+ }, e);
1530
+ const r = e.network || K, n = { name: G.Embed, network: r, data: [] };
1531
+ if (w(n, "output", () => {
1532
+ if (e.data)
1533
+ return H([Gt.OP_RETURN].concat(e.data));
1534
+ }), w(n, "data", () => {
1535
+ if (!e.output)
1536
+ return;
1537
+ const s = b(e.output);
1538
+ if (s != null)
1539
+ return s.slice(1);
1540
+ }), t.validate && e.output) {
1541
+ const s = b(e.output);
1542
+ if (s[0] !== Gt.OP_RETURN)
1543
+ throw new TypeError("Output is invalid");
1544
+ if (!s.slice(1).every(f.Buffer))
1545
+ throw new TypeError("Output is invalid");
1546
+ if (e.data && !ie(e.data, n.data))
1547
+ throw new TypeError("Data mismatch");
1548
+ }
1549
+ return Object.assign(n, e);
1550
+ }
1551
+ const ue = E, Ce = ue.OP_RESERVED;
1552
+ function Ne(e, t) {
1553
+ if (!e.input && !e.output && !(e.pubkeys && e.m !== void 0) && !e.signatures)
1554
+ throw new TypeError("Not enough data");
1555
+ t = Object.assign({ validate: !0 }, t || {});
1556
+ function r(c) {
1557
+ return Y(c) || (t.allowIncomplete && c === ue.OP_0) !== void 0;
1558
+ }
1559
+ f({
1560
+ network: f.maybe(f.Object),
1561
+ m: f.maybe(f.Number),
1562
+ n: f.maybe(f.Number),
1563
+ output: f.maybe(f.Buffer),
1564
+ pubkeys: f.maybe(f.arrayOf(L)),
1565
+ signatures: f.maybe(f.arrayOf(r)),
1566
+ input: f.maybe(f.Buffer)
1567
+ }, e);
1568
+ const s = {
1569
+ network: e.network || K,
1570
+ name: G.P2MS
1571
+ };
1572
+ let i = [], o = !1;
1573
+ function u(c) {
1574
+ o || (o = !0, i = b(c), s.m = i[0] - Ce, s.n = i[i.length - 2] - Ce, s.pubkeys = i.slice(1, -2));
1575
+ }
1576
+ if (w(s, "output", () => {
1577
+ if (e.m && s.n && e.pubkeys)
1578
+ return H([].concat(Ce + e.m, e.pubkeys, Ce + s.n, ue.OP_CHECKMULTISIG));
1579
+ }), w(s, "m", () => {
1580
+ if (s.output)
1581
+ return u(s.output), s.m;
1582
+ }), w(s, "n", () => {
1583
+ if (s.pubkeys)
1584
+ return s.pubkeys.length;
1585
+ }), w(s, "pubkeys", () => {
1586
+ if (e.output)
1587
+ return u(e.output), s.pubkeys;
1588
+ }), w(s, "signatures", () => {
1589
+ if (!e.input)
1590
+ return;
1591
+ const c = b(e.input);
1592
+ if (c != null)
1593
+ return c.slice(1);
1594
+ }), w(s, "input", () => {
1595
+ if (e.signatures)
1596
+ return H([ue.OP_0].concat(e.signatures));
1597
+ }), w(s, "witness", () => {
1598
+ if (s.input)
1599
+ return [];
1600
+ }), w(s, "name", () => {
1601
+ if (!(!s.m || !s.n))
1602
+ return `p2ms(${s.m} of ${s.n})`;
1603
+ }), t.validate) {
1604
+ if (e.output) {
1605
+ if (u(e.output), !f.Number(i[0]))
1606
+ throw new TypeError("Output is invalid");
1607
+ if (!f.Number(i[i.length - 2]))
1608
+ throw new TypeError("Output is invalid");
1609
+ if (i[i.length - 1] !== ue.OP_CHECKMULTISIG)
1610
+ throw new TypeError("Output is invalid");
1611
+ if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== i.length - 3)
1612
+ throw new TypeError("Output is invalid");
1613
+ if (!s.pubkeys.every((c) => L(c)))
1614
+ throw new TypeError("Output is invalid");
1615
+ if (e.m !== void 0 && e.m !== s.m)
1616
+ throw new TypeError("m mismatch");
1617
+ if (e.n !== void 0 && e.n !== s.n)
1618
+ throw new TypeError("n mismatch");
1619
+ if (e.pubkeys && !ie(e.pubkeys, s.pubkeys))
1620
+ throw new TypeError("Pubkeys mismatch");
1621
+ }
1622
+ if (e.pubkeys) {
1623
+ if (e.n !== void 0 && e.n !== e.pubkeys.length)
1624
+ throw new TypeError("Pubkey count mismatch");
1625
+ if (s.n = e.pubkeys.length, s.n < s.m)
1626
+ throw new TypeError("Pubkey count cannot be less than m");
1627
+ }
1628
+ if (e.signatures) {
1629
+ if (e.signatures.length < s.m)
1630
+ throw new TypeError("Not enough signatures provided");
1631
+ if (e.signatures.length > s.m)
1632
+ throw new TypeError("Too many signatures provided");
1633
+ }
1634
+ if (e.input) {
1635
+ if (e.input[0] !== ue.OP_0)
1636
+ throw new TypeError("Input is invalid");
1637
+ if (s.signatures.length === 0 || !s.signatures.every(r))
1638
+ throw new TypeError("Input has invalid signature(s)");
1639
+ if (e.signatures && !ie(e.signatures, s.signatures))
1640
+ throw new TypeError("Signature mismatch");
1641
+ if (e.m !== void 0 && e.m !== e.signatures.length)
1642
+ throw new TypeError("Signature count mismatch");
1643
+ }
1644
+ }
1645
+ return Object.assign(s, e);
1646
+ }
1647
+ const Kt = E;
1648
+ function Ut(e, t) {
1649
+ if (!e.input && !e.output && !e.pubkey && !e.input && !e.signature)
1650
+ throw new TypeError("Not enough data");
1651
+ t = Object.assign({ validate: !0 }, t || {}), f({
1652
+ network: f.maybe(f.Object),
1653
+ output: f.maybe(f.Buffer),
1654
+ pubkey: f.maybe(L),
1655
+ signature: f.maybe(Y),
1656
+ input: f.maybe(f.Buffer)
1657
+ }, e);
1658
+ const r = C(() => b(e.input)), n = e.network || K, s = {
1659
+ name: G.P2PK,
1660
+ network: n,
1661
+ pubkey: void 0
1662
+ };
1663
+ if (w(s, "output", () => {
1664
+ if (e.pubkey)
1665
+ return H([e.pubkey, Kt.OP_CHECKSIG]);
1666
+ }), w(s, "pubkey", () => {
1667
+ if (e.output)
1668
+ return e.output.slice(1, -1);
1669
+ }), w(s, "signature", () => {
1670
+ if (e.input)
1671
+ return r()[0];
1672
+ }), w(s, "input", () => {
1673
+ if (e.signature)
1674
+ return H([e.signature]);
1675
+ }), w(s, "witness", () => {
1676
+ if (s.input)
1677
+ return [];
1678
+ }), t.validate) {
1679
+ if (e.output) {
1680
+ if (e.output[e.output.length - 1] !== Kt.OP_CHECKSIG)
1681
+ throw new TypeError("Output is invalid");
1682
+ if (!L(s.pubkey))
1683
+ throw new TypeError("Output pubkey is invalid");
1684
+ if (e.pubkey && !e.pubkey.equals(s.pubkey))
1685
+ throw new TypeError("Pubkey mismatch");
1686
+ }
1687
+ if (e.signature && e.input && !e.input.equals(s.input))
1688
+ throw new TypeError("Signature mismatch");
1689
+ if (e.input) {
1690
+ if (r().length !== 1)
1691
+ throw new TypeError("Input is invalid");
1692
+ if (!Y(s.signature))
1693
+ throw new TypeError("Input has invalid signature");
1694
+ }
1695
+ }
1696
+ return Object.assign(s, e);
1697
+ }
1698
+ const ds = {
1699
+ p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
1700
+ n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
1701
+ b: 7n,
1702
+ Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
1703
+ Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n
1704
+ }, { p: se, n: Kr, Gx: ws, Gy: gs, b: Mr } = ds, We = 32, mt = 64, W = (e = "") => {
1705
+ throw new Error(e);
1706
+ }, qr = (e) => typeof e == "bigint", $r = (e) => typeof e == "string", ms = (e) => e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array", He = (e, t) => !ms(e) || typeof t == "number" && t > 0 && e.length !== t ? W("Uint8Array expected") : e, Xr = (e) => new Uint8Array(e), ys = (e) => Uint8Array.from(e), Dr = (e, t) => e.toString(16).padStart(t, "0"), zr = (e) => Array.from(He(e)).map((t) => Dr(t, 2)).join(""), Q = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, Mt = (e) => {
1707
+ if (e >= Q._0 && e <= Q._9)
1708
+ return e - Q._0;
1709
+ if (e >= Q.A && e <= Q.F)
1710
+ return e - (Q.A - 10);
1711
+ if (e >= Q.a && e <= Q.f)
1712
+ return e - (Q.a - 10);
1713
+ }, jr = (e) => {
1714
+ const t = "hex invalid";
1715
+ if (!$r(e))
1716
+ return W(t);
1717
+ const r = e.length, n = r / 2;
1718
+ if (r % 2)
1719
+ return W(t);
1720
+ const s = Xr(n);
1721
+ for (let i = 0, o = 0; i < n; i++, o += 2) {
1722
+ const u = Mt(e.charCodeAt(o)), c = Mt(e.charCodeAt(o + 1));
1723
+ if (u === void 0 || c === void 0)
1724
+ return W(t);
1725
+ s[i] = u * 16 + c;
1726
+ }
1727
+ return s;
1728
+ }, Yr = (e, t) => He($r(e) ? jr(e) : ys(He(e)), t), qt = (...e) => {
1729
+ const t = Xr(e.reduce((n, s) => n + He(s).length, 0));
1730
+ let r = 0;
1731
+ return e.forEach((n) => {
1732
+ t.set(n, r), r += n.length;
1733
+ }), t;
1734
+ }, ze = BigInt, Ue = (e, t, r, n = "bad number: out of range") => qr(e) && t <= e && e < r ? e : W(n), g = (e, t = se) => {
1735
+ const r = e % t;
1736
+ return r >= 0n ? r : t + r;
1737
+ }, Es = (e, t) => {
1738
+ (e === 0n || t <= 0n) && W("no inverse n=" + e + " mod=" + t);
1739
+ let r = g(e, t), n = t, s = 0n, i = 1n;
1740
+ for (; r !== 0n; ) {
1741
+ const o = n / r, u = n % r, c = s - i * o;
1742
+ n = r, r = u, s = i, i = c;
1743
+ }
1744
+ return n === 1n ? g(s, t) : W("no inverse");
1745
+ }, $t = (e) => e instanceof R ? e : W("Point expected"), Zr = (e) => g(g(e * e) * e + Mr), Xt = (e) => Ue(e, 0n, se), Ge = (e) => Ue(e, 1n, se), Ss = (e) => Ue(e, 1n, Kr), yt = (e) => (e & 1n) === 0n, Qr = (e) => Uint8Array.of(e), _s = (e) => Qr(yt(e) ? 2 : 3), bs = (e) => {
1746
+ const t = Zr(Ge(e));
1747
+ let r = 1n;
1748
+ for (let n = t, s = (se + 1n) / 4n; s > 0n; s >>= 1n)
1749
+ s & 1n && (r = r * n % se), n = n * n % se;
1750
+ return g(r * r) === t ? r : W("sqrt invalid");
1751
+ };
1752
+ class R {
1753
+ static BASE;
1754
+ static ZERO;
1755
+ px;
1756
+ py;
1757
+ pz;
1758
+ constructor(t, r, n) {
1759
+ this.px = Xt(t), this.py = Ge(r), this.pz = Xt(n), Object.freeze(this);
1760
+ }
1761
+ /** Convert Uint8Array or hex string to Point. */
1762
+ static fromBytes(t) {
1763
+ He(t);
1764
+ let r;
1765
+ const n = t[0], s = t.subarray(1), i = Dt(s, 0, We), o = t.length;
1766
+ if (o === We + 1 && [2, 3].includes(n)) {
1767
+ let u = bs(i);
1768
+ const c = yt(u);
1769
+ yt(ze(n)) !== c && (u = g(-u)), r = new R(i, u, 1n);
1770
+ }
1771
+ return o === mt + 1 && n === 4 && (r = new R(i, Dt(s, We, mt), 1n)), r ? r.assertValidity() : W("bad point: not on curve");
1772
+ }
1773
+ /** Equality check: compare points P&Q. */
1774
+ equals(t) {
1775
+ const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = $t(t), c = g(r * u), a = g(i * s), p = g(n * u), l = g(o * s);
1776
+ return c === a && p === l;
1777
+ }
1778
+ is0() {
1779
+ return this.equals(ne);
1780
+ }
1781
+ /** Flip point over y coordinate. */
1782
+ negate() {
1783
+ return new R(this.px, g(-this.py), this.pz);
1784
+ }
1785
+ /** Point doubling: P+P, complete formula. */
1786
+ double() {
1787
+ return this.add(this);
1788
+ }
1789
+ /**
1790
+ * Point addition: P+Q, complete, exception-free formula
1791
+ * (Renes-Costello-Batina, algo 1 of [2015/1060](https://eprint.iacr.org/2015/1060)).
1792
+ * Cost: `12M + 0S + 3*a + 3*b3 + 23add`.
1793
+ */
1794
+ // prettier-ignore
1795
+ add(t) {
1796
+ const { px: r, py: n, pz: s } = this, { px: i, py: o, pz: u } = $t(t), c = 0n, a = Mr;
1797
+ let p = 0n, l = 0n, m = 0n;
1798
+ const T = g(a * 3n);
1799
+ let y = g(r * i), O = g(n * o), I = g(s * u), B = g(r + n), v = g(i + o);
1800
+ B = g(B * v), v = g(y + O), B = g(B - v), v = g(r + s);
1801
+ let S = g(i + u);
1802
+ return v = g(v * S), S = g(y + I), v = g(v - S), S = g(n + s), p = g(o + u), S = g(S * p), p = g(O + I), S = g(S - p), m = g(c * v), p = g(T * I), m = g(p + m), p = g(O - m), m = g(O + m), l = g(p * m), O = g(y + y), O = g(O + y), I = g(c * I), v = g(T * v), O = g(O + I), I = g(y - I), I = g(c * I), v = g(v + I), y = g(O * v), l = g(l + y), y = g(S * v), p = g(B * p), p = g(p - y), y = g(B * O), m = g(S * m), m = g(m + y), new R(p, l, m);
1803
+ }
1804
+ /**
1805
+ * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
1806
+ * Uses {@link wNAF} for base point.
1807
+ * Uses fake point to mitigate side-channel leakage.
1808
+ * @param n scalar by which point is multiplied
1809
+ * @param safe safe mode guards against timing attacks; unsafe mode is faster
1810
+ */
1811
+ multiply(t, r = !0) {
1812
+ if (!r && t === 0n)
1813
+ return ne;
1814
+ if (Ss(t), t === 1n)
1815
+ return this;
1816
+ if (this.equals(ce))
1817
+ return As(t).p;
1818
+ let n = ne, s = ce;
1819
+ for (let i = this; t > 0n; i = i.double(), t >>= 1n)
1820
+ t & 1n ? n = n.add(i) : r && (s = s.add(i));
1821
+ return n;
1822
+ }
1823
+ /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
1824
+ toAffine() {
1825
+ const { px: t, py: r, pz: n } = this;
1826
+ if (this.equals(ne))
1827
+ return { x: 0n, y: 0n };
1828
+ if (n === 1n)
1829
+ return { x: t, y: r };
1830
+ const s = Es(n, se);
1831
+ return g(n * s) !== 1n && W("inverse invalid"), { x: g(t * s), y: g(r * s) };
1832
+ }
1833
+ /** Checks if the point is valid and on-curve. */
1834
+ assertValidity() {
1835
+ const { x: t, y: r } = this.toAffine();
1836
+ return Ge(t), Ge(r), g(r * r) === Zr(t) ? this : W("bad point: not on curve");
1837
+ }
1838
+ /** Converts point to 33/65-byte Uint8Array. */
1839
+ toBytes(t = !0) {
1840
+ const { x: r, y: n } = this.assertValidity().toAffine(), s = zt(r);
1841
+ return t ? qt(_s(n), s) : qt(Qr(4), s, zt(n));
1842
+ }
1843
+ /** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */
1844
+ static fromAffine(t) {
1845
+ const { x: r, y: n } = t;
1846
+ return r === 0n && n === 0n ? ne : new R(r, n, 1n);
1847
+ }
1848
+ toHex(t) {
1849
+ return zr(this.toBytes(t));
1850
+ }
1851
+ static fromPrivateKey(t) {
1852
+ return ce.multiply(Os(t));
1853
+ }
1854
+ static fromHex(t) {
1855
+ return R.fromBytes(Yr(t));
1856
+ }
1857
+ get x() {
1858
+ return this.toAffine().x;
1859
+ }
1860
+ get y() {
1861
+ return this.toAffine().y;
1862
+ }
1863
+ toRawBytes(t) {
1864
+ return this.toBytes(t);
1865
+ }
1866
+ }
1867
+ const ce = new R(ws, gs, 1n), ne = new R(0n, 1n, 0n);
1868
+ R.BASE = ce;
1869
+ R.ZERO = ne;
1870
+ const Jr = (e) => ze("0x" + (zr(e) || "0")), Dt = (e, t, r) => Jr(e.subarray(t, r)), Ts = 2n ** 256n, zt = (e) => jr(Dr(Ue(e, 0n, Ts), mt)), Os = (e) => {
1871
+ const t = qr(e) ? e : Jr(Yr(e, We));
1872
+ return Ue(t, 1n, Kr, "private key invalid 3");
1873
+ }, je = 8, Ps = 256, en = Math.ceil(Ps / je) + 1, Et = 2 ** (je - 1), Is = () => {
1874
+ const e = [];
1875
+ let t = ce, r = t;
1876
+ for (let n = 0; n < en; n++) {
1877
+ r = t, e.push(r);
1878
+ for (let s = 1; s < Et; s++)
1879
+ r = r.add(t), e.push(r);
1880
+ t = r.double();
1881
+ }
1882
+ return e;
1883
+ };
1884
+ let jt;
1885
+ const Yt = (e, t) => {
1886
+ const r = t.negate();
1887
+ return e ? r : t;
1888
+ }, As = (e) => {
1889
+ const t = jt || (jt = Is());
1890
+ let r = ne, n = ce;
1891
+ const s = 2 ** je, i = s, o = ze(s - 1), u = ze(je);
1892
+ for (let c = 0; c < en; c++) {
1893
+ let a = Number(e & o);
1894
+ e >>= u, a > Et && (a -= i, e += 1n);
1895
+ const p = c * Et, l = p, m = p + Math.abs(a) - 1, T = c % 2 !== 0, y = a < 0;
1896
+ a === 0 ? n = n.add(Yt(T, t[l])) : r = r.add(Yt(y, t[m]));
1897
+ }
1898
+ return { p: r, f: n };
1899
+ }, we = E;
1900
+ function he(e, t) {
1901
+ if (!e.address && !e.hash && !e.output && !e.redeem && !e.input)
1902
+ throw new TypeError("Not enough data");
1903
+ t = Object.assign({ validate: !0 }, t || {}), f({
1904
+ network: f.maybe(f.Object),
1905
+ address: f.maybe(f.String),
1906
+ hash: f.maybe(f.BufferN(20)),
1907
+ output: f.maybe(f.BufferN(23)),
1908
+ redeem: f.maybe({
1909
+ network: f.maybe(f.Object),
1910
+ output: f.maybe(f.Buffer),
1911
+ input: f.maybe(f.Buffer),
1912
+ witness: f.maybe(f.arrayOf(f.Buffer))
1913
+ }),
1914
+ input: f.maybe(f.Buffer),
1915
+ witness: f.maybe(f.arrayOf(f.Buffer))
1916
+ }, e);
1917
+ let r = e.network;
1918
+ r || (r = e.redeem && e.redeem.network || K);
1919
+ const n = {
1920
+ network: r,
1921
+ name: G.P2SH
1922
+ }, s = C(() => {
1923
+ const u = h.from(Oe.decode(e.address)), c = u.readUInt8(0), a = u.slice(1);
1924
+ return { version: c, hash: a };
1925
+ }), i = C(() => b(e.input)), o = C(() => {
1926
+ const u = i(), c = u[u.length - 1];
1927
+ return {
1928
+ network: r,
1929
+ output: c === we.OP_FALSE ? h.from([]) : c,
1930
+ input: H(u.slice(0, -1)),
1931
+ witness: e.witness || []
1932
+ };
1933
+ });
1934
+ if (w(n, "address", () => {
1935
+ if (!n.hash)
1936
+ return;
1937
+ const u = h.allocUnsafe(21);
1938
+ return u.writeUInt8(n.network.scriptHash, 0), n.hash.copy(u, 1), Oe.encode(u);
1939
+ }), w(n, "hash", () => {
1940
+ if (e.output)
1941
+ return e.output.slice(2, 22);
1942
+ if (e.address)
1943
+ return s().hash;
1944
+ if (n.redeem && n.redeem.output)
1945
+ return x(n.redeem.output);
1946
+ }), w(n, "output", () => {
1947
+ if (n.hash)
1948
+ return H([we.OP_HASH160, n.hash, we.OP_EQUAL]);
1949
+ }), w(n, "redeem", () => {
1950
+ if (e.input)
1951
+ return o();
1952
+ }), w(n, "input", () => {
1953
+ if (!(!e.redeem || !e.redeem.input || !e.redeem.output))
1954
+ return H([].concat(b(e.redeem.input), e.redeem.output));
1955
+ }), w(n, "witness", () => {
1956
+ if (n.redeem && n.redeem.witness)
1957
+ return n.redeem.witness;
1958
+ if (n.input)
1959
+ return [];
1960
+ }), w(n, "name", () => {
1961
+ const u = ["p2sh"];
1962
+ return n.redeem !== void 0 && n.redeem.name !== void 0 && u.push(n.redeem.name), u.join("-");
1963
+ }), t.validate) {
1964
+ let u = h.from([]);
1965
+ if (e.address) {
1966
+ if (s().version !== r.scriptHash)
1967
+ throw new TypeError("Invalid version or Network mismatch");
1968
+ if (s().hash.length !== 20)
1969
+ throw new TypeError("Invalid address");
1970
+ u = s().hash;
1971
+ }
1972
+ if (e.hash) {
1973
+ if (u.length > 0 && !u.equals(e.hash))
1974
+ throw new TypeError("Hash mismatch");
1975
+ u = e.hash;
1976
+ }
1977
+ if (e.output) {
1978
+ if (e.output.length !== 23 || e.output[0] !== we.OP_HASH160 || e.output[1] !== 20 || e.output[22] !== we.OP_EQUAL)
1979
+ throw new TypeError("Output is invalid");
1980
+ const a = e.output.slice(2, 22);
1981
+ if (u.length > 0 && !u.equals(a))
1982
+ throw new TypeError("Hash mismatch");
1983
+ u = a;
1984
+ }
1985
+ const c = (a) => {
1986
+ if (a.output) {
1987
+ const p = b(a.output);
1988
+ if (!p || p.length < 1)
1989
+ throw new TypeError("Redeem.output too short");
1990
+ if (a.output.byteLength > 520)
1991
+ throw new TypeError("Redeem.output unspendable if larger than 520 bytes");
1992
+ if (It(p) > 201)
1993
+ throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");
1994
+ const l = x(a.output);
1995
+ if (u.length > 0 && !u.equals(l))
1996
+ throw new TypeError("Hash mismatch");
1997
+ u = l;
1998
+ }
1999
+ if (a.input) {
2000
+ const p = a.input.length > 0, l = a.witness && a.witness.length > 0;
2001
+ if (!p && !l)
2002
+ throw new TypeError("Empty input");
2003
+ if (p && l)
2004
+ throw new TypeError("Input and witness provided");
2005
+ if (p) {
2006
+ const m = b(a.input);
2007
+ if (!Je(m))
2008
+ throw new TypeError("Non push-only scriptSig");
2009
+ }
2010
+ }
2011
+ };
2012
+ if (e.input) {
2013
+ const a = i();
2014
+ if (!a || a.length < 1)
2015
+ throw new TypeError("Input too short");
2016
+ if (!h.isBuffer(o().output))
2017
+ throw new TypeError("Input is invalid");
2018
+ c(o());
2019
+ }
2020
+ if (e.redeem) {
2021
+ if (e.redeem.network && e.redeem.network !== r)
2022
+ throw new TypeError("Network mismatch");
2023
+ if (e.input) {
2024
+ const a = o();
2025
+ if (e.redeem.output && !e.redeem.output.equals(a.output))
2026
+ throw new TypeError("Redeem.output mismatch");
2027
+ if (e.redeem.input && !e.redeem.input.equals(a.input))
2028
+ throw new TypeError("Redeem.input mismatch");
2029
+ }
2030
+ c(e.redeem);
2031
+ }
2032
+ if (e.witness && e.redeem && e.redeem.witness && !ie(e.redeem.witness, e.witness))
2033
+ throw new TypeError("Witness and redeem.witness mismatch");
2034
+ }
2035
+ return Object.assign(n, e);
2036
+ }
2037
+ const Zt = E, Qt = 1, Hs = 80;
2038
+ function pe(e, t) {
2039
+ if (!e.address && !e.output && !e.pubkey && !e.internalPubkey && !(e.witness && e.witness.length > 1))
2040
+ throw new TypeError("Not enough data");
2041
+ t = Object.assign({ validate: !0 }, t || {}), f({
2042
+ address: f.maybe(f.String),
2043
+ input: f.maybe(f.BufferN(0)),
2044
+ network: f.maybe(f.Object),
2045
+ output: f.maybe(f.BufferN(34)),
2046
+ internalPubkey: f.maybe(f.BufferN(32)),
2047
+ hash: f.maybe(f.BufferN(32)),
2048
+ pubkey: f.maybe(f.BufferN(32)),
2049
+ signature: f.maybe(f.anyOf(f.BufferN(64), f.BufferN(65))),
2050
+ witness: f.maybe(f.arrayOf(f.Buffer)),
2051
+ scriptTree: f.maybe(Pt),
2052
+ redeem: f.maybe({
2053
+ output: f.maybe(f.Buffer),
2054
+ redeemVersion: f.maybe(f.Number),
2055
+ witness: f.maybe(f.arrayOf(f.Buffer))
2056
+ }),
2057
+ redeemVersion: f.maybe(f.Number)
2058
+ }, e);
2059
+ const r = C(() => vt(e.address)), n = C(() => {
2060
+ if (!(!e.witness || !e.witness.length))
2061
+ return e.witness.length >= 2 && e.witness[e.witness.length - 1][0] === Hs ? e.witness.slice(0, -1) : e.witness.slice();
2062
+ }), s = C(() => {
2063
+ if (e.scriptTree)
2064
+ return De(e.scriptTree);
2065
+ if (e.hash)
2066
+ return { hash: e.hash };
2067
+ }), i = e.network || K, o = {
2068
+ name: G.P2TR,
2069
+ network: i
2070
+ };
2071
+ if (w(o, "address", () => {
2072
+ if (!o.pubkey)
2073
+ return;
2074
+ const u = P.bech32m.toWords(o.pubkey);
2075
+ return u.unshift(Qt), P.bech32m.encode(i.bech32, u);
2076
+ }), w(o, "hash", () => {
2077
+ const u = s();
2078
+ if (u)
2079
+ return u.hash;
2080
+ const c = n();
2081
+ if (c && c.length > 1) {
2082
+ const a = c[c.length - 1], p = a[0] & Ee, l = c[c.length - 2], m = j({
2083
+ output: l,
2084
+ version: p
2085
+ });
2086
+ return Xe(a, m);
2087
+ }
2088
+ }), w(o, "output", () => {
2089
+ if (o.pubkey)
2090
+ return H([Zt.OP_1, o.pubkey]);
2091
+ }), w(o, "redeemVersion", () => e.redeemVersion ? e.redeemVersion : e.redeem && e.redeem.redeemVersion !== void 0 && e.redeem.redeemVersion !== null ? e.redeem.redeemVersion : Nt), w(o, "redeem", () => {
2092
+ const u = n();
2093
+ if (!(!u || u.length < 2))
2094
+ return {
2095
+ output: u[u.length - 2],
2096
+ witness: u.slice(0, -2),
2097
+ redeemVersion: u[u.length - 1][0] & Ee
2098
+ };
2099
+ }), w(o, "pubkey", () => {
2100
+ if (e.pubkey)
2101
+ return e.pubkey;
2102
+ if (e.output)
2103
+ return e.output.slice(2);
2104
+ if (e.address)
2105
+ return r().data;
2106
+ if (o.internalPubkey) {
2107
+ const u = ye(o.internalPubkey, o.hash);
2108
+ if (u)
2109
+ return u.x;
2110
+ }
2111
+ }), w(o, "internalPubkey", () => {
2112
+ if (e.internalPubkey)
2113
+ return e.internalPubkey;
2114
+ const u = n();
2115
+ if (u && u.length > 1)
2116
+ return u[u.length - 1].slice(1, 33);
2117
+ }), w(o, "signature", () => {
2118
+ if (e.signature)
2119
+ return e.signature;
2120
+ const u = n();
2121
+ if (!(!u || u.length !== 1))
2122
+ return u[0];
2123
+ }), w(o, "witness", () => {
2124
+ if (e.witness)
2125
+ return e.witness;
2126
+ const u = s();
2127
+ if (u && e.redeem && e.redeem.output && e.internalPubkey) {
2128
+ const c = j({
2129
+ output: e.redeem.output,
2130
+ version: o.redeemVersion
2131
+ }), a = Ae(u, c);
2132
+ if (!a)
2133
+ return;
2134
+ const p = ye(e.internalPubkey, u.hash);
2135
+ if (!p)
2136
+ return;
2137
+ const l = N.concat([N.from([o.redeemVersion | p.parity]), e.internalPubkey].concat(a));
2138
+ return [e.redeem.output, l];
2139
+ }
2140
+ if (e.signature)
2141
+ return [e.signature];
2142
+ }), t.validate) {
2143
+ let u = N.from([]);
2144
+ if (e.address) {
2145
+ if (i && i.bech32 !== r().prefix)
2146
+ throw new TypeError("Invalid prefix or Network mismatch");
2147
+ if (r().version !== Qt)
2148
+ throw new TypeError("Invalid address version");
2149
+ if (r().data.length !== 32)
2150
+ throw new TypeError("Invalid address data");
2151
+ u = r().data;
2152
+ }
2153
+ if (e.pubkey) {
2154
+ if (u.length > 0 && !u.equals(e.pubkey))
2155
+ throw new TypeError("Pubkey mismatch");
2156
+ u = e.pubkey;
2157
+ }
2158
+ if (e.output) {
2159
+ if (e.output.length !== 34 || e.output[0] !== Zt.OP_1 || e.output[1] !== 32)
2160
+ throw new TypeError("Output is invalid");
2161
+ if (u.length > 0 && !u.equals(e.output.slice(2)))
2162
+ throw new TypeError("Pubkey mismatch");
2163
+ u = e.output.slice(2);
2164
+ }
2165
+ if (e.internalPubkey) {
2166
+ const p = ye(e.internalPubkey, o.hash);
2167
+ if (u.length > 0 && !u.equals(p.x))
2168
+ throw new TypeError("Pubkey mismatch");
2169
+ u = p.x;
2170
+ }
2171
+ const c = s();
2172
+ if (e.hash && c && !e.hash.equals(c.hash))
2173
+ throw new TypeError("Hash mismatch");
2174
+ if (e.redeem && e.redeem.output && c) {
2175
+ const p = j({
2176
+ output: e.redeem.output,
2177
+ version: o.redeemVersion
2178
+ });
2179
+ if (!Ae(c, p))
2180
+ throw new TypeError("Redeem script not in tree");
2181
+ }
2182
+ const a = n();
2183
+ if (e.redeem && o.redeem) {
2184
+ if (e.redeem.redeemVersion && e.redeem.redeemVersion !== o.redeem.redeemVersion)
2185
+ throw new TypeError("Redeem.redeemVersion and witness mismatch");
2186
+ if (e.redeem.output) {
2187
+ if (b(e.redeem.output).length === 0)
2188
+ throw new TypeError("Redeem.output is invalid");
2189
+ if (o.redeem.output && !e.redeem.output.equals(o.redeem.output))
2190
+ throw new TypeError("Redeem.output and witness mismatch");
2191
+ }
2192
+ if (e.redeem.witness && o.redeem.witness && !ie(e.redeem.witness, o.redeem.witness))
2193
+ throw new TypeError("Redeem.witness and witness mismatch");
2194
+ }
2195
+ if (a && a.length)
2196
+ if (a.length === 1) {
2197
+ if (e.signature && !e.signature.equals(a[0]))
2198
+ throw new TypeError("Signature mismatch");
2199
+ } else {
2200
+ const p = a[a.length - 1];
2201
+ if (p.length < 33)
2202
+ throw new TypeError(`The control-block length is too small. Got ${p.length}, expected min 33.`);
2203
+ if ((p.length - 33) % 32 !== 0)
2204
+ throw new TypeError(`The control-block length of ${p.length} is incorrect!`);
2205
+ const l = (p.length - 33) / 32;
2206
+ if (l > 128)
2207
+ throw new TypeError(`The script path is too long. Got ${l}, expected max 128.`);
2208
+ const m = p.slice(1, 33);
2209
+ if (e.internalPubkey && !e.internalPubkey.equals(m))
2210
+ throw new TypeError("Internal pubkey mismatch");
2211
+ if (!Fr().isXOnlyPoint(m))
2212
+ throw new TypeError("Invalid internalPubkey for p2tr witness");
2213
+ const T = p[0] & Ee, y = a[a.length - 2], O = j({
2214
+ output: y,
2215
+ version: T
2216
+ }), I = Xe(p, O), B = ye(m, I);
2217
+ if (!B)
2218
+ throw new TypeError("Invalid outputKey for p2tr witness");
2219
+ if (u.length && !u.equals(B.x))
2220
+ throw new TypeError("Pubkey mismatch for p2tr witness");
2221
+ if (B.parity !== (p[0] & 1))
2222
+ throw new Error("Incorrect parity");
2223
+ }
2224
+ }
2225
+ return Object.assign(o, e);
2226
+ }
2227
+ const Jt = E, vs = h.alloc(0);
2228
+ function xe(e, t) {
2229
+ if (!e.address && !e.hash && !e.output && !e.pubkey && !e.witness)
2230
+ throw new TypeError("Not enough data");
2231
+ t = Object.assign({ validate: !0 }, t || {}), f({
2232
+ address: f.maybe(f.String),
2233
+ hash: f.maybe(f.BufferN(20)),
2234
+ input: f.maybe(f.BufferN(0)),
2235
+ network: f.maybe(f.Object),
2236
+ output: f.maybe(f.BufferN(22)),
2237
+ pubkey: f.maybe(L),
2238
+ signature: f.maybe(Y),
2239
+ witness: f.maybe(f.arrayOf(f.Buffer))
2240
+ }, e);
2241
+ const r = C(() => {
2242
+ const i = P.bech32.decode(e.address), o = i.words.shift(), u = P.bech32.fromWords(i.words);
2243
+ return {
2244
+ version: o,
2245
+ prefix: i.prefix,
2246
+ data: h.from(u)
2247
+ };
2248
+ }), n = e.network || K, s = {
2249
+ name: G.P2WPKH,
2250
+ network: n
2251
+ };
2252
+ if (w(s, "address", () => {
2253
+ if (!s.hash)
2254
+ return;
2255
+ const i = P.bech32.toWords(s.hash);
2256
+ return i.unshift(0), P.bech32.encode(n.bech32, i);
2257
+ }), w(s, "hash", () => {
2258
+ if (e.output)
2259
+ return e.output.slice(2, 22);
2260
+ if (e.address)
2261
+ return r().data;
2262
+ if (e.pubkey || s.pubkey)
2263
+ return x(e.pubkey || s.pubkey);
2264
+ }), w(s, "output", () => {
2265
+ if (s.hash)
2266
+ return H([Jt.OP_0, s.hash]);
2267
+ }), w(s, "pubkey", () => {
2268
+ if (e.pubkey)
2269
+ return e.pubkey;
2270
+ if (e.witness)
2271
+ return e.witness[1];
2272
+ }), w(s, "signature", () => {
2273
+ if (e.witness)
2274
+ return e.witness[0];
2275
+ }), w(s, "input", () => {
2276
+ if (s.witness)
2277
+ return vs;
2278
+ }), w(s, "witness", () => {
2279
+ if (e.pubkey && e.signature)
2280
+ return [e.signature, e.pubkey];
2281
+ }), t.validate) {
2282
+ let i = h.from([]);
2283
+ if (e.address) {
2284
+ if (n && n.bech32 !== r().prefix)
2285
+ throw new TypeError("Invalid prefix or Network mismatch");
2286
+ if (r().version !== 0)
2287
+ throw new TypeError("Invalid address version");
2288
+ if (r().data.length !== 20)
2289
+ throw new TypeError("Invalid address data");
2290
+ i = r().data;
2291
+ }
2292
+ if (e.hash) {
2293
+ if (i.length > 0 && !i.equals(e.hash))
2294
+ throw new TypeError("Hash mismatch");
2295
+ i = e.hash;
2296
+ }
2297
+ if (e.output) {
2298
+ if (e.output.length !== 22 || e.output[0] !== Jt.OP_0 || e.output[1] !== 20)
2299
+ throw new TypeError("Output is invalid");
2300
+ if (i.length > 0 && !i.equals(e.output.slice(2)))
2301
+ throw new TypeError("Hash mismatch");
2302
+ i = e.output.slice(2);
2303
+ }
2304
+ if (e.pubkey) {
2305
+ const o = x(e.pubkey);
2306
+ if (i.length > 0 && !i.equals(o))
2307
+ throw new TypeError("Hash mismatch");
2308
+ if (i = o, !L(e.pubkey) || e.pubkey.length !== 33)
2309
+ throw new TypeError("Invalid pubkey for p2wpkh");
2310
+ }
2311
+ if (e.witness) {
2312
+ if (e.witness.length !== 2)
2313
+ throw new TypeError("Witness is invalid");
2314
+ if (!Y(e.witness[0]))
2315
+ throw new TypeError("Witness has invalid signature");
2316
+ if (!L(e.witness[1]) || e.witness[1].length !== 33)
2317
+ throw new TypeError("Witness has invalid pubkey");
2318
+ if (e.signature && !e.signature.equals(e.witness[0]))
2319
+ throw new TypeError("Signature mismatch");
2320
+ if (e.pubkey && !e.pubkey.equals(e.witness[1]))
2321
+ throw new TypeError("Pubkey mismatch");
2322
+ const o = x(e.witness[1]);
2323
+ if (i.length > 0 && !i.equals(o))
2324
+ throw new TypeError("Hash mismatch");
2325
+ }
2326
+ }
2327
+ return Object.assign(s, e);
2328
+ }
2329
+ const er = E, ft = h.alloc(0);
2330
+ function Re(e) {
2331
+ return !!(h.isBuffer(e) && e.length === 65 && e[0] === 4 && L(e));
2332
+ }
2333
+ function le(e, t) {
2334
+ if (!e.address && !e.hash && !e.output && !e.redeem && !e.witness)
2335
+ throw new TypeError("Not enough data");
2336
+ t = Object.assign({ validate: !0 }, t || {}), f({
2337
+ network: f.maybe(f.Object),
2338
+ address: f.maybe(f.String),
2339
+ hash: f.maybe(f.BufferN(32)),
2340
+ output: f.maybe(f.BufferN(34)),
2341
+ redeem: f.maybe({
2342
+ input: f.maybe(f.Buffer),
2343
+ network: f.maybe(f.Object),
2344
+ output: f.maybe(f.Buffer),
2345
+ witness: f.maybe(f.arrayOf(f.Buffer))
2346
+ }),
2347
+ input: f.maybe(f.BufferN(0)),
2348
+ witness: f.maybe(f.arrayOf(f.Buffer))
2349
+ }, e);
2350
+ const r = C(() => {
2351
+ const o = P.bech32.decode(e.address), u = o.words.shift(), c = P.bech32.fromWords(o.words);
2352
+ return {
2353
+ version: u,
2354
+ prefix: o.prefix,
2355
+ data: h.from(c)
2356
+ };
2357
+ }), n = C(() => b(e.redeem.input));
2358
+ let s = e.network;
2359
+ s || (s = e.redeem && e.redeem.network || K);
2360
+ const i = {
2361
+ network: s,
2362
+ name: G.P2WSH
2363
+ };
2364
+ if (w(i, "address", () => {
2365
+ if (!i.hash)
2366
+ return;
2367
+ const o = P.bech32.toWords(i.hash);
2368
+ return o.unshift(0), P.bech32.encode(s.bech32, o);
2369
+ }), w(i, "hash", () => {
2370
+ if (e.output)
2371
+ return e.output.slice(2);
2372
+ if (e.address)
2373
+ return r().data;
2374
+ if (i.redeem && i.redeem.output)
2375
+ return M(i.redeem.output);
2376
+ }), w(i, "output", () => {
2377
+ if (i.hash)
2378
+ return H([er.OP_0, i.hash]);
2379
+ }), w(i, "redeem", () => {
2380
+ if (e.witness)
2381
+ return {
2382
+ output: e.witness[e.witness.length - 1],
2383
+ input: ft,
2384
+ witness: e.witness.slice(0, -1)
2385
+ };
2386
+ }), w(i, "input", () => {
2387
+ if (i.witness)
2388
+ return ft;
2389
+ }), w(i, "witness", () => {
2390
+ if (e.redeem && e.redeem.input && e.redeem.input.length > 0 && e.redeem.output && e.redeem.output.length > 0) {
2391
+ const o = Cr(n());
2392
+ return i.redeem = Object.assign({ witness: o }, e.redeem), i.redeem.input = ft, [].concat(o, e.redeem.output);
2393
+ }
2394
+ if (e.redeem && e.redeem.output && e.redeem.witness)
2395
+ return [].concat(e.redeem.witness, e.redeem.output);
2396
+ }), w(i, "name", () => {
2397
+ const o = ["p2wsh"];
2398
+ return i.redeem !== void 0 && i.redeem.name !== void 0 && o.push(i.redeem.name), o.join("-");
2399
+ }), t.validate) {
2400
+ let o = h.from([]);
2401
+ if (e.address) {
2402
+ if (r().prefix !== s.bech32)
2403
+ throw new TypeError("Invalid prefix or Network mismatch");
2404
+ if (r().version !== 0)
2405
+ throw new TypeError("Invalid address version");
2406
+ if (r().data.length !== 32)
2407
+ throw new TypeError("Invalid address data");
2408
+ o = r().data;
2409
+ }
2410
+ if (e.hash) {
2411
+ if (o.length > 0 && !o.equals(e.hash))
2412
+ throw new TypeError("Hash mismatch");
2413
+ o = e.hash;
2414
+ }
2415
+ if (e.output) {
2416
+ if (e.output.length !== 34 || e.output[0] !== er.OP_0 || e.output[1] !== 32)
2417
+ throw new TypeError("Output is invalid");
2418
+ const u = e.output.slice(2);
2419
+ if (o.length > 0 && !o.equals(u))
2420
+ throw new TypeError("Hash mismatch");
2421
+ o = u;
2422
+ }
2423
+ if (e.redeem) {
2424
+ if (e.redeem.network && e.redeem.network !== s)
2425
+ throw new TypeError("Network mismatch");
2426
+ if (e.redeem.input && e.redeem.input.length > 0 && e.redeem.witness && e.redeem.witness.length > 0)
2427
+ throw new TypeError("Ambiguous witness source");
2428
+ if (e.redeem.output) {
2429
+ const u = b(e.redeem.output);
2430
+ if (!u || u.length < 1)
2431
+ throw new TypeError("Redeem.output is invalid");
2432
+ if (e.redeem.output.byteLength > 3600)
2433
+ throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");
2434
+ if (It(u) > 201)
2435
+ throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");
2436
+ const c = M(e.redeem.output);
2437
+ if (o.length > 0 && !o.equals(c))
2438
+ throw new TypeError("Hash mismatch");
2439
+ o = c;
2440
+ }
2441
+ if (e.redeem.input && !Je(n()))
2442
+ throw new TypeError("Non push-only scriptSig");
2443
+ if (e.witness && e.redeem.witness && !ie(e.witness, e.redeem.witness))
2444
+ throw new TypeError("Witness and redeem.witness mismatch");
2445
+ if (e.redeem.input && n().some(Re) || e.redeem.output && (b(e.redeem.output) || []).some(Re))
2446
+ throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey");
2447
+ }
2448
+ if (e.witness && e.witness.length > 0) {
2449
+ const u = e.witness[e.witness.length - 1];
2450
+ if (e.redeem && e.redeem.output && !e.redeem.output.equals(u))
2451
+ throw new TypeError("Witness and redeem.output mismatch");
2452
+ if (e.witness.some(Re) || (b(u) || []).some(Re))
2453
+ throw new TypeError("Witness contains uncompressed pubkey");
2454
+ }
2455
+ }
2456
+ return Object.assign(i, e);
2457
+ }
2458
+ const { typeforce: $ } = Qe;
2459
+ function X(e) {
2460
+ const t = e.length;
2461
+ return Pe(t) + t;
2462
+ }
2463
+ function ks(e) {
2464
+ const t = e.length;
2465
+ return Pe(t) + e.reduce((r, n) => r + X(n), 0);
2466
+ }
2467
+ const re = h.allocUnsafe(0), tr = [], ct = h.from("0000000000000000000000000000000000000000000000000000000000000000", "hex"), rr = h.from("0000000000000000000000000000000000000000000000000000000000000001", "hex"), Ns = h.from("ffffffffffffffff", "hex"), Us = {
2468
+ script: re,
2469
+ valueBuffer: Ns
2470
+ };
2471
+ function xs(e) {
2472
+ return e.value !== void 0;
2473
+ }
2474
+ class d {
2475
+ constructor() {
2476
+ this.version = 1, this.locktime = 0, this.ins = [], this.outs = [];
2477
+ }
2478
+ static fromBuffer(t, r) {
2479
+ const n = new as(t), s = new d();
2480
+ s.version = n.readInt32();
2481
+ const i = n.readUInt8(), o = n.readUInt8();
2482
+ let u = !1;
2483
+ i === d.ADVANCED_TRANSACTION_MARKER && o === d.ADVANCED_TRANSACTION_FLAG ? u = !0 : n.offset -= 2;
2484
+ const c = n.readVarInt();
2485
+ for (let p = 0; p < c; ++p) {
2486
+ const l = n.readSlice(32), m = n.readUInt32(), T = n.readVarSlice(), y = n.readUInt32();
2487
+ s.ins.push({
2488
+ hash: l,
2489
+ index: m,
2490
+ script: T,
2491
+ sequence: y,
2492
+ witness: tr
2493
+ });
2494
+ }
2495
+ const a = n.readVarInt();
2496
+ for (let p = 0; p < a; ++p)
2497
+ s.outs.push({
2498
+ value: n.readUInt64(),
2499
+ script: n.readVarSlice()
2500
+ });
2501
+ if (u) {
2502
+ for (let p = 0; p < c; ++p)
2503
+ s.ins[p].witness = n.readVector();
2504
+ if (!s.hasWitnesses())
2505
+ throw new Error("Transaction has superfluous witness data");
2506
+ }
2507
+ if (s.locktime = n.readUInt32(), r)
2508
+ return s;
2509
+ if (n.offset !== t.length)
2510
+ throw new Error("Transaction has unexpected data");
2511
+ return s;
2512
+ }
2513
+ static fromHex(t) {
2514
+ return d.fromBuffer(h.from(t, "hex"), !1);
2515
+ }
2516
+ static isCoinbaseHash(t) {
2517
+ $(lt, t);
2518
+ for (let r = 0; r < 32; ++r)
2519
+ if (t[r] !== 0)
2520
+ return !1;
2521
+ return !0;
2522
+ }
2523
+ isCoinbase() {
2524
+ return this.ins.length === 1 && d.isCoinbaseHash(this.ins[0].hash);
2525
+ }
2526
+ addInput(t, r, n, s) {
2527
+ return $(D(lt, z, dt(z), dt(V)), arguments), vr(n) && (n = d.DEFAULT_SEQUENCE), this.ins.push({
2528
+ hash: t,
2529
+ index: r,
2530
+ script: s || re,
2531
+ sequence: n,
2532
+ witness: tr
2533
+ }) - 1;
2534
+ }
2535
+ addOutput(t, r) {
2536
+ return $(D(V, Le), arguments), this.outs.push({
2537
+ script: t,
2538
+ value: r
2539
+ }) - 1;
2540
+ }
2541
+ hasWitnesses() {
2542
+ return this.ins.some((t) => t.witness.length !== 0);
2543
+ }
2544
+ weight() {
2545
+ const t = this.byteLength(!1), r = this.byteLength(!0);
2546
+ return t * 3 + r;
2547
+ }
2548
+ virtualSize() {
2549
+ return Math.ceil(this.weight() / 4);
2550
+ }
2551
+ byteLength(t = !0) {
2552
+ const r = t && this.hasWitnesses();
2553
+ return (r ? 10 : 8) + Pe(this.ins.length) + Pe(this.outs.length) + this.ins.reduce((n, s) => n + 40 + X(s.script), 0) + this.outs.reduce((n, s) => n + 8 + X(s.script), 0) + (r ? this.ins.reduce((n, s) => n + ks(s.witness), 0) : 0);
2554
+ }
2555
+ clone() {
2556
+ const t = new d();
2557
+ return t.version = this.version, t.locktime = this.locktime, t.ins = this.ins.map((r) => ({
2558
+ hash: r.hash,
2559
+ index: r.index,
2560
+ script: r.script,
2561
+ sequence: r.sequence,
2562
+ witness: r.witness
2563
+ })), t.outs = this.outs.map((r) => ({
2564
+ script: r.script,
2565
+ value: r.value
2566
+ })), t;
2567
+ }
2568
+ hashForSignature(t, r, n) {
2569
+ if ($(D(z, V, Se), arguments), t >= this.ins.length)
2570
+ return rr;
2571
+ const s = H(b(r).filter((u) => u !== E.OP_CODESEPARATOR)), i = this.clone();
2572
+ if ((n & 31) === d.SIGHASH_NONE)
2573
+ i.outs = [], i.ins.forEach((u, c) => {
2574
+ c !== t && (u.sequence = 0);
2575
+ });
2576
+ else if ((n & 31) === d.SIGHASH_SINGLE) {
2577
+ if (t >= this.outs.length)
2578
+ return rr;
2579
+ i.outs.length = t + 1;
2580
+ for (let u = 0; u < t; u++)
2581
+ i.outs[u] = Us;
2582
+ i.ins.forEach((u, c) => {
2583
+ c !== t && (u.sequence = 0);
2584
+ });
2585
+ }
2586
+ n & d.SIGHASH_ANYONECANPAY ? (i.ins = [i.ins[t]], i.ins[0].script = s) : (i.ins.forEach((u) => {
2587
+ u.script = re;
2588
+ }), i.ins[t].script = s);
2589
+ const o = h.allocUnsafe(i.byteLength(!1) + 4);
2590
+ return o.writeInt32LE(n, o.length - 4), i.__toBuffer(o, 0, !1), te(o);
2591
+ }
2592
+ hashForWitnessV1(t, r, n, s, i, o) {
2593
+ if ($(D(z, $.arrayOf(V), $.arrayOf(Le), z), arguments), n.length !== this.ins.length || r.length !== this.ins.length)
2594
+ throw new Error("Must supply prevout script and value for all inputs");
2595
+ const u = s === d.SIGHASH_DEFAULT ? d.SIGHASH_ALL : s & d.SIGHASH_OUTPUT_MASK, a = (s & d.SIGHASH_INPUT_MASK) === d.SIGHASH_ANYONECANPAY, p = u === d.SIGHASH_NONE, l = u === d.SIGHASH_SINGLE;
2596
+ let m = re, T = re, y = re, O = re, I = re;
2597
+ if (!a) {
2598
+ let _ = k.withCapacity(36 * this.ins.length);
2599
+ this.ins.forEach((A) => {
2600
+ _.writeSlice(A.hash), _.writeUInt32(A.index);
2601
+ }), m = M(_.end()), _ = k.withCapacity(8 * this.ins.length), n.forEach((A) => _.writeUInt64(A)), T = M(_.end()), _ = k.withCapacity(r.map(X).reduce((A, J) => A + J)), r.forEach((A) => _.writeVarSlice(A)), y = M(_.end()), _ = k.withCapacity(4 * this.ins.length), this.ins.forEach((A) => _.writeUInt32(A.sequence)), O = M(_.end());
2602
+ }
2603
+ if (p || l) {
2604
+ if (l && t < this.outs.length) {
2605
+ const _ = this.outs[t], A = k.withCapacity(8 + X(_.script));
2606
+ A.writeUInt64(_.value), A.writeVarSlice(_.script), I = M(A.end());
2607
+ }
2608
+ } else {
2609
+ if (!this.outs.length)
2610
+ throw new Error("Add outputs to the transaction before signing.");
2611
+ const _ = this.outs.map((J) => 8 + X(J.script)).reduce((J, Sn) => J + Sn), A = k.withCapacity(_);
2612
+ this.outs.forEach((J) => {
2613
+ A.writeUInt64(J.value), A.writeVarSlice(J.script);
2614
+ }), I = M(A.end());
2615
+ }
2616
+ const B = (i ? 2 : 0) + (o ? 1 : 0), v = 174 - (a ? 49 : 0) - (p ? 32 : 0) + (o ? 32 : 0) + (i ? 37 : 0), S = k.withCapacity(v);
2617
+ if (S.writeUInt8(s), S.writeInt32(this.version), S.writeUInt32(this.locktime), S.writeSlice(m), S.writeSlice(T), S.writeSlice(y), S.writeSlice(O), p || l || S.writeSlice(I), S.writeUInt8(B), a) {
2618
+ const _ = this.ins[t];
2619
+ S.writeSlice(_.hash), S.writeUInt32(_.index), S.writeUInt64(n[t]), S.writeVarSlice(r[t]), S.writeUInt32(_.sequence);
2620
+ } else
2621
+ S.writeUInt32(t);
2622
+ if (o) {
2623
+ const _ = k.withCapacity(X(o));
2624
+ _.writeVarSlice(o), S.writeSlice(M(_.end()));
2625
+ }
2626
+ return l && S.writeSlice(I), i && (S.writeSlice(i), S.writeUInt8(0), S.writeUInt32(4294967295)), ke("TapSighash", h.concat([h.from([0]), S.end()]));
2627
+ }
2628
+ hashForWitnessV0(t, r, n, s) {
2629
+ $(D(z, V, Le, z), arguments);
2630
+ let i = h.from([]), o, u = ct, c = ct, a = ct;
2631
+ if (s & d.SIGHASH_ANYONECANPAY || (i = h.allocUnsafe(36 * this.ins.length), o = new k(i, 0), this.ins.forEach((l) => {
2632
+ o.writeSlice(l.hash), o.writeUInt32(l.index);
2633
+ }), c = te(i)), !(s & d.SIGHASH_ANYONECANPAY) && (s & 31) !== d.SIGHASH_SINGLE && (s & 31) !== d.SIGHASH_NONE && (i = h.allocUnsafe(4 * this.ins.length), o = new k(i, 0), this.ins.forEach((l) => {
2634
+ o.writeUInt32(l.sequence);
2635
+ }), a = te(i)), (s & 31) !== d.SIGHASH_SINGLE && (s & 31) !== d.SIGHASH_NONE) {
2636
+ const l = this.outs.reduce((m, T) => m + 8 + X(T.script), 0);
2637
+ i = h.allocUnsafe(l), o = new k(i, 0), this.outs.forEach((m) => {
2638
+ o.writeUInt64(m.value), o.writeVarSlice(m.script);
2639
+ }), u = te(i);
2640
+ } else if ((s & 31) === d.SIGHASH_SINGLE && t < this.outs.length) {
2641
+ const l = this.outs[t];
2642
+ i = h.allocUnsafe(8 + X(l.script)), o = new k(i, 0), o.writeUInt64(l.value), o.writeVarSlice(l.script), u = te(i);
2643
+ }
2644
+ i = h.allocUnsafe(156 + X(r)), o = new k(i, 0);
2645
+ const p = this.ins[t];
2646
+ return o.writeInt32(this.version), o.writeSlice(c), o.writeSlice(a), o.writeSlice(p.hash), o.writeUInt32(p.index), o.writeVarSlice(r), o.writeUInt64(n), o.writeUInt32(p.sequence), o.writeSlice(u), o.writeUInt32(this.locktime), o.writeUInt32(s), te(i);
2647
+ }
2648
+ getHash(t) {
2649
+ return t && this.isCoinbase() ? h.alloc(32, 0) : te(this.__toBuffer(void 0, void 0, t));
2650
+ }
2651
+ getId() {
2652
+ return kt(this.getHash(!1)).toString("hex");
2653
+ }
2654
+ toBuffer(t, r) {
2655
+ return this.__toBuffer(t, r, !0);
2656
+ }
2657
+ toHex() {
2658
+ return this.toBuffer(void 0, void 0).toString("hex");
2659
+ }
2660
+ setInputScript(t, r) {
2661
+ $(D(Se, V), arguments), this.ins[t].script = r;
2662
+ }
2663
+ setWitness(t, r) {
2664
+ $(D(Se, [V]), arguments), this.ins[t].witness = r;
2665
+ }
2666
+ __toBuffer(t, r, n = !1) {
2667
+ t || (t = h.allocUnsafe(this.byteLength(n)));
2668
+ const s = new k(t, r || 0);
2669
+ s.writeInt32(this.version);
2670
+ const i = n && this.hasWitnesses();
2671
+ return i && (s.writeUInt8(d.ADVANCED_TRANSACTION_MARKER), s.writeUInt8(d.ADVANCED_TRANSACTION_FLAG)), s.writeVarInt(this.ins.length), this.ins.forEach((o) => {
2672
+ s.writeSlice(o.hash), s.writeUInt32(o.index), s.writeVarSlice(o.script), s.writeUInt32(o.sequence);
2673
+ }), s.writeVarInt(this.outs.length), this.outs.forEach((o) => {
2674
+ xs(o) ? s.writeUInt64(o.value) : s.writeSlice(o.valueBuffer), s.writeVarSlice(o.script);
2675
+ }), i && this.ins.forEach((o) => {
2676
+ s.writeVector(o.witness);
2677
+ }), s.writeUInt32(this.locktime), r !== void 0 ? t.slice(r, s.offset) : t;
2678
+ }
2679
+ }
2680
+ d.DEFAULT_SEQUENCE = 4294967295;
2681
+ d.SIGHASH_DEFAULT = 0;
2682
+ d.SIGHASH_ALL = 1;
2683
+ d.SIGHASH_NONE = 2;
2684
+ d.SIGHASH_SINGLE = 3;
2685
+ d.SIGHASH_ANYONECANPAY = 128;
2686
+ d.SIGHASH_OUTPUT_MASK = 3;
2687
+ d.SIGHASH_INPUT_MASK = 128;
2688
+ d.ADVANCED_TRANSACTION_MARKER = 0;
2689
+ d.ADVANCED_TRANSACTION_FLAG = 1;
2690
+ d.TRUC_VERSION = 3;
2691
+ d.TRUC_MAX_VSIZE = 1e4;
2692
+ d.TRUC_CHILD_MAX_VSIZE = 1e3;
2693
+ const be = (e) => {
2694
+ const t = e.length === 32 ? e : e.slice(1, 33);
2695
+ return h.isBuffer(t) ? t : h.from(t);
2696
+ };
2697
+ function nr(e, t, r) {
2698
+ const n = Ds(t, e, r);
2699
+ try {
2700
+ const i = $s(t, n).concat(n.script).concat(n.controlBlock);
2701
+ return { finalScriptWitness: Te(i) };
2702
+ } catch (s) {
2703
+ throw new Error(`Can not finalize taproot input #${e}: ${s}`);
2704
+ }
2705
+ }
2706
+ function Be(e, t) {
2707
+ const r = t ? h.from([t]) : h.from([]);
2708
+ return h.concat([e, r]);
2709
+ }
2710
+ function q(e) {
2711
+ return e && !!(e.tapInternalKey || e.tapMerkleRoot || e.tapLeafScript && e.tapLeafScript.length || e.tapBip32Derivation && e.tapBip32Derivation.length || e.witnessUtxo && nn(e.witnessUtxo.script));
2712
+ }
2713
+ function at(e, t) {
2714
+ return e && !!(e.tapInternalKey || e.tapTree || e.tapBip32Derivation && e.tapBip32Derivation.length || t);
2715
+ }
2716
+ function sr(e, t, r) {
2717
+ Ks(e, t, r), qs(e, t, r);
2718
+ }
2719
+ function ir(e, t, r) {
2720
+ Ms(e, t, r), Cs(e, t);
2721
+ }
2722
+ function Cs(e, t) {
2723
+ if (!t.tapTree && !t.tapInternalKey)
2724
+ return;
2725
+ const r = t.tapInternalKey || e.tapInternalKey, n = t.tapTree || e.tapTree;
2726
+ if (r) {
2727
+ const { script: s } = e, i = Rs(r, n);
2728
+ if (s && !s.equals(i))
2729
+ throw new Error("Error adding output. Script or address missmatch.");
2730
+ }
2731
+ }
2732
+ function Rs(e, t) {
2733
+ const r = t && Bs(t.leaves), { output: n } = pe({
2734
+ internalPubkey: e,
2735
+ scriptTree: r
2736
+ });
2737
+ return n;
2738
+ }
2739
+ function Bs(e = []) {
2740
+ return e.length === 1 && e[0].depth === 0 ? {
2741
+ output: e[0].script,
2742
+ version: e[0].leafVersion
2743
+ } : Gs(e);
2744
+ }
2745
+ function Fs(e, t) {
2746
+ return Ls(e).some((n) => on(n, Vs, t));
2747
+ }
2748
+ function Vs(e) {
2749
+ return {
2750
+ signature: e.slice(0, 64),
2751
+ hashType: e.slice(64)[0] || d.SIGHASH_DEFAULT
2752
+ };
2753
+ }
2754
+ function Ls(e) {
2755
+ const t = [];
2756
+ if (e.tapKeySig && t.push(e.tapKeySig), e.tapScriptSig && t.push(...e.tapScriptSig.map((r) => r.signature)), !t.length) {
2757
+ const r = Ws(e.finalScriptWitness);
2758
+ r && t.push(r);
2759
+ }
2760
+ return t;
2761
+ }
2762
+ function Ws(e) {
2763
+ if (!e)
2764
+ return;
2765
+ const t = e.slice(2);
2766
+ if (t.length === 64 || t.length === 65)
2767
+ return t;
2768
+ }
2769
+ function Gs(e) {
2770
+ let t;
2771
+ for (const r of e)
2772
+ if (t = St(r, t), !t)
2773
+ throw new Error("No room left to insert tapleaf in tree");
2774
+ return t;
2775
+ }
2776
+ function St(e, t, r = 0) {
2777
+ if (r > Wr)
2778
+ throw new Error("Max taptree depth exceeded.");
2779
+ if (e.depth === r)
2780
+ return t ? void 0 : {
2781
+ output: e.script,
2782
+ version: e.leafVersion
2783
+ };
2784
+ if (Ze(t))
2785
+ return;
2786
+ const n = St(e, t && t[0], r + 1);
2787
+ if (n)
2788
+ return [n, t && t[1]];
2789
+ const s = St(e, t && t[1], r + 1);
2790
+ if (s)
2791
+ return [t && t[0], s];
2792
+ }
2793
+ function Ks(e, t, r) {
2794
+ const n = q(e) && ae(t), s = ae(e) && q(t), i = e === t && q(t) && ae(t);
2795
+ if (n || s || i)
2796
+ throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
2797
+ }
2798
+ function Ms(e, t, r) {
2799
+ const n = at(e) && ae(t), s = ae(e) && at(t), i = e === t && at(t) && ae(t);
2800
+ if (n || s || i)
2801
+ throw new Error(`Invalid arguments for Psbt.${r}. Cannot use both taproot and non-taproot fields.`);
2802
+ }
2803
+ function qs(e, t, r) {
2804
+ if (t.tapMerkleRoot) {
2805
+ const n = (t.tapLeafScript || []).every((i) => ht(i, t.tapMerkleRoot)), s = (e.tapLeafScript || []).every((i) => ht(i, t.tapMerkleRoot));
2806
+ if (!n || !s)
2807
+ throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
2808
+ } else if (e.tapMerkleRoot && !(t.tapLeafScript || []).every((s) => ht(s, e.tapMerkleRoot)))
2809
+ throw new Error(`Invalid arguments for Psbt.${r}. Tapleaf not part of taptree.`);
2810
+ }
2811
+ function ht(e, t) {
2812
+ if (!t)
2813
+ return !0;
2814
+ const r = j({
2815
+ output: e.script,
2816
+ version: e.leafVersion
2817
+ });
2818
+ return Xe(e.controlBlock, r).equals(t);
2819
+ }
2820
+ function $s(e, t) {
2821
+ const r = j({
2822
+ output: t.script,
2823
+ version: t.leafVersion
2824
+ });
2825
+ return (e.tapScriptSig || []).filter((n) => n.leafHash.equals(r)).map((n) => Xs(t.script, n)).sort((n, s) => s.positionInScript - n.positionInScript).map((n) => n.signature);
2826
+ }
2827
+ function Xs(e, t) {
2828
+ return Object.assign({
2829
+ positionInScript: sn(t.pubkey, e)
2830
+ }, t);
2831
+ }
2832
+ function Ds(e, t, r) {
2833
+ if (!e.tapScriptSig || !e.tapScriptSig.length)
2834
+ throw new Error(`Can not finalize taproot input #${t}. No tapleaf script signature provided.`);
2835
+ const n = (e.tapLeafScript || []).sort((s, i) => s.controlBlock.length - i.controlBlock.length).find((s) => zs(s, e.tapScriptSig, r));
2836
+ if (!n)
2837
+ throw new Error(`Can not finalize taproot input #${t}. Signature for tapleaf script not found.`);
2838
+ return n;
2839
+ }
2840
+ function zs(e, t, r) {
2841
+ const n = j({
2842
+ output: e.script,
2843
+ version: e.leafVersion
2844
+ });
2845
+ return (!r || r.equals(n)) && t.find((i) => i.leafHash.equals(n)) !== void 0;
2846
+ }
2847
+ function ae(e) {
2848
+ return e && !!(e.redeemScript || e.witnessScript || e.bip32Derivation && e.bip32Derivation.length);
2849
+ }
2850
+ const or = E, ur = 16, fr = 2, cr = 40;
2851
+ function tn(e, t) {
2852
+ if (!e.address && !e.output && !e.program && (typeof e.deploymentVersion > "u" || !e.hash160))
2853
+ throw new TypeError("At least one of address, output or program must be provided");
2854
+ t = Object.assign({ validate: !0 }, t || {}), f({
2855
+ address: f.maybe(f.String),
2856
+ output: f.maybe(f.Buffer),
2857
+ program: f.maybe(f.Buffer),
2858
+ network: f.maybe(f.Object),
2859
+ deploymentVersion: f.maybe(f.Number),
2860
+ hash160: f.maybe(f.BufferN(20))
2861
+ }, e);
2862
+ const r = () => {
2863
+ if (typeof e.deploymentVersion < "u" && typeof e.hash160 < "u") {
2864
+ if (e.hash160.length !== 20)
2865
+ throw new TypeError("hash160 must be exactly 20 bytes");
2866
+ if (e.deploymentVersion < 0 || e.deploymentVersion > 255)
2867
+ throw new TypeError("deploymentVersion must fit in one byte");
2868
+ return h.concat([h.of(e.deploymentVersion), e.hash160]);
2869
+ }
2870
+ }, n = C(() => vt(e.address)), s = e.network || K, i = {
2871
+ name: G.P2OP,
2872
+ network: s,
2873
+ deploymentVersion: 0
2874
+ };
2875
+ if (w(i, "program", () => {
2876
+ if (e.program)
2877
+ return e.program;
2878
+ const o = r();
2879
+ if (o)
2880
+ return o;
2881
+ if (e.output) {
2882
+ if (e.output[0] !== or.OP_16)
2883
+ throw new TypeError("Invalid P2OP script");
2884
+ let u = 1, c;
2885
+ if (e.output[1] < 76)
2886
+ c = e.output[1], u = 2;
2887
+ else if (e.output[1] === 76)
2888
+ c = e.output[2], u = 3;
2889
+ else
2890
+ throw new TypeError("Unsupported push opcode in P2OP script");
2891
+ return e.output.slice(u, u + c);
2892
+ }
2893
+ if (e.address)
2894
+ return n().data;
2895
+ }), w(i, "deploymentVersion", () => {
2896
+ if (i.program)
2897
+ return i.program[0];
2898
+ }), w(i, "hash160", () => {
2899
+ if (i.program)
2900
+ return i.program.slice(1);
2901
+ }), w(i, "output", () => {
2902
+ if (i.program)
2903
+ return H([or.OP_16, i.program]);
2904
+ }), w(i, "address", () => {
2905
+ if (!i.program)
2906
+ return;
2907
+ if (!s.bech32Opnet)
2908
+ throw new TypeError("Network does not support opnet");
2909
+ const o = P.bech32m.toWords(i.program);
2910
+ return o.unshift(ur), P.bech32m.encode(s.bech32Opnet, o);
2911
+ }), t.validate) {
2912
+ let o = N.alloc(0);
2913
+ if (e.address) {
2914
+ const u = n();
2915
+ if (s.bech32Opnet !== u.prefix)
2916
+ throw new TypeError("Invalid prefix or network mismatch");
2917
+ if (u.version !== ur)
2918
+ throw new TypeError("Invalid witness version for p2op");
2919
+ if (u.data.length < fr || u.data.length > cr)
2920
+ throw new TypeError("Invalid witness program length");
2921
+ o = u.data;
2922
+ }
2923
+ if (e.program) {
2924
+ if (o.length && !o.equals(e.program))
2925
+ throw new TypeError("Program mismatch");
2926
+ o = e.program;
2927
+ }
2928
+ if (!o.length && e.deploymentVersion !== void 0 && e.hash160 && (o = r()), e.output) {
2929
+ const u = i.program;
2930
+ if (o.length && !o.equals(u))
2931
+ throw new TypeError("Program mismatch (output vs other source)");
2932
+ o = u;
2933
+ }
2934
+ if (o.length < fr || o.length > cr)
2935
+ throw new TypeError(`Witness program must be 2–40 bytes. Was ${o.length} bytes`);
2936
+ if (e.deploymentVersion !== void 0 && e.deploymentVersion !== o[0])
2937
+ throw new TypeError("deploymentVersion mismatch");
2938
+ if (e.hash160 && !e.hash160.equals(o.slice(1)))
2939
+ throw new TypeError("hash160 mismatch");
2940
+ }
2941
+ return Object.assign(i, e);
2942
+ }
2943
+ function oe(e) {
2944
+ return (t) => {
2945
+ try {
2946
+ return e({ output: t }), !0;
2947
+ } catch {
2948
+ return !1;
2949
+ }
2950
+ };
2951
+ }
2952
+ const js = oe(Ne), Ys = oe(Ut), Zs = oe(de), st = oe(xe), ar = oe(le), rn = oe(he), nn = oe(pe), Hi = (e) => e.length === 4 && e[0] === 81 && e[1] === 2 && e[2] === 78 && e[3] === 115;
2953
+ function Te(e) {
2954
+ let t = h.allocUnsafe(0);
2955
+ function r(o) {
2956
+ t = h.concat([t, h.from(o)]);
2957
+ }
2958
+ function n(o) {
2959
+ const u = t.length, c = Ke.encodingLength(o);
2960
+ t = h.concat([t, h.allocUnsafe(c)]), Ke.encode(o, t, u);
2961
+ }
2962
+ function s(o) {
2963
+ n(o.length), r(o);
2964
+ }
2965
+ function i(o) {
2966
+ n(o.length), o.forEach(s);
2967
+ }
2968
+ return i(e), t;
2969
+ }
2970
+ function _t(e) {
2971
+ if (e.length === 32)
2972
+ return;
2973
+ if (![33, 65].includes(e.length)) {
2974
+ console.warn(`Unsupported key length=${e.length}. Must be 33 (compressed) or 65 (uncompressed).`);
2975
+ return;
2976
+ }
2977
+ let t;
2978
+ try {
2979
+ t = R.fromHex(e);
2980
+ } catch {
2981
+ throw new Error("Invalid secp256k1 public key bytes. Cannot parse.");
2982
+ }
2983
+ const r = hr(t.x), n = hr(t.y), i = t.y % 2n === 0n ? 6 : 7, o = h.alloc(65);
2984
+ o[0] = i, r.copy(o, 1), n.copy(o, 33);
2985
+ const u = h.concat([h.from([4]), r, n]);
2986
+ return {
2987
+ hybrid: o,
2988
+ uncompressed: u
2989
+ };
2990
+ }
2991
+ function hr(e) {
2992
+ let t = e.toString(16);
2993
+ return t = t.padStart(64, "0"), t.length > 64 && (t = t.slice(-64)), h.from(t, "hex");
2994
+ }
2995
+ function Fe(e, t) {
2996
+ if (e.equals(t))
2997
+ return !0;
2998
+ if (e.length === 65 && t.length === 65) {
2999
+ const r = h.from(e), n = h.from(t);
3000
+ return (r[0] === 6 || r[0] === 7) && (r[0] = 4), (n[0] === 6 || n[0] === 7) && (n[0] = 4), r.equals(n);
3001
+ }
3002
+ return !1;
3003
+ }
3004
+ function sn(e, t) {
3005
+ const r = b(t);
3006
+ if (r === null)
3007
+ throw new Error("Unknown script error");
3008
+ const n = x(e), s = be(e), i = _t(e), o = i?.hybrid ? x(i.hybrid) : void 0, u = i?.uncompressed ? x(i.uncompressed) : void 0;
3009
+ return r.findIndex((c) => {
3010
+ if (typeof c == "number")
3011
+ return !1;
3012
+ if (Fe(c, e) || Fe(c, s) || c.equals(n) || i && (Fe(c, i.uncompressed) || Fe(c, i.hybrid) || o && c.equals(o) || u && c.equals(u)))
3013
+ return !0;
3014
+ });
3015
+ }
3016
+ function it(e, t) {
3017
+ return sn(e, t) !== -1;
3018
+ }
3019
+ function Qs(e, t) {
3020
+ return Js(e).some((n) => on(n, fe.decode, t));
3021
+ }
3022
+ function on(e, t, r) {
3023
+ const { hashType: n } = t(e), s = [];
3024
+ switch (n & d.SIGHASH_ANYONECANPAY && s.push("addInput"), n & 31) {
3025
+ case d.SIGHASH_ALL:
3026
+ break;
3027
+ case d.SIGHASH_SINGLE:
3028
+ case d.SIGHASH_NONE:
3029
+ s.push("addOutput"), s.push("setInputSequence");
3030
+ break;
3031
+ }
3032
+ return s.indexOf(r) === -1;
3033
+ }
3034
+ function Js(e) {
3035
+ let t = [];
3036
+ if ((e.partialSig || []).length === 0) {
3037
+ if (!e.finalScriptSig && !e.finalScriptWitness)
3038
+ return [];
3039
+ t = ei(e);
3040
+ } else
3041
+ t = e.partialSig;
3042
+ return t.map((r) => r.signature);
3043
+ }
3044
+ function ei(e) {
3045
+ const t = e.finalScriptSig ? b(e.finalScriptSig) || [] : [], r = e.finalScriptWitness ? b(e.finalScriptWitness) || [] : [];
3046
+ return t.concat(r).filter((n) => h.isBuffer(n) && Y(n)).map((n) => ({ signature: n }));
3047
+ }
3048
+ const ee = E;
3049
+ function de(e, t) {
3050
+ if (!e.address && !e.hash && !e.output && !e.pubkey && !e.input)
3051
+ throw new TypeError("Not enough data");
3052
+ t = Object.assign({ validate: !0 }, t || {}), f({
3053
+ network: f.maybe(f.Object),
3054
+ address: f.maybe(f.String),
3055
+ hash: f.maybe(f.BufferN(20)),
3056
+ output: f.maybe(f.BufferN(25)),
3057
+ pubkey: f.maybe(L),
3058
+ signature: f.maybe(Y),
3059
+ input: f.maybe(f.Buffer)
3060
+ }, e);
3061
+ const r = C(() => {
3062
+ const o = h.from(Oe.decode(e.address)), u = o.readUInt8(0), c = o.slice(1);
3063
+ return { version: u, hash: c };
3064
+ }), n = C(() => b(e.input)), s = e.network || K, i = {
3065
+ name: G.P2PKH,
3066
+ network: s,
3067
+ hash: void 0
3068
+ };
3069
+ if (w(i, "address", () => {
3070
+ if (!i.hash)
3071
+ return;
3072
+ const o = h.allocUnsafe(21);
3073
+ return o.writeUInt8(s.pubKeyHash, 0), i.hash.copy(o, 1), Oe.encode(o);
3074
+ }), w(i, "hash", () => {
3075
+ if (e.output)
3076
+ return e.output.slice(3, 23);
3077
+ if (e.address)
3078
+ return r().hash;
3079
+ if (e.pubkey || i.pubkey)
3080
+ return x(e.pubkey || i.pubkey);
3081
+ }), w(i, "output", () => {
3082
+ if (i.hash)
3083
+ return H([
3084
+ ee.OP_DUP,
3085
+ ee.OP_HASH160,
3086
+ i.hash,
3087
+ ee.OP_EQUALVERIFY,
3088
+ ee.OP_CHECKSIG
3089
+ ]);
3090
+ }), w(i, "pubkey", () => {
3091
+ if (e.input)
3092
+ return n()[1];
3093
+ }), w(i, "signature", () => {
3094
+ if (e.input)
3095
+ return n()[0];
3096
+ }), w(i, "input", () => {
3097
+ if (!e.pubkey || !e.signature)
3098
+ return;
3099
+ let o = e.pubkey;
3100
+ if (e.useHybrid || e.useUncompressed) {
3101
+ const u = _t(e.pubkey);
3102
+ u && (e.useUncompressed ? o = u.uncompressed : o = u.hybrid);
3103
+ }
3104
+ return H([e.signature, o]);
3105
+ }), w(i, "witness", () => {
3106
+ if (i.input)
3107
+ return [];
3108
+ }), t.validate) {
3109
+ let o = h.from([]);
3110
+ if (e.address) {
3111
+ if (r().version !== s.pubKeyHash)
3112
+ throw new TypeError("Invalid version or Network mismatch");
3113
+ if (r().hash.length !== 20)
3114
+ throw new TypeError("Invalid address");
3115
+ o = r().hash;
3116
+ }
3117
+ if (e.hash) {
3118
+ if (o.length > 0 && !o.equals(e.hash))
3119
+ throw new TypeError("Hash mismatch");
3120
+ o = e.hash;
3121
+ }
3122
+ if (e.output) {
3123
+ if (e.output.length !== 25 || e.output[0] !== ee.OP_DUP || e.output[1] !== ee.OP_HASH160 || e.output[2] !== 20 || e.output[23] !== ee.OP_EQUALVERIFY || e.output[24] !== ee.OP_CHECKSIG)
3124
+ throw new TypeError("Output is invalid");
3125
+ const u = e.output.slice(3, 23);
3126
+ if (o.length > 0 && !o.equals(u))
3127
+ throw new TypeError("Hash mismatch");
3128
+ o = u;
3129
+ }
3130
+ if (e.pubkey) {
3131
+ const u = x(e.pubkey);
3132
+ let c = o.length > 0 && !o.equals(u);
3133
+ if (c && (e.pubkey.length === 33 && (e.pubkey[0] === 2 || e.pubkey[0] === 3) || e.pubkey.length === 65 && e.pubkey[0] === 4)) {
3134
+ const a = _t(e.pubkey);
3135
+ if (a) {
3136
+ const p = x(a.uncompressed);
3137
+ if (o.equals(p))
3138
+ c = !1, e.useUncompressed = !0;
3139
+ else {
3140
+ const l = x(a.hybrid);
3141
+ c = !o.equals(l), c || (e.useHybrid = !0);
3142
+ }
3143
+ }
3144
+ }
3145
+ if (c)
3146
+ throw new TypeError("Hash mismatch");
3147
+ o = u;
3148
+ }
3149
+ if (e.input) {
3150
+ const u = n();
3151
+ if (u.length !== 2)
3152
+ throw new TypeError("Input is invalid");
3153
+ if (!Y(u[0]))
3154
+ throw new TypeError("Input has invalid signature");
3155
+ if (!L(u[1]))
3156
+ throw new TypeError("Input has invalid pubkey");
3157
+ if (e.signature && !e.signature.equals(u[0]))
3158
+ throw new TypeError("Signature mismatch");
3159
+ if (e.pubkey && !e.pubkey.equals(u[1]))
3160
+ throw new TypeError("Pubkey mismatch");
3161
+ const c = x(u[1]);
3162
+ if (o.length > 0 && !o.equals(c))
3163
+ throw new TypeError("Hash mismatch (input)");
3164
+ }
3165
+ }
3166
+ return Object.assign(i, e);
3167
+ }
3168
+ var G;
3169
+ (function(e) {
3170
+ e.P2PK = "p2pk", e.P2PKH = "p2pkh", e.P2SH = "p2sh", e.P2MS = "p2ms", e.P2WPKH = "p2wpkh", e.P2WSH = "p2wsh", e.P2TR = "p2tr", e.P2OP = "p2op", e.Embed = "embed", e.ScriptRedeem = "scriptRedeem";
3171
+ })(G || (G = {}));
3172
+ const ti = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
3173
+ __proto__: null,
3174
+ LEAF_VERSION_TAPSCRIPT: Nt,
3175
+ MAX_TAPTREE_DEPTH: Wr,
3176
+ get PaymentType() {
3177
+ return G;
3178
+ },
3179
+ findScriptPath: Ae,
3180
+ p2data: ls,
3181
+ p2ms: Ne,
3182
+ p2op: tn,
3183
+ p2pk: Ut,
3184
+ p2pkh: de,
3185
+ p2sh: he,
3186
+ p2tr: pe,
3187
+ p2wpkh: xe,
3188
+ p2wsh: le,
3189
+ prop: w,
3190
+ rootHashFromPath: Xe,
3191
+ tapTweakHash: Gr,
3192
+ tapleafHash: j,
3193
+ toHashTree: De,
3194
+ tweakKey: ye,
3195
+ value: C
3196
+ }, Symbol.toStringTag, { value: "Module" })), ri = {
3197
+ network: K,
3198
+ maximumFeeRate: 5e3
3199
+ };
3200
+ class bt {
3201
+ constructor(t = {}, r = new Ct.Psbt(new un())) {
3202
+ this.data = r, this.opts = Object.assign({}, ri, t), this.__CACHE = {
3203
+ __NON_WITNESS_UTXO_TX_CACHE: [],
3204
+ __NON_WITNESS_UTXO_BUF_CACHE: [],
3205
+ __TX_IN_CACHE: {},
3206
+ __TX: this.data.globalMap.unsignedTx.tx,
3207
+ __UNSAFE_SIGN_NONSEGWIT: !1
3208
+ }, t.version === 3 ? this.setVersionTRUC() : this.data.inputs.length === 0 && this.setVersion(2);
3209
+ const n = (s, i, o, u) => {
3210
+ Object.defineProperty(s, i, {
3211
+ enumerable: o,
3212
+ writable: u
3213
+ });
3214
+ };
3215
+ n(this, "__CACHE", !1, !0), n(this, "opts", !1, !0);
3216
+ }
3217
+ get inputCount() {
3218
+ return this.data.inputs.length;
3219
+ }
3220
+ get version() {
3221
+ return this.__CACHE.__TX.version;
3222
+ }
3223
+ set version(t) {
3224
+ this.setVersion(t);
3225
+ }
3226
+ get locktime() {
3227
+ return this.__CACHE.__TX.locktime;
3228
+ }
3229
+ set locktime(t) {
3230
+ this.setLocktime(t);
3231
+ }
3232
+ get txInputs() {
3233
+ return this.__CACHE.__TX.ins.map((t) => ({
3234
+ hash: Wt(t.hash),
3235
+ index: t.index,
3236
+ sequence: t.sequence
3237
+ }));
3238
+ }
3239
+ get txOutputs() {
3240
+ return this.__CACHE.__TX.outs.map((t) => {
3241
+ let r;
3242
+ try {
3243
+ r = es(t.script, this.opts.network);
3244
+ } catch {
3245
+ }
3246
+ return {
3247
+ script: Wt(t.script),
3248
+ value: t.value,
3249
+ address: r
3250
+ };
3251
+ });
3252
+ }
3253
+ static fromBase64(t, r = {}) {
3254
+ const n = h.from(t, "base64");
3255
+ return this.fromBuffer(n, r);
3256
+ }
3257
+ static fromHex(t, r = {}) {
3258
+ const n = h.from(t, "hex");
3259
+ return this.fromBuffer(n, r);
3260
+ }
3261
+ static fromBuffer(t, r = {}) {
3262
+ const n = Ct.Psbt.fromBuffer(t, ni), s = new bt(r, n);
3263
+ return fi(s.__CACHE.__TX, s.__CACHE), s;
3264
+ }
3265
+ combine(...t) {
3266
+ return this.data.combine(...t.map((r) => r.data)), this;
3267
+ }
3268
+ clone() {
3269
+ return bt.fromBuffer(this.data.toBuffer(), JSON.parse(JSON.stringify(this.opts)));
3270
+ }
3271
+ setMaximumFeeRate(t) {
3272
+ Ve(t), this.opts.maximumFeeRate = t;
3273
+ }
3274
+ setVersion(t) {
3275
+ Ve(t), ge(this.data.inputs, "setVersion");
3276
+ const r = this.__CACHE;
3277
+ return r.__TX.version = t, r.__EXTRACTED_TX = void 0, this;
3278
+ }
3279
+ setVersionTRUC() {
3280
+ return this.setVersion(d.TRUC_VERSION);
3281
+ }
3282
+ setLocktime(t) {
3283
+ Ve(t), ge(this.data.inputs, "setLocktime");
3284
+ const r = this.__CACHE;
3285
+ return r.__TX.locktime = t, r.__EXTRACTED_TX = void 0, this;
3286
+ }
3287
+ setInputSequence(t, r) {
3288
+ Ve(r), ge(this.data.inputs, "setInputSequence");
3289
+ const n = this.__CACHE;
3290
+ if (n.__TX.ins.length <= t)
3291
+ throw new Error("Input index too high");
3292
+ return n.__TX.ins[t].sequence = r, n.__EXTRACTED_TX = void 0, this;
3293
+ }
3294
+ addInputs(t, r = !0) {
3295
+ return t.forEach((n) => this.addInput(n, r)), this;
3296
+ }
3297
+ addInput(t, r = !0) {
3298
+ if (!t || t.hash === void 0 || t.index === void 0)
3299
+ throw new Error("Invalid arguments for Psbt.addInput. Requires single object with at least [hash] and [index]");
3300
+ sr(t, t, "addInput"), r && ge(this.data.inputs, "addInput"), t.witnessScript && Ye(t.witnessScript);
3301
+ const n = this.__CACHE;
3302
+ this.data.addInput(t);
3303
+ const s = n.__TX.ins[n.__TX.ins.length - 1];
3304
+ an(n, s);
3305
+ const i = this.data.inputs.length - 1, o = this.data.inputs[i];
3306
+ return o.nonWitnessUtxo && Ot(this.__CACHE, o, i), n.__FEE = void 0, n.__FEE_RATE = void 0, n.__EXTRACTED_TX = void 0, this;
3307
+ }
3308
+ addOutputs(t) {
3309
+ return t.forEach((r) => this.addOutput(r)), this;
3310
+ }
3311
+ addOutput(t) {
3312
+ if (arguments.length > 1 || !t || t.value === void 0 || t.address === void 0 && t.script === void 0)
3313
+ throw new Error("Invalid arguments for Psbt.addOutput. Requires single object with at least [script or address] and [value]");
3314
+ ge(this.data.inputs, "addOutput");
3315
+ const { address: r } = t;
3316
+ if (typeof r == "string") {
3317
+ const { network: s } = this.opts, i = ts(r, s);
3318
+ t = Object.assign({}, t, { script: i });
3319
+ }
3320
+ ir(t, t, "addOutput");
3321
+ const n = this.__CACHE;
3322
+ return this.data.addOutput(t), n.__FEE = void 0, n.__FEE_RATE = void 0, n.__EXTRACTED_TX = void 0, this;
3323
+ }
3324
+ extractTransaction(t, r) {
3325
+ if (r && (this.data.inputs = this.data.inputs.filter((i) => !i.partialSig)), !this.data.inputs.every(fn))
3326
+ throw new Error("Not finalized");
3327
+ const n = this.__CACHE;
3328
+ if (t || ii(this, n, this.opts), n.__EXTRACTED_TX)
3329
+ return n.__EXTRACTED_TX;
3330
+ const s = n.__TX.clone();
3331
+ return gn(this.data.inputs, s, n, !0, r), s;
3332
+ }
3333
+ getFeeRate(t = !1) {
3334
+ return gr("__FEE_RATE", "fee rate", this.data.inputs, this.__CACHE, t);
3335
+ }
3336
+ getFee(t = !1) {
3337
+ return gr("__FEE", "fee", this.data.inputs, this.__CACHE, t);
3338
+ }
3339
+ finalizeAllInputs() {
3340
+ return U.checkForInput(this.data.inputs, 0), me(this.data.inputs.length).forEach((t) => this.finalizeInput(t)), this;
3341
+ }
3342
+ finalizeInput(t, r, n) {
3343
+ const s = U.checkForInput(this.data.inputs, t);
3344
+ return q(s) ? this._finalizeTaprootInput(t, s, void 0, r) : this._finalizeInput(t, s, r, n ?? !0);
3345
+ }
3346
+ finalizeTaprootInput(t, r, n = nr) {
3347
+ const s = U.checkForInput(this.data.inputs, t);
3348
+ if (q(s))
3349
+ return this._finalizeTaprootInput(t, s, r, n);
3350
+ throw new Error(`Cannot finalize input #${t}. Not Taproot.`);
3351
+ }
3352
+ getInputType(t) {
3353
+ const r = U.checkForInput(this.data.inputs, t), n = mn(t, r, this.__CACHE), s = ut(n, t, "input", r.redeemScript || yi(r.finalScriptSig), r.witnessScript || Ei(r.finalScriptWitness)), i = s.type === "raw" ? "" : s.type + "-", o = En(s.meaningfulScript);
3354
+ return i + o;
3355
+ }
3356
+ inputHasPubkey(t, r) {
3357
+ const n = U.checkForInput(this.data.inputs, t);
3358
+ return gi(r, n, t, this.__CACHE);
3359
+ }
3360
+ inputHasHDKey(t, r) {
3361
+ const n = U.checkForInput(this.data.inputs, t), s = lr(r);
3362
+ return !!n.bip32Derivation && n.bip32Derivation.some(s);
3363
+ }
3364
+ outputHasPubkey(t, r) {
3365
+ const n = U.checkForOutput(this.data.outputs, t);
3366
+ return mi(r, n, t, this.__CACHE);
3367
+ }
3368
+ outputHasHDKey(t, r) {
3369
+ const n = U.checkForOutput(this.data.outputs, t), s = lr(r);
3370
+ return !!n.bip32Derivation && n.bip32Derivation.some(s);
3371
+ }
3372
+ validateSignaturesOfAllInputs(t) {
3373
+ return U.checkForInput(this.data.inputs, 0), me(this.data.inputs.length).map((n) => this.validateSignaturesOfInput(n, t)).reduce((n, s) => s === !0 && n, !0);
3374
+ }
3375
+ validateSignaturesOfInput(t, r, n) {
3376
+ const s = this.data.inputs[t];
3377
+ return q(s) ? this.validateSignaturesOfTaprootInput(t, r, n) : this._validateSignaturesOfInput(t, r, n);
3378
+ }
3379
+ signAllInputsHD(t, r = [d.SIGHASH_ALL]) {
3380
+ if (!t || !t.publicKey || !t.fingerprint)
3381
+ throw new Error("Need HDSigner to sign input");
3382
+ const n = [];
3383
+ for (const s of me(this.data.inputs.length))
3384
+ try {
3385
+ this.signInputHD(s, t, r), n.push(!0);
3386
+ } catch {
3387
+ n.push(!1);
3388
+ }
3389
+ if (n.every((s) => s === !1))
3390
+ throw new Error("No inputs were signed");
3391
+ return this;
3392
+ }
3393
+ signAllInputsHDAsync(t, r = [d.SIGHASH_ALL]) {
3394
+ return new Promise((n, s) => {
3395
+ if (!t || !t.publicKey || !t.fingerprint)
3396
+ return s(new Error("Need HDSigner to sign input"));
3397
+ const i = [], o = [];
3398
+ for (const u of me(this.data.inputs.length))
3399
+ o.push(this.signInputHDAsync(u, t, r).then(() => {
3400
+ i.push(!0);
3401
+ }, () => {
3402
+ i.push(!1);
3403
+ }));
3404
+ return Promise.all(o).then(() => {
3405
+ if (i.every((u) => u === !1))
3406
+ return s(new Error("No inputs were signed"));
3407
+ n();
3408
+ });
3409
+ });
3410
+ }
3411
+ signInputHD(t, r, n = [d.SIGHASH_ALL]) {
3412
+ if (!r || !r.publicKey || !r.fingerprint)
3413
+ throw new Error("Need HDSigner to sign input");
3414
+ return Er(t, this.data.inputs, r).forEach((i) => this.signInput(t, i, n)), this;
3415
+ }
3416
+ signInputHDAsync(t, r, n = [d.SIGHASH_ALL]) {
3417
+ return new Promise((s, i) => {
3418
+ if (!r || !r.publicKey || !r.fingerprint)
3419
+ return i(new Error("Need HDSigner to sign input"));
3420
+ const u = Er(t, this.data.inputs, r).map((c) => this.signInputAsync(t, c, n));
3421
+ return Promise.all(u).then(() => {
3422
+ s();
3423
+ }).catch(i);
3424
+ });
3425
+ }
3426
+ signAllInputs(t, r) {
3427
+ if (!t || !t.publicKey)
3428
+ throw new Error("Need Signer to sign input");
3429
+ const n = [];
3430
+ for (const s of me(this.data.inputs.length))
3431
+ try {
3432
+ this.signInput(s, t, r), n.push(!0);
3433
+ } catch {
3434
+ n.push(!1);
3435
+ }
3436
+ if (n.every((s) => s === !1))
3437
+ throw new Error("No inputs were signed");
3438
+ return this;
3439
+ }
3440
+ signAllInputsAsync(t, r) {
3441
+ return new Promise((n, s) => {
3442
+ if (!t || !t.publicKey)
3443
+ return s(new Error("Need Signer to sign input"));
3444
+ const i = [], o = [];
3445
+ for (const [u] of this.data.inputs.entries())
3446
+ o.push(this.signInputAsync(u, t, r).then(() => {
3447
+ i.push(!0);
3448
+ }, () => {
3449
+ i.push(!1);
3450
+ }));
3451
+ return Promise.all(o).then(() => {
3452
+ if (i.every((u) => u === !1))
3453
+ return s(new Error("No inputs were signed"));
3454
+ n();
3455
+ });
3456
+ });
3457
+ }
3458
+ signInput(t, r, n) {
3459
+ if (!r || !r.publicKey)
3460
+ throw new Error("Need Signer to sign input");
3461
+ const s = U.checkForInput(this.data.inputs, t);
3462
+ return q(s) ? this._signTaprootInput(t, s, r, void 0, n) : this._signInput(t, r, n);
3463
+ }
3464
+ signTaprootInput(t, r, n, s) {
3465
+ if (!r || !r.publicKey)
3466
+ throw new Error("Need Signer to sign input");
3467
+ const i = U.checkForInput(this.data.inputs, t);
3468
+ if (q(i))
3469
+ return this._signTaprootInput(t, i, r, n, s);
3470
+ throw new Error(`Input #${t} is not of type Taproot.`);
3471
+ }
3472
+ signInputAsync(t, r, n) {
3473
+ return Promise.resolve().then(() => {
3474
+ if (!r || !r.publicKey)
3475
+ throw new Error("Need Signer to sign input");
3476
+ const s = U.checkForInput(this.data.inputs, t);
3477
+ return q(s) ? this._signTaprootInputAsync(t, s, r, void 0, n) : this._signInputAsync(t, r, n);
3478
+ });
3479
+ }
3480
+ signTaprootInputAsync(t, r, n, s) {
3481
+ return Promise.resolve().then(() => {
3482
+ if (!r || !r.publicKey)
3483
+ throw new Error("Need Signer to sign input");
3484
+ const i = U.checkForInput(this.data.inputs, t);
3485
+ if (q(i))
3486
+ return this._signTaprootInputAsync(t, i, r, n, s);
3487
+ throw new Error(`Input #${t} is not of type Taproot.`);
3488
+ });
3489
+ }
3490
+ toBuffer() {
3491
+ return pt(this.__CACHE), this.data.toBuffer();
3492
+ }
3493
+ toHex() {
3494
+ return pt(this.__CACHE), this.data.toHex();
3495
+ }
3496
+ toBase64() {
3497
+ return pt(this.__CACHE), this.data.toBase64();
3498
+ }
3499
+ updateGlobal(t) {
3500
+ return this.data.updateGlobal(t), this;
3501
+ }
3502
+ updateInput(t, r) {
3503
+ return r.witnessScript && Ye(r.witnessScript), sr(this.data.inputs[t], r, "updateInput"), this.data.updateInput(t, r), r.nonWitnessUtxo && Ot(this.__CACHE, this.data.inputs[t], t), this;
3504
+ }
3505
+ updateOutput(t, r) {
3506
+ const n = this.data.outputs[t];
3507
+ return ir(n, r, "updateOutput"), this.data.updateOutput(t, r), this;
3508
+ }
3509
+ addUnknownKeyValToGlobal(t) {
3510
+ return this.data.addUnknownKeyValToGlobal(t), this;
3511
+ }
3512
+ addUnknownKeyValToInput(t, r) {
3513
+ return this.data.addUnknownKeyValToInput(t, r), this;
3514
+ }
3515
+ addUnknownKeyValToOutput(t, r) {
3516
+ return this.data.addUnknownKeyValToOutput(t, r), this;
3517
+ }
3518
+ clearFinalizedInput(t) {
3519
+ return this.data.clearFinalizedInput(t), this;
3520
+ }
3521
+ checkTaprootHashesForSig(t, r, n, s, i) {
3522
+ if (typeof n.signSchnorr != "function")
3523
+ throw new Error(`Need Schnorr Signer to sign taproot input #${t}.`);
3524
+ const o = Tt(t, r, this.data.inputs, n.publicKey, this.__CACHE, s, i);
3525
+ if (!o || !o.length)
3526
+ throw new Error(`Can not sign for input #${t} with the key ${n.publicKey.toString("hex")}`);
3527
+ return o;
3528
+ }
3529
+ _finalizeInput(t, r, n = ci, s = !0) {
3530
+ const { script: i, isP2SH: o, isP2WSH: u, isSegwit: c } = li(t, r, this.__CACHE);
3531
+ if (!i)
3532
+ throw new Error(`No script found for input #${t}`);
3533
+ oi(r);
3534
+ const { finalScriptSig: a, finalScriptWitness: p } = n(t, r, i, c, o, u, s);
3535
+ if (a && this.data.updateInput(t, { finalScriptSig: a }), p && this.data.updateInput(t, { finalScriptWitness: p }), !a && !p)
3536
+ throw new Error(`Unknown error finalizing input #${t}`);
3537
+ return this.data.clearFinalizedInput(t), this;
3538
+ }
3539
+ _finalizeTaprootInput(t, r, n, s = nr) {
3540
+ if (!r.witnessUtxo)
3541
+ throw new Error(`Cannot finalize input #${t}. Missing witness utxo.`);
3542
+ if (r.tapKeySig) {
3543
+ const i = pe({
3544
+ output: r.witnessUtxo.script,
3545
+ signature: r.tapKeySig
3546
+ }), o = Te(i.witness);
3547
+ this.data.updateInput(t, { finalScriptWitness: o });
3548
+ } else {
3549
+ const { finalScriptWitness: i } = s(t, r, n);
3550
+ this.data.updateInput(t, { finalScriptWitness: i });
3551
+ }
3552
+ return this.data.clearFinalizedInput(t), this;
3553
+ }
3554
+ _validateSignaturesOfInput(t, r, n) {
3555
+ const s = this.data.inputs[t], i = (s || {}).partialSig;
3556
+ if (!s || !i || i.length < 1)
3557
+ throw new Error("No signatures to validate");
3558
+ if (typeof r != "function")
3559
+ throw new Error("Need validator function to validate signatures");
3560
+ const o = n ? i.filter((l) => l.pubkey.equals(n)) : i;
3561
+ if (o.length < 1)
3562
+ throw new Error("No signatures for this pubkey");
3563
+ const u = [];
3564
+ let c, a, p;
3565
+ for (const l of o) {
3566
+ const m = fe.decode(l.signature), { hash: T, script: y } = p !== m.hashType ? pn(t, Object.assign({}, s, {
3567
+ sighashType: m.hashType
3568
+ }), this.__CACHE, !0) : { hash: c, script: a };
3569
+ p = m.hashType, c = T, a = y, cn(l.pubkey, y, "verify"), u.push(r(l.pubkey, T, m.signature));
3570
+ }
3571
+ return u.every((l) => l === !0);
3572
+ }
3573
+ validateSignaturesOfTaprootInput(t, r, n) {
3574
+ const s = this.data.inputs[t], i = (s || {}).tapKeySig, o = (s || {}).tapScriptSig;
3575
+ if (!s && !i && !(o && !o.length))
3576
+ throw new Error("No signatures to validate");
3577
+ if (typeof r != "function")
3578
+ throw new Error("Need validator function to validate signatures");
3579
+ n = n && be(n);
3580
+ const u = n ? Tt(t, s, this.data.inputs, n, this.__CACHE) : hi(t, s, this.data.inputs, this.__CACHE);
3581
+ if (!u.length)
3582
+ throw new Error("No signatures for this pubkey");
3583
+ const c = u.find((p) => !p.leafHash);
3584
+ let a = 0;
3585
+ if (i && c) {
3586
+ if (!r(c.pubkey, c.hash, yr(i)))
3587
+ return !1;
3588
+ a++;
3589
+ }
3590
+ if (o)
3591
+ for (const p of o) {
3592
+ const l = u.find((m) => p.pubkey.equals(m.pubkey));
3593
+ if (l) {
3594
+ if (!r(p.pubkey, l.hash, yr(p.signature)))
3595
+ return !1;
3596
+ a++;
3597
+ }
3598
+ }
3599
+ return a > 0;
3600
+ }
3601
+ _signInput(t, r, n = [d.SIGHASH_ALL]) {
3602
+ const { hash: s, sighashType: i } = mr(this.data.inputs, t, r.publicKey, this.__CACHE, n), o = [
3603
+ {
3604
+ pubkey: r.publicKey,
3605
+ signature: fe.encode(r.sign(s), i)
3606
+ }
3607
+ ];
3608
+ return this.data.updateInput(t, { partialSig: o }), this;
3609
+ }
3610
+ _signTaprootInput(t, r, n, s, i = [d.SIGHASH_DEFAULT]) {
3611
+ const o = this.checkTaprootHashesForSig(t, r, n, s, i), u = o.filter((a) => !a.leafHash).map((a) => Be(n.signSchnorr(a.hash), r.sighashType))[0], c = o.filter((a) => !!a.leafHash).map((a) => ({
3612
+ pubkey: be(n.publicKey),
3613
+ signature: Be(n.signSchnorr(a.hash), r.sighashType),
3614
+ leafHash: a.leafHash
3615
+ }));
3616
+ return u && this.data.updateInput(t, { tapKeySig: u }), c.length && this.data.updateInput(t, { tapScriptSig: c }), this;
3617
+ }
3618
+ _signInputAsync(t, r, n = [d.SIGHASH_ALL]) {
3619
+ const { hash: s, sighashType: i } = mr(this.data.inputs, t, r.publicKey, this.__CACHE, n);
3620
+ return Promise.resolve(r.sign(s)).then((o) => {
3621
+ const u = [
3622
+ {
3623
+ pubkey: r.publicKey,
3624
+ signature: fe.encode(o, i)
3625
+ }
3626
+ ];
3627
+ this.data.updateInput(t, { partialSig: u });
3628
+ });
3629
+ }
3630
+ async _signTaprootInputAsync(t, r, n, s, i = [d.SIGHASH_DEFAULT]) {
3631
+ const o = this.checkTaprootHashesForSig(t, r, n, s, i), u = [], c = o.filter((l) => !l.leafHash)[0];
3632
+ if (c) {
3633
+ const l = Promise.resolve(n.signSchnorr(c.hash)).then((m) => ({
3634
+ tapKeySig: Be(m, r.sighashType)
3635
+ }));
3636
+ u.push(l);
3637
+ }
3638
+ const a = o.filter((l) => !!l.leafHash);
3639
+ if (a.length) {
3640
+ const l = a.map(async (m) => {
3641
+ const T = await n.signSchnorr(m.hash);
3642
+ return { tapScriptSig: [
3643
+ {
3644
+ pubkey: be(n.publicKey),
3645
+ signature: Be(T, r.sighashType),
3646
+ leafHash: m.leafHash
3647
+ }
3648
+ ] };
3649
+ });
3650
+ u.push(...l);
3651
+ }
3652
+ const p = await Promise.all(u);
3653
+ for (const l of p)
3654
+ this.data.updateInput(t, l);
3655
+ }
3656
+ }
3657
+ const ni = (e) => new un(e);
3658
+ class un {
3659
+ constructor(t = h.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
3660
+ this.tx = d.fromBuffer(t), ui(this.tx), Object.defineProperty(this, "tx", {
3661
+ enumerable: !1,
3662
+ writable: !0
3663
+ });
3664
+ }
3665
+ getInputOutputCounts() {
3666
+ return {
3667
+ inputCount: this.tx.ins.length,
3668
+ outputCount: this.tx.outs.length
3669
+ };
3670
+ }
3671
+ addInput(t) {
3672
+ if (t.hash === void 0 || t.index === void 0 || !h.isBuffer(t.hash) && typeof t.hash != "string" || typeof t.index != "number")
3673
+ throw new Error("Error adding input.");
3674
+ const r = typeof t.hash == "string" ? kt(h.from(t.hash, "hex")) : t.hash;
3675
+ this.tx.addInput(r, t.index, t.sequence);
3676
+ }
3677
+ addOutput(t) {
3678
+ if (t.script === void 0 || t.value === void 0 || !h.isBuffer(t.script) || typeof t.value != "number")
3679
+ throw new Error("Error adding output.");
3680
+ this.tx.addOutput(t.script, t.value);
3681
+ }
3682
+ toBuffer() {
3683
+ return this.tx.toBuffer();
3684
+ }
3685
+ }
3686
+ function si(e, t, r) {
3687
+ switch (r) {
3688
+ case "pubkey":
3689
+ case "pubkeyhash":
3690
+ case "witnesspubkeyhash":
3691
+ return pr(1, e.partialSig);
3692
+ case "multisig":
3693
+ const n = Ne({
3694
+ output: t
3695
+ });
3696
+ return pr(n.m, e.partialSig, n.pubkeys);
3697
+ case "nonstandard":
3698
+ return !0;
3699
+ default:
3700
+ return !1;
3701
+ }
3702
+ }
3703
+ function pt(e) {
3704
+ if (e.__UNSAFE_SIGN_NONSEGWIT !== !1)
3705
+ throw new Error("Not BIP174 compliant, can not export");
3706
+ }
3707
+ function pr(e, t, r) {
3708
+ if (!t)
3709
+ return !1;
3710
+ let n;
3711
+ if (r ? n = r.map((s) => {
3712
+ const i = Si(s);
3713
+ return t.find((o) => o.pubkey.equals(i));
3714
+ }).filter((s) => !!s) : n = t, n.length > e)
3715
+ throw new Error("Too many signatures");
3716
+ return n.length === e;
3717
+ }
3718
+ function fn(e) {
3719
+ return !!e.finalScriptSig || !!e.finalScriptWitness;
3720
+ }
3721
+ function lr(e) {
3722
+ return (t) => !(!t.masterFingerprint.equals(e.fingerprint) || !e.derivePath(t.path).publicKey.equals(t.pubkey));
3723
+ }
3724
+ function Ve(e) {
3725
+ if (typeof e != "number" || e !== Math.floor(e) || e > 4294967295 || e < 0)
3726
+ throw new Error("Invalid 32 bit integer");
3727
+ }
3728
+ function ii(e, t, r) {
3729
+ const n = t.__FEE_RATE || e.getFeeRate(), s = t.__EXTRACTED_TX.virtualSize(), i = n * s;
3730
+ if (n >= r.maximumFeeRate)
3731
+ throw new Error(`Warning: You are paying around ${(i / 1e8).toFixed(8)} in fees, which is ${n} satoshi per byte for a transaction with a VSize of ${s} bytes (segwit counted as 0.25 byte per byte). Use setMaximumFeeRate method to raise your threshold, or pass true to the first arg of extractTransaction.`);
3732
+ }
3733
+ function ge(e, t) {
3734
+ e.forEach((r) => {
3735
+ if (q(r) ? Fs(r, t) : Qs(r, t))
3736
+ throw new Error("Can not modify transaction, signatures exist.");
3737
+ });
3738
+ }
3739
+ function oi(e) {
3740
+ if (!e.sighashType || !e.partialSig)
3741
+ return;
3742
+ const { partialSig: t, sighashType: r } = e;
3743
+ t.forEach((n) => {
3744
+ const { hashType: s } = fe.decode(n.signature);
3745
+ if (r !== s)
3746
+ throw new Error("Signature sighash does not match input sighash type");
3747
+ });
3748
+ }
3749
+ function cn(e, t, r) {
3750
+ if (!it(e, t))
3751
+ throw new Error(`Can not ${r} for this input with the key ${e.toString("hex")}`);
3752
+ }
3753
+ function ui(e) {
3754
+ if (!e.ins.every((r) => r.script && r.script.length === 0 && r.witness && r.witness.length === 0))
3755
+ throw new Error("Format Error: Transaction ScriptSigs are not empty");
3756
+ }
3757
+ function fi(e, t) {
3758
+ e.ins.forEach((r) => {
3759
+ an(t, r);
3760
+ });
3761
+ }
3762
+ function an(e, t) {
3763
+ const r = kt(h.from(t.hash)).toString("hex") + ":" + t.index;
3764
+ if (e.__TX_IN_CACHE[r])
3765
+ throw new Error("Duplicate input detected.");
3766
+ e.__TX_IN_CACHE[r] = 1;
3767
+ }
3768
+ function hn(e, t) {
3769
+ return (r, n, s, i) => {
3770
+ const o = e({
3771
+ redeem: { output: s }
3772
+ }).output;
3773
+ if (!n.equals(o))
3774
+ throw new Error(`${t} for ${i} #${r} doesn't match the scriptPubKey in the prevout`);
3775
+ };
3776
+ }
3777
+ const dr = hn(he, "Redeem script"), wr = hn(le, "Witness script");
3778
+ function gr(e, t, r, n, s = !1) {
3779
+ if (!r.every(fn))
3780
+ throw new Error(`PSBT must be finalized to calculate ${t}`);
3781
+ if (e === "__FEE_RATE" && n.__FEE_RATE)
3782
+ return n.__FEE_RATE;
3783
+ if (e === "__FEE" && n.__FEE)
3784
+ return n.__FEE;
3785
+ let i, o = !0;
3786
+ if (n.__EXTRACTED_TX ? (i = n.__EXTRACTED_TX, o = !1) : i = n.__TX.clone(), gn(r, i, n, o, s), e === "__FEE_RATE")
3787
+ return n.__FEE_RATE;
3788
+ if (e === "__FEE")
3789
+ return n.__FEE;
3790
+ }
3791
+ function ci(e, t, r, n, s, i, o = !0, u) {
3792
+ const c = En(r);
3793
+ if (!si(t, r, c) && o)
3794
+ throw new Error(`Can not finalize input #${e}`);
3795
+ return ai(r, c, t.partialSig, n, s, i, u);
3796
+ }
3797
+ function ai(e, t, r, n, s, i, o) {
3798
+ let u, c;
3799
+ const a = pi(e, t, r), p = i ? le({ redeem: a }) : null, l = s ? he({ redeem: p || a }) : null;
3800
+ return n ? (p ? c = Te(p.witness) : a ? c = Te(a.witness) : c = Te(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]), {
3801
+ finalScriptSig: u,
3802
+ finalScriptWitness: c
3803
+ };
3804
+ }
3805
+ function mr(e, t, r, n, s) {
3806
+ const i = U.checkForInput(e, t), { hash: o, sighashType: u, script: c } = pn(t, i, n, !1, s);
3807
+ return cn(r, c, "sign"), {
3808
+ hash: o,
3809
+ sighashType: u
3810
+ };
3811
+ }
3812
+ function pn(e, t, r, n, s) {
3813
+ const i = r.__TX, o = t.sighashType || d.SIGHASH_ALL;
3814
+ dn(o, s);
3815
+ let u, c;
3816
+ if (t.nonWitnessUtxo) {
3817
+ const l = ot(r, t, e), m = i.ins[e].hash, T = l.getHash();
3818
+ if (!m.equals(T))
3819
+ throw new Error(`Non-witness UTXO hash for input #${e} doesn't match the hash specified in the prevout`);
3820
+ const y = i.ins[e].index;
3821
+ c = l.outs[y];
3822
+ } else if (t.witnessUtxo)
3823
+ c = t.witnessUtxo;
3824
+ else
3825
+ throw new Error("Need a Utxo input item for signing");
3826
+ const { meaningfulScript: a, type: p } = ut(c.script, e, "input", t.redeemScript, t.witnessScript);
3827
+ if (["p2sh-p2wsh", "p2wsh"].indexOf(p) >= 0)
3828
+ u = i.hashForWitnessV0(e, a, c.value, o);
3829
+ else if (st(a)) {
3830
+ const l = de({
3831
+ hash: a.slice(2)
3832
+ }).output;
3833
+ u = i.hashForWitnessV0(e, l, c.value, o);
3834
+ } else {
3835
+ if (t.nonWitnessUtxo === void 0 && r.__UNSAFE_SIGN_NONSEGWIT === !1)
3836
+ throw new Error(`Input #${e} has witnessUtxo but non-segwit script: ${a.toString("hex")}`);
3837
+ !n && r.__UNSAFE_SIGN_NONSEGWIT !== !1 && console.warn(`Warning: Signing non-segwit inputs without the full parent transaction means there is a chance that a miner could feed you incorrect information to trick you into paying large fees. This behavior is the same as Psbt's predecessor (TransactionBuilder - now removed) when signing non-segwit scripts. You are not able to export this Psbt with toBuffer|toBase64|toHex since it is not BIP174 compliant.
3838
+ *********************
3839
+ PROCEED WITH CAUTION!
3840
+ *********************`), u = i.hashForSignature(e, a, o);
3841
+ }
3842
+ return {
3843
+ script: a,
3844
+ sighashType: o,
3845
+ hash: u
3846
+ };
3847
+ }
3848
+ function hi(e, t, r, n) {
3849
+ const s = [];
3850
+ if (t.tapInternalKey) {
3851
+ const o = ln(e, t, n);
3852
+ o && s.push(o);
3853
+ }
3854
+ if (t.tapScriptSig) {
3855
+ const o = t.tapScriptSig.map((u) => u.pubkey);
3856
+ s.push(...o);
3857
+ }
3858
+ return s.map((o) => Tt(e, t, r, o, n)).flat();
3859
+ }
3860
+ function ln(e, t, r) {
3861
+ const { script: n } = xt(e, t, r);
3862
+ return nn(n) ? h.from(n.subarray(2, 34)) : null;
3863
+ }
3864
+ function yr(e) {
3865
+ return e.length === 64 ? e : h.from(e.subarray(0, 64));
3866
+ }
3867
+ function Tt(e, t, r, n, s, i, o) {
3868
+ const u = s.__TX, c = t.sighashType || d.SIGHASH_DEFAULT;
3869
+ dn(c, o);
3870
+ const a = r.map((y, O) => xt(O, y, s)), p = a.map((y) => y.script), l = a.map((y) => y.value), m = [];
3871
+ if (t.tapInternalKey && !i) {
3872
+ const y = ln(e, t, s) || h.from([]);
3873
+ if (be(n).equals(y)) {
3874
+ const O = u.hashForWitnessV1(e, p, l, c);
3875
+ m.push({ pubkey: n, hash: O });
3876
+ }
3877
+ }
3878
+ const T = (t.tapLeafScript || []).filter((y) => it(n, y.script)).map((y) => {
3879
+ const O = j({
3880
+ output: y.script,
3881
+ version: y.leafVersion
3882
+ });
3883
+ return Object.assign({ hash: O }, y);
3884
+ }).filter((y) => !i || i.equals(y.hash)).map((y) => {
3885
+ const O = u.hashForWitnessV1(e, p, l, c, y.hash);
3886
+ return {
3887
+ pubkey: n,
3888
+ hash: O,
3889
+ leafHash: y.hash
3890
+ };
3891
+ });
3892
+ return m.concat(T);
3893
+ }
3894
+ function dn(e, t) {
3895
+ if (t && t.indexOf(e) < 0) {
3896
+ const r = wi(e);
3897
+ throw new Error(`Sighash type is not allowed. Retry the sign method passing the sighashTypes array of whitelisted types. Sighash type: ${r}`);
3898
+ }
3899
+ }
3900
+ function pi(e, t, r) {
3901
+ let n;
3902
+ switch (t) {
3903
+ case "multisig": {
3904
+ const s = di(e, r);
3905
+ n = Ne({
3906
+ output: e,
3907
+ signatures: s
3908
+ });
3909
+ break;
3910
+ }
3911
+ case "pubkey":
3912
+ n = Ut({
3913
+ output: e,
3914
+ signature: r[0].signature
3915
+ });
3916
+ break;
3917
+ case "pubkeyhash":
3918
+ n = de({
3919
+ output: e,
3920
+ pubkey: r[0].pubkey,
3921
+ signature: r[0].signature
3922
+ });
3923
+ break;
3924
+ case "witnesspubkeyhash":
3925
+ n = xe({
3926
+ output: e,
3927
+ pubkey: r[0].pubkey,
3928
+ signature: r[0].signature
3929
+ });
3930
+ break;
3931
+ }
3932
+ return n;
3933
+ }
3934
+ function li(e, t, r) {
3935
+ const n = r.__TX, s = {
3936
+ script: null,
3937
+ isSegwit: !1,
3938
+ isP2SH: !1,
3939
+ isP2WSH: !1
3940
+ };
3941
+ if (s.isP2SH = !!t.redeemScript, s.isP2WSH = !!t.witnessScript, t.witnessScript)
3942
+ s.script = t.witnessScript;
3943
+ else if (t.redeemScript)
3944
+ s.script = t.redeemScript;
3945
+ else if (t.nonWitnessUtxo) {
3946
+ const i = ot(r, t, e), o = n.ins[e].index;
3947
+ s.script = i.outs[o].script;
3948
+ } else t.witnessUtxo && (s.script = t.witnessUtxo.script);
3949
+ if (t.witnessScript || st(s.script))
3950
+ s.isSegwit = !0;
3951
+ else
3952
+ try {
3953
+ const i = s.script;
3954
+ if (!i)
3955
+ throw new TypeError("Invalid script for segwit address");
3956
+ s.isSegwit = jn(i);
3957
+ } catch {
3958
+ }
3959
+ return s;
3960
+ }
3961
+ function Er(e, t, r) {
3962
+ const n = U.checkForInput(t, e);
3963
+ if (!n.bip32Derivation || n.bip32Derivation.length === 0)
3964
+ throw new Error("Need bip32Derivation to sign with HD");
3965
+ const s = n.bip32Derivation.map((i) => {
3966
+ if (i.masterFingerprint.equals(r.fingerprint))
3967
+ return i;
3968
+ }).filter((i) => !!i);
3969
+ if (s.length === 0)
3970
+ throw new Error("Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint");
3971
+ return s.map((i) => {
3972
+ const o = r.derivePath(i.path);
3973
+ if (!i.pubkey.equals(o.publicKey))
3974
+ throw new Error("pubkey did not match bip32Derivation");
3975
+ return o;
3976
+ });
3977
+ }
3978
+ function di(e, t) {
3979
+ return Ne({ output: e }).pubkeys.map((n) => (t.filter((s) => s.pubkey.equals(n))[0] || {}).signature).filter((n) => !!n);
3980
+ }
3981
+ function wn(e) {
3982
+ let t = 0;
3983
+ function r(o) {
3984
+ return t += o, e.slice(t - o, t);
3985
+ }
3986
+ function n() {
3987
+ const o = Ke.decode(e, t);
3988
+ return t += Ke.decode.bytes, o;
3989
+ }
3990
+ function s() {
3991
+ return r(n());
3992
+ }
3993
+ function i() {
3994
+ const o = n(), u = [];
3995
+ for (let c = 0; c < o; c++)
3996
+ u.push(s());
3997
+ return u;
3998
+ }
3999
+ return i();
4000
+ }
4001
+ function wi(e) {
4002
+ let t = e & d.SIGHASH_ANYONECANPAY ? "SIGHASH_ANYONECANPAY | " : "";
4003
+ switch (e & 31) {
4004
+ case d.SIGHASH_ALL:
4005
+ t += "SIGHASH_ALL";
4006
+ break;
4007
+ case d.SIGHASH_SINGLE:
4008
+ t += "SIGHASH_SINGLE";
4009
+ break;
4010
+ case d.SIGHASH_NONE:
4011
+ t += "SIGHASH_NONE";
4012
+ break;
4013
+ }
4014
+ return t;
4015
+ }
4016
+ function Ot(e, t, r) {
4017
+ e.__NON_WITNESS_UTXO_BUF_CACHE[r] = t.nonWitnessUtxo, e.__NON_WITNESS_UTXO_TX_CACHE[r] = d.fromBuffer(t.nonWitnessUtxo);
4018
+ const n = e, s = r;
4019
+ delete t.nonWitnessUtxo, Object.defineProperty(t, "nonWitnessUtxo", {
4020
+ enumerable: !0,
4021
+ get() {
4022
+ const i = n.__NON_WITNESS_UTXO_BUF_CACHE[s], o = n.__NON_WITNESS_UTXO_TX_CACHE[s];
4023
+ if (i !== void 0)
4024
+ return i;
4025
+ {
4026
+ const u = o.toBuffer();
4027
+ return n.__NON_WITNESS_UTXO_BUF_CACHE[s] = u, u;
4028
+ }
4029
+ },
4030
+ set(i) {
4031
+ n.__NON_WITNESS_UTXO_BUF_CACHE[s] = i;
4032
+ }
4033
+ });
4034
+ }
4035
+ function gn(e, t, r, n, s) {
4036
+ let i = 0;
4037
+ e.forEach((a, p) => {
4038
+ if (n && a.finalScriptSig && (t.ins[p].script = a.finalScriptSig), n && a.finalScriptWitness && (t.ins[p].witness = wn(a.finalScriptWitness)), a.witnessUtxo)
4039
+ i += a.witnessUtxo.value;
4040
+ else if (a.nonWitnessUtxo) {
4041
+ const l = ot(r, a, p), m = t.ins[p].index, T = l.outs[m];
4042
+ i += T.value;
4043
+ }
4044
+ });
4045
+ const o = t.outs.reduce((a, p) => a + p.value, 0), u = i - o;
4046
+ if (!s && u < 0)
4047
+ throw new Error(`Outputs are spending more than Inputs ${i} < ${o}`);
4048
+ const c = t.virtualSize();
4049
+ r.__FEE = u, r.__EXTRACTED_TX = t, r.__FEE_RATE = Math.floor(u / c);
4050
+ }
4051
+ function ot(e, t, r) {
4052
+ const n = e.__NON_WITNESS_UTXO_TX_CACHE;
4053
+ return n[r] || Ot(e, t, r), n[r];
4054
+ }
4055
+ function mn(e, t, r) {
4056
+ const { script: n } = xt(e, t, r);
4057
+ return n;
4058
+ }
4059
+ function xt(e, t, r) {
4060
+ if (t.witnessUtxo !== void 0)
4061
+ return {
4062
+ script: t.witnessUtxo.script,
4063
+ value: t.witnessUtxo.value
4064
+ };
4065
+ if (t.nonWitnessUtxo !== void 0) {
4066
+ const s = ot(r, t, e).outs[r.__TX.ins[e].index];
4067
+ return { script: s.script, value: s.value };
4068
+ } else
4069
+ throw new Error("Can't find pubkey in input without Utxo data");
4070
+ }
4071
+ function gi(e, t, r, n) {
4072
+ const s = mn(r, t, n), { meaningfulScript: i } = ut(s, r, "input", t.redeemScript, t.witnessScript);
4073
+ return it(e, i);
4074
+ }
4075
+ function mi(e, t, r, n) {
4076
+ const s = n.__TX.outs[r].script, { meaningfulScript: i } = ut(s, r, "output", t.redeemScript, t.witnessScript);
4077
+ return it(e, i);
4078
+ }
4079
+ function yi(e) {
4080
+ if (!e)
4081
+ return;
4082
+ const t = b(e);
4083
+ if (!t)
4084
+ return;
4085
+ const r = t[t.length - 1];
4086
+ if (!(!h.isBuffer(r) || yn(r) || _i(r) || !b(r)))
4087
+ return r;
4088
+ }
4089
+ function Ei(e) {
4090
+ if (!e)
4091
+ return;
4092
+ const t = wn(e), r = t[t.length - 1];
4093
+ if (!(yn(r) || !b(r)))
4094
+ return r;
4095
+ }
4096
+ function Si(e) {
4097
+ if (e.length === 65) {
4098
+ const t = e[64] & 1, r = e.slice(0, 33);
4099
+ return r[0] = 2 | t, r;
4100
+ }
4101
+ return e.slice();
4102
+ }
4103
+ function yn(e) {
4104
+ return e.length === 33 && Rr(e);
4105
+ }
4106
+ function _i(e) {
4107
+ return Y(e);
4108
+ }
4109
+ function ut(e, t, r, n, s) {
4110
+ const i = rn(e), o = i && n && ar(n), u = ar(e);
4111
+ if (i && n === void 0)
4112
+ throw new Error("scriptPubkey is P2SH but redeemScript missing");
4113
+ if ((u || o) && s === void 0)
4114
+ throw new Error("scriptPubkey or redeemScript is P2WSH but witnessScript missing");
4115
+ let c;
4116
+ return o ? (c = s, dr(t, e, n, r), wr(t, n, s, r), Ye(c)) : u ? (c = s, wr(t, e, s, r), Ye(c)) : i ? (c = n, dr(t, e, n, r)) : c = e, {
4117
+ meaningfulScript: c,
4118
+ type: o ? "p2sh-p2wsh" : i ? "p2sh" : u ? "p2wsh" : "raw"
4119
+ };
4120
+ }
4121
+ function Ye(e) {
4122
+ if (st(e) || rn(e))
4123
+ throw new Error("P2WPKH or P2SH can not be contained within P2WSH");
4124
+ }
4125
+ function En(e) {
4126
+ return st(e) ? "witnesspubkeyhash" : Zs(e) ? "pubkeyhash" : js(e) ? "multisig" : Ys(e) ? "pubkey" : "nonstandard";
4127
+ }
4128
+ function me(e) {
4129
+ return [...Array(e).keys()];
4130
+ }
4131
+ const vi = {
4132
+ payments: ti,
4133
+ script: Xn,
4134
+ crypto: is
4135
+ };
4136
+ export {
4137
+ Pi as A,
4138
+ nn as B,
4139
+ sn as C,
4140
+ ci as D,
4141
+ Zs as E,
4142
+ st as F,
4143
+ jn as G,
4144
+ ar as H,
4145
+ rn as I,
4146
+ Hi as J,
4147
+ Ys as K,
4148
+ js as L,
4149
+ bt as M,
4150
+ ns as N,
4151
+ G as P,
4152
+ d as T,
4153
+ le as a,
4154
+ K as b,
4155
+ xe as c,
4156
+ pe as d,
4157
+ vi as e,
4158
+ H as f,
4159
+ es as g,
4160
+ ts as h,
4161
+ Ai as i,
4162
+ he as j,
4163
+ de as k,
4164
+ Ut as l,
4165
+ b as m,
4166
+ vt as n,
4167
+ E as o,
4168
+ Ne as p,
4169
+ Qn as q,
4170
+ $n as r,
4171
+ _t as s,
4172
+ be as t,
4173
+ te as u,
4174
+ x as v,
4175
+ rs as w,
4176
+ Gr as x,
4177
+ M as y,
4178
+ Ii as z
4179
+ };