@btc-vision/bitcoin 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/package.json +95 -0
  4. package/src/address.d.ts +42 -0
  5. package/src/address.js +191 -0
  6. package/src/bip66.d.ts +7 -0
  7. package/src/bip66.js +103 -0
  8. package/src/block.d.ts +30 -0
  9. package/src/block.js +224 -0
  10. package/src/bufferutils.d.ts +54 -0
  11. package/src/bufferutils.js +182 -0
  12. package/src/crypto.d.ts +18 -0
  13. package/src/crypto.js +128 -0
  14. package/src/ecc_lib.d.ts +17 -0
  15. package/src/ecc_lib.js +122 -0
  16. package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
  17. package/src/hooks/AdvancedSignatureManager.js +88 -0
  18. package/src/hooks/HookedSigner.d.ts +4 -0
  19. package/src/hooks/HookedSigner.js +90 -0
  20. package/src/hooks/SignatureManager.d.ts +35 -0
  21. package/src/hooks/SignatureManager.js +72 -0
  22. package/src/index.d.ts +42 -0
  23. package/src/index.js +87 -0
  24. package/src/merkle.d.ts +10 -0
  25. package/src/merkle.js +30 -0
  26. package/src/networks.d.ts +29 -0
  27. package/src/networks.js +71 -0
  28. package/src/ops.d.ts +126 -0
  29. package/src/ops.js +131 -0
  30. package/src/payments/bip341.d.ts +49 -0
  31. package/src/payments/bip341.js +124 -0
  32. package/src/payments/embed.d.ts +9 -0
  33. package/src/payments/embed.js +54 -0
  34. package/src/payments/index.d.ts +48 -0
  35. package/src/payments/index.js +69 -0
  36. package/src/payments/lazy.d.ts +2 -0
  37. package/src/payments/lazy.js +32 -0
  38. package/src/payments/p2ms.d.ts +9 -0
  39. package/src/payments/p2ms.js +158 -0
  40. package/src/payments/p2pk.d.ts +10 -0
  41. package/src/payments/p2pk.js +82 -0
  42. package/src/payments/p2pkh.d.ts +10 -0
  43. package/src/payments/p2pkh.js +143 -0
  44. package/src/payments/p2sh.d.ts +10 -0
  45. package/src/payments/p2sh.js +204 -0
  46. package/src/payments/p2tr.d.ts +10 -0
  47. package/src/payments/p2tr.js +315 -0
  48. package/src/payments/p2wpkh.d.ts +10 -0
  49. package/src/payments/p2wpkh.js +146 -0
  50. package/src/payments/p2wsh.d.ts +10 -0
  51. package/src/payments/p2wsh.js +226 -0
  52. package/src/psbt/bip371.d.ts +42 -0
  53. package/src/psbt/bip371.js +424 -0
  54. package/src/psbt/psbtutils.d.ts +64 -0
  55. package/src/psbt/psbtutils.js +191 -0
  56. package/src/psbt.d.ts +235 -0
  57. package/src/psbt.js +1825 -0
  58. package/src/push_data.d.ts +29 -0
  59. package/src/push_data.js +83 -0
  60. package/src/script.d.ts +42 -0
  61. package/src/script.js +231 -0
  62. package/src/script_number.d.ts +19 -0
  63. package/src/script_number.js +78 -0
  64. package/src/script_signature.d.ts +21 -0
  65. package/src/script_signature.js +79 -0
  66. package/src/transaction.d.ts +60 -0
  67. package/src/transaction.js +571 -0
  68. package/src/types.d.ts +54 -0
  69. package/src/types.js +106 -0
@@ -0,0 +1,571 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.Transaction = void 0;
4
+ const bufferutils_1 = require('./bufferutils');
5
+ const bcrypto = require('./crypto');
6
+ const bscript = require('./script');
7
+ const script_1 = require('./script');
8
+ const types = require('./types');
9
+ const { typeforce } = types;
10
+ function varSliceSize(someScript) {
11
+ const length = someScript.length;
12
+ return bufferutils_1.varuint.encodingLength(length) + length;
13
+ }
14
+ function vectorSize(someVector) {
15
+ const length = someVector.length;
16
+ return (
17
+ bufferutils_1.varuint.encodingLength(length) +
18
+ someVector.reduce((sum, witness) => {
19
+ return sum + varSliceSize(witness);
20
+ }, 0)
21
+ );
22
+ }
23
+ const EMPTY_BUFFER = Buffer.allocUnsafe(0);
24
+ const EMPTY_WITNESS = [];
25
+ const ZERO = Buffer.from(
26
+ '0000000000000000000000000000000000000000000000000000000000000000',
27
+ 'hex',
28
+ );
29
+ const ONE = Buffer.from(
30
+ '0000000000000000000000000000000000000000000000000000000000000001',
31
+ 'hex',
32
+ );
33
+ const VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex');
34
+ const BLANK_OUTPUT = {
35
+ script: EMPTY_BUFFER,
36
+ valueBuffer: VALUE_UINT64_MAX,
37
+ };
38
+ function isOutput(out) {
39
+ return out.value !== undefined;
40
+ }
41
+ /**
42
+ * Represents a Bitcoin transaction.
43
+ */
44
+ class Transaction {
45
+ static DEFAULT_SEQUENCE = 0xffffffff;
46
+ static SIGHASH_DEFAULT = 0x00;
47
+ static SIGHASH_ALL = 0x01;
48
+ static SIGHASH_NONE = 0x02;
49
+ static SIGHASH_SINGLE = 0x03;
50
+ static SIGHASH_ANYONECANPAY = 0x80;
51
+ static SIGHASH_OUTPUT_MASK = 0x03;
52
+ static SIGHASH_INPUT_MASK = 0x80;
53
+ static ADVANCED_TRANSACTION_MARKER = 0x00;
54
+ static ADVANCED_TRANSACTION_FLAG = 0x01;
55
+ version = 1;
56
+ locktime = 0;
57
+ ins = [];
58
+ outs = [];
59
+ static fromBuffer(buffer, _NO_STRICT) {
60
+ const bufferReader = new bufferutils_1.BufferReader(buffer);
61
+ const tx = new Transaction();
62
+ tx.version = bufferReader.readInt32();
63
+ const marker = bufferReader.readUInt8();
64
+ const flag = bufferReader.readUInt8();
65
+ let hasWitnesses = false;
66
+ if (
67
+ marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
68
+ flag === Transaction.ADVANCED_TRANSACTION_FLAG
69
+ ) {
70
+ hasWitnesses = true;
71
+ } else {
72
+ bufferReader.offset -= 2;
73
+ }
74
+ const vinLen = bufferReader.readVarInt();
75
+ for (let i = 0; i < vinLen; ++i) {
76
+ tx.ins.push({
77
+ hash: bufferReader.readSlice(32),
78
+ index: bufferReader.readUInt32(),
79
+ script: bufferReader.readVarSlice(),
80
+ sequence: bufferReader.readUInt32(),
81
+ witness: EMPTY_WITNESS,
82
+ });
83
+ }
84
+ const voutLen = bufferReader.readVarInt();
85
+ for (let i = 0; i < voutLen; ++i) {
86
+ tx.outs.push({
87
+ value: bufferReader.readUInt64(),
88
+ script: bufferReader.readVarSlice(),
89
+ });
90
+ }
91
+ if (hasWitnesses) {
92
+ for (let i = 0; i < vinLen; ++i) {
93
+ tx.ins[i].witness = bufferReader.readVector();
94
+ }
95
+ // was this pointless?
96
+ if (!tx.hasWitnesses())
97
+ throw new Error('Transaction has superfluous witness data');
98
+ }
99
+ tx.locktime = bufferReader.readUInt32();
100
+ if (_NO_STRICT) return tx;
101
+ if (bufferReader.offset !== buffer.length)
102
+ throw new Error('Transaction has unexpected data');
103
+ return tx;
104
+ }
105
+ static fromHex(hex) {
106
+ return Transaction.fromBuffer(Buffer.from(hex, 'hex'), false);
107
+ }
108
+ static isCoinbaseHash(buffer) {
109
+ typeforce(types.Hash256bit, buffer);
110
+ for (let i = 0; i < 32; ++i) {
111
+ if (buffer[i] !== 0) return false;
112
+ }
113
+ return true;
114
+ }
115
+ isCoinbase() {
116
+ return (
117
+ this.ins.length === 1 &&
118
+ Transaction.isCoinbaseHash(this.ins[0].hash)
119
+ );
120
+ }
121
+ addInput(hash, index, sequence, scriptSig) {
122
+ typeforce(
123
+ types.tuple(
124
+ types.Hash256bit,
125
+ types.UInt32,
126
+ types.maybe(types.UInt32),
127
+ types.maybe(types.Buffer),
128
+ ),
129
+ arguments,
130
+ );
131
+ if (types.Null(sequence)) {
132
+ sequence = Transaction.DEFAULT_SEQUENCE;
133
+ }
134
+ // Add the input and return the input's index
135
+ return (
136
+ this.ins.push({
137
+ hash,
138
+ index,
139
+ script: scriptSig || EMPTY_BUFFER,
140
+ sequence: sequence,
141
+ witness: EMPTY_WITNESS,
142
+ }) - 1
143
+ );
144
+ }
145
+ addOutput(scriptPubKey, value) {
146
+ typeforce(types.tuple(types.Buffer, types.Satoshi), arguments);
147
+ // Add the output and return the output's index
148
+ return (
149
+ this.outs.push({
150
+ script: scriptPubKey,
151
+ value,
152
+ }) - 1
153
+ );
154
+ }
155
+ hasWitnesses() {
156
+ return this.ins.some(x => {
157
+ return x.witness.length !== 0;
158
+ });
159
+ }
160
+ weight() {
161
+ const base = this.byteLength(false);
162
+ const total = this.byteLength(true);
163
+ return base * 3 + total;
164
+ }
165
+ virtualSize() {
166
+ return Math.ceil(this.weight() / 4);
167
+ }
168
+ byteLength(_ALLOW_WITNESS = true) {
169
+ const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
170
+ return (
171
+ (hasWitnesses ? 10 : 8) +
172
+ bufferutils_1.varuint.encodingLength(this.ins.length) +
173
+ bufferutils_1.varuint.encodingLength(this.outs.length) +
174
+ this.ins.reduce((sum, input) => {
175
+ return sum + 40 + varSliceSize(input.script);
176
+ }, 0) +
177
+ this.outs.reduce((sum, output) => {
178
+ return sum + 8 + varSliceSize(output.script);
179
+ }, 0) +
180
+ (hasWitnesses
181
+ ? this.ins.reduce((sum, input) => {
182
+ return sum + vectorSize(input.witness);
183
+ }, 0)
184
+ : 0)
185
+ );
186
+ }
187
+ clone() {
188
+ const newTx = new Transaction();
189
+ newTx.version = this.version;
190
+ newTx.locktime = this.locktime;
191
+ newTx.ins = this.ins.map(txIn => {
192
+ return {
193
+ hash: txIn.hash,
194
+ index: txIn.index,
195
+ script: txIn.script,
196
+ sequence: txIn.sequence,
197
+ witness: txIn.witness,
198
+ };
199
+ });
200
+ newTx.outs = this.outs.map(txOut => {
201
+ return {
202
+ script: txOut.script,
203
+ value: txOut.value,
204
+ };
205
+ });
206
+ return newTx;
207
+ }
208
+ /**
209
+ * Hash transaction for signing a specific input.
210
+ *
211
+ * Bitcoin uses a different hash for each signed transaction input.
212
+ * This method copies the transaction, makes the necessary changes based on the
213
+ * hashType, and then hashes the result.
214
+ * This hash can then be used to sign the provided transaction input.
215
+ */
216
+ hashForSignature(inIndex, prevOutScript, hashType) {
217
+ typeforce(
218
+ types.tuple(
219
+ types.UInt32,
220
+ types.Buffer,
221
+ /* types.UInt8 */ types.Number,
222
+ ),
223
+ arguments,
224
+ );
225
+ // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
226
+ if (inIndex >= this.ins.length) return ONE;
227
+ // ignore OP_CODESEPARATOR
228
+ const ourScript = bscript.compile(
229
+ bscript.decompile(prevOutScript).filter(x => {
230
+ return x !== script_1.OPS.OP_CODESEPARATOR;
231
+ }),
232
+ );
233
+ const txTmp = this.clone();
234
+ // SIGHASH_NONE: ignore all outputs? (wildcard payee)
235
+ if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
236
+ txTmp.outs = [];
237
+ // ignore sequence numbers (except at inIndex)
238
+ txTmp.ins.forEach((input, i) => {
239
+ if (i === inIndex) return;
240
+ input.sequence = 0;
241
+ });
242
+ // SIGHASH_SINGLE: ignore all outputs, except at the same index?
243
+ } else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
244
+ // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
245
+ if (inIndex >= this.outs.length) return ONE;
246
+ // truncate outputs after
247
+ txTmp.outs.length = inIndex + 1;
248
+ // "blank" outputs before
249
+ for (let i = 0; i < inIndex; i++) {
250
+ txTmp.outs[i] = BLANK_OUTPUT;
251
+ }
252
+ // ignore sequence numbers (except at inIndex)
253
+ txTmp.ins.forEach((input, y) => {
254
+ if (y === inIndex) return;
255
+ input.sequence = 0;
256
+ });
257
+ }
258
+ // SIGHASH_ANYONECANPAY: ignore inputs entirely?
259
+ if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
260
+ txTmp.ins = [txTmp.ins[inIndex]];
261
+ txTmp.ins[0].script = ourScript;
262
+ // SIGHASH_ALL: only ignore input scripts
263
+ } else {
264
+ // "blank" others input scripts
265
+ txTmp.ins.forEach(input => {
266
+ input.script = EMPTY_BUFFER;
267
+ });
268
+ txTmp.ins[inIndex].script = ourScript;
269
+ }
270
+ // serialize and hash
271
+ const buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
272
+ buffer.writeInt32LE(hashType, buffer.length - 4);
273
+ txTmp.__toBuffer(buffer, 0, false);
274
+ return bcrypto.hash256(buffer);
275
+ }
276
+ hashForWitnessV1(
277
+ inIndex,
278
+ prevOutScripts,
279
+ values,
280
+ hashType,
281
+ leafHash,
282
+ annex,
283
+ ) {
284
+ // https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#common-signature-message
285
+ typeforce(
286
+ types.tuple(
287
+ types.UInt32,
288
+ typeforce.arrayOf(types.Buffer),
289
+ typeforce.arrayOf(types.Satoshi),
290
+ types.UInt32,
291
+ ),
292
+ arguments,
293
+ );
294
+ if (
295
+ values.length !== this.ins.length ||
296
+ prevOutScripts.length !== this.ins.length
297
+ ) {
298
+ throw new Error(
299
+ 'Must supply prevout script and value for all inputs',
300
+ );
301
+ }
302
+ const outputType =
303
+ hashType === Transaction.SIGHASH_DEFAULT
304
+ ? Transaction.SIGHASH_ALL
305
+ : hashType & Transaction.SIGHASH_OUTPUT_MASK;
306
+ const inputType = hashType & Transaction.SIGHASH_INPUT_MASK;
307
+ const isAnyoneCanPay = inputType === Transaction.SIGHASH_ANYONECANPAY;
308
+ const isNone = outputType === Transaction.SIGHASH_NONE;
309
+ const isSingle = outputType === Transaction.SIGHASH_SINGLE;
310
+ let hashPrevouts = EMPTY_BUFFER;
311
+ let hashAmounts = EMPTY_BUFFER;
312
+ let hashScriptPubKeys = EMPTY_BUFFER;
313
+ let hashSequences = EMPTY_BUFFER;
314
+ let hashOutputs = EMPTY_BUFFER;
315
+ if (!isAnyoneCanPay) {
316
+ let bufferWriter = bufferutils_1.BufferWriter.withCapacity(
317
+ 36 * this.ins.length,
318
+ );
319
+ this.ins.forEach(txIn => {
320
+ bufferWriter.writeSlice(txIn.hash);
321
+ bufferWriter.writeUInt32(txIn.index);
322
+ });
323
+ hashPrevouts = bcrypto.sha256(bufferWriter.end());
324
+ bufferWriter = bufferutils_1.BufferWriter.withCapacity(
325
+ 8 * this.ins.length,
326
+ );
327
+ values.forEach(value => bufferWriter.writeUInt64(value));
328
+ hashAmounts = bcrypto.sha256(bufferWriter.end());
329
+ bufferWriter = bufferutils_1.BufferWriter.withCapacity(
330
+ prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
331
+ );
332
+ prevOutScripts.forEach(prevOutScript =>
333
+ bufferWriter.writeVarSlice(prevOutScript),
334
+ );
335
+ hashScriptPubKeys = bcrypto.sha256(bufferWriter.end());
336
+ bufferWriter = bufferutils_1.BufferWriter.withCapacity(
337
+ 4 * this.ins.length,
338
+ );
339
+ this.ins.forEach(txIn => bufferWriter.writeUInt32(txIn.sequence));
340
+ hashSequences = bcrypto.sha256(bufferWriter.end());
341
+ }
342
+ if (!(isNone || isSingle)) {
343
+ if (!this.outs.length)
344
+ throw new Error(
345
+ 'Add outputs to the transaction before signing.',
346
+ );
347
+ const txOutsSize = this.outs
348
+ .map(output => 8 + varSliceSize(output.script))
349
+ .reduce((a, b) => a + b);
350
+ const bufferWriter =
351
+ bufferutils_1.BufferWriter.withCapacity(txOutsSize);
352
+ this.outs.forEach(out => {
353
+ bufferWriter.writeUInt64(out.value);
354
+ bufferWriter.writeVarSlice(out.script);
355
+ });
356
+ hashOutputs = bcrypto.sha256(bufferWriter.end());
357
+ } else if (isSingle && inIndex < this.outs.length) {
358
+ const output = this.outs[inIndex];
359
+ const bufferWriter = bufferutils_1.BufferWriter.withCapacity(
360
+ 8 + varSliceSize(output.script),
361
+ );
362
+ bufferWriter.writeUInt64(output.value);
363
+ bufferWriter.writeVarSlice(output.script);
364
+ hashOutputs = bcrypto.sha256(bufferWriter.end());
365
+ }
366
+ const spendType = (leafHash ? 2 : 0) + (annex ? 1 : 0);
367
+ // Length calculation from:
368
+ // https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-14
369
+ // With extension from:
370
+ // https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#signature-validation
371
+ const sigMsgSize =
372
+ 174 -
373
+ (isAnyoneCanPay ? 49 : 0) -
374
+ (isNone ? 32 : 0) +
375
+ (annex ? 32 : 0) +
376
+ (leafHash ? 37 : 0);
377
+ const sigMsgWriter =
378
+ bufferutils_1.BufferWriter.withCapacity(sigMsgSize);
379
+ sigMsgWriter.writeUInt8(hashType);
380
+ // Transaction
381
+ sigMsgWriter.writeInt32(this.version);
382
+ sigMsgWriter.writeUInt32(this.locktime);
383
+ sigMsgWriter.writeSlice(hashPrevouts);
384
+ sigMsgWriter.writeSlice(hashAmounts);
385
+ sigMsgWriter.writeSlice(hashScriptPubKeys);
386
+ sigMsgWriter.writeSlice(hashSequences);
387
+ if (!(isNone || isSingle)) {
388
+ sigMsgWriter.writeSlice(hashOutputs);
389
+ }
390
+ // Input
391
+ sigMsgWriter.writeUInt8(spendType);
392
+ if (isAnyoneCanPay) {
393
+ const input = this.ins[inIndex];
394
+ sigMsgWriter.writeSlice(input.hash);
395
+ sigMsgWriter.writeUInt32(input.index);
396
+ sigMsgWriter.writeUInt64(values[inIndex]);
397
+ sigMsgWriter.writeVarSlice(prevOutScripts[inIndex]);
398
+ sigMsgWriter.writeUInt32(input.sequence);
399
+ } else {
400
+ sigMsgWriter.writeUInt32(inIndex);
401
+ }
402
+ if (annex) {
403
+ const bufferWriter = bufferutils_1.BufferWriter.withCapacity(
404
+ varSliceSize(annex),
405
+ );
406
+ bufferWriter.writeVarSlice(annex);
407
+ sigMsgWriter.writeSlice(bcrypto.sha256(bufferWriter.end()));
408
+ }
409
+ // Output
410
+ if (isSingle) {
411
+ sigMsgWriter.writeSlice(hashOutputs);
412
+ }
413
+ // BIP342 extension
414
+ if (leafHash) {
415
+ sigMsgWriter.writeSlice(leafHash);
416
+ sigMsgWriter.writeUInt8(0);
417
+ sigMsgWriter.writeUInt32(0xffffffff);
418
+ }
419
+ // Extra zero byte because:
420
+ // https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-19
421
+ return bcrypto.taggedHash(
422
+ 'TapSighash',
423
+ Buffer.concat([Buffer.from([0x00]), sigMsgWriter.end()]),
424
+ );
425
+ }
426
+ hashForWitnessV0(inIndex, prevOutScript, value, hashType) {
427
+ typeforce(
428
+ types.tuple(
429
+ types.UInt32,
430
+ types.Buffer,
431
+ types.Satoshi,
432
+ types.UInt32,
433
+ ),
434
+ arguments,
435
+ );
436
+ let tbuffer = Buffer.from([]);
437
+ let bufferWriter;
438
+ let hashOutputs = ZERO;
439
+ let hashPrevouts = ZERO;
440
+ let hashSequence = ZERO;
441
+ if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
442
+ tbuffer = Buffer.allocUnsafe(36 * this.ins.length);
443
+ bufferWriter = new bufferutils_1.BufferWriter(tbuffer, 0);
444
+ this.ins.forEach(txIn => {
445
+ bufferWriter.writeSlice(txIn.hash);
446
+ bufferWriter.writeUInt32(txIn.index);
447
+ });
448
+ hashPrevouts = bcrypto.hash256(tbuffer);
449
+ }
450
+ if (
451
+ !(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
452
+ (hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
453
+ (hashType & 0x1f) !== Transaction.SIGHASH_NONE
454
+ ) {
455
+ tbuffer = Buffer.allocUnsafe(4 * this.ins.length);
456
+ bufferWriter = new bufferutils_1.BufferWriter(tbuffer, 0);
457
+ this.ins.forEach(txIn => {
458
+ bufferWriter.writeUInt32(txIn.sequence);
459
+ });
460
+ hashSequence = bcrypto.hash256(tbuffer);
461
+ }
462
+ if (
463
+ (hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
464
+ (hashType & 0x1f) !== Transaction.SIGHASH_NONE
465
+ ) {
466
+ const txOutsSize = this.outs.reduce((sum, output) => {
467
+ return sum + 8 + varSliceSize(output.script);
468
+ }, 0);
469
+ tbuffer = Buffer.allocUnsafe(txOutsSize);
470
+ bufferWriter = new bufferutils_1.BufferWriter(tbuffer, 0);
471
+ this.outs.forEach(out => {
472
+ bufferWriter.writeUInt64(out.value);
473
+ bufferWriter.writeVarSlice(out.script);
474
+ });
475
+ hashOutputs = bcrypto.hash256(tbuffer);
476
+ } else if (
477
+ (hashType & 0x1f) === Transaction.SIGHASH_SINGLE &&
478
+ inIndex < this.outs.length
479
+ ) {
480
+ const output = this.outs[inIndex];
481
+ tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script));
482
+ bufferWriter = new bufferutils_1.BufferWriter(tbuffer, 0);
483
+ bufferWriter.writeUInt64(output.value);
484
+ bufferWriter.writeVarSlice(output.script);
485
+ hashOutputs = bcrypto.hash256(tbuffer);
486
+ }
487
+ tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
488
+ bufferWriter = new bufferutils_1.BufferWriter(tbuffer, 0);
489
+ const input = this.ins[inIndex];
490
+ bufferWriter.writeInt32(this.version);
491
+ bufferWriter.writeSlice(hashPrevouts);
492
+ bufferWriter.writeSlice(hashSequence);
493
+ bufferWriter.writeSlice(input.hash);
494
+ bufferWriter.writeUInt32(input.index);
495
+ bufferWriter.writeVarSlice(prevOutScript);
496
+ bufferWriter.writeUInt64(value);
497
+ bufferWriter.writeUInt32(input.sequence);
498
+ bufferWriter.writeSlice(hashOutputs);
499
+ bufferWriter.writeUInt32(this.locktime);
500
+ bufferWriter.writeUInt32(hashType);
501
+ return bcrypto.hash256(tbuffer);
502
+ }
503
+ getHash(forWitness) {
504
+ // wtxid for coinbase is always 32 bytes of 0x00
505
+ if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0);
506
+ return bcrypto.hash256(
507
+ this.__toBuffer(undefined, undefined, forWitness),
508
+ );
509
+ }
510
+ getId() {
511
+ // transaction hash's are displayed in reverse order
512
+ return (0, bufferutils_1.reverseBuffer)(this.getHash(false)).toString(
513
+ 'hex',
514
+ );
515
+ }
516
+ toBuffer(buffer, initialOffset) {
517
+ return this.__toBuffer(buffer, initialOffset, true);
518
+ }
519
+ toHex() {
520
+ return this.toBuffer(undefined, undefined).toString('hex');
521
+ }
522
+ setInputScript(index, scriptSig) {
523
+ typeforce(types.tuple(types.Number, types.Buffer), arguments);
524
+ this.ins[index].script = scriptSig;
525
+ }
526
+ setWitness(index, witness) {
527
+ typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
528
+ this.ins[index].witness = witness;
529
+ }
530
+ __toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
531
+ if (!buffer)
532
+ buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
533
+ const bufferWriter = new bufferutils_1.BufferWriter(
534
+ buffer,
535
+ initialOffset || 0,
536
+ );
537
+ bufferWriter.writeInt32(this.version);
538
+ const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
539
+ if (hasWitnesses) {
540
+ bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
541
+ bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
542
+ }
543
+ bufferWriter.writeVarInt(this.ins.length);
544
+ this.ins.forEach(txIn => {
545
+ bufferWriter.writeSlice(txIn.hash);
546
+ bufferWriter.writeUInt32(txIn.index);
547
+ bufferWriter.writeVarSlice(txIn.script);
548
+ bufferWriter.writeUInt32(txIn.sequence);
549
+ });
550
+ bufferWriter.writeVarInt(this.outs.length);
551
+ this.outs.forEach(txOut => {
552
+ if (isOutput(txOut)) {
553
+ bufferWriter.writeUInt64(txOut.value);
554
+ } else {
555
+ bufferWriter.writeSlice(txOut.valueBuffer);
556
+ }
557
+ bufferWriter.writeVarSlice(txOut.script);
558
+ });
559
+ if (hasWitnesses) {
560
+ this.ins.forEach(input => {
561
+ bufferWriter.writeVector(input.witness);
562
+ });
563
+ }
564
+ bufferWriter.writeUInt32(this.locktime);
565
+ // avoid slicing unless necessary
566
+ if (initialOffset !== undefined)
567
+ return buffer.slice(initialOffset, bufferWriter.offset);
568
+ return buffer;
569
+ }
570
+ }
571
+ exports.Transaction = Transaction;
package/src/types.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ /// <reference types="node" />
2
+ export declare const typeforce: any;
3
+ /**
4
+ * Checks if two arrays of Buffers are equal.
5
+ * @param a - The first array of Buffers.
6
+ * @param b - The second array of Buffers.
7
+ * @returns True if the arrays are equal, false otherwise.
8
+ */
9
+ export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
10
+ /**
11
+ * Checks if the given value is a valid elliptic curve point.
12
+ * @param p - The value to check.
13
+ * @returns True if the value is a valid elliptic curve point, false otherwise.
14
+ */
15
+ export declare function isPoint(p: Buffer | number | undefined | null): boolean;
16
+ export declare function Satoshi(value: number): boolean;
17
+ export interface XOnlyPointAddTweakResult {
18
+ parity: 1 | 0;
19
+ xOnlyPubkey: Uint8Array;
20
+ }
21
+ export interface Tapleaf {
22
+ output: Buffer;
23
+ version?: number;
24
+ }
25
+ export declare const TAPLEAF_VERSION_MASK = 254;
26
+ export declare function isTapleaf(o: any): o is Tapleaf;
27
+ /**
28
+ * Binary tree repsenting script path spends for a Taproot input.
29
+ * Each node is either a single Tapleaf, or a pair of Tapleaf | Taptree.
30
+ * The tree has no balancing requirements.
31
+ */
32
+ export type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
33
+ export declare function isTaptree(scriptTree: any): scriptTree is Taptree;
34
+ export interface TinySecp256k1Interface {
35
+ isXOnlyPoint(p: Uint8Array): boolean;
36
+ xOnlyPointAddTweak(p: Uint8Array, tweak: Uint8Array): XOnlyPointAddTweakResult | null;
37
+ }
38
+ export declare const Buffer256bit: any;
39
+ export declare const Hash160bit: any;
40
+ export declare const Hash256bit: any;
41
+ export declare const Number: any;
42
+ export declare const Array: any;
43
+ export declare const Boolean: any;
44
+ export declare const String: any;
45
+ export declare const Buffer: any;
46
+ export declare const Hex: any;
47
+ export declare const maybe: any;
48
+ export declare const tuple: any;
49
+ export declare const UInt8: any;
50
+ export declare const UInt32: any;
51
+ export declare const Function: any;
52
+ export declare const BufferN: any;
53
+ export declare const Null: any;
54
+ export declare const oneOf: any;