@btc-vision/bitcoin 6.5.3 → 6.5.4

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 (92) 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-Ngcm87h_.js} +670 -626
  5. package/browser/chunks/{psbt-BIwOrKer.js → psbt-CcM4rw3q.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 -2
  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 +2 -2
  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 +3 -2
  92. package/test/integration/taproot.spec.ts +2 -1
@@ -1,265 +1,236 @@
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 oe, i as L, a as Y } from "./psbt-CcM4rw3q.js";
2
+ import { o as _, c as l, i as H, d as m, b as fe, e as pe, 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 G, s as Q } 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);
70
- return this[o] = i, i;
11
+ const u = f.call(this);
12
+ return this[o] = u, u;
71
13
  },
72
- set(i) {
14
+ set(u) {
73
15
  Object.defineProperty(this, o, {
74
16
  configurable: !0,
75
17
  enumerable: !0,
76
- value: i,
18
+ value: u,
77
19
  writable: !0
78
20
  });
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 z = _, Z = 16, J = 2, ee = 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
+ }, u = y(() => oe(e.address)), s = e.network || v, i = {
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(i, "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] !== z.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 u().data;
72
+ }), p(i, "deploymentVersion", () => {
73
+ if (i.program)
74
+ return i.program[0];
75
+ }), p(i, "hash160", () => {
76
+ if (i.program)
77
+ return i.program.subarray(1);
78
+ }), p(i, "output", () => {
79
+ if (i.program)
80
+ return l([z.OP_16, i.program]);
81
+ }), p(i, "address", () => {
82
+ if (!i.program) return;
83
+ if (!s.bech32Opnet)
84
+ throw new TypeError("Network does not support opnet");
85
+ const n = g.bech32m.toWords(i.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 = u();
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 < J || t.data.length > ee)
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 = i.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 < J || n.length > ee)
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(i, 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
+ }), u = y(() => m(e.input)), s = e.network || v, i = {
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(i, "address", () => {
143
+ if (!i.hash) return;
144
+ const n = c.allocUnsafe(21);
145
+ return n.writeUInt8(s.pubKeyHash, 0), i.hash.copy(n, 1), L.encode(n);
146
+ }), p(i, "hash", () => {
147
+ if (e.output) return e.output.subarray(3, 23);
148
+ if (e.address) return f().hash;
149
+ if (e.pubkey || i.pubkey) return T(e.pubkey || i.pubkey);
150
+ }), p(i, "output", () => {
151
+ if (i.hash)
152
+ return l([
153
+ S.OP_DUP,
154
+ S.OP_HASH160,
155
+ i.hash,
156
+ S.OP_EQUALVERIFY,
157
+ S.OP_CHECKSIG
158
+ ]);
159
+ }), p(i, "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 u()[1];
162
+ }), p(i, "signature", () => {
163
+ if (e.input)
164
+ return u()[0];
165
+ }), p(i, "input", () => {
166
+ if (!e.pubkey || !e.signature) return;
167
+ let n = e.pubkey;
168
+ if (e.useHybrid || e.useUncompressed) {
169
+ const t = Y(e.pubkey);
170
+ t && (e.useUncompressed ? n = t.uncompressed : n = t.hybrid);
171
+ }
172
+ return l([e.signature, n]);
173
+ }), p(i, "witness", () => {
174
+ if (i.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 = Y(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 = u();
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(i, 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);
284
- 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];
253
+ let f = e.network;
254
+ f || (f = e.redeem && e.redeem.network || v);
255
+ const u = {
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
+ }), i = y(() => m(e.input)), n = y(() => {
262
+ const t = i(), 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", () => {
300
- 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", () => {
270
+ if (p(u, "address", () => {
271
+ if (!u.hash) return;
272
+ const t = c.allocUnsafe(21);
273
+ return t.writeUInt8(u.network.scriptHash, 0), u.hash.copy(t, 1), L.encode(t);
274
+ }), p(u, "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", () => {
308
- if (i.hash)
309
- return y([B.OP_HASH160, i.hash, B.OP_EQUAL]);
310
- }), h(i, "redeem", () => {
277
+ if (u.redeem && u.redeem.output) return T(u.redeem.output);
278
+ }), p(u, "output", () => {
279
+ if (u.hash)
280
+ return l([q.OP_HASH160, u.hash, q.OP_EQUAL]);
281
+ }), p(u, "redeem", () => {
311
282
  if (e.input)
312
283
  return n();
313
- }), h(i, "input", () => {
284
+ }), p(u, "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", () => {
319
- if (i.redeem && i.redeem.witness) return i.redeem.witness;
320
- if (i.input) return [];
321
- }), h(i, "name", () => {
289
+ }), p(u, "witness", () => {
290
+ if (u.redeem && u.redeem.witness) return u.redeem.witness;
291
+ if (u.input) return [];
292
+ }), p(u, "name", () => {
322
293
  const t = ["p2sh"];
323
- return i.redeem !== void 0 && i.redeem.name !== void 0 && t.push(i.redeem.name), t.join("-");
294
+ return u.redeem !== void 0 && u.redeem.name !== void 0 && t.push(u.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,46 +305,46 @@ 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 (fe(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 (!pe(I))
365
336
  throw new TypeError("Non push-only scriptSig");
366
337
  }
367
338
  }
368
339
  };
369
340
  if (e.input) {
370
- const d = u();
341
+ const d = i();
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
- return Object.assign(i, e);
361
+ return Object.assign(u, e);
362
+ }
363
+ const R = {};
364
+ function We(e) {
365
+ e ? e !== R.eccLib && (Te(e), R.eccLib = e) : R.eccLib = e;
366
+ }
367
+ function X() {
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
+ });
391
384
  }
392
- const Y = v, z = 1, ve = 80;
393
- function Oe(e, o) {
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
+ ], D = 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 u = o;
415
+ for (let s = 0; s < f; s++) {
416
+ const i = e.subarray(33 + 32 * s, 65 + 32 * s);
417
+ u.compare(i) < 0 ? u = M(u, i) : u = M(i, u);
418
+ }
419
+ return u;
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, i) => s.hash.compare(i.hash));
425
+ const [f, u] = o;
426
+ return {
427
+ hash: M(f.hash, u.hash),
428
+ left: f,
429
+ right: u
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 u = x(e.right, o);
437
+ if (u !== void 0) return [...u, e.left.hash];
438
+ } else if (e.hash.equals(o))
439
+ return [];
440
+ }
441
+ function B(e) {
442
+ const o = e.version || D;
443
+ return G(
444
+ "TapLeaf",
445
+ k.concat([k.from([o]), Se(e.output)])
446
+ );
447
+ }
448
+ function _e(e, o) {
449
+ return G("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), u = X().xOnlyPointAddTweak(e, f);
454
+ return !u || u.xOnlyPubkey === null ? null : {
455
+ parity: u.parity,
456
+ x: k.from(u.xOnlyPubkey)
457
+ };
458
+ }
459
+ function M(e, o) {
460
+ return G("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 re = _, te = 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(() => oe(e.address)), u = 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,
429
- network: u
501
+ }), i = e.network || v, n = {
502
+ name: E.P2TR,
503
+ network: i
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(te), g.bech32m.encode(i.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 = u();
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", () => {
450
- const t = i();
522
+ return l([re.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 : D), p(n, "redeem", () => {
524
+ const t = u();
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
- const t = i();
541
+ const t = u();
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
- const t = i();
545
+ const t = u();
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 (i && i.bech32 !== f().prefix)
497
571
  throw new TypeError("Invalid prefix or Network mismatch");
498
- if (p().version !== z)
572
+ if (f().version !== te)
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,42 +580,42 @@ 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] !== re.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))
595
+ if (t && t.length && !X().isXOnlyPoint(t))
522
596
  throw new TypeError("Invalid pubkey for p2tr");
523
- const f = s();
524
- if (e.hash && f && !e.hash.equals(f.hash))
597
+ const h = s();
598
+ if (e.hash && h && !e.hash.equals(h.hash))
525
599
  throw new TypeError("Hash mismatch");
526
- if (e.redeem && e.redeem.output && f) {
527
- const a = H({
600
+ if (e.redeem && e.redeem.output && h) {
601
+ const a = B({
528
602
  output: e.redeem.output,
529
603
  version: n.redeemVersion
530
604
  });
531
- if (!q(f, a))
605
+ if (!x(h, a))
532
606
  throw new TypeError("Redeem script not in tree");
533
607
  }
534
- const d = i();
608
+ const d = u();
535
609
  if (e.redeem && n.redeem) {
536
610
  if (e.redeem.redeemVersion && e.redeem.redeemVersion !== n.redeem.redeemVersion)
537
611
  throw new TypeError("Redeem.redeemVersion and witness mismatch");
538
612
  if (e.redeem.output) {
539
- if (w(e.redeem.output).length === 0)
613
+ if (m(e.redeem.output).length === 0)
540
614
  throw new TypeError("Redeem.output is invalid");
541
615
  if (n.redeem.output && !e.redeem.output.equals(n.redeem.output))
542
616
  throw new TypeError("Redeem.output and witness mismatch");
543
617
  }
544
- if (e.redeem.witness && n.redeem.witness && !N(e.redeem.witness, n.redeem.witness))
618
+ if (e.redeem.witness && n.redeem.witness && !j(e.redeem.witness, n.redeem.witness))
545
619
  throw new TypeError("Redeem.witness and witness mismatch");
546
620
  }
547
621
  if (d && d.length)
@@ -558,29 +632,29 @@ function Oe(e, o) {
558
632
  throw new TypeError(
559
633
  `The control-block length of ${a.length} is incorrect!`
560
634
  );
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(_))
635
+ const w = (a.length - 33) / 32;
636
+ if (w > 128)
637
+ throw new TypeError(`The script path is too long. Got ${w}, expected max 128.`);
638
+ const I = a.subarray(1, 33);
639
+ if (e.internalPubkey && !e.internalPubkey.equals(I))
566
640
  throw new TypeError("Internal pubkey mismatch");
567
- if (!K().isXOnlyPoint(_))
641
+ if (!X().isXOnlyPoint(I))
568
642
  throw new TypeError("Invalid internalPubkey for p2tr witness");
569
- const he = a[0] & W, pe = d[d.length - 2], fe = H({
570
- output: pe,
643
+ const he = a[0] & $, de = d[d.length - 2], ae = B({
644
+ output: de,
571
645
  version: he
572
- }), de = L(a, fe), U = j(_, de);
573
- if (!U)
646
+ }), ce = F(a, ae), C = V(I, ce);
647
+ if (!C)
574
648
  throw new TypeError("Invalid outputKey for p2tr witness");
575
- if (t.length && !t.equals(U.x))
649
+ if (t.length && !t.equals(C.x))
576
650
  throw new TypeError("Pubkey mismatch for p2tr witness");
577
- if (U.parity !== (a[0] & 1)) throw new Error("Incorrect parity");
651
+ if (C.parity !== (a[0] & 1)) throw new Error("Incorrect parity");
578
652
  }
579
653
  }
580
654
  return Object.assign(n, e);
581
655
  }
582
- const Z = v, _e = m.alloc(0);
583
- function Se(e, o) {
656
+ const ne = _, Ne = c.alloc(0);
657
+ function Be(e, o) {
584
658
  if (!e.address && !e.hash && !e.output && !e.pubkey && !e.witness)
585
659
  throw new TypeError("Not enough data");
586
660
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -590,93 +664,93 @@ function Se(e, o) {
590
664
  input: r.maybe(r.BufferN(0)),
591
665
  network: r.maybe(r.Object),
592
666
  output: r.maybe(r.BufferN(22)),
593
- pubkey: r.maybe(T),
594
- signature: r.maybe(S),
667
+ pubkey: r.maybe(P),
668
+ signature: r.maybe(H),
595
669
  witness: r.maybe(r.arrayOf(r.Buffer))
596
670
  },
597
671
  e
598
672
  );
599
- const p = l(() => {
600
- const u = b.bech32.decode(e.address), n = u.words.shift(), t = b.bech32.fromWords(u.words);
673
+ const f = y(() => {
674
+ const i = g.bech32.decode(e.address), n = i.words.shift(), t = g.bech32.fromWords(i.words);
601
675
  return {
602
676
  version: n,
603
- prefix: u.prefix,
604
- data: m.from(t)
677
+ prefix: i.prefix,
678
+ data: c.from(t)
605
679
  };
606
- }), i = e.network || P, s = {
607
- name: k.P2WPKH,
608
- network: i
680
+ }), u = e.network || v, s = {
681
+ name: E.P2WPKH,
682
+ network: u
609
683
  };
610
- if (h(s, "address", () => {
684
+ if (p(s, "address", () => {
611
685
  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", () => {
686
+ const i = g.bech32.toWords(s.hash);
687
+ return i.unshift(0), g.bech32.encode(u.bech32, i);
688
+ }), p(s, "hash", () => {
615
689
  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", () => {
690
+ if (e.address) return f().data;
691
+ if (e.pubkey || s.pubkey) return T(e.pubkey || s.pubkey);
692
+ }), p(s, "output", () => {
619
693
  if (s.hash)
620
- return y([Z.OP_0, s.hash]);
621
- }), h(s, "pubkey", () => {
694
+ return l([ne.OP_0, s.hash]);
695
+ }), p(s, "pubkey", () => {
622
696
  if (e.pubkey) return e.pubkey;
623
697
  if (e.witness)
624
698
  return e.witness[1];
625
- }), h(s, "signature", () => {
699
+ }), p(s, "signature", () => {
626
700
  if (e.witness)
627
701
  return e.witness[0];
628
- }), h(s, "input", () => {
702
+ }), p(s, "input", () => {
629
703
  if (s.witness)
630
- return _e;
631
- }), h(s, "witness", () => {
704
+ return Ne;
705
+ }), p(s, "witness", () => {
632
706
  if (e.pubkey && e.signature)
633
707
  return [e.signature, e.pubkey];
634
708
  }), o.validate) {
635
- let u = m.from([]);
709
+ let i = c.from([]);
636
710
  if (e.address) {
637
- if (i && i.bech32 !== p().prefix)
711
+ if (u && u.bech32 !== f().prefix)
638
712
  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;
713
+ if (f().version !== 0) throw new TypeError("Invalid address version");
714
+ if (f().data.length !== 20) throw new TypeError("Invalid address data");
715
+ i = f().data;
642
716
  }
643
717
  if (e.hash) {
644
- if (u.length > 0 && !u.equals(e.hash)) throw new TypeError("Hash mismatch");
645
- u = e.hash;
718
+ if (i.length > 0 && !i.equals(e.hash)) throw new TypeError("Hash mismatch");
719
+ i = e.hash;
646
720
  }
647
721
  if (e.output) {
648
- if (e.output.length !== 22 || e.output[0] !== Z.OP_0 || e.output[1] !== 20)
722
+ if (e.output.length !== 22 || e.output[0] !== ne.OP_0 || e.output[1] !== 20)
649
723
  throw new TypeError("Output is invalid");
650
- if (u.length > 0 && !u.equals(e.output.subarray(2)))
724
+ if (i.length > 0 && !i.equals(e.output.subarray(2)))
651
725
  throw new TypeError("Hash mismatch");
652
- u = e.output.subarray(2);
726
+ i = e.output.subarray(2);
653
727
  }
654
728
  if (e.pubkey) {
655
- const n = E(e.pubkey);
656
- if (u.length > 0 && !u.equals(n)) throw new TypeError("Hash mismatch");
657
- if (u = n, !T(e.pubkey) || e.pubkey.length !== 33)
729
+ const n = T(e.pubkey);
730
+ if (i.length > 0 && !i.equals(n)) throw new TypeError("Hash mismatch");
731
+ if (i = n, !P(e.pubkey) || e.pubkey.length !== 33)
658
732
  throw new TypeError("Invalid pubkey for p2wpkh");
659
733
  }
660
734
  if (e.witness) {
661
735
  if (e.witness.length !== 2) throw new TypeError("Witness is invalid");
662
- if (!S(e.witness[0]))
736
+ if (!H(e.witness[0]))
663
737
  throw new TypeError("Witness has invalid signature");
664
- if (!T(e.witness[1]) || e.witness[1].length !== 33)
738
+ if (!P(e.witness[1]) || e.witness[1].length !== 33)
665
739
  throw new TypeError("Witness has invalid pubkey");
666
740
  if (e.signature && !e.signature.equals(e.witness[0]))
667
741
  throw new TypeError("Signature mismatch");
668
742
  if (e.pubkey && !e.pubkey.equals(e.witness[1])) throw new TypeError("Pubkey mismatch");
669
- const n = E(e.witness[1]);
670
- if (u.length > 0 && !u.equals(n)) throw new TypeError("Hash mismatch");
743
+ const n = T(e.witness[1]);
744
+ if (i.length > 0 && !i.equals(n)) throw new TypeError("Hash mismatch");
671
745
  }
672
746
  }
673
747
  return Object.assign(s, e);
674
748
  }
675
- const J = v, $ = m.alloc(0);
676
- function R(e) {
677
- return !!(m.isBuffer(e) && e.length === 65 && e[0] === 4 && T(e));
749
+ const se = _, K = c.alloc(0);
750
+ function A(e) {
751
+ return !!(c.isBuffer(e) && e.length === 65 && e[0] === 4 && P(e));
678
752
  }
679
- function Ie(e, o) {
753
+ function je(e, o) {
680
754
  if (!e.address && !e.hash && !e.output && !e.redeem && !e.witness)
681
755
  throw new TypeError("Not enough data");
682
756
  o = Object.assign({ validate: !0 }, o || {}), r(
@@ -696,66 +770,66 @@ function Ie(e, o) {
696
770
  },
697
771
  e
698
772
  );
699
- const p = l(() => {
700
- const n = b.bech32.decode(e.address), t = n.words.shift(), f = b.bech32.fromWords(n.words);
773
+ const f = y(() => {
774
+ const n = g.bech32.decode(e.address), t = n.words.shift(), h = g.bech32.fromWords(n.words);
701
775
  return {
702
776
  version: t,
703
777
  prefix: n.prefix,
704
- data: m.from(f)
778
+ data: c.from(h)
705
779
  };
706
- }), i = l(() => w(e.redeem.input));
780
+ }), u = y(() => m(e.redeem.input));
707
781
  let s = e.network;
708
- s || (s = e.redeem && e.redeem.network || P);
709
- const u = {
782
+ s || (s = e.redeem && e.redeem.network || v);
783
+ const i = {
710
784
  network: s,
711
- name: k.P2WSH
785
+ name: E.P2WSH
712
786
  };
713
- if (h(u, "address", () => {
714
- 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
+ if (p(i, "address", () => {
788
+ if (!i.hash) return;
789
+ const n = g.bech32.toWords(i.hash);
790
+ return n.unshift(0), g.bech32.encode(s.bech32, n);
791
+ }), p(i, "hash", () => {
718
792
  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", () => {
722
- if (u.hash)
723
- return y([J.OP_0, u.hash]);
724
- }), h(u, "redeem", () => {
793
+ if (e.address) return f().data;
794
+ if (i.redeem && i.redeem.output) return Q(i.redeem.output);
795
+ }), p(i, "output", () => {
796
+ if (i.hash)
797
+ return l([se.OP_0, i.hash]);
798
+ }), p(i, "redeem", () => {
725
799
  if (e.witness)
726
800
  return {
727
801
  output: e.witness[e.witness.length - 1],
728
- input: $,
802
+ input: K,
729
803
  witness: e.witness.slice(0, -1)
730
804
  };
731
- }), h(u, "input", () => {
732
- if (u.witness)
733
- return $;
734
- }), h(u, "witness", () => {
805
+ }), p(i, "input", () => {
806
+ if (i.witness)
807
+ return K;
808
+ }), p(i, "witness", () => {
735
809
  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);
810
+ const n = me(u());
811
+ return i.redeem = Object.assign({ witness: n }, e.redeem), i.redeem.input = K, [].concat(n, e.redeem.output);
738
812
  }
739
813
  if (e.redeem && e.redeem.output && e.redeem.witness)
740
814
  return [].concat(e.redeem.witness, e.redeem.output);
741
- }), h(u, "name", () => {
815
+ }), p(i, "name", () => {
742
816
  const n = ["p2wsh"];
743
- return u.redeem !== void 0 && u.redeem.name !== void 0 && n.push(u.redeem.name), n.join("-");
817
+ return i.redeem !== void 0 && i.redeem.name !== void 0 && n.push(i.redeem.name), n.join("-");
744
818
  }), o.validate) {
745
- let n = m.from([]);
819
+ let n = c.from([]);
746
820
  if (e.address) {
747
- if (p().prefix !== s.bech32)
821
+ if (f().prefix !== s.bech32)
748
822
  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;
823
+ if (f().version !== 0) throw new TypeError("Invalid address version");
824
+ if (f().data.length !== 32) throw new TypeError("Invalid address data");
825
+ n = f().data;
752
826
  }
753
827
  if (e.hash) {
754
828
  if (n.length > 0 && !n.equals(e.hash)) throw new TypeError("Hash mismatch");
755
829
  n = e.hash;
756
830
  }
757
831
  if (e.output) {
758
- if (e.output.length !== 34 || e.output[0] !== J.OP_0 || e.output[1] !== 32)
832
+ if (e.output.length !== 34 || e.output[0] !== se.OP_0 || e.output[1] !== 32)
759
833
  throw new TypeError("Output is invalid");
760
834
  const t = e.output.subarray(2);
761
835
  if (n.length > 0 && !n.equals(t)) throw new TypeError("Hash mismatch");
@@ -767,281 +841,251 @@ function Ie(e, o) {
767
841
  if (e.redeem.input && e.redeem.input.length > 0 && e.redeem.witness && e.redeem.witness.length > 0)
768
842
  throw new TypeError("Ambiguous witness source");
769
843
  if (e.redeem.output) {
770
- const t = w(e.redeem.output);
844
+ const t = m(e.redeem.output);
771
845
  if (!t || t.length < 1)
772
846
  throw new TypeError("Redeem.output is invalid");
773
847
  if (e.redeem.output.byteLength > 3600)
774
848
  throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");
775
- if (ue(t) > 201)
849
+ if (fe(t) > 201)
776
850
  throw new TypeError(
777
851
  "Redeem.output unspendable with more than 201 non-push ops"
778
852
  );
779
- const f = G(e.redeem.output);
780
- if (n.length > 0 && !n.equals(f)) throw new TypeError("Hash mismatch");
781
- n = f;
853
+ const h = Q(e.redeem.output);
854
+ if (n.length > 0 && !n.equals(h)) throw new TypeError("Hash mismatch");
855
+ n = h;
782
856
  }
783
- if (e.redeem.input && !ie(i()))
857
+ if (e.redeem.input && !pe(u()))
784
858
  throw new TypeError("Non push-only scriptSig");
785
- if (e.witness && e.redeem.witness && !N(e.witness, e.redeem.witness))
859
+ if (e.witness && e.redeem.witness && !j(e.witness, e.redeem.witness))
786
860
  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))
861
+ if (e.redeem.input && u().some(A) || e.redeem.output && (m(e.redeem.output) || []).some(A))
788
862
  throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey");
789
863
  }
790
864
  if (e.witness && e.witness.length > 0) {
791
865
  const t = e.witness[e.witness.length - 1];
792
866
  if (e.redeem && e.redeem.output && !e.redeem.output.equals(t))
793
867
  throw new TypeError("Witness and redeem.output mismatch");
794
- if (e.witness.some(R) || (w(t) || []).some(R))
868
+ if (e.witness.some(A) || (m(t) || []).some(A))
795
869
  throw new TypeError("Witness contains uncompressed pubkey");
796
870
  }
797
871
  }
798
- return Object.assign(u, e);
872
+ return Object.assign(i, e);
799
873
  }
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");
874
+ const ie = _;
875
+ function qe(e, o) {
876
+ if (!e.data && !e.output) throw new TypeError("Not enough data");
804
877
  o = Object.assign({ validate: !0 }, o || {}), r(
805
878
  {
806
- address: r.maybe(r.String),
879
+ network: r.maybe(r.Object),
807
880
  output: r.maybe(r.Buffer),
808
- program: r.maybe(r.Buffer),
881
+ data: r.maybe(r.arrayOf(r.Buffer))
882
+ },
883
+ e
884
+ );
885
+ const f = e.network || v, u = { name: E.Embed, network: f, data: [] };
886
+ if (p(u, "output", () => {
887
+ if (e.data)
888
+ return l([ie.OP_RETURN].concat(e.data));
889
+ }), p(u, "data", () => {
890
+ if (!e.output) return;
891
+ const s = m(e.output);
892
+ if (s != null)
893
+ return s.slice(1);
894
+ }), o.validate && e.output) {
895
+ const s = m(e.output);
896
+ if (s[0] !== ie.OP_RETURN) throw new TypeError("Output is invalid");
897
+ if (!s.slice(1).every(r.Buffer)) throw new TypeError("Output is invalid");
898
+ if (e.data && !j(e.data, u.data)) throw new TypeError("Data mismatch");
899
+ }
900
+ return Object.assign(u, e);
901
+ }
902
+ const N = _, U = N.OP_RESERVED;
903
+ function Ve(e, o) {
904
+ if (!e.input && !e.output && !(e.pubkeys && e.m !== void 0) && !e.signatures)
905
+ throw new TypeError("Not enough data");
906
+ o = Object.assign({ validate: !0 }, o || {});
907
+ function f(h) {
908
+ return H(h) || (o.allowIncomplete && h === N.OP_0) !== void 0;
909
+ }
910
+ r(
911
+ {
809
912
  network: r.maybe(r.Object),
810
- deploymentVersion: r.maybe(r.Number),
811
- hash160: r.maybe(r.BufferN(20))
913
+ m: r.maybe(r.Number),
914
+ n: r.maybe(r.Number),
915
+ output: r.maybe(r.Buffer),
916
+ pubkeys: r.maybe(r.arrayOf(P)),
917
+ signatures: r.maybe(r.arrayOf(f)),
918
+ input: r.maybe(r.Buffer)
812
919
  },
813
920
  e
814
921
  );
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
922
+ const s = {
923
+ network: e.network || v,
924
+ name: E.P2MS
826
925
  };
827
- if (h(u, "program", () => {
828
- if (e.program) return e.program;
829
- const n = p();
830
- if (n) return n;
926
+ let i = [], n = !1;
927
+ function t(h) {
928
+ n || (n = !0, i = m(h), s.m = i[0] - U, s.n = i[i.length - 2] - U, s.pubkeys = i.slice(1, -2));
929
+ }
930
+ if (p(s, "output", () => {
931
+ if (e.m && s.n && e.pubkeys)
932
+ return l(
933
+ [].concat(
934
+ U + e.m,
935
+ e.pubkeys,
936
+ U + s.n,
937
+ N.OP_CHECKMULTISIG
938
+ )
939
+ );
940
+ }), p(s, "m", () => {
941
+ if (s.output)
942
+ return t(s.output), s.m;
943
+ }), p(s, "n", () => {
944
+ if (s.pubkeys)
945
+ return s.pubkeys.length;
946
+ }), p(s, "pubkeys", () => {
947
+ if (e.output)
948
+ return t(e.output), s.pubkeys;
949
+ }), p(s, "signatures", () => {
950
+ if (!e.input) return;
951
+ const h = m(e.input);
952
+ if (h != null)
953
+ return h.slice(1);
954
+ }), p(s, "input", () => {
955
+ if (e.signatures)
956
+ return l([N.OP_0].concat(e.signatures));
957
+ }), p(s, "witness", () => {
958
+ if (s.input)
959
+ return [];
960
+ }), p(s, "name", () => {
961
+ if (!(!s.m || !s.n))
962
+ return `p2ms(${s.m} of ${s.n})`;
963
+ }), o.validate) {
831
964
  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);
965
+ if (t(e.output), !r.Number(i[0])) throw new TypeError("Output is invalid");
966
+ if (!r.Number(i[i.length - 2])) throw new TypeError("Output is invalid");
967
+ if (i[i.length - 1] !== N.OP_CHECKMULTISIG)
968
+ throw new TypeError("Output is invalid");
969
+ if (s.m <= 0 || s.n > 16 || s.m > s.n || s.n !== i.length - 3)
970
+ throw new TypeError("Output is invalid");
971
+ if (!s.pubkeys.every((h) => P(h))) throw new TypeError("Output is invalid");
972
+ if (e.m !== void 0 && e.m !== s.m) throw new TypeError("m mismatch");
973
+ if (e.n !== void 0 && e.n !== s.n) throw new TypeError("n mismatch");
974
+ if (e.pubkeys && !j(e.pubkeys, s.pubkeys))
975
+ throw new TypeError("Pubkeys mismatch");
841
976
  }
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;
977
+ if (e.pubkeys) {
978
+ if (e.n !== void 0 && e.n !== e.pubkeys.length)
979
+ throw new TypeError("Pubkey count mismatch");
980
+ if (s.n = e.pubkeys.length, s.n < s.m) throw new TypeError("Pubkey count cannot be less than m");
870
981
  }
871
- if (e.program) {
872
- if (n.length && !n.equals(e.program)) throw new TypeError("Program mismatch");
873
- n = e.program;
982
+ if (e.signatures) {
983
+ if (e.signatures.length < s.m) throw new TypeError("Not enough signatures provided");
984
+ if (e.signatures.length > s.m) throw new TypeError("Too many signatures provided");
874
985
  }
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;
986
+ if (e.input) {
987
+ if (e.input[0] !== N.OP_0) throw new TypeError("Input is invalid");
988
+ if (s.signatures.length === 0 || !s.signatures.every(f))
989
+ throw new TypeError("Input has invalid signature(s)");
990
+ if (e.signatures && !j(e.signatures, s.signatures))
991
+ throw new TypeError("Signature mismatch");
992
+ if (e.m !== void 0 && e.m !== e.signatures.length)
993
+ throw new TypeError("Signature count mismatch");
880
994
  }
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
995
  }
888
- return Object.assign(u, e);
996
+ return Object.assign(s, e);
889
997
  }
890
- const O = v;
891
- function Ne(e, o) {
892
- if (!e.address && !e.hash && !e.output && !e.pubkey && !e.input)
998
+ const ue = _;
999
+ function Re(e, o) {
1000
+ if (!e.input && !e.output && !e.pubkey && !e.input && !e.signature)
893
1001
  throw new TypeError("Not enough data");
894
1002
  o = Object.assign({ validate: !0 }, o || {}), r(
895
1003
  {
896
1004
  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),
1005
+ output: r.maybe(r.Buffer),
1006
+ pubkey: r.maybe(P),
1007
+ signature: r.maybe(H),
902
1008
  input: r.maybe(r.Buffer)
903
1009
  },
904
1010
  e
905
1011
  );
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
1012
+ const f = y(() => m(e.input)), u = e.network || v, s = {
1013
+ name: E.P2PK,
1014
+ network: u,
1015
+ pubkey: void 0
913
1016
  };
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", () => {
1017
+ if (p(s, "output", () => {
1018
+ if (e.pubkey)
1019
+ return l([e.pubkey, ue.OP_CHECKSIG]);
1020
+ }), p(s, "pubkey", () => {
1021
+ if (e.output)
1022
+ return e.output.subarray(1, -1);
1023
+ }), p(s, "signature", () => {
935
1024
  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)
1025
+ return f()[0];
1026
+ }), p(s, "input", () => {
1027
+ if (e.signature)
1028
+ return l([e.signature]);
1029
+ }), p(s, "witness", () => {
1030
+ if (s.input)
947
1031
  return [];
948
1032
  }), 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
1033
  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)
1034
+ if (e.output[e.output.length - 1] !== ue.OP_CHECKSIG)
964
1035
  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;
1036
+ if (!P(s.pubkey)) throw new TypeError("Output pubkey is invalid");
1037
+ if (e.pubkey && !e.pubkey.equals(s.pubkey)) throw new TypeError("Pubkey mismatch");
987
1038
  }
1039
+ if (e.signature && e.input && !e.input.equals(s.input))
1040
+ throw new TypeError("Signature mismatch");
988
1041
  if (e.input) {
989
- const t = i();
990
- if (t.length !== 2) throw new TypeError("Input is invalid");
991
- if (!S(t[0]))
1042
+ if (f().length !== 1) throw new TypeError("Input is invalid");
1043
+ if (!H(s.signature))
992
1044
  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
1045
  }
1001
1046
  }
1002
- return Object.assign(u, e);
1047
+ return Object.assign(s, e);
1003
1048
  }
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({
1049
+ const Ce = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1006
1050
  __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
1051
+ LEAF_VERSION_TAPSCRIPT: D,
1052
+ MAX_TAPTREE_DEPTH: Oe,
1053
+ PaymentType: E,
1054
+ findScriptPath: x,
1055
+ p2data: qe,
1056
+ p2ms: Ve,
1057
+ p2op: ge,
1058
+ p2pk: Re,
1059
+ p2pkh: ke,
1060
+ p2sh: Ee,
1061
+ p2tr: He,
1062
+ p2wpkh: Be,
1063
+ p2wsh: je,
1064
+ prop: p,
1065
+ rootHashFromPath: F,
1066
+ tapleafHash: B,
1067
+ toHashTree: W,
1068
+ tweakKey: V,
1069
+ value: y
1027
1070
  }, Symbol.toStringTag, { value: "Module" }));
1028
1071
  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
1072
+ D as L,
1073
+ Oe as M,
1074
+ E as P,
1075
+ Ee as a,
1076
+ Be as b,
1077
+ je as c,
1078
+ He as d,
1079
+ ge as e,
1080
+ Re as f,
1081
+ Ve as g,
1082
+ B as h,
1083
+ Ce as i,
1084
+ We as j,
1085
+ W as k,
1086
+ x as l,
1087
+ _e as m,
1088
+ ke as p,
1089
+ F as r,
1090
+ V as t
1047
1091
  };