@btc-vision/bitcoin 6.5.3 → 6.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/browser/address.d.ts +2 -6
  2. package/browser/bech32utils.d.ts +6 -0
  3. package/browser/chunks/{crypto-0PweVewC.js → crypto-BhCpKpek.js} +4 -4
  4. package/browser/chunks/{payments-CgasufRS.js → payments-yjA0Evsv.js} +639 -597
  5. package/browser/chunks/{psbt-BIwOrKer.js → psbt-URK2hBFc.js} +1259 -1316
  6. package/browser/chunks/{script-CROJPzz_.js → script-DyPItFEl.js} +33 -33
  7. package/browser/chunks/{transaction-DchBu35N.js → transaction-C_UbhMGn.js} +3 -3
  8. package/browser/chunks/{utils-CO5kmxe9.js → utils-DNZi-T5W.js} +26 -26
  9. package/browser/index.d.ts +3 -1
  10. package/browser/index.js +102 -90
  11. package/browser/payments/embed.d.ts +1 -1
  12. package/browser/payments/index.d.ts +12 -104
  13. package/browser/payments/p2ms.d.ts +1 -1
  14. package/browser/payments/p2op.d.ts +1 -1
  15. package/browser/payments/p2pk.d.ts +1 -1
  16. package/browser/payments/p2pkh.d.ts +1 -1
  17. package/browser/payments/p2sh.d.ts +1 -1
  18. package/browser/payments/p2tr.d.ts +1 -1
  19. package/browser/payments/p2wpkh.d.ts +1 -1
  20. package/browser/payments/p2wsh.d.ts +1 -1
  21. package/browser/payments/types.d.ts +93 -0
  22. package/browser/psbt/bip371.d.ts +0 -1
  23. package/browser/psbt/psbtutils.d.ts +0 -7
  24. package/browser/pubkey.d.ts +8 -0
  25. package/browser/script.d.ts +1 -2
  26. package/browser/script_signature.d.ts +1 -0
  27. package/browser/types.d.ts +3 -0
  28. package/build/address.d.ts +2 -6
  29. package/build/address.js +19 -37
  30. package/build/bech32utils.d.ts +6 -0
  31. package/build/bech32utils.js +26 -0
  32. package/build/index.d.ts +3 -1
  33. package/build/index.js +1 -0
  34. package/build/payments/embed.d.ts +1 -1
  35. package/build/payments/embed.js +1 -1
  36. package/build/payments/index.d.ts +12 -104
  37. package/build/payments/index.js +12 -24
  38. package/build/payments/p2ms.d.ts +1 -1
  39. package/build/payments/p2ms.js +1 -1
  40. package/build/payments/p2op.d.ts +1 -1
  41. package/build/payments/p2op.js +2 -2
  42. package/build/payments/p2pk.d.ts +1 -1
  43. package/build/payments/p2pk.js +1 -1
  44. package/build/payments/p2pkh.d.ts +1 -1
  45. package/build/payments/p2pkh.js +2 -2
  46. package/build/payments/p2sh.d.ts +1 -1
  47. package/build/payments/p2sh.js +1 -1
  48. package/build/payments/p2tr.d.ts +1 -1
  49. package/build/payments/p2tr.js +2 -6
  50. package/build/payments/p2wpkh.d.ts +1 -1
  51. package/build/payments/p2wpkh.js +1 -1
  52. package/build/payments/p2wsh.d.ts +1 -1
  53. package/build/payments/p2wsh.js +2 -2
  54. package/build/payments/types.d.ts +93 -0
  55. package/build/payments/types.js +13 -0
  56. package/build/psbt/bip371.d.ts +0 -1
  57. package/build/psbt/bip371.js +2 -6
  58. package/build/psbt/psbtutils.d.ts +0 -7
  59. package/build/psbt/psbtutils.js +2 -54
  60. package/build/psbt.js +3 -2
  61. package/build/pubkey.d.ts +8 -0
  62. package/build/pubkey.js +56 -0
  63. package/build/script.d.ts +1 -2
  64. package/build/script.js +1 -4
  65. package/build/script_signature.d.ts +1 -0
  66. package/build/script_signature.js +4 -1
  67. package/build/tsconfig.tsbuildinfo +1 -1
  68. package/build/types.d.ts +3 -0
  69. package/package.json +5 -2
  70. package/src/address.ts +20 -50
  71. package/src/bech32utils.ts +43 -0
  72. package/src/index.ts +2 -3
  73. package/src/payments/embed.ts +2 -2
  74. package/src/payments/index.ts +40 -164
  75. package/src/payments/p2ms.ts +2 -2
  76. package/src/payments/p2op.ts +2 -2
  77. package/src/payments/p2pk.ts +2 -2
  78. package/src/payments/p2pkh.ts +3 -3
  79. package/src/payments/p2sh.ts +2 -10
  80. package/src/payments/p2tr.ts +6 -5
  81. package/src/payments/p2wpkh.ts +1 -1
  82. package/src/payments/p2wsh.ts +8 -2
  83. package/src/payments/types.ts +154 -0
  84. package/src/psbt/bip371.ts +6 -13
  85. package/src/psbt/psbtutils.ts +2 -104
  86. package/src/psbt.ts +3 -2
  87. package/src/pubkey.ts +99 -0
  88. package/src/script.ts +2 -7
  89. package/src/script_signature.ts +10 -1
  90. package/src/types.ts +5 -0
  91. package/test/address.spec.ts +8 -7
  92. package/test/integration/taproot.spec.ts +2 -1
  93. package/test/payments.spec.ts +4 -4
@@ -1,72 +1,14 @@
1
- import { t as F, e as g, B as m, h as E, a as G } from "./crypto-0PweVewC.js";
2
- import { g as K, b as P, i as x, d as b, f as se, a as X } from "./psbt-BIwOrKer.js";
3
- import { b as ae, g as me, h as we, t as r, s as N, j as T, i as ce, T as W } from "./utils-CO5kmxe9.js";
4
- import { d as w, o as v, c as y, i as S, e as ue, f as ie, g as ye } from "./script-CROJPzz_.js";
5
- const M = 192, le = 128, be = (e) => "left" in e && "right" in e;
6
- function L(e, o) {
7
- if (e.length < 33)
8
- throw new TypeError(
9
- `The control-block length is too small. Got ${e.length}, expected min 33.`
10
- );
11
- const p = (e.length - 33) / 32;
12
- let i = o;
13
- for (let s = 0; s < p; s++) {
14
- const u = e.subarray(33 + 32 * s, 65 + 32 * s);
15
- i.compare(u) < 0 ? i = C(i, u) : i = C(u, i);
16
- }
17
- return i;
18
- }
19
- function A(e) {
20
- if (ae(e)) return { hash: H(e) };
21
- const o = [A(e[0]), A(e[1])];
22
- o.sort((s, u) => s.hash.compare(u.hash));
23
- const [p, i] = o;
24
- return {
25
- hash: C(p.hash, i.hash),
26
- left: p,
27
- right: i
28
- };
29
- }
30
- function q(e, o) {
31
- if (be(e)) {
32
- const p = q(e.left, o);
33
- if (p !== void 0) return [...p, e.right.hash];
34
- const i = q(e.right, o);
35
- if (i !== void 0) return [...i, e.left.hash];
36
- } else if (e.hash.equals(o))
37
- return [];
38
- }
39
- function H(e) {
40
- const o = e.version || M;
41
- return F(
42
- "TapLeaf",
43
- g.concat([g.from([o]), ge(e.output)])
44
- );
45
- }
46
- function oe(e, o) {
47
- return F("TapTweak", g.concat(o ? [e, o] : [e]));
48
- }
49
- function j(e, o) {
50
- if (!g.isBuffer(e) || e.length !== 32 || o && o.length !== 32) return null;
51
- const p = oe(e, o), i = K().xOnlyPointAddTweak(e, p);
52
- return !i || i.xOnlyPubkey === null ? null : {
53
- parity: i.parity,
54
- x: g.from(i.xOnlyPubkey)
55
- };
56
- }
57
- function C(e, o) {
58
- return F("TapBranch", g.concat([e, o]));
59
- }
60
- function ge(e) {
61
- const o = me(e.length), p = g.allocUnsafe(o);
62
- return we(e.length, p), g.concat([p, e]);
63
- }
64
- function h(e, o, p) {
1
+ import { b as v, d as g, f as ie, i as L, a as D } from "./psbt-URK2hBFc.js";
2
+ import { o as _, c as l, i as H, d as m, b as oe, e as fe, f as me } from "./script-DyPItFEl.js";
3
+ import { t as r, d as P, s as j, b as we, e as le, f as ye, i as be, T as $ } from "./utils-DNZi-T5W.js";
4
+ import { a as k, B as c, h as T, t as X, s as Y } from "./crypto-BhCpKpek.js";
5
+ var E = /* @__PURE__ */ ((e) => (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", e))(E || {});
6
+ function p(e, o, f) {
65
7
  Object.defineProperty(e, o, {
66
8
  configurable: !0,
67
9
  enumerable: !0,
68
10
  get() {
69
- const i = p.call(this);
11
+ const i = f.call(this);
70
12
  return this[o] = i, i;
71
13
  },
72
14
  set(i) {
@@ -79,187 +21,216 @@ function h(e, o, p) {
79
21
  }
80
22
  });
81
23
  }
82
- function l(e) {
24
+ function y(e) {
83
25
  let o;
84
26
  return () => (o !== void 0 || (o = e()), o);
85
27
  }
86
- const D = v;
87
- function ke(e, o) {
88
- if (!e.data && !e.output) throw new TypeError("Not enough data");
28
+ const Q = _, z = 16, Z = 2, J = 40;
29
+ function ge(e, o) {
30
+ if (!e.address && !e.output && !e.program && (typeof e.deploymentVersion > "u" || !e.hash160))
31
+ throw new TypeError("At least one of address, output or program must be provided");
89
32
  o = Object.assign({ validate: !0 }, o || {}), r(
90
33
  {
91
- network: r.maybe(r.Object),
34
+ address: r.maybe(r.String),
92
35
  output: r.maybe(r.Buffer),
93
- data: r.maybe(r.arrayOf(r.Buffer))
94
- },
95
- e
96
- );
97
- const p = e.network || P, i = { name: k.Embed, network: p, data: [] };
98
- if (h(i, "output", () => {
99
- if (e.data)
100
- return y([D.OP_RETURN].concat(e.data));
101
- }), h(i, "data", () => {
102
- if (!e.output) return;
103
- const s = w(e.output);
104
- if (s != null)
105
- return s.slice(1);
106
- }), o.validate && e.output) {
107
- const s = w(e.output);
108
- if (s[0] !== D.OP_RETURN) throw new TypeError("Output is invalid");
109
- if (!s.slice(1).every(r.Buffer)) throw new TypeError("Output is invalid");
110
- if (e.data && !N(e.data, i.data)) throw new TypeError("Data mismatch");
111
- }
112
- return Object.assign(i, e);
113
- }
114
- const I = v, V = I.OP_RESERVED;
115
- function Ee(e, o) {
116
- if (!e.input && !e.output && !(e.pubkeys && e.m !== void 0) && !e.signatures)
117
- throw new TypeError("Not enough data");
118
- o = Object.assign({ validate: !0 }, o || {});
119
- function p(f) {
120
- return S(f) || (o.allowIncomplete && f === I.OP_0) !== void 0;
121
- }
122
- r(
123
- {
36
+ program: r.maybe(r.Buffer),
124
37
  network: r.maybe(r.Object),
125
- m: r.maybe(r.Number),
126
- n: r.maybe(r.Number),
127
- output: r.maybe(r.Buffer),
128
- pubkeys: r.maybe(r.arrayOf(T)),
129
- signatures: r.maybe(r.arrayOf(p)),
130
- input: r.maybe(r.Buffer)
38
+ deploymentVersion: r.maybe(r.Number),
39
+ hash160: r.maybe(r.BufferN(20))
131
40
  },
132
41
  e
133
42
  );
134
- const s = {
135
- network: e.network || P,
136
- name: k.P2MS
43
+ const f = () => {
44
+ if (typeof e.deploymentVersion < "u" && typeof e.hash160 < "u") {
45
+ if (e.hash160.length !== 20) throw new TypeError("hash160 must be exactly 20 bytes");
46
+ if (e.deploymentVersion < 0 || e.deploymentVersion > 255)
47
+ throw new TypeError("deploymentVersion must fit in one byte");
48
+ return c.concat([c.of(e.deploymentVersion), e.hash160]);
49
+ }
50
+ }, i = y(() => ie(e.address)), s = e.network || v, u = {
51
+ name: E.P2OP,
52
+ network: s,
53
+ deploymentVersion: 0
137
54
  };
138
- let u = [], n = !1;
139
- function t(f) {
140
- n || (n = !0, u = w(f), s.m = u[0] - V, s.n = u[u.length - 2] - V, s.pubkeys = u.slice(1, -2));
141
- }
142
- if (h(s, "output", () => {
143
- if (e.m && s.n && e.pubkeys)
144
- return y(
145
- [].concat(
146
- V + e.m,
147
- e.pubkeys,
148
- V + s.n,
149
- I.OP_CHECKMULTISIG
150
- )
151
- );
152
- }), h(s, "m", () => {
153
- if (s.output)
154
- return t(s.output), s.m;
155
- }), h(s, "n", () => {
156
- if (s.pubkeys)
157
- return s.pubkeys.length;
158
- }), h(s, "pubkeys", () => {
159
- if (e.output)
160
- return t(e.output), s.pubkeys;
161
- }), h(s, "signatures", () => {
162
- if (!e.input) return;
163
- const f = w(e.input);
164
- if (f != null)
165
- return f.slice(1);
166
- }), h(s, "input", () => {
167
- if (e.signatures)
168
- return y([I.OP_0].concat(e.signatures));
169
- }), h(s, "witness", () => {
170
- if (s.input)
171
- return [];
172
- }), h(s, "name", () => {
173
- if (!(!s.m || !s.n))
174
- return `p2ms(${s.m} of ${s.n})`;
175
- }), o.validate) {
55
+ if (p(u, "program", () => {
56
+ if (e.program) return e.program;
57
+ const n = f();
58
+ if (n) return n;
176
59
  if (e.output) {
177
- if (t(e.output), !r.Number(u[0])) throw new TypeError("Output is invalid");
178
- if (!r.Number(u[u.length - 2])) throw new TypeError("Output is invalid");
179
- if (u[u.length - 1] !== I.OP_CHECKMULTISIG)
180
- throw new TypeError("Output is invalid");
181
- if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== u.length - 3)
182
- throw new TypeError("Output is invalid");
183
- if (!s.pubkeys.every((f) => T(f))) throw new TypeError("Output is invalid");
184
- if (e.m !== void 0 && e.m !== s.m) throw new TypeError("m mismatch");
185
- if (e.n !== void 0 && e.n !== s.n) throw new TypeError("n mismatch");
186
- if (e.pubkeys && !N(e.pubkeys, s.pubkeys))
187
- throw new TypeError("Pubkeys mismatch");
60
+ if (e.output[0] !== Q.OP_16) throw new TypeError("Invalid P2OP script");
61
+ let t = 1, h;
62
+ if (e.output[1] < 76)
63
+ h = e.output[1], t = 2;
64
+ else if (e.output[1] === 76)
65
+ h = e.output[2], t = 3;
66
+ else
67
+ throw new TypeError("Unsupported push opcode in P2OP script");
68
+ return e.output.subarray(t, t + h);
188
69
  }
189
- if (e.pubkeys) {
190
- if (e.n !== void 0 && e.n !== e.pubkeys.length)
191
- throw new TypeError("Pubkey count mismatch");
192
- if (s.n = e.pubkeys.length, s.n < s.m) throw new TypeError("Pubkey count cannot be less than m");
70
+ if (e.address)
71
+ return i().data;
72
+ }), p(u, "deploymentVersion", () => {
73
+ if (u.program)
74
+ return u.program[0];
75
+ }), p(u, "hash160", () => {
76
+ if (u.program)
77
+ return u.program.subarray(1);
78
+ }), p(u, "output", () => {
79
+ if (u.program)
80
+ return l([Q.OP_16, u.program]);
81
+ }), p(u, "address", () => {
82
+ if (!u.program) return;
83
+ if (!s.bech32Opnet)
84
+ throw new TypeError("Network does not support opnet");
85
+ const n = g.bech32m.toWords(u.program);
86
+ return n.unshift(z), g.bech32m.encode(s.bech32Opnet, n);
87
+ }), o.validate) {
88
+ let n = k.alloc(0);
89
+ if (e.address) {
90
+ const t = i();
91
+ if (s.bech32Opnet !== t.prefix)
92
+ throw new TypeError("Invalid prefix or network mismatch");
93
+ if (t.version !== z)
94
+ throw new TypeError("Invalid witness version for p2op");
95
+ if (t.data.length < Z || t.data.length > J)
96
+ throw new TypeError("Invalid witness program length");
97
+ n = t.data;
193
98
  }
194
- if (e.signatures) {
195
- if (e.signatures.length < s.m) throw new TypeError("Not enough signatures provided");
196
- if (e.signatures.length > s.m) throw new TypeError("Too many signatures provided");
99
+ if (e.program) {
100
+ if (n.length && !n.equals(e.program)) throw new TypeError("Program mismatch");
101
+ n = e.program;
197
102
  }
198
- if (e.input) {
199
- if (e.input[0] !== I.OP_0) throw new TypeError("Input is invalid");
200
- if (s.signatures.length === 0 || !s.signatures.every(p))
201
- throw new TypeError("Input has invalid signature(s)");
202
- if (e.signatures && !N(e.signatures, s.signatures))
203
- throw new TypeError("Signature mismatch");
204
- if (e.m !== void 0 && e.m !== e.signatures.length)
205
- throw new TypeError("Signature count mismatch");
103
+ if (!n.length && e.deploymentVersion !== void 0 && e.hash160 && (n = f()), e.output) {
104
+ const t = u.program;
105
+ if (n.length && !n.equals(t))
106
+ throw new TypeError("Program mismatch (output vs other source)");
107
+ n = t;
206
108
  }
109
+ if (n.length < Z || n.length > J)
110
+ throw new TypeError(`Witness program must be 2–40 bytes. Was ${n.length} bytes`);
111
+ if (e.deploymentVersion !== void 0 && e.deploymentVersion !== n[0])
112
+ throw new TypeError("deploymentVersion mismatch");
113
+ if (e.hash160 && !e.hash160.equals(n.subarray(1)))
114
+ throw new TypeError("hash160 mismatch");
207
115
  }
208
- return Object.assign(s, e);
116
+ return Object.assign(u, e);
209
117
  }
210
- const Q = v;
211
- function Te(e, o) {
212
- if (!e.input && !e.output && !e.pubkey && !e.input && !e.signature)
118
+ const S = _;
119
+ function ke(e, o) {
120
+ if (!e.address && !e.hash && !e.output && !e.pubkey && !e.input)
213
121
  throw new TypeError("Not enough data");
214
122
  o = Object.assign({ validate: !0 }, o || {}), r(
215
123
  {
216
124
  network: r.maybe(r.Object),
217
- output: r.maybe(r.Buffer),
218
- pubkey: r.maybe(T),
219
- signature: r.maybe(S),
125
+ address: r.maybe(r.String),
126
+ hash: r.maybe(r.BufferN(20)),
127
+ output: r.maybe(r.BufferN(25)),
128
+ pubkey: r.maybe(P),
129
+ signature: r.maybe(H),
220
130
  input: r.maybe(r.Buffer)
221
131
  },
222
132
  e
223
133
  );
224
- const p = l(() => w(e.input)), i = e.network || P, s = {
225
- name: k.P2PK,
226
- network: i,
227
- pubkey: void 0
134
+ const f = y(() => {
135
+ const n = c.from(L.decode(e.address)), t = n.readUInt8(0), h = n.subarray(1);
136
+ return { version: t, hash: h };
137
+ }), i = y(() => m(e.input)), s = e.network || v, u = {
138
+ name: E.P2PKH,
139
+ network: s,
140
+ hash: void 0
228
141
  };
229
- if (h(s, "output", () => {
230
- if (e.pubkey)
231
- return y([e.pubkey, Q.OP_CHECKSIG]);
232
- }), h(s, "pubkey", () => {
233
- if (e.output)
234
- return e.output.subarray(1, -1);
235
- }), h(s, "signature", () => {
142
+ if (p(u, "address", () => {
143
+ if (!u.hash) return;
144
+ const n = c.allocUnsafe(21);
145
+ return n.writeUInt8(s.pubKeyHash, 0), u.hash.copy(n, 1), L.encode(n);
146
+ }), p(u, "hash", () => {
147
+ if (e.output) return e.output.subarray(3, 23);
148
+ if (e.address) return f().hash;
149
+ if (e.pubkey || u.pubkey) return T(e.pubkey || u.pubkey);
150
+ }), p(u, "output", () => {
151
+ if (u.hash)
152
+ return l([
153
+ S.OP_DUP,
154
+ S.OP_HASH160,
155
+ u.hash,
156
+ S.OP_EQUALVERIFY,
157
+ S.OP_CHECKSIG
158
+ ]);
159
+ }), p(u, "pubkey", () => {
236
160
  if (e.input)
237
- return p()[0];
238
- }), h(s, "input", () => {
239
- if (e.signature)
240
- return y([e.signature]);
241
- }), h(s, "witness", () => {
242
- if (s.input)
161
+ return i()[1];
162
+ }), p(u, "signature", () => {
163
+ if (e.input)
164
+ return i()[0];
165
+ }), p(u, "input", () => {
166
+ if (!e.pubkey || !e.signature) return;
167
+ let n = e.pubkey;
168
+ if (e.useHybrid || e.useUncompressed) {
169
+ const t = D(e.pubkey);
170
+ t && (e.useUncompressed ? n = t.uncompressed : n = t.hybrid);
171
+ }
172
+ return l([e.signature, n]);
173
+ }), p(u, "witness", () => {
174
+ if (u.input)
243
175
  return [];
244
176
  }), o.validate) {
177
+ let n = c.from([]);
178
+ if (e.address) {
179
+ if (f().version !== s.pubKeyHash)
180
+ throw new TypeError("Invalid version or Network mismatch");
181
+ if (f().hash.length !== 20)
182
+ throw new TypeError("Invalid address");
183
+ n = f().hash;
184
+ }
185
+ if (e.hash) {
186
+ if (n.length > 0 && !n.equals(e.hash))
187
+ throw new TypeError("Hash mismatch");
188
+ n = e.hash;
189
+ }
245
190
  if (e.output) {
246
- if (e.output[e.output.length - 1] !== Q.OP_CHECKSIG)
191
+ if (e.output.length !== 25 || e.output[0] !== S.OP_DUP || e.output[1] !== S.OP_HASH160 || e.output[2] !== 20 || e.output[23] !== S.OP_EQUALVERIFY || e.output[24] !== S.OP_CHECKSIG)
247
192
  throw new TypeError("Output is invalid");
248
- if (!T(s.pubkey)) throw new TypeError("Output pubkey is invalid");
249
- if (e.pubkey && !e.pubkey.equals(s.pubkey)) throw new TypeError("Pubkey mismatch");
193
+ const t = e.output.subarray(3, 23);
194
+ if (n.length > 0 && !n.equals(t)) throw new TypeError("Hash mismatch");
195
+ n = t;
196
+ }
197
+ if (e.pubkey) {
198
+ const t = T(e.pubkey);
199
+ let h = n.length > 0 && !n.equals(t);
200
+ if (h && (e.pubkey.length === 33 && (e.pubkey[0] === 2 || e.pubkey[0] === 3) || e.pubkey.length === 65 && e.pubkey[0] === 4)) {
201
+ const d = D(e.pubkey);
202
+ if (d) {
203
+ const a = T(d.uncompressed);
204
+ if (n.equals(a))
205
+ h = !1, e.useUncompressed = !0;
206
+ else {
207
+ const w = T(d.hybrid);
208
+ h = !n.equals(w), h || (e.useHybrid = !0);
209
+ }
210
+ }
211
+ }
212
+ if (h)
213
+ throw new TypeError("Hash mismatch");
214
+ n = t;
250
215
  }
251
- if (e.signature && e.input && !e.input.equals(s.input))
252
- throw new TypeError("Signature mismatch");
253
216
  if (e.input) {
254
- if (p().length !== 1) throw new TypeError("Input is invalid");
255
- if (!S(s.signature))
217
+ const t = i();
218
+ if (t.length !== 2) throw new TypeError("Input is invalid");
219
+ if (!H(t[0]))
256
220
  throw new TypeError("Input has invalid signature");
221
+ if (!P(t[1])) throw new TypeError("Input has invalid pubkey");
222
+ if (e.signature && !e.signature.equals(t[0]))
223
+ throw new TypeError("Signature mismatch");
224
+ if (e.pubkey && !e.pubkey.equals(t[1]))
225
+ throw new TypeError("Pubkey mismatch");
226
+ const h = T(t[1]);
227
+ if (n.length > 0 && !n.equals(h)) throw new TypeError("Hash mismatch (input)");
257
228
  }
258
229
  }
259
- return Object.assign(s, e);
230
+ return Object.assign(u, e);
260
231
  }
261
- const B = v;
262
- function Pe(e, o) {
232
+ const q = _;
233
+ function Ee(e, o) {
263
234
  if (!e.address && !e.hash && !e.output && !e.redeem && !e.input)
264
235
  throw new TypeError("Not enough data");
265
236
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -279,52 +250,52 @@ function Pe(e, o) {
279
250
  },
280
251
  e
281
252
  );
282
- let p = e.network;
283
- p || (p = e.redeem && e.redeem.network || P);
253
+ let f = e.network;
254
+ f || (f = e.redeem && e.redeem.network || v);
284
255
  const i = {
285
- network: p,
286
- name: k.P2SH
287
- }, s = l(() => {
288
- const t = m.from(x.decode(e.address)), f = t.readUInt8(0), d = t.subarray(1);
289
- return { version: f, hash: d };
290
- }), u = l(() => w(e.input)), n = l(() => {
291
- const t = u(), f = t[t.length - 1];
256
+ network: f,
257
+ name: E.P2SH
258
+ }, s = y(() => {
259
+ const t = c.from(L.decode(e.address)), h = t.readUInt8(0), d = t.subarray(1);
260
+ return { version: h, hash: d };
261
+ }), u = y(() => m(e.input)), n = y(() => {
262
+ const t = u(), h = t[t.length - 1];
292
263
  return {
293
- network: p,
294
- output: f === B.OP_FALSE ? m.from([]) : f,
295
- input: y(t.slice(0, -1)),
264
+ network: f,
265
+ output: h === q.OP_FALSE ? c.from([]) : h,
266
+ input: l(t.slice(0, -1)),
296
267
  witness: e.witness || []
297
268
  };
298
269
  });
299
- if (h(i, "address", () => {
270
+ if (p(i, "address", () => {
300
271
  if (!i.hash) return;
301
- const t = m.allocUnsafe(21);
302
- return t.writeUInt8(i.network.scriptHash, 0), i.hash.copy(t, 1), x.encode(t);
303
- }), h(i, "hash", () => {
272
+ const t = c.allocUnsafe(21);
273
+ return t.writeUInt8(i.network.scriptHash, 0), i.hash.copy(t, 1), L.encode(t);
274
+ }), p(i, "hash", () => {
304
275
  if (e.output) return e.output.subarray(2, 22);
305
276
  if (e.address) return s().hash;
306
- if (i.redeem && i.redeem.output) return E(i.redeem.output);
307
- }), h(i, "output", () => {
277
+ if (i.redeem && i.redeem.output) return T(i.redeem.output);
278
+ }), p(i, "output", () => {
308
279
  if (i.hash)
309
- return y([B.OP_HASH160, i.hash, B.OP_EQUAL]);
310
- }), h(i, "redeem", () => {
280
+ return l([q.OP_HASH160, i.hash, q.OP_EQUAL]);
281
+ }), p(i, "redeem", () => {
311
282
  if (e.input)
312
283
  return n();
313
- }), h(i, "input", () => {
284
+ }), p(i, "input", () => {
314
285
  if (!(!e.redeem || !e.redeem.input || !e.redeem.output))
315
- return y(
316
- [].concat(w(e.redeem.input), e.redeem.output)
286
+ return l(
287
+ [].concat(m(e.redeem.input), e.redeem.output)
317
288
  );
318
- }), h(i, "witness", () => {
289
+ }), p(i, "witness", () => {
319
290
  if (i.redeem && i.redeem.witness) return i.redeem.witness;
320
291
  if (i.input) return [];
321
- }), h(i, "name", () => {
292
+ }), p(i, "name", () => {
322
293
  const t = ["p2sh"];
323
294
  return i.redeem !== void 0 && i.redeem.name !== void 0 && t.push(i.redeem.name), t.join("-");
324
295
  }), o.validate) {
325
- let t = m.from([]);
296
+ let t = c.from([]);
326
297
  if (e.address) {
327
- if (s().version !== p.scriptHash)
298
+ if (s().version !== f.scriptHash)
328
299
  throw new TypeError("Invalid version or Network mismatch");
329
300
  if (s().hash.length !== 20) throw new TypeError("Invalid address");
330
301
  t = s().hash;
@@ -334,34 +305,34 @@ function Pe(e, o) {
334
305
  t = e.hash;
335
306
  }
336
307
  if (e.output) {
337
- if (e.output.length !== 23 || e.output[0] !== B.OP_HASH160 || e.output[1] !== 20 || e.output[22] !== B.OP_EQUAL)
308
+ if (e.output.length !== 23 || e.output[0] !== q.OP_HASH160 || e.output[1] !== 20 || e.output[22] !== q.OP_EQUAL)
338
309
  throw new TypeError("Output is invalid");
339
310
  const d = e.output.subarray(2, 22);
340
311
  if (t.length > 0 && !t.equals(d)) throw new TypeError("Hash mismatch");
341
312
  t = d;
342
313
  }
343
- const f = (d) => {
314
+ const h = (d) => {
344
315
  if (d.output) {
345
- const a = w(d.output);
316
+ const a = m(d.output);
346
317
  if (!a || a.length < 1)
347
318
  throw new TypeError("Redeem.output too short");
348
319
  if (d.output.byteLength > 520)
349
320
  throw new TypeError("Redeem.output unspendable if larger than 520 bytes");
350
- if (ue(a) > 201)
321
+ if (oe(a) > 201)
351
322
  throw new TypeError(
352
323
  "Redeem.output unspendable with more than 201 non-push ops"
353
324
  );
354
- const c = E(d.output);
355
- if (t.length > 0 && !t.equals(c)) throw new TypeError("Hash mismatch");
356
- t = c;
325
+ const w = T(d.output);
326
+ if (t.length > 0 && !t.equals(w)) throw new TypeError("Hash mismatch");
327
+ t = w;
357
328
  }
358
329
  if (d.input) {
359
- const a = d.input.length > 0, c = d.witness && d.witness.length > 0;
360
- if (!a && !c) throw new TypeError("Empty input");
361
- if (a && c) throw new TypeError("Input and witness provided");
330
+ const a = d.input.length > 0, w = d.witness && d.witness.length > 0;
331
+ if (!a && !w) throw new TypeError("Empty input");
332
+ if (a && w) throw new TypeError("Input and witness provided");
362
333
  if (a) {
363
- const _ = w(d.input);
364
- if (!ie(_))
334
+ const I = m(d.input);
335
+ if (!fe(I))
365
336
  throw new TypeError("Non push-only scriptSig");
366
337
  }
367
338
  }
@@ -369,11 +340,11 @@ function Pe(e, o) {
369
340
  if (e.input) {
370
341
  const d = u();
371
342
  if (!d || d.length < 1) throw new TypeError("Input too short");
372
- if (!m.isBuffer(n().output)) throw new TypeError("Input is invalid");
373
- f(n());
343
+ if (!c.isBuffer(n().output)) throw new TypeError("Input is invalid");
344
+ h(n());
374
345
  }
375
346
  if (e.redeem) {
376
- if (e.redeem.network && e.redeem.network !== p)
347
+ if (e.redeem.network && e.redeem.network !== f)
377
348
  throw new TypeError("Network mismatch");
378
349
  if (e.input) {
379
350
  const d = n();
@@ -382,15 +353,118 @@ function Pe(e, o) {
382
353
  if (e.redeem.input && !e.redeem.input.equals(d.input))
383
354
  throw new TypeError("Redeem.input mismatch");
384
355
  }
385
- f(e.redeem);
356
+ h(e.redeem);
386
357
  }
387
- if (e.witness && e.redeem && e.redeem.witness && !N(e.redeem.witness, e.witness))
358
+ if (e.witness && e.redeem && e.redeem.witness && !j(e.redeem.witness, e.witness))
388
359
  throw new TypeError("Witness and redeem.witness mismatch");
389
360
  }
390
361
  return Object.assign(i, e);
391
362
  }
392
- const Y = v, z = 1, ve = 80;
393
- function Oe(e, o) {
363
+ const R = {};
364
+ function We(e) {
365
+ e ? e !== R.eccLib && (Te(e), R.eccLib = e) : R.eccLib = e;
366
+ }
367
+ function pe() {
368
+ if (!R.eccLib)
369
+ throw new Error(
370
+ "No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance"
371
+ );
372
+ return R.eccLib;
373
+ }
374
+ const O = (e) => c.from(e, "hex");
375
+ function Te(e) {
376
+ b(typeof e.isXOnlyPoint == "function"), b(e.isXOnlyPoint(O("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))), b(e.isXOnlyPoint(O("fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e"))), b(e.isXOnlyPoint(O("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"))), b(e.isXOnlyPoint(O("0000000000000000000000000000000000000000000000000000000000000001"))), b(
377
+ !e.isXOnlyPoint(O("0000000000000000000000000000000000000000000000000000000000000000"))
378
+ ), b(
379
+ !e.isXOnlyPoint(O("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"))
380
+ ), b(typeof e.xOnlyPointAddTweak == "function"), Pe.forEach((o) => {
381
+ const f = e.xOnlyPointAddTweak(O(o.pubkey), O(o.tweak));
382
+ o.result === null ? b(f === null) : (b(f !== null), b(f.parity === o.parity), b(c.from(f.xOnlyPubkey).equals(O(o.result))));
383
+ });
384
+ }
385
+ function b(e) {
386
+ if (!e) throw new Error("ecc library invalid");
387
+ }
388
+ const Pe = [
389
+ {
390
+ pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
391
+ tweak: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
392
+ parity: -1,
393
+ result: null
394
+ },
395
+ {
396
+ pubkey: "1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b",
397
+ tweak: "a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac",
398
+ parity: 1,
399
+ result: "e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf"
400
+ },
401
+ {
402
+ pubkey: "2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
403
+ tweak: "823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47",
404
+ parity: 0,
405
+ result: "9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c"
406
+ }
407
+ ], G = 192, Oe = 128, ve = (e) => "left" in e && "right" in e;
408
+ function F(e, o) {
409
+ if (e.length < 33)
410
+ throw new TypeError(
411
+ `The control-block length is too small. Got ${e.length}, expected min 33.`
412
+ );
413
+ const f = (e.length - 33) / 32;
414
+ let i = o;
415
+ for (let s = 0; s < f; s++) {
416
+ const u = e.subarray(33 + 32 * s, 65 + 32 * s);
417
+ i.compare(u) < 0 ? i = M(i, u) : i = M(u, i);
418
+ }
419
+ return i;
420
+ }
421
+ function W(e) {
422
+ if (we(e)) return { hash: B(e) };
423
+ const o = [W(e[0]), W(e[1])];
424
+ o.sort((s, u) => s.hash.compare(u.hash));
425
+ const [f, i] = o;
426
+ return {
427
+ hash: M(f.hash, i.hash),
428
+ left: f,
429
+ right: i
430
+ };
431
+ }
432
+ function x(e, o) {
433
+ if (ve(e)) {
434
+ const f = x(e.left, o);
435
+ if (f !== void 0) return [...f, e.right.hash];
436
+ const i = x(e.right, o);
437
+ if (i !== void 0) return [...i, e.left.hash];
438
+ } else if (e.hash.equals(o))
439
+ return [];
440
+ }
441
+ function B(e) {
442
+ const o = e.version || G;
443
+ return X(
444
+ "TapLeaf",
445
+ k.concat([k.from([o]), Se(e.output)])
446
+ );
447
+ }
448
+ function _e(e, o) {
449
+ return X("TapTweak", k.concat(o ? [e, o] : [e]));
450
+ }
451
+ function V(e, o) {
452
+ if (!k.isBuffer(e) || e.length !== 32 || o && o.length !== 32) return null;
453
+ const f = _e(e, o), i = pe().xOnlyPointAddTweak(e, f);
454
+ return !i || i.xOnlyPubkey === null ? null : {
455
+ parity: i.parity,
456
+ x: k.from(i.xOnlyPubkey)
457
+ };
458
+ }
459
+ function M(e, o) {
460
+ return X("TapBranch", k.concat([e, o]));
461
+ }
462
+ function Se(e) {
463
+ const o = le(e.length), f = k.allocUnsafe(o);
464
+ return ye(e.length, f), k.concat([f, e]);
465
+ }
466
+ const ee = _, re = 1, Ie = 80;
467
+ function He(e, o) {
394
468
  if (!e.address && !e.output && !e.pubkey && !e.internalPubkey && !(e.witness && e.witness.length > 1))
395
469
  throw new TypeError("Not enough data");
396
470
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -406,7 +480,7 @@ function Oe(e, o) {
406
480
  // tweaked with `hash` from `internalPubkey`
407
481
  signature: r.maybe(r.anyOf(r.BufferN(64), r.BufferN(65))),
408
482
  witness: r.maybe(r.arrayOf(r.Buffer)),
409
- scriptTree: r.maybe(ce),
483
+ scriptTree: r.maybe(be),
410
484
  redeem: r.maybe({
411
485
  output: r.maybe(r.Buffer),
412
486
  // tapleaf script
@@ -418,87 +492,87 @@ function Oe(e, o) {
418
492
  },
419
493
  e
420
494
  );
421
- const p = l(() => se(e.address)), i = l(() => {
495
+ const f = y(() => ie(e.address)), i = y(() => {
422
496
  if (!(!e.witness || !e.witness.length))
423
- return e.witness.length >= 2 && e.witness[e.witness.length - 1][0] === ve ? e.witness.slice(0, -1) : e.witness.slice();
424
- }), s = l(() => {
425
- if (e.scriptTree) return A(e.scriptTree);
497
+ return e.witness.length >= 2 && e.witness[e.witness.length - 1][0] === Ie ? e.witness.slice(0, -1) : e.witness.slice();
498
+ }), s = y(() => {
499
+ if (e.scriptTree) return W(e.scriptTree);
426
500
  if (e.hash) return { hash: e.hash };
427
- }), u = e.network || P, n = {
428
- name: k.P2TR,
501
+ }), u = e.network || v, n = {
502
+ name: E.P2TR,
429
503
  network: u
430
504
  };
431
- if (h(n, "address", () => {
505
+ if (p(n, "address", () => {
432
506
  if (!n.pubkey) return;
433
- const t = b.bech32m.toWords(n.pubkey);
434
- return t.unshift(z), b.bech32m.encode(u.bech32, t);
435
- }), h(n, "hash", () => {
507
+ const t = g.bech32m.toWords(n.pubkey);
508
+ return t.unshift(re), g.bech32m.encode(u.bech32, t);
509
+ }), p(n, "hash", () => {
436
510
  const t = s();
437
511
  if (t) return t.hash;
438
- const f = i();
439
- if (f && f.length > 1) {
440
- const d = f[f.length - 1], a = d[0] & W, c = f[f.length - 2], _ = H({
441
- output: c,
512
+ const h = i();
513
+ if (h && h.length > 1) {
514
+ const d = h[h.length - 1], a = d[0] & $, w = h[h.length - 2], I = B({
515
+ output: w,
442
516
  version: a
443
517
  });
444
- return L(d, _);
518
+ return F(d, I);
445
519
  }
446
- }), h(n, "output", () => {
520
+ }), p(n, "output", () => {
447
521
  if (n.pubkey)
448
- return y([Y.OP_1, n.pubkey]);
449
- }), h(n, "redeemVersion", () => e.redeemVersion ? e.redeemVersion : e.redeem && e.redeem.redeemVersion !== void 0 && e.redeem.redeemVersion !== null ? e.redeem.redeemVersion : M), h(n, "redeem", () => {
522
+ return l([ee.OP_1, n.pubkey]);
523
+ }), p(n, "redeemVersion", () => e.redeemVersion ? e.redeemVersion : e.redeem && e.redeem.redeemVersion !== void 0 && e.redeem.redeemVersion !== null ? e.redeem.redeemVersion : G), p(n, "redeem", () => {
450
524
  const t = i();
451
525
  if (!(!t || t.length < 2))
452
526
  return {
453
527
  output: t[t.length - 2],
454
528
  witness: t.slice(0, -2),
455
- redeemVersion: t[t.length - 1][0] & W
529
+ redeemVersion: t[t.length - 1][0] & $
456
530
  };
457
- }), h(n, "pubkey", () => {
531
+ }), p(n, "pubkey", () => {
458
532
  if (e.pubkey) return e.pubkey;
459
533
  if (e.output) return e.output.subarray(2);
460
- if (e.address) return p().data;
534
+ if (e.address) return f().data;
461
535
  if (n.internalPubkey) {
462
- const t = j(n.internalPubkey, n.hash);
536
+ const t = V(n.internalPubkey, n.hash);
463
537
  if (t) return t.x;
464
538
  }
465
- }), h(n, "internalPubkey", () => {
539
+ }), p(n, "internalPubkey", () => {
466
540
  if (e.internalPubkey) return e.internalPubkey;
467
541
  const t = i();
468
542
  if (t && t.length > 1) return t[t.length - 1].subarray(1, 33);
469
- }), h(n, "signature", () => {
543
+ }), p(n, "signature", () => {
470
544
  if (e.signature) return e.signature;
471
545
  const t = i();
472
546
  if (!(!t || t.length !== 1))
473
547
  return t[0];
474
- }), h(n, "witness", () => {
548
+ }), p(n, "witness", () => {
475
549
  if (e.witness) return e.witness;
476
550
  const t = s();
477
551
  if (t && e.redeem && e.redeem.output && e.internalPubkey) {
478
- const f = H({
552
+ const h = B({
479
553
  output: e.redeem.output,
480
554
  version: n.redeemVersion
481
- }), d = q(t, f);
555
+ }), d = x(t, h);
482
556
  if (!d) return;
483
- const a = j(e.internalPubkey, t.hash);
557
+ const a = V(e.internalPubkey, t.hash);
484
558
  if (!a) return;
485
- const c = g.concat(
486
- [g.from([n.redeemVersion | a.parity]), e.internalPubkey].concat(
559
+ const w = k.concat(
560
+ [k.from([n.redeemVersion | a.parity]), e.internalPubkey].concat(
487
561
  d
488
562
  )
489
563
  );
490
- return [e.redeem.output, c];
564
+ return [e.redeem.output, w];
491
565
  }
492
566
  if (e.signature) return [e.signature];
493
567
  }), o.validate) {
494
- let t = g.from([]);
568
+ let t = k.from([]);
495
569
  if (e.address) {
496
- if (u && u.bech32 !== p().prefix)
570
+ if (u && u.bech32 !== f().prefix)
497
571
  throw new TypeError("Invalid prefix or Network mismatch");
498
- if (p().version !== z)
572
+ if (f().version !== re)
499
573
  throw new TypeError("Invalid address version");
500
- if (p().data.length !== 32) throw new TypeError("Invalid address data");
501
- t = p().data;
574
+ if (f().data.length !== 32) throw new TypeError("Invalid address data");
575
+ t = f().data;
502
576
  }
503
577
  if (e.pubkey) {
504
578
  if (t.length > 0 && !t.equals(e.pubkey))
@@ -506,29 +580,27 @@ function Oe(e, o) {
506
580
  t = e.pubkey;
507
581
  }
508
582
  if (e.output) {
509
- if (e.output.length !== 34 || e.output[0] !== Y.OP_1 || e.output[1] !== 32)
583
+ if (e.output.length !== 34 || e.output[0] !== ee.OP_1 || e.output[1] !== 32)
510
584
  throw new TypeError("Output is invalid");
511
585
  if (t.length > 0 && !t.equals(e.output.subarray(2)))
512
586
  throw new TypeError("Pubkey mismatch");
513
587
  t = e.output.subarray(2);
514
588
  }
515
589
  if (e.internalPubkey) {
516
- const a = j(e.internalPubkey, n.hash);
590
+ const a = V(e.internalPubkey, n.hash);
517
591
  if (t.length > 0 && !t.equals(a.x))
518
592
  throw new TypeError("Pubkey mismatch");
519
593
  t = a.x;
520
594
  }
521
- if (t && t.length && !K().isXOnlyPoint(t))
522
- throw new TypeError("Invalid pubkey for p2tr");
523
- const f = s();
524
- if (e.hash && f && !e.hash.equals(f.hash))
595
+ const h = s();
596
+ if (e.hash && h && !e.hash.equals(h.hash))
525
597
  throw new TypeError("Hash mismatch");
526
- if (e.redeem && e.redeem.output && f) {
527
- const a = H({
598
+ if (e.redeem && e.redeem.output && h) {
599
+ const a = B({
528
600
  output: e.redeem.output,
529
601
  version: n.redeemVersion
530
602
  });
531
- if (!q(f, a))
603
+ if (!x(h, a))
532
604
  throw new TypeError("Redeem script not in tree");
533
605
  }
534
606
  const d = i();
@@ -536,12 +608,12 @@ function Oe(e, o) {
536
608
  if (e.redeem.redeemVersion && e.redeem.redeemVersion !== n.redeem.redeemVersion)
537
609
  throw new TypeError("Redeem.redeemVersion and witness mismatch");
538
610
  if (e.redeem.output) {
539
- if (w(e.redeem.output).length === 0)
611
+ if (m(e.redeem.output).length === 0)
540
612
  throw new TypeError("Redeem.output is invalid");
541
613
  if (n.redeem.output && !e.redeem.output.equals(n.redeem.output))
542
614
  throw new TypeError("Redeem.output and witness mismatch");
543
615
  }
544
- if (e.redeem.witness && n.redeem.witness && !N(e.redeem.witness, n.redeem.witness))
616
+ if (e.redeem.witness && n.redeem.witness && !j(e.redeem.witness, n.redeem.witness))
545
617
  throw new TypeError("Redeem.witness and witness mismatch");
546
618
  }
547
619
  if (d && d.length)
@@ -558,29 +630,29 @@ function Oe(e, o) {
558
630
  throw new TypeError(
559
631
  `The control-block length of ${a.length} is incorrect!`
560
632
  );
561
- const c = (a.length - 33) / 32;
562
- if (c > 128)
563
- throw new TypeError(`The script path is too long. Got ${c}, expected max 128.`);
564
- const _ = a.subarray(1, 33);
565
- if (e.internalPubkey && !e.internalPubkey.equals(_))
633
+ const w = (a.length - 33) / 32;
634
+ if (w > 128)
635
+ throw new TypeError(`The script path is too long. Got ${w}, expected max 128.`);
636
+ const I = a.subarray(1, 33);
637
+ if (e.internalPubkey && !e.internalPubkey.equals(I))
566
638
  throw new TypeError("Internal pubkey mismatch");
567
- if (!K().isXOnlyPoint(_))
639
+ if (!pe().isXOnlyPoint(I))
568
640
  throw new TypeError("Invalid internalPubkey for p2tr witness");
569
- const he = a[0] & W, pe = d[d.length - 2], fe = H({
570
- output: pe,
641
+ const he = a[0] & $, de = d[d.length - 2], ae = B({
642
+ output: de,
571
643
  version: he
572
- }), de = L(a, fe), U = j(_, de);
573
- if (!U)
644
+ }), ce = F(a, ae), C = V(I, ce);
645
+ if (!C)
574
646
  throw new TypeError("Invalid outputKey for p2tr witness");
575
- if (t.length && !t.equals(U.x))
647
+ if (t.length && !t.equals(C.x))
576
648
  throw new TypeError("Pubkey mismatch for p2tr witness");
577
- if (U.parity !== (a[0] & 1)) throw new Error("Incorrect parity");
649
+ if (C.parity !== (a[0] & 1)) throw new Error("Incorrect parity");
578
650
  }
579
651
  }
580
652
  return Object.assign(n, e);
581
653
  }
582
- const Z = v, _e = m.alloc(0);
583
- function Se(e, o) {
654
+ const te = _, Ne = c.alloc(0);
655
+ function Be(e, o) {
584
656
  if (!e.address && !e.hash && !e.output && !e.pubkey && !e.witness)
585
657
  throw new TypeError("Not enough data");
586
658
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -590,93 +662,93 @@ function Se(e, o) {
590
662
  input: r.maybe(r.BufferN(0)),
591
663
  network: r.maybe(r.Object),
592
664
  output: r.maybe(r.BufferN(22)),
593
- pubkey: r.maybe(T),
594
- signature: r.maybe(S),
665
+ pubkey: r.maybe(P),
666
+ signature: r.maybe(H),
595
667
  witness: r.maybe(r.arrayOf(r.Buffer))
596
668
  },
597
669
  e
598
670
  );
599
- const p = l(() => {
600
- const u = b.bech32.decode(e.address), n = u.words.shift(), t = b.bech32.fromWords(u.words);
671
+ const f = y(() => {
672
+ const u = g.bech32.decode(e.address), n = u.words.shift(), t = g.bech32.fromWords(u.words);
601
673
  return {
602
674
  version: n,
603
675
  prefix: u.prefix,
604
- data: m.from(t)
676
+ data: c.from(t)
605
677
  };
606
- }), i = e.network || P, s = {
607
- name: k.P2WPKH,
678
+ }), i = e.network || v, s = {
679
+ name: E.P2WPKH,
608
680
  network: i
609
681
  };
610
- if (h(s, "address", () => {
682
+ if (p(s, "address", () => {
611
683
  if (!s.hash) return;
612
- const u = b.bech32.toWords(s.hash);
613
- return u.unshift(0), b.bech32.encode(i.bech32, u);
614
- }), h(s, "hash", () => {
684
+ const u = g.bech32.toWords(s.hash);
685
+ return u.unshift(0), g.bech32.encode(i.bech32, u);
686
+ }), p(s, "hash", () => {
615
687
  if (e.output) return e.output.subarray(2, 22);
616
- if (e.address) return p().data;
617
- if (e.pubkey || s.pubkey) return E(e.pubkey || s.pubkey);
618
- }), h(s, "output", () => {
688
+ if (e.address) return f().data;
689
+ if (e.pubkey || s.pubkey) return T(e.pubkey || s.pubkey);
690
+ }), p(s, "output", () => {
619
691
  if (s.hash)
620
- return y([Z.OP_0, s.hash]);
621
- }), h(s, "pubkey", () => {
692
+ return l([te.OP_0, s.hash]);
693
+ }), p(s, "pubkey", () => {
622
694
  if (e.pubkey) return e.pubkey;
623
695
  if (e.witness)
624
696
  return e.witness[1];
625
- }), h(s, "signature", () => {
697
+ }), p(s, "signature", () => {
626
698
  if (e.witness)
627
699
  return e.witness[0];
628
- }), h(s, "input", () => {
700
+ }), p(s, "input", () => {
629
701
  if (s.witness)
630
- return _e;
631
- }), h(s, "witness", () => {
702
+ return Ne;
703
+ }), p(s, "witness", () => {
632
704
  if (e.pubkey && e.signature)
633
705
  return [e.signature, e.pubkey];
634
706
  }), o.validate) {
635
- let u = m.from([]);
707
+ let u = c.from([]);
636
708
  if (e.address) {
637
- if (i && i.bech32 !== p().prefix)
709
+ if (i && i.bech32 !== f().prefix)
638
710
  throw new TypeError("Invalid prefix or Network mismatch");
639
- if (p().version !== 0) throw new TypeError("Invalid address version");
640
- if (p().data.length !== 20) throw new TypeError("Invalid address data");
641
- u = p().data;
711
+ if (f().version !== 0) throw new TypeError("Invalid address version");
712
+ if (f().data.length !== 20) throw new TypeError("Invalid address data");
713
+ u = f().data;
642
714
  }
643
715
  if (e.hash) {
644
716
  if (u.length > 0 && !u.equals(e.hash)) throw new TypeError("Hash mismatch");
645
717
  u = e.hash;
646
718
  }
647
719
  if (e.output) {
648
- if (e.output.length !== 22 || e.output[0] !== Z.OP_0 || e.output[1] !== 20)
720
+ if (e.output.length !== 22 || e.output[0] !== te.OP_0 || e.output[1] !== 20)
649
721
  throw new TypeError("Output is invalid");
650
722
  if (u.length > 0 && !u.equals(e.output.subarray(2)))
651
723
  throw new TypeError("Hash mismatch");
652
724
  u = e.output.subarray(2);
653
725
  }
654
726
  if (e.pubkey) {
655
- const n = E(e.pubkey);
727
+ const n = T(e.pubkey);
656
728
  if (u.length > 0 && !u.equals(n)) throw new TypeError("Hash mismatch");
657
- if (u = n, !T(e.pubkey) || e.pubkey.length !== 33)
729
+ if (u = n, !P(e.pubkey) || e.pubkey.length !== 33)
658
730
  throw new TypeError("Invalid pubkey for p2wpkh");
659
731
  }
660
732
  if (e.witness) {
661
733
  if (e.witness.length !== 2) throw new TypeError("Witness is invalid");
662
- if (!S(e.witness[0]))
734
+ if (!H(e.witness[0]))
663
735
  throw new TypeError("Witness has invalid signature");
664
- if (!T(e.witness[1]) || e.witness[1].length !== 33)
736
+ if (!P(e.witness[1]) || e.witness[1].length !== 33)
665
737
  throw new TypeError("Witness has invalid pubkey");
666
738
  if (e.signature && !e.signature.equals(e.witness[0]))
667
739
  throw new TypeError("Signature mismatch");
668
740
  if (e.pubkey && !e.pubkey.equals(e.witness[1])) throw new TypeError("Pubkey mismatch");
669
- const n = E(e.witness[1]);
741
+ const n = T(e.witness[1]);
670
742
  if (u.length > 0 && !u.equals(n)) throw new TypeError("Hash mismatch");
671
743
  }
672
744
  }
673
745
  return Object.assign(s, e);
674
746
  }
675
- const J = v, $ = m.alloc(0);
676
- function R(e) {
677
- return !!(m.isBuffer(e) && e.length === 65 && e[0] === 4 && T(e));
747
+ const ne = _, K = c.alloc(0);
748
+ function A(e) {
749
+ return !!(c.isBuffer(e) && e.length === 65 && e[0] === 4 && P(e));
678
750
  }
679
- function Ie(e, o) {
751
+ function je(e, o) {
680
752
  if (!e.address && !e.hash && !e.output && !e.redeem && !e.witness)
681
753
  throw new TypeError("Not enough data");
682
754
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -696,66 +768,66 @@ function Ie(e, o) {
696
768
  },
697
769
  e
698
770
  );
699
- const p = l(() => {
700
- const n = b.bech32.decode(e.address), t = n.words.shift(), f = b.bech32.fromWords(n.words);
771
+ const f = y(() => {
772
+ const n = g.bech32.decode(e.address), t = n.words.shift(), h = g.bech32.fromWords(n.words);
701
773
  return {
702
774
  version: t,
703
775
  prefix: n.prefix,
704
- data: m.from(f)
776
+ data: c.from(h)
705
777
  };
706
- }), i = l(() => w(e.redeem.input));
778
+ }), i = y(() => m(e.redeem.input));
707
779
  let s = e.network;
708
- s || (s = e.redeem && e.redeem.network || P);
780
+ s || (s = e.redeem && e.redeem.network || v);
709
781
  const u = {
710
782
  network: s,
711
- name: k.P2WSH
783
+ name: E.P2WSH
712
784
  };
713
- if (h(u, "address", () => {
785
+ if (p(u, "address", () => {
714
786
  if (!u.hash) return;
715
- const n = b.bech32.toWords(u.hash);
716
- return n.unshift(0), b.bech32.encode(s.bech32, n);
717
- }), h(u, "hash", () => {
787
+ const n = g.bech32.toWords(u.hash);
788
+ return n.unshift(0), g.bech32.encode(s.bech32, n);
789
+ }), p(u, "hash", () => {
718
790
  if (e.output) return e.output.subarray(2);
719
- if (e.address) return p().data;
720
- if (u.redeem && u.redeem.output) return G(u.redeem.output);
721
- }), h(u, "output", () => {
791
+ if (e.address) return f().data;
792
+ if (u.redeem && u.redeem.output) return Y(u.redeem.output);
793
+ }), p(u, "output", () => {
722
794
  if (u.hash)
723
- return y([J.OP_0, u.hash]);
724
- }), h(u, "redeem", () => {
795
+ return l([ne.OP_0, u.hash]);
796
+ }), p(u, "redeem", () => {
725
797
  if (e.witness)
726
798
  return {
727
799
  output: e.witness[e.witness.length - 1],
728
- input: $,
800
+ input: K,
729
801
  witness: e.witness.slice(0, -1)
730
802
  };
731
- }), h(u, "input", () => {
803
+ }), p(u, "input", () => {
732
804
  if (u.witness)
733
- return $;
734
- }), h(u, "witness", () => {
805
+ return K;
806
+ }), p(u, "witness", () => {
735
807
  if (e.redeem && e.redeem.input && e.redeem.input.length > 0 && e.redeem.output && e.redeem.output.length > 0) {
736
- const n = ye(i());
737
- return u.redeem = Object.assign({ witness: n }, e.redeem), u.redeem.input = $, [].concat(n, e.redeem.output);
808
+ const n = me(i());
809
+ return u.redeem = Object.assign({ witness: n }, e.redeem), u.redeem.input = K, [].concat(n, e.redeem.output);
738
810
  }
739
811
  if (e.redeem && e.redeem.output && e.redeem.witness)
740
812
  return [].concat(e.redeem.witness, e.redeem.output);
741
- }), h(u, "name", () => {
813
+ }), p(u, "name", () => {
742
814
  const n = ["p2wsh"];
743
815
  return u.redeem !== void 0 && u.redeem.name !== void 0 && n.push(u.redeem.name), n.join("-");
744
816
  }), o.validate) {
745
- let n = m.from([]);
817
+ let n = c.from([]);
746
818
  if (e.address) {
747
- if (p().prefix !== s.bech32)
819
+ if (f().prefix !== s.bech32)
748
820
  throw new TypeError("Invalid prefix or Network mismatch");
749
- if (p().version !== 0) throw new TypeError("Invalid address version");
750
- if (p().data.length !== 32) throw new TypeError("Invalid address data");
751
- n = p().data;
821
+ if (f().version !== 0) throw new TypeError("Invalid address version");
822
+ if (f().data.length !== 32) throw new TypeError("Invalid address data");
823
+ n = f().data;
752
824
  }
753
825
  if (e.hash) {
754
826
  if (n.length > 0 && !n.equals(e.hash)) throw new TypeError("Hash mismatch");
755
827
  n = e.hash;
756
828
  }
757
829
  if (e.output) {
758
- if (e.output.length !== 34 || e.output[0] !== J.OP_0 || e.output[1] !== 32)
830
+ if (e.output.length !== 34 || e.output[0] !== ne.OP_0 || e.output[1] !== 32)
759
831
  throw new TypeError("Output is invalid");
760
832
  const t = e.output.subarray(2);
761
833
  if (n.length > 0 && !n.equals(t)) throw new TypeError("Hash mismatch");
@@ -767,281 +839,251 @@ function Ie(e, o) {
767
839
  if (e.redeem.input && e.redeem.input.length > 0 && e.redeem.witness && e.redeem.witness.length > 0)
768
840
  throw new TypeError("Ambiguous witness source");
769
841
  if (e.redeem.output) {
770
- const t = w(e.redeem.output);
842
+ const t = m(e.redeem.output);
771
843
  if (!t || t.length < 1)
772
844
  throw new TypeError("Redeem.output is invalid");
773
845
  if (e.redeem.output.byteLength > 3600)
774
846
  throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");
775
- if (ue(t) > 201)
847
+ if (oe(t) > 201)
776
848
  throw new TypeError(
777
849
  "Redeem.output unspendable with more than 201 non-push ops"
778
850
  );
779
- const f = G(e.redeem.output);
780
- if (n.length > 0 && !n.equals(f)) throw new TypeError("Hash mismatch");
781
- n = f;
851
+ const h = Y(e.redeem.output);
852
+ if (n.length > 0 && !n.equals(h)) throw new TypeError("Hash mismatch");
853
+ n = h;
782
854
  }
783
- if (e.redeem.input && !ie(i()))
855
+ if (e.redeem.input && !fe(i()))
784
856
  throw new TypeError("Non push-only scriptSig");
785
- if (e.witness && e.redeem.witness && !N(e.witness, e.redeem.witness))
857
+ if (e.witness && e.redeem.witness && !j(e.witness, e.redeem.witness))
786
858
  throw new TypeError("Witness and redeem.witness mismatch");
787
- if (e.redeem.input && i().some(R) || e.redeem.output && (w(e.redeem.output) || []).some(R))
859
+ if (e.redeem.input && i().some(A) || e.redeem.output && (m(e.redeem.output) || []).some(A))
788
860
  throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey");
789
861
  }
790
862
  if (e.witness && e.witness.length > 0) {
791
863
  const t = e.witness[e.witness.length - 1];
792
864
  if (e.redeem && e.redeem.output && !e.redeem.output.equals(t))
793
865
  throw new TypeError("Witness and redeem.output mismatch");
794
- if (e.witness.some(R) || (w(t) || []).some(R))
866
+ if (e.witness.some(A) || (m(t) || []).some(A))
795
867
  throw new TypeError("Witness contains uncompressed pubkey");
796
868
  }
797
869
  }
798
870
  return Object.assign(u, e);
799
871
  }
800
- const ee = v, re = 16, te = 2, ne = 40;
801
- function He(e, o) {
802
- if (!e.address && !e.output && !e.program && (typeof e.deploymentVersion > "u" || !e.hash160))
803
- throw new TypeError("At least one of address, output or program must be provided");
872
+ const se = _;
873
+ function qe(e, o) {
874
+ if (!e.data && !e.output) throw new TypeError("Not enough data");
804
875
  o = Object.assign({ validate: !0 }, o || {}), r(
805
876
  {
806
- address: r.maybe(r.String),
877
+ network: r.maybe(r.Object),
807
878
  output: r.maybe(r.Buffer),
808
- program: r.maybe(r.Buffer),
879
+ data: r.maybe(r.arrayOf(r.Buffer))
880
+ },
881
+ e
882
+ );
883
+ const f = e.network || v, i = { name: E.Embed, network: f, data: [] };
884
+ if (p(i, "output", () => {
885
+ if (e.data)
886
+ return l([se.OP_RETURN].concat(e.data));
887
+ }), p(i, "data", () => {
888
+ if (!e.output) return;
889
+ const s = m(e.output);
890
+ if (s != null)
891
+ return s.slice(1);
892
+ }), o.validate && e.output) {
893
+ const s = m(e.output);
894
+ if (s[0] !== se.OP_RETURN) throw new TypeError("Output is invalid");
895
+ if (!s.slice(1).every(r.Buffer)) throw new TypeError("Output is invalid");
896
+ if (e.data && !j(e.data, i.data)) throw new TypeError("Data mismatch");
897
+ }
898
+ return Object.assign(i, e);
899
+ }
900
+ const N = _, U = N.OP_RESERVED;
901
+ function Ve(e, o) {
902
+ if (!e.input && !e.output && !(e.pubkeys && e.m !== void 0) && !e.signatures)
903
+ throw new TypeError("Not enough data");
904
+ o = Object.assign({ validate: !0 }, o || {});
905
+ function f(h) {
906
+ return H(h) || (o.allowIncomplete && h === N.OP_0) !== void 0;
907
+ }
908
+ r(
909
+ {
809
910
  network: r.maybe(r.Object),
810
- deploymentVersion: r.maybe(r.Number),
811
- hash160: r.maybe(r.BufferN(20))
911
+ m: r.maybe(r.Number),
912
+ n: r.maybe(r.Number),
913
+ output: r.maybe(r.Buffer),
914
+ pubkeys: r.maybe(r.arrayOf(P)),
915
+ signatures: r.maybe(r.arrayOf(f)),
916
+ input: r.maybe(r.Buffer)
812
917
  },
813
918
  e
814
919
  );
815
- const p = () => {
816
- if (typeof e.deploymentVersion < "u" && typeof e.hash160 < "u") {
817
- if (e.hash160.length !== 20) throw new TypeError("hash160 must be exactly 20 bytes");
818
- if (e.deploymentVersion < 0 || e.deploymentVersion > 255)
819
- throw new TypeError("deploymentVersion must fit in one byte");
820
- return m.concat([m.of(e.deploymentVersion), e.hash160]);
821
- }
822
- }, i = l(() => se(e.address)), s = e.network || P, u = {
823
- name: k.P2OP,
824
- network: s,
825
- deploymentVersion: 0
920
+ const s = {
921
+ network: e.network || v,
922
+ name: E.P2MS
826
923
  };
827
- if (h(u, "program", () => {
828
- if (e.program) return e.program;
829
- const n = p();
830
- if (n) return n;
924
+ let u = [], n = !1;
925
+ function t(h) {
926
+ n || (n = !0, u = m(h), s.m = u[0] - U, s.n = u[u.length - 2] - U, s.pubkeys = u.slice(1, -2));
927
+ }
928
+ if (p(s, "output", () => {
929
+ if (e.m && s.n && e.pubkeys)
930
+ return l(
931
+ [].concat(
932
+ U + e.m,
933
+ e.pubkeys,
934
+ U + s.n,
935
+ N.OP_CHECKMULTISIG
936
+ )
937
+ );
938
+ }), p(s, "m", () => {
939
+ if (s.output)
940
+ return t(s.output), s.m;
941
+ }), p(s, "n", () => {
942
+ if (s.pubkeys)
943
+ return s.pubkeys.length;
944
+ }), p(s, "pubkeys", () => {
945
+ if (e.output)
946
+ return t(e.output), s.pubkeys;
947
+ }), p(s, "signatures", () => {
948
+ if (!e.input) return;
949
+ const h = m(e.input);
950
+ if (h != null)
951
+ return h.slice(1);
952
+ }), p(s, "input", () => {
953
+ if (e.signatures)
954
+ return l([N.OP_0].concat(e.signatures));
955
+ }), p(s, "witness", () => {
956
+ if (s.input)
957
+ return [];
958
+ }), p(s, "name", () => {
959
+ if (!(!s.m || !s.n))
960
+ return `p2ms(${s.m} of ${s.n})`;
961
+ }), o.validate) {
831
962
  if (e.output) {
832
- if (e.output[0] !== ee.OP_16) throw new TypeError("Invalid P2OP script");
833
- let t = 1, f;
834
- if (e.output[1] < 76)
835
- f = e.output[1], t = 2;
836
- else if (e.output[1] === 76)
837
- f = e.output[2], t = 3;
838
- else
839
- throw new TypeError("Unsupported push opcode in P2OP script");
840
- return e.output.subarray(t, t + f);
963
+ if (t(e.output), !r.Number(u[0])) throw new TypeError("Output is invalid");
964
+ if (!r.Number(u[u.length - 2])) throw new TypeError("Output is invalid");
965
+ if (u[u.length - 1] !== N.OP_CHECKMULTISIG)
966
+ throw new TypeError("Output is invalid");
967
+ if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== u.length - 3)
968
+ throw new TypeError("Output is invalid");
969
+ if (!s.pubkeys.every((h) => P(h))) throw new TypeError("Output is invalid");
970
+ if (e.m !== void 0 && e.m !== s.m) throw new TypeError("m mismatch");
971
+ if (e.n !== void 0 && e.n !== s.n) throw new TypeError("n mismatch");
972
+ if (e.pubkeys && !j(e.pubkeys, s.pubkeys))
973
+ throw new TypeError("Pubkeys mismatch");
841
974
  }
842
- if (e.address)
843
- return i().data;
844
- }), h(u, "deploymentVersion", () => {
845
- if (u.program)
846
- return u.program[0];
847
- }), h(u, "hash160", () => {
848
- if (u.program)
849
- return u.program.subarray(1);
850
- }), h(u, "output", () => {
851
- if (u.program)
852
- return y([ee.OP_16, u.program]);
853
- }), h(u, "address", () => {
854
- if (!u.program) return;
855
- if (!s.bech32Opnet)
856
- throw new TypeError("Network does not support opnet");
857
- const n = b.bech32m.toWords(u.program);
858
- return n.unshift(re), b.bech32m.encode(s.bech32Opnet, n);
859
- }), o.validate) {
860
- let n = g.alloc(0);
861
- if (e.address) {
862
- const t = i();
863
- if (s.bech32Opnet !== t.prefix)
864
- throw new TypeError("Invalid prefix or network mismatch");
865
- if (t.version !== re)
866
- throw new TypeError("Invalid witness version for p2op");
867
- if (t.data.length < te || t.data.length > ne)
868
- throw new TypeError("Invalid witness program length");
869
- n = t.data;
975
+ if (e.pubkeys) {
976
+ if (e.n !== void 0 && e.n !== e.pubkeys.length)
977
+ throw new TypeError("Pubkey count mismatch");
978
+ if (s.n = e.pubkeys.length, s.n < s.m) throw new TypeError("Pubkey count cannot be less than m");
870
979
  }
871
- if (e.program) {
872
- if (n.length && !n.equals(e.program)) throw new TypeError("Program mismatch");
873
- n = e.program;
980
+ if (e.signatures) {
981
+ if (e.signatures.length < s.m) throw new TypeError("Not enough signatures provided");
982
+ if (e.signatures.length > s.m) throw new TypeError("Too many signatures provided");
874
983
  }
875
- if (!n.length && e.deploymentVersion !== void 0 && e.hash160 && (n = p()), e.output) {
876
- const t = u.program;
877
- if (n.length && !n.equals(t))
878
- throw new TypeError("Program mismatch (output vs other source)");
879
- n = t;
984
+ if (e.input) {
985
+ if (e.input[0] !== N.OP_0) throw new TypeError("Input is invalid");
986
+ if (s.signatures.length === 0 || !s.signatures.every(f))
987
+ throw new TypeError("Input has invalid signature(s)");
988
+ if (e.signatures && !j(e.signatures, s.signatures))
989
+ throw new TypeError("Signature mismatch");
990
+ if (e.m !== void 0 && e.m !== e.signatures.length)
991
+ throw new TypeError("Signature count mismatch");
880
992
  }
881
- if (n.length < te || n.length > ne)
882
- throw new TypeError(`Witness program must be 2–40 bytes. Was ${n.length} bytes`);
883
- if (e.deploymentVersion !== void 0 && e.deploymentVersion !== n[0])
884
- throw new TypeError("deploymentVersion mismatch");
885
- if (e.hash160 && !e.hash160.equals(n.subarray(1)))
886
- throw new TypeError("hash160 mismatch");
887
993
  }
888
- return Object.assign(u, e);
994
+ return Object.assign(s, e);
889
995
  }
890
- const O = v;
891
- function Ne(e, o) {
892
- if (!e.address && !e.hash && !e.output && !e.pubkey && !e.input)
996
+ const ue = _;
997
+ function Re(e, o) {
998
+ if (!e.input && !e.output && !e.pubkey && !e.input && !e.signature)
893
999
  throw new TypeError("Not enough data");
894
1000
  o = Object.assign({ validate: !0 }, o || {}), r(
895
1001
  {
896
1002
  network: r.maybe(r.Object),
897
- address: r.maybe(r.String),
898
- hash: r.maybe(r.BufferN(20)),
899
- output: r.maybe(r.BufferN(25)),
900
- pubkey: r.maybe(T),
901
- signature: r.maybe(S),
1003
+ output: r.maybe(r.Buffer),
1004
+ pubkey: r.maybe(P),
1005
+ signature: r.maybe(H),
902
1006
  input: r.maybe(r.Buffer)
903
1007
  },
904
1008
  e
905
1009
  );
906
- const p = l(() => {
907
- const n = m.from(x.decode(e.address)), t = n.readUInt8(0), f = n.subarray(1);
908
- return { version: t, hash: f };
909
- }), i = l(() => w(e.input)), s = e.network || P, u = {
910
- name: k.P2PKH,
911
- network: s,
912
- hash: void 0
1010
+ const f = y(() => m(e.input)), i = e.network || v, s = {
1011
+ name: E.P2PK,
1012
+ network: i,
1013
+ pubkey: void 0
913
1014
  };
914
- if (h(u, "address", () => {
915
- if (!u.hash) return;
916
- const n = m.allocUnsafe(21);
917
- return n.writeUInt8(s.pubKeyHash, 0), u.hash.copy(n, 1), x.encode(n);
918
- }), h(u, "hash", () => {
919
- if (e.output) return e.output.subarray(3, 23);
920
- if (e.address) return p().hash;
921
- if (e.pubkey || u.pubkey) return E(e.pubkey || u.pubkey);
922
- }), h(u, "output", () => {
923
- if (u.hash)
924
- return y([
925
- O.OP_DUP,
926
- O.OP_HASH160,
927
- u.hash,
928
- O.OP_EQUALVERIFY,
929
- O.OP_CHECKSIG
930
- ]);
931
- }), h(u, "pubkey", () => {
932
- if (e.input)
933
- return i()[1];
934
- }), h(u, "signature", () => {
1015
+ if (p(s, "output", () => {
1016
+ if (e.pubkey)
1017
+ return l([e.pubkey, ue.OP_CHECKSIG]);
1018
+ }), p(s, "pubkey", () => {
1019
+ if (e.output)
1020
+ return e.output.subarray(1, -1);
1021
+ }), p(s, "signature", () => {
935
1022
  if (e.input)
936
- return i()[0];
937
- }), h(u, "input", () => {
938
- if (!e.pubkey || !e.signature) return;
939
- let n = e.pubkey;
940
- if (e.useHybrid || e.useUncompressed) {
941
- const t = X(e.pubkey);
942
- t && (e.useUncompressed ? n = t.uncompressed : n = t.hybrid);
943
- }
944
- return y([e.signature, n]);
945
- }), h(u, "witness", () => {
946
- if (u.input)
1023
+ return f()[0];
1024
+ }), p(s, "input", () => {
1025
+ if (e.signature)
1026
+ return l([e.signature]);
1027
+ }), p(s, "witness", () => {
1028
+ if (s.input)
947
1029
  return [];
948
1030
  }), o.validate) {
949
- let n = m.from([]);
950
- if (e.address) {
951
- if (p().version !== s.pubKeyHash)
952
- throw new TypeError("Invalid version or Network mismatch");
953
- if (p().hash.length !== 20)
954
- throw new TypeError("Invalid address");
955
- n = p().hash;
956
- }
957
- if (e.hash) {
958
- if (n.length > 0 && !n.equals(e.hash))
959
- throw new TypeError("Hash mismatch");
960
- n = e.hash;
961
- }
962
1031
  if (e.output) {
963
- if (e.output.length !== 25 || e.output[0] !== O.OP_DUP || e.output[1] !== O.OP_HASH160 || e.output[2] !== 20 || e.output[23] !== O.OP_EQUALVERIFY || e.output[24] !== O.OP_CHECKSIG)
1032
+ if (e.output[e.output.length - 1] !== ue.OP_CHECKSIG)
964
1033
  throw new TypeError("Output is invalid");
965
- const t = e.output.subarray(3, 23);
966
- if (n.length > 0 && !n.equals(t)) throw new TypeError("Hash mismatch");
967
- n = t;
968
- }
969
- if (e.pubkey) {
970
- const t = E(e.pubkey);
971
- let f = n.length > 0 && !n.equals(t);
972
- if (f && (e.pubkey.length === 33 && (e.pubkey[0] === 2 || e.pubkey[0] === 3) || e.pubkey.length === 65 && e.pubkey[0] === 4)) {
973
- const d = X(e.pubkey);
974
- if (d) {
975
- const a = E(d.uncompressed);
976
- if (n.equals(a))
977
- f = !1, e.useUncompressed = !0;
978
- else {
979
- const c = E(d.hybrid);
980
- f = !n.equals(c), f || (e.useHybrid = !0);
981
- }
982
- }
983
- }
984
- if (f)
985
- throw new TypeError("Hash mismatch");
986
- n = t;
1034
+ if (!P(s.pubkey)) throw new TypeError("Output pubkey is invalid");
1035
+ if (e.pubkey && !e.pubkey.equals(s.pubkey)) throw new TypeError("Pubkey mismatch");
987
1036
  }
1037
+ if (e.signature && e.input && !e.input.equals(s.input))
1038
+ throw new TypeError("Signature mismatch");
988
1039
  if (e.input) {
989
- const t = i();
990
- if (t.length !== 2) throw new TypeError("Input is invalid");
991
- if (!S(t[0]))
1040
+ if (f().length !== 1) throw new TypeError("Input is invalid");
1041
+ if (!H(s.signature))
992
1042
  throw new TypeError("Input has invalid signature");
993
- if (!T(t[1])) throw new TypeError("Input has invalid pubkey");
994
- if (e.signature && !e.signature.equals(t[0]))
995
- throw new TypeError("Signature mismatch");
996
- if (e.pubkey && !e.pubkey.equals(t[1]))
997
- throw new TypeError("Pubkey mismatch");
998
- const f = E(t[1]);
999
- if (n.length > 0 && !n.equals(f)) throw new TypeError("Hash mismatch (input)");
1000
1043
  }
1001
1044
  }
1002
- return Object.assign(u, e);
1045
+ return Object.assign(s, e);
1003
1046
  }
1004
- var k = /* @__PURE__ */ ((e) => (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", e))(k || {});
1005
- const Re = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1047
+ const Ce = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1006
1048
  __proto__: null,
1007
- LEAF_VERSION_TAPSCRIPT: M,
1008
- MAX_TAPTREE_DEPTH: le,
1009
- PaymentType: k,
1010
- findScriptPath: q,
1011
- p2data: ke,
1012
- p2ms: Ee,
1013
- p2op: He,
1014
- p2pk: Te,
1015
- p2pkh: Ne,
1016
- p2sh: Pe,
1017
- p2tr: Oe,
1018
- p2wpkh: Se,
1019
- p2wsh: Ie,
1020
- prop: h,
1021
- rootHashFromPath: L,
1022
- tapTweakHash: oe,
1023
- tapleafHash: H,
1024
- toHashTree: A,
1025
- tweakKey: j,
1026
- value: l
1049
+ LEAF_VERSION_TAPSCRIPT: G,
1050
+ MAX_TAPTREE_DEPTH: Oe,
1051
+ PaymentType: E,
1052
+ findScriptPath: x,
1053
+ p2data: qe,
1054
+ p2ms: Ve,
1055
+ p2op: ge,
1056
+ p2pk: Re,
1057
+ p2pkh: ke,
1058
+ p2sh: Ee,
1059
+ p2tr: He,
1060
+ p2wpkh: Be,
1061
+ p2wsh: je,
1062
+ prop: p,
1063
+ rootHashFromPath: F,
1064
+ tapleafHash: B,
1065
+ toHashTree: W,
1066
+ tweakKey: V,
1067
+ value: y
1027
1068
  }, Symbol.toStringTag, { value: "Module" }));
1028
1069
  export {
1029
- M as L,
1030
- le as M,
1031
- k as P,
1032
- Pe as a,
1033
- Se as b,
1034
- Ie as c,
1035
- Oe as d,
1036
- He as e,
1037
- H as f,
1038
- Te as g,
1039
- Ee as h,
1040
- Re as i,
1041
- A as j,
1042
- q as k,
1043
- oe as l,
1044
- Ne as p,
1045
- L as r,
1046
- j as t
1070
+ G as L,
1071
+ Oe as M,
1072
+ E as P,
1073
+ Ee as a,
1074
+ Be as b,
1075
+ je as c,
1076
+ He as d,
1077
+ ge as e,
1078
+ Re as f,
1079
+ Ve as g,
1080
+ B as h,
1081
+ Ce as i,
1082
+ We as j,
1083
+ W as k,
1084
+ x as l,
1085
+ _e as m,
1086
+ ke as p,
1087
+ F as r,
1088
+ V as t
1047
1089
  };