@btc-vision/bitcoin 6.4.11 → 6.5.1

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.
@@ -0,0 +1,421 @@
1
+ import { B as a, d as I, b as N, t as Z } from "./crypto-C6FlKKmp.js";
2
+ import { f as z, k as K, l as D, a as C, m as b, n as P, o as _, N as $, S as W, g as R, p as V, B as m, r as M, F as Q, q as J, h as tt } from "./utils-BKmkTzNZ.js";
3
+ import { c as et, d as st, o as it } from "./script-COWGdiOo.js";
4
+ const { typeforce: A } = K;
5
+ function p(f) {
6
+ const t = f.length;
7
+ return R(t) + t;
8
+ }
9
+ function rt(f) {
10
+ const t = f.length;
11
+ return R(t) + f.reduce((e, s) => e + p(s), 0);
12
+ }
13
+ const H = a.allocUnsafe(0), T = [], k = a.from(
14
+ "0000000000000000000000000000000000000000000000000000000000000000",
15
+ "hex"
16
+ ), F = a.from(
17
+ "0000000000000000000000000000000000000000000000000000000000000001",
18
+ "hex"
19
+ ), nt = a.from("ffffffffffffffff", "hex"), ot = {
20
+ script: H,
21
+ valueBuffer: nt
22
+ };
23
+ function ht(f) {
24
+ return f.value !== void 0;
25
+ }
26
+ class o {
27
+ constructor() {
28
+ this.version = 1, this.locktime = 0, this.ins = [], this.outs = [];
29
+ }
30
+ static {
31
+ this.DEFAULT_SEQUENCE = 4294967295;
32
+ }
33
+ static {
34
+ this.SIGHASH_DEFAULT = 0;
35
+ }
36
+ static {
37
+ this.SIGHASH_ALL = 1;
38
+ }
39
+ static {
40
+ this.SIGHASH_NONE = 2;
41
+ }
42
+ static {
43
+ this.SIGHASH_SINGLE = 3;
44
+ }
45
+ static {
46
+ this.SIGHASH_ANYONECANPAY = 128;
47
+ }
48
+ static {
49
+ this.SIGHASH_OUTPUT_MASK = 3;
50
+ }
51
+ static {
52
+ this.SIGHASH_INPUT_MASK = 128;
53
+ }
54
+ static {
55
+ this.ADVANCED_TRANSACTION_MARKER = 0;
56
+ }
57
+ static {
58
+ this.ADVANCED_TRANSACTION_FLAG = 1;
59
+ }
60
+ static {
61
+ this.TRUC_VERSION = 3;
62
+ }
63
+ static {
64
+ this.TRUC_MAX_VSIZE = 1e4;
65
+ }
66
+ static {
67
+ this.TRUC_CHILD_MAX_VSIZE = 1e3;
68
+ }
69
+ static fromBuffer(t, e) {
70
+ const s = new z(t), i = new o();
71
+ i.version = s.readInt32();
72
+ const n = s.readUInt8(), r = s.readUInt8();
73
+ let h = !1;
74
+ n === o.ADVANCED_TRANSACTION_MARKER && r === o.ADVANCED_TRANSACTION_FLAG ? h = !0 : s.offset -= 2;
75
+ const g = s.readVarInt();
76
+ for (let w = 0; w < g; ++w) {
77
+ const S = s.readSlice(32), U = s.readUInt32(), v = s.readVarSlice(), L = s.readUInt32();
78
+ i.ins.push({
79
+ hash: S,
80
+ index: U,
81
+ script: v,
82
+ sequence: L,
83
+ witness: T
84
+ });
85
+ }
86
+ const d = s.readVarInt();
87
+ for (let w = 0; w < d; ++w)
88
+ i.outs.push({
89
+ value: s.readUInt64(),
90
+ script: s.readVarSlice()
91
+ });
92
+ if (h) {
93
+ for (let w = 0; w < g; ++w)
94
+ i.ins[w].witness = s.readVector();
95
+ if (!i.hasWitnesses()) throw new Error("Transaction has superfluous witness data");
96
+ }
97
+ if (i.locktime = s.readUInt32(), e) return i;
98
+ if (s.offset !== t.length)
99
+ throw new Error("Transaction has unexpected data");
100
+ return i;
101
+ }
102
+ static fromHex(t) {
103
+ return o.fromBuffer(a.from(t, "hex"), !1);
104
+ }
105
+ static isCoinbaseHash(t) {
106
+ A(D, t);
107
+ for (let e = 0; e < 32; ++e)
108
+ if (t[e] !== 0) return !1;
109
+ return !0;
110
+ }
111
+ isCoinbase() {
112
+ return this.ins.length === 1 && o.isCoinbaseHash(this.ins[0].hash);
113
+ }
114
+ addInput(t, e, s, i) {
115
+ return A(
116
+ C(
117
+ D,
118
+ b,
119
+ P(b),
120
+ P(_)
121
+ ),
122
+ arguments
123
+ ), $(s) && (s = o.DEFAULT_SEQUENCE), this.ins.push({
124
+ hash: t,
125
+ index: e,
126
+ script: i || H,
127
+ sequence: s,
128
+ witness: T
129
+ }) - 1;
130
+ }
131
+ addOutput(t, e) {
132
+ return A(C(_, W), arguments), this.outs.push({
133
+ script: t,
134
+ value: e
135
+ }) - 1;
136
+ }
137
+ hasWitnesses() {
138
+ return this.ins.some((t) => t.witness.length !== 0);
139
+ }
140
+ weight() {
141
+ const t = this.byteLength(!1), e = this.byteLength(!0);
142
+ return t * 3 + e;
143
+ }
144
+ virtualSize() {
145
+ return Math.ceil(this.weight() / 4);
146
+ }
147
+ byteLength(t = !0) {
148
+ const e = t && this.hasWitnesses();
149
+ return (e ? 10 : 8) + R(this.ins.length) + R(this.outs.length) + this.ins.reduce((s, i) => s + 40 + p(i.script), 0) + this.outs.reduce((s, i) => s + 8 + p(i.script), 0) + (e ? this.ins.reduce((s, i) => s + rt(i.witness), 0) : 0);
150
+ }
151
+ clone() {
152
+ const t = new o();
153
+ return t.version = this.version, t.locktime = this.locktime, t.ins = this.ins.map((e) => ({
154
+ hash: e.hash,
155
+ index: e.index,
156
+ script: e.script,
157
+ sequence: e.sequence,
158
+ witness: e.witness
159
+ })), t.outs = this.outs.map((e) => ({
160
+ script: e.script,
161
+ value: e.value
162
+ })), t;
163
+ }
164
+ /**
165
+ * Hash transaction for signing a specific input.
166
+ *
167
+ * Bitcoin uses a different hash for each signed transaction input.
168
+ * This method copies the transaction, makes the necessary changes based on the
169
+ * hashType, and then hashes the result.
170
+ * This hash can then be used to sign the provided transaction input.
171
+ */
172
+ hashForSignature(t, e, s) {
173
+ if (A(
174
+ C(
175
+ b,
176
+ _,
177
+ /* types.UInt8 */
178
+ V
179
+ ),
180
+ arguments
181
+ ), t >= this.ins.length) return F;
182
+ const i = et(
183
+ st(e).filter((h) => h !== it.OP_CODESEPARATOR)
184
+ ), n = this.clone();
185
+ if ((s & 31) === o.SIGHASH_NONE)
186
+ n.outs = [], n.ins.forEach((h, g) => {
187
+ g !== t && (h.sequence = 0);
188
+ });
189
+ else if ((s & 31) === o.SIGHASH_SINGLE) {
190
+ if (t >= this.outs.length) return F;
191
+ n.outs.length = t + 1;
192
+ for (let h = 0; h < t; h++)
193
+ n.outs[h] = ot;
194
+ n.ins.forEach((h, g) => {
195
+ g !== t && (h.sequence = 0);
196
+ });
197
+ }
198
+ s & o.SIGHASH_ANYONECANPAY ? (n.ins = [n.ins[t]], n.ins[0].script = i) : (n.ins.forEach((h) => {
199
+ h.script = H;
200
+ }), n.ins[t].script = i);
201
+ const r = a.allocUnsafe(n.byteLength(!1) + 4);
202
+ return r.writeInt32LE(s, r.length - 4), n.__toBuffer(r, 0, !1), I(r);
203
+ }
204
+ hashForWitnessV1(t, e, s, i, n, r) {
205
+ if (A(
206
+ C(
207
+ b,
208
+ A.arrayOf(_),
209
+ A.arrayOf(W),
210
+ b
211
+ ),
212
+ arguments
213
+ ), s.length !== this.ins.length || e.length !== this.ins.length)
214
+ throw new Error("Must supply prevout script and value for all inputs");
215
+ const h = i === o.SIGHASH_DEFAULT ? o.SIGHASH_ALL : i & o.SIGHASH_OUTPUT_MASK, d = (i & o.SIGHASH_INPUT_MASK) === o.SIGHASH_ANYONECANPAY, w = h === o.SIGHASH_NONE, S = h === o.SIGHASH_SINGLE;
216
+ let U = H, v = H, L = H, O = H, G = H;
217
+ if (!d) {
218
+ let c = m.withCapacity(36 * this.ins.length);
219
+ this.ins.forEach((l) => {
220
+ c.writeSlice(l.hash), c.writeUInt32(l.index);
221
+ }), U = N(c.end()), c = m.withCapacity(8 * this.ins.length), s.forEach((l) => c.writeUInt64(l)), v = N(c.end()), c = m.withCapacity(
222
+ e.map(p).reduce((l, E) => l + E)
223
+ ), e.forEach((l) => c.writeVarSlice(l)), L = N(c.end()), c = m.withCapacity(4 * this.ins.length), this.ins.forEach((l) => c.writeUInt32(l.sequence)), O = N(c.end());
224
+ }
225
+ if (w || S) {
226
+ if (S && t < this.outs.length) {
227
+ const c = this.outs[t], l = m.withCapacity(8 + p(c.script));
228
+ l.writeUInt64(c.value), l.writeVarSlice(c.script), G = N(l.end());
229
+ }
230
+ } else {
231
+ if (!this.outs.length)
232
+ throw new Error("Add outputs to the transaction before signing.");
233
+ const c = this.outs.map((E) => 8 + p(E.script)).reduce((E, X) => E + X), l = m.withCapacity(c);
234
+ this.outs.forEach((E) => {
235
+ l.writeUInt64(E.value), l.writeVarSlice(E.script);
236
+ }), G = N(l.end());
237
+ }
238
+ const j = (n ? 2 : 0) + (r ? 1 : 0), x = 174 - (d ? 49 : 0) - (w ? 32 : 0) + (r ? 32 : 0) + (n ? 37 : 0), u = m.withCapacity(x);
239
+ if (u.writeUInt8(i), u.writeInt32(this.version), u.writeUInt32(this.locktime), u.writeSlice(U), u.writeSlice(v), u.writeSlice(L), u.writeSlice(O), w || S || u.writeSlice(G), u.writeUInt8(j), d) {
240
+ const c = this.ins[t];
241
+ u.writeSlice(c.hash), u.writeUInt32(c.index), u.writeUInt64(s[t]), u.writeVarSlice(e[t]), u.writeUInt32(c.sequence);
242
+ } else
243
+ u.writeUInt32(t);
244
+ if (r) {
245
+ const c = m.withCapacity(p(r));
246
+ c.writeVarSlice(r), u.writeSlice(N(c.end()));
247
+ }
248
+ return S && u.writeSlice(G), n && (u.writeSlice(n), u.writeUInt8(0), u.writeUInt32(4294967295)), Z(
249
+ "TapSighash",
250
+ a.concat([a.from([0]), u.end()])
251
+ );
252
+ }
253
+ hashForWitnessV0(t, e, s, i) {
254
+ A(C(b, _, W, b), arguments);
255
+ let n = a.from([]), r, h = k, g = k, d = k;
256
+ if (i & o.SIGHASH_ANYONECANPAY || (n = a.allocUnsafe(36 * this.ins.length), r = new m(n, 0), this.ins.forEach((S) => {
257
+ r.writeSlice(S.hash), r.writeUInt32(S.index);
258
+ }), g = I(n)), !(i & o.SIGHASH_ANYONECANPAY) && (i & 31) !== o.SIGHASH_SINGLE && (i & 31) !== o.SIGHASH_NONE && (n = a.allocUnsafe(4 * this.ins.length), r = new m(n, 0), this.ins.forEach((S) => {
259
+ r.writeUInt32(S.sequence);
260
+ }), d = I(n)), (i & 31) !== o.SIGHASH_SINGLE && (i & 31) !== o.SIGHASH_NONE) {
261
+ const S = this.outs.reduce((U, v) => U + 8 + p(v.script), 0);
262
+ n = a.allocUnsafe(S), r = new m(n, 0), this.outs.forEach((U) => {
263
+ r.writeUInt64(U.value), r.writeVarSlice(U.script);
264
+ }), h = I(n);
265
+ } else if ((i & 31) === o.SIGHASH_SINGLE && t < this.outs.length) {
266
+ const S = this.outs[t];
267
+ n = a.allocUnsafe(8 + p(S.script)), r = new m(n, 0), r.writeUInt64(S.value), r.writeVarSlice(S.script), h = I(n);
268
+ }
269
+ n = a.allocUnsafe(156 + p(e)), r = new m(n, 0);
270
+ const w = this.ins[t];
271
+ return r.writeInt32(this.version), r.writeSlice(g), r.writeSlice(d), r.writeSlice(w.hash), r.writeUInt32(w.index), r.writeVarSlice(e), r.writeUInt64(s), r.writeUInt32(w.sequence), r.writeSlice(h), r.writeUInt32(this.locktime), r.writeUInt32(i), I(n);
272
+ }
273
+ getHash(t) {
274
+ return t && this.isCoinbase() ? a.alloc(32, 0) : I(this.__toBuffer(void 0, void 0, t));
275
+ }
276
+ getId() {
277
+ return M(this.getHash(!1)).toString("hex");
278
+ }
279
+ toBuffer(t, e) {
280
+ return this.__toBuffer(t, e, !0);
281
+ }
282
+ toHex() {
283
+ return this.toBuffer(void 0, void 0).toString("hex");
284
+ }
285
+ setInputScript(t, e) {
286
+ A(C(V, _), arguments), this.ins[t].script = e;
287
+ }
288
+ setWitness(t, e) {
289
+ A(C(V, [_]), arguments), this.ins[t].witness = e;
290
+ }
291
+ __toBuffer(t, e, s = !1) {
292
+ t || (t = a.allocUnsafe(this.byteLength(s)));
293
+ const i = new m(t, e || 0);
294
+ i.writeInt32(this.version);
295
+ const n = s && this.hasWitnesses();
296
+ return n && (i.writeUInt8(o.ADVANCED_TRANSACTION_MARKER), i.writeUInt8(o.ADVANCED_TRANSACTION_FLAG)), i.writeVarInt(this.ins.length), this.ins.forEach((r) => {
297
+ i.writeSlice(r.hash), i.writeUInt32(r.index), i.writeVarSlice(r.script), i.writeUInt32(r.sequence);
298
+ }), i.writeVarInt(this.outs.length), this.outs.forEach((r) => {
299
+ ht(r) ? i.writeUInt64(r.value) : i.writeSlice(r.valueBuffer), i.writeVarSlice(r.script);
300
+ }), n && this.ins.forEach((r) => {
301
+ i.writeVector(r.witness);
302
+ }), i.writeUInt32(this.locktime), e !== void 0 ? t.slice(e, i.offset) : t;
303
+ }
304
+ }
305
+ const wt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
306
+ __proto__: null,
307
+ Transaction: o
308
+ }, Symbol.toStringTag, { value: "Module" })), { typeforce: ct } = K, B = new TypeError("Cannot compute merkle root for zero transactions"), q = new TypeError("Cannot compute witness commit for non-segwit block");
309
+ class y {
310
+ constructor() {
311
+ this.version = 1, this.prevHash = void 0, this.merkleRoot = void 0, this.timestamp = 0, this.witnessCommit = void 0, this.bits = 0, this.nonce = 0, this.transactions = void 0;
312
+ }
313
+ static fromBuffer(t) {
314
+ if (t.length < 80) throw new Error("Buffer too small (< 80 bytes)");
315
+ const e = new z(t), s = new y();
316
+ if (s.version = e.readInt32(), s.prevHash = e.readSlice(32), s.merkleRoot = e.readSlice(32), s.timestamp = e.readUInt32(), s.bits = e.readUInt32(), s.nonce = e.readUInt32(), t.length === 80) return s;
317
+ const i = () => {
318
+ const h = o.fromBuffer(e.buffer.slice(e.offset), !0);
319
+ return e.offset += h.byteLength(), h;
320
+ }, n = e.readVarInt();
321
+ s.transactions = [];
322
+ for (let h = 0; h < n; ++h) {
323
+ const g = i();
324
+ s.transactions.push(g);
325
+ }
326
+ const r = s.getWitnessCommit();
327
+ return r && (s.witnessCommit = r), s;
328
+ }
329
+ static fromHex(t) {
330
+ return y.fromBuffer(a.from(t, "hex"));
331
+ }
332
+ static calculateTarget(t) {
333
+ const e = ((t & 4278190080) >> 24) - 3, s = t & 8388607, i = a.alloc(32, 0);
334
+ return i.writeUIntBE(s, 29 - e, 3), i;
335
+ }
336
+ static calculateMerkleRoot(t, e) {
337
+ if (ct([{ getHash: Q }], t), t.length === 0) throw B;
338
+ if (e && !Y(t)) throw q;
339
+ const s = t.map((n) => n.getHash(e)), i = J(s, I);
340
+ return e ? I(a.concat([i, t[0].ins[0].witness[0]])) : i;
341
+ }
342
+ getWitnessCommit() {
343
+ if (!Y(this.transactions)) return null;
344
+ const t = this.transactions[0].outs.filter(
345
+ (s) => s.script.slice(0, 6).equals(a.from("6a24aa21a9ed", "hex"))
346
+ ).map((s) => s.script.slice(6, 38));
347
+ if (t.length === 0) return null;
348
+ const e = t[t.length - 1];
349
+ return e instanceof a && e.length === 32 ? e : null;
350
+ }
351
+ hasWitnessCommit() {
352
+ return this.witnessCommit instanceof a && this.witnessCommit.length === 32 || this.getWitnessCommit() !== null;
353
+ }
354
+ hasWitness() {
355
+ return at(this.transactions);
356
+ }
357
+ weight() {
358
+ const t = this.byteLength(!1, !1), e = this.byteLength(!1, !0);
359
+ return t * 3 + e;
360
+ }
361
+ byteLength(t, e = !0) {
362
+ return t || !this.transactions ? 80 : 80 + R(this.transactions.length) + this.transactions.reduce((s, i) => s + i.byteLength(e), 0);
363
+ }
364
+ getHash() {
365
+ return I(this.toBuffer(!0));
366
+ }
367
+ getId() {
368
+ return M(this.getHash()).toString("hex");
369
+ }
370
+ getUTCDate() {
371
+ const t = /* @__PURE__ */ new Date(0);
372
+ return t.setUTCSeconds(this.timestamp), t;
373
+ }
374
+ // TODO: buffer, offset compatibility
375
+ toBuffer(t) {
376
+ const e = a.allocUnsafe(this.byteLength(t)), s = new m(e);
377
+ if (s.writeInt32(this.version), s.writeSlice(this.prevHash), s.writeSlice(this.merkleRoot), s.writeUInt32(this.timestamp), s.writeUInt32(this.bits), s.writeUInt32(this.nonce), t || !this.transactions) return e;
378
+ const i = tt(this.transactions.length, e, s.offset);
379
+ return s.offset += i.bytes, this.transactions.forEach((n) => {
380
+ const r = n.byteLength();
381
+ n.toBuffer(e, s.offset), s.offset += r;
382
+ }), e;
383
+ }
384
+ toHex(t) {
385
+ return this.toBuffer(t).toString("hex");
386
+ }
387
+ checkTxRoots() {
388
+ const t = this.hasWitnessCommit();
389
+ return !t && this.hasWitness() ? !1 : this.__checkMerkleRoot() && (t ? this.__checkWitnessCommit() : !0);
390
+ }
391
+ checkProofOfWork() {
392
+ const t = M(this.getHash()), e = y.calculateTarget(this.bits);
393
+ return t.compare(e) <= 0;
394
+ }
395
+ __checkMerkleRoot() {
396
+ if (!this.transactions) throw B;
397
+ const t = y.calculateMerkleRoot(this.transactions);
398
+ return this.merkleRoot.compare(t) === 0;
399
+ }
400
+ __checkWitnessCommit() {
401
+ if (!this.transactions) throw B;
402
+ if (!this.hasWitnessCommit()) throw q;
403
+ const t = y.calculateMerkleRoot(this.transactions, !0);
404
+ return this.witnessCommit.compare(t) === 0;
405
+ }
406
+ }
407
+ function Y(f) {
408
+ return f instanceof Array && f[0] && f[0].ins && f[0].ins instanceof Array && f[0].ins[0] && f[0].ins[0].witness && f[0].ins[0].witness instanceof Array && f[0].ins[0].witness.length > 0;
409
+ }
410
+ function at(f) {
411
+ return f instanceof Array && f.some(
412
+ (t) => typeof t == "object" && t.ins instanceof Array && t.ins.some(
413
+ (e) => typeof e == "object" && e.witness instanceof Array && e.witness.length > 0
414
+ )
415
+ );
416
+ }
417
+ export {
418
+ y as B,
419
+ o as T,
420
+ wt as a
421
+ };