@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.
- 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-Ngcm87h_.js} +670 -626
- package/browser/chunks/{psbt-BIwOrKer.js → psbt-CcM4rw3q.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 -2
- 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 +2 -2
- 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 +3 -2
- package/test/integration/taproot.spec.ts +2 -1
|
@@ -1,265 +1,236 @@
|
|
|
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 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
|
|
70
|
-
return this[o] =
|
|
11
|
+
const u = f.call(this);
|
|
12
|
+
return this[o] = u, u;
|
|
71
13
|
},
|
|
72
|
-
set(
|
|
14
|
+
set(u) {
|
|
73
15
|
Object.defineProperty(this, o, {
|
|
74
16
|
configurable: !0,
|
|
75
17
|
enumerable: !0,
|
|
76
|
-
value:
|
|
18
|
+
value: u,
|
|
77
19
|
writable: !0
|
|
78
20
|
});
|
|
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 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
|
-
|
|
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
|
+
}, u = y(() => oe(e.address)), s = e.network || v, i = {
|
|
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(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 (
|
|
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] !== 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.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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.
|
|
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 = 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(
|
|
116
|
+
return Object.assign(i, 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
|
+
}), 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 (
|
|
230
|
-
if (
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
|
238
|
-
}),
|
|
239
|
-
if (e.
|
|
240
|
-
return
|
|
241
|
-
}),
|
|
242
|
-
if (
|
|
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
|
|
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 = 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
|
-
|
|
255
|
-
if (
|
|
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(
|
|
230
|
+
return Object.assign(i, 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
|
-
|
|
284
|
-
const
|
|
285
|
-
network:
|
|
286
|
-
name:
|
|
287
|
-
}, s =
|
|
288
|
-
const t =
|
|
289
|
-
return { version:
|
|
290
|
-
}),
|
|
291
|
-
const t =
|
|
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:
|
|
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 (
|
|
300
|
-
if (!
|
|
301
|
-
const t =
|
|
302
|
-
return t.writeUInt8(
|
|
303
|
-
}),
|
|
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 (
|
|
307
|
-
}),
|
|
308
|
-
if (
|
|
309
|
-
return
|
|
310
|
-
}),
|
|
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
|
-
}),
|
|
284
|
+
}), p(u, "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
|
-
}),
|
|
319
|
-
if (
|
|
320
|
-
if (
|
|
321
|
-
}),
|
|
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
|
|
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 =
|
|
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,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] !==
|
|
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 (fe(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 (!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 =
|
|
341
|
+
const d = i();
|
|
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
|
-
return Object.assign(
|
|
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
|
-
|
|
393
|
-
|
|
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(
|
|
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(() => 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] ===
|
|
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
|
-
}),
|
|
428
|
-
name:
|
|
429
|
-
network:
|
|
501
|
+
}), i = e.network || v, n = {
|
|
502
|
+
name: E.P2TR,
|
|
503
|
+
network: i
|
|
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(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
|
|
439
|
-
if (
|
|
440
|
-
const d =
|
|
441
|
-
output:
|
|
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
|
|
518
|
+
return F(d, I);
|
|
445
519
|
}
|
|
446
|
-
}),
|
|
520
|
+
}), p(n, "output", () => {
|
|
447
521
|
if (n.pubkey)
|
|
448
|
-
return
|
|
449
|
-
}),
|
|
450
|
-
const t =
|
|
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] &
|
|
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
|
-
const t =
|
|
541
|
+
const t = u();
|
|
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
|
-
const t =
|
|
545
|
+
const t = u();
|
|
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 (
|
|
570
|
+
if (i && i.bech32 !== f().prefix)
|
|
497
571
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
498
|
-
if (
|
|
572
|
+
if (f().version !== te)
|
|
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,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] !==
|
|
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 =
|
|
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 && !
|
|
595
|
+
if (t && t.length && !X().isXOnlyPoint(t))
|
|
522
596
|
throw new TypeError("Invalid pubkey for p2tr");
|
|
523
|
-
const
|
|
524
|
-
if (e.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 &&
|
|
527
|
-
const a =
|
|
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 (!
|
|
605
|
+
if (!x(h, a))
|
|
532
606
|
throw new TypeError("Redeem script not in tree");
|
|
533
607
|
}
|
|
534
|
-
const d =
|
|
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 (
|
|
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 && !
|
|
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
|
|
562
|
-
if (
|
|
563
|
-
throw new TypeError(`The script path is too long. Got ${
|
|
564
|
-
const
|
|
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 (!
|
|
641
|
+
if (!X().isXOnlyPoint(I))
|
|
568
642
|
throw new TypeError("Invalid internalPubkey for p2tr witness");
|
|
569
|
-
const he = a[0] &
|
|
570
|
-
output:
|
|
643
|
+
const he = a[0] & $, de = d[d.length - 2], ae = B({
|
|
644
|
+
output: de,
|
|
571
645
|
version: he
|
|
572
|
-
}),
|
|
573
|
-
if (!
|
|
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(
|
|
649
|
+
if (t.length && !t.equals(C.x))
|
|
576
650
|
throw new TypeError("Pubkey mismatch for p2tr witness");
|
|
577
|
-
if (
|
|
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
|
|
583
|
-
function
|
|
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(
|
|
594
|
-
signature: r.maybe(
|
|
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
|
|
600
|
-
const
|
|
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:
|
|
604
|
-
data:
|
|
677
|
+
prefix: i.prefix,
|
|
678
|
+
data: c.from(t)
|
|
605
679
|
};
|
|
606
|
-
}),
|
|
607
|
-
name:
|
|
608
|
-
network:
|
|
680
|
+
}), u = e.network || v, s = {
|
|
681
|
+
name: E.P2WPKH,
|
|
682
|
+
network: u
|
|
609
683
|
};
|
|
610
|
-
if (
|
|
684
|
+
if (p(s, "address", () => {
|
|
611
685
|
if (!s.hash) return;
|
|
612
|
-
const
|
|
613
|
-
return
|
|
614
|
-
}),
|
|
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
|
|
617
|
-
if (e.pubkey || s.pubkey) return
|
|
618
|
-
}),
|
|
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
|
|
621
|
-
}),
|
|
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
|
-
}),
|
|
699
|
+
}), p(s, "signature", () => {
|
|
626
700
|
if (e.witness)
|
|
627
701
|
return e.witness[0];
|
|
628
|
-
}),
|
|
702
|
+
}), p(s, "input", () => {
|
|
629
703
|
if (s.witness)
|
|
630
|
-
return
|
|
631
|
-
}),
|
|
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
|
|
709
|
+
let i = c.from([]);
|
|
636
710
|
if (e.address) {
|
|
637
|
-
if (
|
|
711
|
+
if (u && u.bech32 !== f().prefix)
|
|
638
712
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
639
|
-
if (
|
|
640
|
-
if (
|
|
641
|
-
|
|
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 (
|
|
645
|
-
|
|
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] !==
|
|
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 (
|
|
724
|
+
if (i.length > 0 && !i.equals(e.output.subarray(2)))
|
|
651
725
|
throw new TypeError("Hash mismatch");
|
|
652
|
-
|
|
726
|
+
i = e.output.subarray(2);
|
|
653
727
|
}
|
|
654
728
|
if (e.pubkey) {
|
|
655
|
-
const n =
|
|
656
|
-
if (
|
|
657
|
-
if (
|
|
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 (!
|
|
736
|
+
if (!H(e.witness[0]))
|
|
663
737
|
throw new TypeError("Witness has invalid signature");
|
|
664
|
-
if (!
|
|
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 =
|
|
670
|
-
if (
|
|
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
|
|
676
|
-
function
|
|
677
|
-
return !!(
|
|
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
|
|
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
|
|
700
|
-
const n =
|
|
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:
|
|
778
|
+
data: c.from(h)
|
|
705
779
|
};
|
|
706
|
-
}),
|
|
780
|
+
}), u = y(() => m(e.redeem.input));
|
|
707
781
|
let s = e.network;
|
|
708
|
-
s || (s = e.redeem && e.redeem.network ||
|
|
709
|
-
const
|
|
782
|
+
s || (s = e.redeem && e.redeem.network || v);
|
|
783
|
+
const i = {
|
|
710
784
|
network: s,
|
|
711
|
-
name:
|
|
785
|
+
name: E.P2WSH
|
|
712
786
|
};
|
|
713
|
-
if (
|
|
714
|
-
if (!
|
|
715
|
-
const n =
|
|
716
|
-
return n.unshift(0),
|
|
717
|
-
}),
|
|
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
|
|
720
|
-
if (
|
|
721
|
-
}),
|
|
722
|
-
if (
|
|
723
|
-
return
|
|
724
|
-
}),
|
|
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
|
-
}),
|
|
732
|
-
if (
|
|
733
|
-
return
|
|
734
|
-
}),
|
|
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 =
|
|
737
|
-
return
|
|
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
|
-
}),
|
|
815
|
+
}), p(i, "name", () => {
|
|
742
816
|
const n = ["p2wsh"];
|
|
743
|
-
return
|
|
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 =
|
|
819
|
+
let n = c.from([]);
|
|
746
820
|
if (e.address) {
|
|
747
|
-
if (
|
|
821
|
+
if (f().prefix !== s.bech32)
|
|
748
822
|
throw new TypeError("Invalid prefix or Network mismatch");
|
|
749
|
-
if (
|
|
750
|
-
if (
|
|
751
|
-
n =
|
|
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] !==
|
|
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 =
|
|
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 (
|
|
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
|
|
780
|
-
if (n.length > 0 && !n.equals(
|
|
781
|
-
n =
|
|
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 && !
|
|
857
|
+
if (e.redeem.input && !pe(u()))
|
|
784
858
|
throw new TypeError("Non push-only scriptSig");
|
|
785
|
-
if (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 &&
|
|
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(
|
|
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(
|
|
872
|
+
return Object.assign(i, e);
|
|
799
873
|
}
|
|
800
|
-
const
|
|
801
|
-
function
|
|
802
|
-
if (!e.
|
|
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
|
-
|
|
879
|
+
network: r.maybe(r.Object),
|
|
807
880
|
output: r.maybe(r.Buffer),
|
|
808
|
-
|
|
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
|
-
|
|
811
|
-
|
|
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
|
|
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
|
|
922
|
+
const s = {
|
|
923
|
+
network: e.network || v,
|
|
924
|
+
name: E.P2MS
|
|
826
925
|
};
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
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]
|
|
833
|
-
|
|
834
|
-
if (
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
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.
|
|
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;
|
|
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.
|
|
872
|
-
if (
|
|
873
|
-
|
|
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 (
|
|
876
|
-
|
|
877
|
-
if (
|
|
878
|
-
throw new TypeError("
|
|
879
|
-
|
|
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(
|
|
996
|
+
return Object.assign(s, e);
|
|
889
997
|
}
|
|
890
|
-
const
|
|
891
|
-
function
|
|
892
|
-
if (!e.
|
|
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
|
-
|
|
898
|
-
|
|
899
|
-
|
|
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
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
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 (
|
|
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", () => {
|
|
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
|
|
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)
|
|
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
|
|
1034
|
+
if (e.output[e.output.length - 1] !== ue.OP_CHECKSIG)
|
|
964
1035
|
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;
|
|
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
|
-
|
|
990
|
-
if (
|
|
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(
|
|
1047
|
+
return Object.assign(s, e);
|
|
1003
1048
|
}
|
|
1004
|
-
|
|
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:
|
|
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
|
|
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
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
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
|
};
|