@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.
- package/browser/address.d.ts +2 -6
- package/browser/bech32utils.d.ts +6 -0
- package/browser/chunks/{crypto-0PweVewC.js → crypto-BhCpKpek.js} +4 -4
- package/browser/chunks/{payments-CgasufRS.js → payments-yjA0Evsv.js} +639 -597
- package/browser/chunks/{psbt-BIwOrKer.js → psbt-URK2hBFc.js} +1259 -1316
- package/browser/chunks/{script-CROJPzz_.js → script-DyPItFEl.js} +33 -33
- package/browser/chunks/{transaction-DchBu35N.js → transaction-C_UbhMGn.js} +3 -3
- package/browser/chunks/{utils-CO5kmxe9.js → utils-DNZi-T5W.js} +26 -26
- package/browser/index.d.ts +3 -1
- package/browser/index.js +102 -90
- package/browser/payments/embed.d.ts +1 -1
- package/browser/payments/index.d.ts +12 -104
- package/browser/payments/p2ms.d.ts +1 -1
- package/browser/payments/p2op.d.ts +1 -1
- package/browser/payments/p2pk.d.ts +1 -1
- package/browser/payments/p2pkh.d.ts +1 -1
- package/browser/payments/p2sh.d.ts +1 -1
- package/browser/payments/p2tr.d.ts +1 -1
- package/browser/payments/p2wpkh.d.ts +1 -1
- package/browser/payments/p2wsh.d.ts +1 -1
- package/browser/payments/types.d.ts +93 -0
- package/browser/psbt/bip371.d.ts +0 -1
- package/browser/psbt/psbtutils.d.ts +0 -7
- package/browser/pubkey.d.ts +8 -0
- package/browser/script.d.ts +1 -2
- package/browser/script_signature.d.ts +1 -0
- package/browser/types.d.ts +3 -0
- package/build/address.d.ts +2 -6
- package/build/address.js +19 -37
- package/build/bech32utils.d.ts +6 -0
- package/build/bech32utils.js +26 -0
- package/build/index.d.ts +3 -1
- package/build/index.js +1 -0
- package/build/payments/embed.d.ts +1 -1
- package/build/payments/embed.js +1 -1
- package/build/payments/index.d.ts +12 -104
- package/build/payments/index.js +12 -24
- package/build/payments/p2ms.d.ts +1 -1
- package/build/payments/p2ms.js +1 -1
- package/build/payments/p2op.d.ts +1 -1
- package/build/payments/p2op.js +2 -2
- package/build/payments/p2pk.d.ts +1 -1
- package/build/payments/p2pk.js +1 -1
- package/build/payments/p2pkh.d.ts +1 -1
- package/build/payments/p2pkh.js +2 -2
- package/build/payments/p2sh.d.ts +1 -1
- package/build/payments/p2sh.js +1 -1
- package/build/payments/p2tr.d.ts +1 -1
- package/build/payments/p2tr.js +2 -6
- package/build/payments/p2wpkh.d.ts +1 -1
- package/build/payments/p2wpkh.js +1 -1
- package/build/payments/p2wsh.d.ts +1 -1
- package/build/payments/p2wsh.js +2 -2
- package/build/payments/types.d.ts +93 -0
- package/build/payments/types.js +13 -0
- package/build/psbt/bip371.d.ts +0 -1
- package/build/psbt/bip371.js +2 -6
- package/build/psbt/psbtutils.d.ts +0 -7
- package/build/psbt/psbtutils.js +2 -54
- package/build/psbt.js +3 -2
- package/build/pubkey.d.ts +8 -0
- package/build/pubkey.js +56 -0
- package/build/script.d.ts +1 -2
- package/build/script.js +1 -4
- package/build/script_signature.d.ts +1 -0
- package/build/script_signature.js +4 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types.d.ts +3 -0
- package/package.json +5 -2
- package/src/address.ts +20 -50
- package/src/bech32utils.ts +43 -0
- package/src/index.ts +2 -3
- package/src/payments/embed.ts +2 -2
- package/src/payments/index.ts +40 -164
- package/src/payments/p2ms.ts +2 -2
- package/src/payments/p2op.ts +2 -2
- package/src/payments/p2pk.ts +2 -2
- package/src/payments/p2pkh.ts +3 -3
- package/src/payments/p2sh.ts +2 -10
- package/src/payments/p2tr.ts +6 -5
- package/src/payments/p2wpkh.ts +1 -1
- package/src/payments/p2wsh.ts +8 -2
- package/src/payments/types.ts +154 -0
- package/src/psbt/bip371.ts +6 -13
- package/src/psbt/psbtutils.ts +2 -104
- package/src/psbt.ts +3 -2
- package/src/pubkey.ts +99 -0
- package/src/script.ts +2 -7
- package/src/script_signature.ts +10 -1
- package/src/types.ts +5 -0
- package/test/address.spec.ts +8 -7
- package/test/integration/taproot.spec.ts +2 -1
- package/test/payments.spec.ts +4 -4
|
@@ -1,72 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
function
|
|
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 =
|
|
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
|
|
24
|
+
function y(e) {
|
|
83
25
|
let o;
|
|
84
26
|
return () => (o !== void 0 || (o = e()), o);
|
|
85
27
|
}
|
|
86
|
-
const
|
|
87
|
-
function
|
|
88
|
-
if (!e.
|
|
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
|
-
|
|
34
|
+
address: r.maybe(r.String),
|
|
92
35
|
output: r.maybe(r.Buffer),
|
|
93
|
-
|
|
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
|
-
|
|
126
|
-
|
|
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
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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 (
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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.
|
|
195
|
-
if (
|
|
196
|
-
|
|
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.
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
throw new TypeError("
|
|
202
|
-
|
|
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(
|
|
116
|
+
return Object.assign(u, e);
|
|
209
117
|
}
|
|
210
|
-
const
|
|
211
|
-
function
|
|
212
|
-
if (!e.
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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 (
|
|
230
|
-
if (
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
|
238
|
-
}),
|
|
239
|
-
if (e.
|
|
240
|
-
return
|
|
241
|
-
}),
|
|
242
|
-
if (
|
|
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
|
|
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
|
-
|
|
249
|
-
if (
|
|
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
|
-
|
|
255
|
-
if (
|
|
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(
|
|
230
|
+
return Object.assign(u, e);
|
|
260
231
|
}
|
|
261
|
-
const
|
|
262
|
-
function
|
|
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
|
|
283
|
-
|
|
253
|
+
let f = e.network;
|
|
254
|
+
f || (f = e.redeem && e.redeem.network || v);
|
|
284
255
|
const i = {
|
|
285
|
-
network:
|
|
286
|
-
name:
|
|
287
|
-
}, s =
|
|
288
|
-
const t =
|
|
289
|
-
return { version:
|
|
290
|
-
}), u =
|
|
291
|
-
const t = 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
|
+
}), u = y(() => m(e.input)), n = y(() => {
|
|
262
|
+
const t = u(), h = t[t.length - 1];
|
|
292
263
|
return {
|
|
293
|
-
network:
|
|
294
|
-
output:
|
|
295
|
-
input:
|
|
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 (
|
|
270
|
+
if (p(i, "address", () => {
|
|
300
271
|
if (!i.hash) return;
|
|
301
|
-
const t =
|
|
302
|
-
return t.writeUInt8(i.network.scriptHash, 0), i.hash.copy(t, 1),
|
|
303
|
-
}),
|
|
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
|
|
307
|
-
}),
|
|
277
|
+
if (i.redeem && i.redeem.output) return T(i.redeem.output);
|
|
278
|
+
}), p(i, "output", () => {
|
|
308
279
|
if (i.hash)
|
|
309
|
-
return
|
|
310
|
-
}),
|
|
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
|
-
}),
|
|
284
|
+
}), p(i, "input", () => {
|
|
314
285
|
if (!(!e.redeem || !e.redeem.input || !e.redeem.output))
|
|
315
|
-
return
|
|
316
|
-
[].concat(
|
|
286
|
+
return l(
|
|
287
|
+
[].concat(m(e.redeem.input), e.redeem.output)
|
|
317
288
|
);
|
|
318
|
-
}),
|
|
289
|
+
}), p(i, "witness", () => {
|
|
319
290
|
if (i.redeem && i.redeem.witness) return i.redeem.witness;
|
|
320
291
|
if (i.input) return [];
|
|
321
|
-
}),
|
|
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 =
|
|
296
|
+
let t = c.from([]);
|
|
326
297
|
if (e.address) {
|
|
327
|
-
if (s().version !==
|
|
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] !==
|
|
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
|
|
314
|
+
const h = (d) => {
|
|
344
315
|
if (d.output) {
|
|
345
|
-
const a =
|
|
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 (
|
|
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
|
|
355
|
-
if (t.length > 0 && !t.equals(
|
|
356
|
-
t =
|
|
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,
|
|
360
|
-
if (!a && !
|
|
361
|
-
if (a &&
|
|
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
|
|
364
|
-
if (!
|
|
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 (!
|
|
373
|
-
|
|
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 !==
|
|
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
|
-
|
|
356
|
+
h(e.redeem);
|
|
386
357
|
}
|
|
387
|
-
if (e.witness && e.redeem && e.redeem.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
|
|
393
|
-
function
|
|
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(
|
|
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
|
|
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] ===
|
|
424
|
-
}), s =
|
|
425
|
-
if (e.scriptTree) return
|
|
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 ||
|
|
428
|
-
name:
|
|
501
|
+
}), u = e.network || v, n = {
|
|
502
|
+
name: E.P2TR,
|
|
429
503
|
network: u
|
|
430
504
|
};
|
|
431
|
-
if (
|
|
505
|
+
if (p(n, "address", () => {
|
|
432
506
|
if (!n.pubkey) return;
|
|
433
|
-
const t =
|
|
434
|
-
return t.unshift(
|
|
435
|
-
}),
|
|
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
|
|
439
|
-
if (
|
|
440
|
-
const d =
|
|
441
|
-
output:
|
|
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
|
|
518
|
+
return F(d, I);
|
|
445
519
|
}
|
|
446
|
-
}),
|
|
520
|
+
}), p(n, "output", () => {
|
|
447
521
|
if (n.pubkey)
|
|
448
|
-
return
|
|
449
|
-
}),
|
|
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] &
|
|
529
|
+
redeemVersion: t[t.length - 1][0] & $
|
|
456
530
|
};
|
|
457
|
-
}),
|
|
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
|
|
534
|
+
if (e.address) return f().data;
|
|
461
535
|
if (n.internalPubkey) {
|
|
462
|
-
const t =
|
|
536
|
+
const t = V(n.internalPubkey, n.hash);
|
|
463
537
|
if (t) return t.x;
|
|
464
538
|
}
|
|
465
|
-
}),
|
|
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
|
-
}),
|
|
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
|
-
}),
|
|
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
|
|
552
|
+
const h = B({
|
|
479
553
|
output: e.redeem.output,
|
|
480
554
|
version: n.redeemVersion
|
|
481
|
-
}), d =
|
|
555
|
+
}), d = x(t, h);
|
|
482
556
|
if (!d) return;
|
|
483
|
-
const a =
|
|
557
|
+
const a = V(e.internalPubkey, t.hash);
|
|
484
558
|
if (!a) return;
|
|
485
|
-
const
|
|
486
|
-
[
|
|
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,
|
|
564
|
+
return [e.redeem.output, w];
|
|
491
565
|
}
|
|
492
566
|
if (e.signature) return [e.signature];
|
|
493
567
|
}), o.validate) {
|
|
494
|
-
let t =
|
|
568
|
+
let t = k.from([]);
|
|
495
569
|
if (e.address) {
|
|
496
|
-
if (u && u.bech32 !==
|
|
570
|
+
if (u && u.bech32 !== f().prefix)
|
|
497
571
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
498
|
-
if (
|
|
572
|
+
if (f().version !== re)
|
|
499
573
|
throw new TypeError("Invalid address version");
|
|
500
|
-
if (
|
|
501
|
-
t =
|
|
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] !==
|
|
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 =
|
|
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
|
-
|
|
522
|
-
|
|
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 &&
|
|
527
|
-
const a =
|
|
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 (!
|
|
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 (
|
|
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 && !
|
|
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
|
|
562
|
-
if (
|
|
563
|
-
throw new TypeError(`The script path is too long. Got ${
|
|
564
|
-
const
|
|
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 (!
|
|
639
|
+
if (!pe().isXOnlyPoint(I))
|
|
568
640
|
throw new TypeError("Invalid internalPubkey for p2tr witness");
|
|
569
|
-
const he = a[0] &
|
|
570
|
-
output:
|
|
641
|
+
const he = a[0] & $, de = d[d.length - 2], ae = B({
|
|
642
|
+
output: de,
|
|
571
643
|
version: he
|
|
572
|
-
}),
|
|
573
|
-
if (!
|
|
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(
|
|
647
|
+
if (t.length && !t.equals(C.x))
|
|
576
648
|
throw new TypeError("Pubkey mismatch for p2tr witness");
|
|
577
|
-
if (
|
|
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
|
|
583
|
-
function
|
|
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(
|
|
594
|
-
signature: r.maybe(
|
|
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
|
|
600
|
-
const u =
|
|
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:
|
|
676
|
+
data: c.from(t)
|
|
605
677
|
};
|
|
606
|
-
}), i = e.network ||
|
|
607
|
-
name:
|
|
678
|
+
}), i = e.network || v, s = {
|
|
679
|
+
name: E.P2WPKH,
|
|
608
680
|
network: i
|
|
609
681
|
};
|
|
610
|
-
if (
|
|
682
|
+
if (p(s, "address", () => {
|
|
611
683
|
if (!s.hash) return;
|
|
612
|
-
const u =
|
|
613
|
-
return u.unshift(0),
|
|
614
|
-
}),
|
|
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
|
|
617
|
-
if (e.pubkey || s.pubkey) return
|
|
618
|
-
}),
|
|
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
|
|
621
|
-
}),
|
|
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
|
-
}),
|
|
697
|
+
}), p(s, "signature", () => {
|
|
626
698
|
if (e.witness)
|
|
627
699
|
return e.witness[0];
|
|
628
|
-
}),
|
|
700
|
+
}), p(s, "input", () => {
|
|
629
701
|
if (s.witness)
|
|
630
|
-
return
|
|
631
|
-
}),
|
|
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 =
|
|
707
|
+
let u = c.from([]);
|
|
636
708
|
if (e.address) {
|
|
637
|
-
if (i && i.bech32 !==
|
|
709
|
+
if (i && i.bech32 !== f().prefix)
|
|
638
710
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
639
|
-
if (
|
|
640
|
-
if (
|
|
641
|
-
u =
|
|
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] !==
|
|
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 =
|
|
727
|
+
const n = T(e.pubkey);
|
|
656
728
|
if (u.length > 0 && !u.equals(n)) throw new TypeError("Hash mismatch");
|
|
657
|
-
if (u = n, !
|
|
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 (!
|
|
734
|
+
if (!H(e.witness[0]))
|
|
663
735
|
throw new TypeError("Witness has invalid signature");
|
|
664
|
-
if (!
|
|
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 =
|
|
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
|
|
676
|
-
function
|
|
677
|
-
return !!(
|
|
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
|
|
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
|
|
700
|
-
const n =
|
|
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:
|
|
776
|
+
data: c.from(h)
|
|
705
777
|
};
|
|
706
|
-
}), i =
|
|
778
|
+
}), i = y(() => m(e.redeem.input));
|
|
707
779
|
let s = e.network;
|
|
708
|
-
s || (s = e.redeem && e.redeem.network ||
|
|
780
|
+
s || (s = e.redeem && e.redeem.network || v);
|
|
709
781
|
const u = {
|
|
710
782
|
network: s,
|
|
711
|
-
name:
|
|
783
|
+
name: E.P2WSH
|
|
712
784
|
};
|
|
713
|
-
if (
|
|
785
|
+
if (p(u, "address", () => {
|
|
714
786
|
if (!u.hash) return;
|
|
715
|
-
const n =
|
|
716
|
-
return n.unshift(0),
|
|
717
|
-
}),
|
|
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
|
|
720
|
-
if (u.redeem && u.redeem.output) return
|
|
721
|
-
}),
|
|
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
|
|
724
|
-
}),
|
|
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
|
-
}),
|
|
803
|
+
}), p(u, "input", () => {
|
|
732
804
|
if (u.witness)
|
|
733
|
-
return
|
|
734
|
-
}),
|
|
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 =
|
|
737
|
-
return u.redeem = Object.assign({ witness: n }, e.redeem), u.redeem.input =
|
|
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
|
-
}),
|
|
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 =
|
|
817
|
+
let n = c.from([]);
|
|
746
818
|
if (e.address) {
|
|
747
|
-
if (
|
|
819
|
+
if (f().prefix !== s.bech32)
|
|
748
820
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
749
|
-
if (
|
|
750
|
-
if (
|
|
751
|
-
n =
|
|
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] !==
|
|
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 =
|
|
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 (
|
|
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
|
|
780
|
-
if (n.length > 0 && !n.equals(
|
|
781
|
-
n =
|
|
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 && !
|
|
855
|
+
if (e.redeem.input && !fe(i()))
|
|
784
856
|
throw new TypeError("Non push-only scriptSig");
|
|
785
|
-
if (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(
|
|
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(
|
|
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
|
|
801
|
-
function
|
|
802
|
-
if (!e.
|
|
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
|
-
|
|
877
|
+
network: r.maybe(r.Object),
|
|
807
878
|
output: r.maybe(r.Buffer),
|
|
808
|
-
|
|
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
|
-
|
|
811
|
-
|
|
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
|
|
816
|
-
|
|
817
|
-
|
|
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
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
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]
|
|
833
|
-
|
|
834
|
-
if (
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
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.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
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.
|
|
872
|
-
if (
|
|
873
|
-
|
|
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 (
|
|
876
|
-
|
|
877
|
-
if (
|
|
878
|
-
throw new TypeError("
|
|
879
|
-
|
|
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(
|
|
994
|
+
return Object.assign(s, e);
|
|
889
995
|
}
|
|
890
|
-
const
|
|
891
|
-
function
|
|
892
|
-
if (!e.
|
|
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
|
-
|
|
898
|
-
|
|
899
|
-
|
|
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
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
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 (
|
|
915
|
-
if (
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
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
|
|
937
|
-
}),
|
|
938
|
-
if (
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
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
|
|
1032
|
+
if (e.output[e.output.length - 1] !== ue.OP_CHECKSIG)
|
|
964
1033
|
throw new TypeError("Output is invalid");
|
|
965
|
-
|
|
966
|
-
if (
|
|
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
|
-
|
|
990
|
-
if (
|
|
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(
|
|
1045
|
+
return Object.assign(s, e);
|
|
1003
1046
|
}
|
|
1004
|
-
|
|
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:
|
|
1008
|
-
MAX_TAPTREE_DEPTH:
|
|
1009
|
-
PaymentType:
|
|
1010
|
-
findScriptPath:
|
|
1011
|
-
p2data:
|
|
1012
|
-
p2ms:
|
|
1013
|
-
p2op:
|
|
1014
|
-
p2pk:
|
|
1015
|
-
p2pkh:
|
|
1016
|
-
p2sh:
|
|
1017
|
-
p2tr:
|
|
1018
|
-
p2wpkh:
|
|
1019
|
-
p2wsh:
|
|
1020
|
-
prop:
|
|
1021
|
-
rootHashFromPath:
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
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
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
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
|
};
|