@btc-vision/bitcoin 6.3.0 → 6.3.2
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/.babelrc +4 -0
- package/.gitattributes +2 -0
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/.prettierrc.json +12 -0
- package/CHANGELOG.md +403 -0
- package/CONTRIBUTING.md +83 -0
- package/browser/address.d.ts +16 -0
- package/{src → browser}/bip66.d.ts +6 -7
- package/{src → browser}/block.d.ts +29 -30
- package/{src → browser}/bufferutils.d.ts +34 -54
- package/browser/crypto/crypto.d.ts +1 -0
- package/{src → browser}/crypto.d.ts +13 -18
- package/browser/ecc_lib.d.ts +3 -0
- package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
- package/browser/hooks/SignatureManager.d.ts +13 -0
- package/browser/index.d.ts +58 -0
- package/browser/index.js +2 -0
- package/browser/index.js.LICENSE.txt +14 -0
- package/browser/merkle.d.ts +1 -0
- package/browser/networks.d.ts +23 -0
- package/{src → browser}/ops.d.ts +126 -126
- package/browser/payments/bip341.d.ts +23 -0
- package/browser/payments/embed.d.ts +2 -0
- package/browser/payments/index.d.ts +41 -0
- package/{src → browser}/payments/lazy.d.ts +2 -2
- package/browser/payments/p2ms.d.ts +2 -0
- package/browser/payments/p2pk.d.ts +2 -0
- package/browser/payments/p2pkh.d.ts +2 -0
- package/browser/payments/p2sh.d.ts +2 -0
- package/browser/payments/p2tr.d.ts +2 -0
- package/browser/payments/p2wpkh.d.ts +2 -0
- package/browser/payments/p2wsh.d.ts +2 -0
- package/browser/psbt/bip371.d.ts +16 -0
- package/browser/psbt/psbtutils.d.ts +26 -0
- package/{src → browser}/psbt.d.ts +167 -235
- package/browser/push_data.d.ts +7 -0
- package/browser/script.d.ts +17 -0
- package/browser/script_number.d.ts +2 -0
- package/browser/script_signature.d.ts +7 -0
- package/{src → browser}/transaction.d.ts +48 -60
- package/{src → browser}/types.d.ts +37 -54
- package/build/address.d.ts +16 -0
- package/build/address.js +148 -0
- package/build/bip66.d.ts +6 -0
- package/build/bip66.js +99 -0
- package/build/block.d.ts +29 -0
- package/build/block.js +181 -0
- package/build/bufferutils.d.ts +34 -0
- package/build/bufferutils.js +141 -0
- package/build/crypto/crypto.d.ts +1 -0
- package/build/crypto/crypto.js +1 -0
- package/build/crypto.d.ts +13 -0
- package/build/crypto.js +87 -0
- package/build/ecc_lib.d.ts +3 -0
- package/build/ecc_lib.js +61 -0
- package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/build/hooks/AdvancedSignatureManager.js +52 -0
- package/build/hooks/HookedSigner.d.ts +4 -0
- package/build/hooks/HookedSigner.js +64 -0
- package/build/hooks/SignatureManager.d.ts +13 -0
- package/build/hooks/SignatureManager.js +45 -0
- package/build/index.d.ts +58 -0
- package/build/index.js +32 -0
- package/build/merkle.d.ts +1 -0
- package/build/merkle.js +19 -0
- package/build/networks.d.ts +23 -0
- package/build/networks.js +121 -0
- package/build/ops.d.ts +126 -0
- package/{src → build}/ops.js +127 -131
- package/build/payments/bip341.d.ts +23 -0
- package/build/payments/bip341.js +82 -0
- package/build/payments/embed.d.ts +2 -0
- package/build/payments/embed.js +39 -0
- package/build/payments/index.d.ts +41 -0
- package/build/payments/index.js +10 -0
- package/build/payments/lazy.d.ts +2 -0
- package/{src → build}/payments/lazy.js +28 -32
- package/build/payments/p2ms.d.ts +2 -0
- package/{src → build}/payments/p2ms.js +128 -158
- package/build/payments/p2pk.d.ts +2 -0
- package/build/payments/p2pk.js +68 -0
- package/build/payments/p2pkh.d.ts +2 -0
- package/build/payments/p2pkh.js +135 -0
- package/build/payments/p2sh.d.ts +2 -0
- package/build/payments/p2sh.js +175 -0
- package/build/payments/p2tr.d.ts +2 -0
- package/build/payments/p2tr.js +254 -0
- package/build/payments/p2wpkh.d.ts +2 -0
- package/build/payments/p2wpkh.js +130 -0
- package/build/payments/p2wsh.d.ts +2 -0
- package/build/payments/p2wsh.js +180 -0
- package/build/psbt/bip371.d.ts +16 -0
- package/build/psbt/bip371.js +246 -0
- package/build/psbt/psbtutils.d.ts +26 -0
- package/build/psbt/psbtutils.js +170 -0
- package/build/psbt.d.ts +167 -0
- package/build/psbt.js +1305 -0
- package/build/push_data.d.ts +7 -0
- package/build/push_data.js +57 -0
- package/build/script.d.ts +17 -0
- package/build/script.js +167 -0
- package/build/script_number.d.ts +2 -0
- package/build/script_number.js +49 -0
- package/build/script_signature.d.ts +7 -0
- package/build/script_signature.js +49 -0
- package/build/transaction.d.ts +48 -0
- package/build/transaction.js +445 -0
- package/build/types.d.ts +37 -0
- package/build/types.js +73 -0
- package/cjs/package.json +3 -0
- package/eslint.config.js +56 -0
- package/gulpfile.js +42 -0
- package/package.json +105 -50
- package/src/{address.js → address.ts} +93 -73
- package/src/{bip66.js → bip66.ts} +23 -19
- package/src/{block.js → block.ts} +114 -105
- package/src/{bufferutils.js → bufferutils.ts} +65 -67
- package/src/crypto/crypto-browser.js +75 -0
- package/src/crypto/crypto.ts +1 -0
- package/src/crypto.ts +108 -0
- package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
- package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
- package/src/hooks/HookedSigner.ts +108 -0
- package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
- package/src/index.ts +86 -0
- package/src/{merkle.js → merkle.ts} +8 -7
- package/src/networks.ts +235 -0
- package/src/ops.ts +282 -0
- package/src/payments/bip341.ts +140 -0
- package/src/payments/embed.ts +55 -0
- package/src/payments/{index.d.ts → index.ts} +20 -10
- package/src/payments/lazy.ts +28 -0
- package/src/payments/p2ms.ts +150 -0
- package/src/payments/{p2pk.js → p2pk.ts} +32 -29
- package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
- package/src/payments/{p2sh.js → p2sh.ts} +72 -71
- package/src/payments/{p2tr.js → p2tr.ts} +114 -125
- package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
- package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
- package/src/psbt/{bip371.js → bip371.ts} +195 -178
- package/src/psbt/psbtutils.ts +299 -0
- package/src/{psbt.js → psbt.ts} +1048 -699
- package/src/{push_data.js → push_data.ts} +35 -21
- package/src/{script.js → script.ts} +93 -77
- package/src/{script_number.js → script_number.ts} +15 -21
- package/src/{script_signature.js → script_signature.ts} +26 -14
- package/src/{transaction.js → transaction.ts} +247 -167
- package/src/types.ts +122 -0
- package/test/address.spec.js +124 -0
- package/test/address.spec.ts +177 -0
- package/test/bitcoin.core.spec.js +170 -0
- package/test/bitcoin.core.spec.ts +234 -0
- package/test/block.spec.js +141 -0
- package/test/block.spec.ts +194 -0
- package/test/bufferutils.spec.js +427 -0
- package/test/bufferutils.spec.ts +513 -0
- package/test/crypto.spec.js +41 -0
- package/test/crypto.spec.ts +55 -0
- package/test/fixtures/address.json +329 -0
- package/test/fixtures/block.json +148 -0
- package/test/fixtures/bufferutils.json +102 -0
- package/test/fixtures/core/README.md +26 -0
- package/test/fixtures/core/base58_encode_decode.json +50 -0
- package/test/fixtures/core/base58_keys_invalid.json +152 -0
- package/test/fixtures/core/base58_keys_valid.json +452 -0
- package/test/fixtures/core/blocks.json +27 -0
- package/test/fixtures/core/sig_canonical.json +7 -0
- package/test/fixtures/core/sig_noncanonical.json +33 -0
- package/test/fixtures/core/sighash.json +3505 -0
- package/test/fixtures/core/tx_valid.json +2023 -0
- package/test/fixtures/crypto.json +43 -0
- package/test/fixtures/ecdsa.json +217 -0
- package/test/fixtures/ecpair.json +141 -0
- package/test/fixtures/embed.json +108 -0
- package/test/fixtures/p2ms.json +434 -0
- package/test/fixtures/p2pk.json +179 -0
- package/test/fixtures/p2pkh.json +276 -0
- package/test/fixtures/p2sh.json +508 -0
- package/test/fixtures/p2tr.json +1198 -0
- package/test/fixtures/p2wpkh.json +290 -0
- package/test/fixtures/p2wsh.json +489 -0
- package/test/fixtures/psbt.json +924 -0
- package/test/fixtures/script.json +465 -0
- package/test/fixtures/script_number.json +225 -0
- package/test/fixtures/signature.json +140 -0
- package/test/fixtures/transaction.json +916 -0
- package/test/integration/_regtest.js +7 -0
- package/test/integration/_regtest.ts +6 -0
- package/test/integration/addresses.spec.js +116 -0
- package/test/integration/addresses.spec.ts +154 -0
- package/test/integration/bip32.spec.js +85 -0
- package/test/integration/bip32.spec.ts +151 -0
- package/test/integration/blocks.spec.js +26 -0
- package/test/integration/blocks.spec.ts +28 -0
- package/test/integration/cltv.spec.js +199 -0
- package/test/integration/cltv.spec.ts +283 -0
- package/test/integration/csv.spec.js +362 -0
- package/test/integration/csv.spec.ts +527 -0
- package/test/integration/payments.spec.js +98 -0
- package/test/integration/payments.spec.ts +135 -0
- package/test/integration/taproot.spec.js +532 -0
- package/test/integration/taproot.spec.ts +707 -0
- package/test/integration/transactions.spec.js +561 -0
- package/test/integration/transactions.spec.ts +769 -0
- package/test/payments.spec.js +97 -0
- package/test/payments.spec.ts +125 -0
- package/test/payments.utils.js +190 -0
- package/test/payments.utils.ts +208 -0
- package/test/psbt.spec.js +1044 -0
- package/test/psbt.spec.ts +1414 -0
- package/test/script.spec.js +151 -0
- package/test/script.spec.ts +210 -0
- package/test/script_number.spec.js +24 -0
- package/test/script_number.spec.ts +29 -0
- package/test/script_signature.spec.js +52 -0
- package/test/script_signature.spec.ts +66 -0
- package/test/transaction.spec.js +269 -0
- package/test/transaction.spec.ts +387 -0
- package/test/ts-node-register.js +5 -0
- package/test/tsconfig.json +45 -0
- package/test/types.spec.js +46 -0
- package/test/types.spec.ts +58 -0
- package/tsconfig.base.json +27 -0
- package/tsconfig.json +19 -0
- package/tsconfig.webpack.json +18 -0
- package/webpack.config.js +79 -0
- package/src/address.d.ts +0 -42
- package/src/crypto.js +0 -128
- package/src/ecc_lib.d.ts +0 -17
- package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
- package/src/hooks/HookedSigner.js +0 -90
- package/src/hooks/SignatureManager.d.ts +0 -35
- package/src/index.d.ts +0 -42
- package/src/index.js +0 -87
- package/src/merkle.d.ts +0 -10
- package/src/networks.d.ts +0 -29
- package/src/networks.js +0 -71
- package/src/payments/bip341.d.ts +0 -49
- package/src/payments/bip341.js +0 -124
- package/src/payments/embed.d.ts +0 -9
- package/src/payments/embed.js +0 -54
- package/src/payments/index.js +0 -69
- package/src/payments/p2ms.d.ts +0 -9
- package/src/payments/p2pk.d.ts +0 -10
- package/src/payments/p2pkh.d.ts +0 -10
- package/src/payments/p2sh.d.ts +0 -10
- package/src/payments/p2tr.d.ts +0 -10
- package/src/payments/p2wpkh.d.ts +0 -10
- package/src/payments/p2wsh.d.ts +0 -10
- package/src/psbt/bip371.d.ts +0 -42
- package/src/psbt/psbtutils.d.ts +0 -64
- package/src/psbt/psbtutils.js +0 -191
- package/src/push_data.d.ts +0 -29
- package/src/script.d.ts +0 -42
- package/src/script_number.d.ts +0 -19
- package/src/script_signature.d.ts +0 -21
- package/src/types.js +0 -106
|
@@ -1,67 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const script_1 = require('./script');
|
|
8
|
-
const types = require('./types');
|
|
1
|
+
import { BufferReader, BufferWriter, reverseBuffer, varuint } from './bufferutils.js';
|
|
2
|
+
import * as bcrypto from './crypto.js';
|
|
3
|
+
import * as bscript from './script.js';
|
|
4
|
+
import { OPS as opcodes } from './script.js';
|
|
5
|
+
import * as types from './types.js';
|
|
6
|
+
|
|
9
7
|
const { typeforce } = types;
|
|
10
|
-
|
|
8
|
+
|
|
9
|
+
function varSliceSize(someScript: Buffer): number {
|
|
11
10
|
const length = someScript.length;
|
|
12
|
-
|
|
11
|
+
|
|
12
|
+
return varuint.encodingLength(length) + length;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
function vectorSize(someVector: Buffer[]): number {
|
|
15
16
|
const length = someVector.length;
|
|
17
|
+
|
|
16
18
|
return (
|
|
17
|
-
|
|
19
|
+
varuint.encodingLength(length) +
|
|
18
20
|
someVector.reduce((sum, witness) => {
|
|
19
21
|
return sum + varSliceSize(witness);
|
|
20
22
|
}, 0)
|
|
21
23
|
);
|
|
22
24
|
}
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
25
|
+
|
|
26
|
+
const EMPTY_BUFFER: Buffer = Buffer.allocUnsafe(0);
|
|
27
|
+
const EMPTY_WITNESS: Buffer[] = [];
|
|
28
|
+
const ZERO: Buffer = Buffer.from(
|
|
26
29
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
27
30
|
'hex',
|
|
28
31
|
);
|
|
29
|
-
const ONE = Buffer.from(
|
|
32
|
+
const ONE: Buffer = Buffer.from(
|
|
30
33
|
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
31
34
|
'hex',
|
|
32
35
|
);
|
|
33
|
-
const VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex');
|
|
36
|
+
const VALUE_UINT64_MAX: Buffer = Buffer.from('ffffffffffffffff', 'hex');
|
|
34
37
|
const BLANK_OUTPUT = {
|
|
35
38
|
script: EMPTY_BUFFER,
|
|
36
39
|
valueBuffer: VALUE_UINT64_MAX,
|
|
37
40
|
};
|
|
38
|
-
|
|
41
|
+
|
|
42
|
+
function isOutput(out: Output): boolean {
|
|
39
43
|
return out.value !== undefined;
|
|
40
44
|
}
|
|
45
|
+
|
|
46
|
+
export interface Output {
|
|
47
|
+
script: Buffer;
|
|
48
|
+
value: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Input {
|
|
52
|
+
hash: Buffer;
|
|
53
|
+
index: number;
|
|
54
|
+
script: Buffer;
|
|
55
|
+
sequence: number;
|
|
56
|
+
witness: Buffer[];
|
|
57
|
+
}
|
|
58
|
+
|
|
41
59
|
/**
|
|
42
60
|
* Represents a Bitcoin transaction.
|
|
43
61
|
*/
|
|
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
|
-
|
|
60
|
-
|
|
62
|
+
export class Transaction {
|
|
63
|
+
static readonly DEFAULT_SEQUENCE = 0xffffffff;
|
|
64
|
+
static readonly SIGHASH_DEFAULT = 0x00;
|
|
65
|
+
static readonly SIGHASH_ALL = 0x01;
|
|
66
|
+
static readonly SIGHASH_NONE = 0x02;
|
|
67
|
+
static readonly SIGHASH_SINGLE = 0x03;
|
|
68
|
+
static readonly SIGHASH_ANYONECANPAY = 0x80;
|
|
69
|
+
static readonly SIGHASH_OUTPUT_MASK = 0x03;
|
|
70
|
+
static readonly SIGHASH_INPUT_MASK = 0x80;
|
|
71
|
+
static readonly ADVANCED_TRANSACTION_MARKER = 0x00;
|
|
72
|
+
static readonly ADVANCED_TRANSACTION_FLAG = 0x01;
|
|
73
|
+
version: number = 1;
|
|
74
|
+
locktime: number = 0;
|
|
75
|
+
ins: Input[] = [];
|
|
76
|
+
outs: Output[] = [];
|
|
77
|
+
|
|
78
|
+
static fromBuffer(buffer: Buffer, _NO_STRICT?: boolean): Transaction {
|
|
79
|
+
const bufferReader = new BufferReader(buffer);
|
|
61
80
|
const tx = new Transaction();
|
|
62
81
|
tx.version = bufferReader.readInt32();
|
|
82
|
+
|
|
63
83
|
const marker = bufferReader.readUInt8();
|
|
64
84
|
const flag = bufferReader.readUInt8();
|
|
85
|
+
|
|
65
86
|
let hasWitnesses = false;
|
|
66
87
|
if (
|
|
67
88
|
marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
|
|
@@ -71,16 +92,23 @@ class Transaction {
|
|
|
71
92
|
} else {
|
|
72
93
|
bufferReader.offset -= 2;
|
|
73
94
|
}
|
|
95
|
+
|
|
74
96
|
const vinLen = bufferReader.readVarInt();
|
|
75
97
|
for (let i = 0; i < vinLen; ++i) {
|
|
98
|
+
const hash = bufferReader.readSlice(32);
|
|
99
|
+
const index = bufferReader.readUInt32();
|
|
100
|
+
const script = bufferReader.readVarSlice();
|
|
101
|
+
const sequence = bufferReader.readUInt32();
|
|
102
|
+
|
|
76
103
|
tx.ins.push({
|
|
77
|
-
hash:
|
|
78
|
-
index:
|
|
79
|
-
script:
|
|
80
|
-
sequence:
|
|
104
|
+
hash: hash,
|
|
105
|
+
index: index,
|
|
106
|
+
script: script,
|
|
107
|
+
sequence: sequence,
|
|
81
108
|
witness: EMPTY_WITNESS,
|
|
82
109
|
});
|
|
83
110
|
}
|
|
111
|
+
|
|
84
112
|
const voutLen = bufferReader.readVarInt();
|
|
85
113
|
for (let i = 0; i < voutLen; ++i) {
|
|
86
114
|
tx.outs.push({
|
|
@@ -88,37 +116,42 @@ class Transaction {
|
|
|
88
116
|
script: bufferReader.readVarSlice(),
|
|
89
117
|
});
|
|
90
118
|
}
|
|
119
|
+
|
|
91
120
|
if (hasWitnesses) {
|
|
92
121
|
for (let i = 0; i < vinLen; ++i) {
|
|
93
122
|
tx.ins[i].witness = bufferReader.readVector();
|
|
94
123
|
}
|
|
124
|
+
|
|
95
125
|
// was this pointless?
|
|
96
|
-
if (!tx.hasWitnesses())
|
|
97
|
-
throw new Error('Transaction has superfluous witness data');
|
|
126
|
+
if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data');
|
|
98
127
|
}
|
|
128
|
+
|
|
99
129
|
tx.locktime = bufferReader.readUInt32();
|
|
130
|
+
|
|
100
131
|
if (_NO_STRICT) return tx;
|
|
101
132
|
if (bufferReader.offset !== buffer.length)
|
|
102
133
|
throw new Error('Transaction has unexpected data');
|
|
134
|
+
|
|
103
135
|
return tx;
|
|
104
136
|
}
|
|
105
|
-
|
|
137
|
+
|
|
138
|
+
static fromHex(hex: string): Transaction {
|
|
106
139
|
return Transaction.fromBuffer(Buffer.from(hex, 'hex'), false);
|
|
107
140
|
}
|
|
108
|
-
|
|
141
|
+
|
|
142
|
+
static isCoinbaseHash(buffer: Buffer): boolean {
|
|
109
143
|
typeforce(types.Hash256bit, buffer);
|
|
110
144
|
for (let i = 0; i < 32; ++i) {
|
|
111
145
|
if (buffer[i] !== 0) return false;
|
|
112
146
|
}
|
|
113
147
|
return true;
|
|
114
148
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
Transaction.isCoinbaseHash(this.ins[0].hash)
|
|
119
|
-
);
|
|
149
|
+
|
|
150
|
+
isCoinbase(): boolean {
|
|
151
|
+
return this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash);
|
|
120
152
|
}
|
|
121
|
-
|
|
153
|
+
|
|
154
|
+
addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number {
|
|
122
155
|
typeforce(
|
|
123
156
|
types.tuple(
|
|
124
157
|
types.Hash256bit,
|
|
@@ -128,22 +161,26 @@ class Transaction {
|
|
|
128
161
|
),
|
|
129
162
|
arguments,
|
|
130
163
|
);
|
|
164
|
+
|
|
131
165
|
if (types.Null(sequence)) {
|
|
132
166
|
sequence = Transaction.DEFAULT_SEQUENCE;
|
|
133
167
|
}
|
|
168
|
+
|
|
134
169
|
// Add the input and return the input's index
|
|
135
170
|
return (
|
|
136
171
|
this.ins.push({
|
|
137
172
|
hash,
|
|
138
173
|
index,
|
|
139
174
|
script: scriptSig || EMPTY_BUFFER,
|
|
140
|
-
sequence: sequence,
|
|
175
|
+
sequence: sequence as number,
|
|
141
176
|
witness: EMPTY_WITNESS,
|
|
142
177
|
}) - 1
|
|
143
178
|
);
|
|
144
179
|
}
|
|
145
|
-
|
|
180
|
+
|
|
181
|
+
addOutput(scriptPubKey: Buffer, value: number): number {
|
|
146
182
|
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments);
|
|
183
|
+
|
|
147
184
|
// Add the output and return the output's index
|
|
148
185
|
return (
|
|
149
186
|
this.outs.push({
|
|
@@ -152,25 +189,30 @@ class Transaction {
|
|
|
152
189
|
}) - 1
|
|
153
190
|
);
|
|
154
191
|
}
|
|
155
|
-
|
|
156
|
-
|
|
192
|
+
|
|
193
|
+
hasWitnesses(): boolean {
|
|
194
|
+
return this.ins.some((x) => {
|
|
157
195
|
return x.witness.length !== 0;
|
|
158
196
|
});
|
|
159
197
|
}
|
|
160
|
-
|
|
198
|
+
|
|
199
|
+
weight(): number {
|
|
161
200
|
const base = this.byteLength(false);
|
|
162
201
|
const total = this.byteLength(true);
|
|
163
202
|
return base * 3 + total;
|
|
164
203
|
}
|
|
165
|
-
|
|
204
|
+
|
|
205
|
+
virtualSize(): number {
|
|
166
206
|
return Math.ceil(this.weight() / 4);
|
|
167
207
|
}
|
|
168
|
-
|
|
208
|
+
|
|
209
|
+
byteLength(_ALLOW_WITNESS: boolean = true): number {
|
|
169
210
|
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
|
211
|
+
|
|
170
212
|
return (
|
|
171
213
|
(hasWitnesses ? 10 : 8) +
|
|
172
|
-
|
|
173
|
-
|
|
214
|
+
varuint.encodingLength(this.ins.length) +
|
|
215
|
+
varuint.encodingLength(this.outs.length) +
|
|
174
216
|
this.ins.reduce((sum, input) => {
|
|
175
217
|
return sum + 40 + varSliceSize(input.script);
|
|
176
218
|
}, 0) +
|
|
@@ -184,11 +226,13 @@ class Transaction {
|
|
|
184
226
|
: 0)
|
|
185
227
|
);
|
|
186
228
|
}
|
|
187
|
-
|
|
229
|
+
|
|
230
|
+
clone(): Transaction {
|
|
188
231
|
const newTx = new Transaction();
|
|
189
232
|
newTx.version = this.version;
|
|
190
233
|
newTx.locktime = this.locktime;
|
|
191
|
-
|
|
234
|
+
|
|
235
|
+
newTx.ins = this.ins.map((txIn) => {
|
|
192
236
|
return {
|
|
193
237
|
hash: txIn.hash,
|
|
194
238
|
index: txIn.index,
|
|
@@ -197,14 +241,17 @@ class Transaction {
|
|
|
197
241
|
witness: txIn.witness,
|
|
198
242
|
};
|
|
199
243
|
});
|
|
200
|
-
|
|
244
|
+
|
|
245
|
+
newTx.outs = this.outs.map((txOut) => {
|
|
201
246
|
return {
|
|
202
247
|
script: txOut.script,
|
|
203
248
|
value: txOut.value,
|
|
204
249
|
};
|
|
205
250
|
});
|
|
251
|
+
|
|
206
252
|
return newTx;
|
|
207
253
|
}
|
|
254
|
+
|
|
208
255
|
/**
|
|
209
256
|
* Hash transaction for signing a specific input.
|
|
210
257
|
*
|
|
@@ -213,74 +260,86 @@ class Transaction {
|
|
|
213
260
|
* hashType, and then hashes the result.
|
|
214
261
|
* This hash can then be used to sign the provided transaction input.
|
|
215
262
|
*/
|
|
216
|
-
hashForSignature(inIndex, prevOutScript, hashType) {
|
|
263
|
+
hashForSignature(inIndex: number, prevOutScript: Buffer, hashType: number): Buffer {
|
|
217
264
|
typeforce(
|
|
218
|
-
types.tuple(
|
|
219
|
-
types.UInt32,
|
|
220
|
-
types.Buffer,
|
|
221
|
-
/* types.UInt8 */ types.Number,
|
|
222
|
-
),
|
|
265
|
+
types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number),
|
|
223
266
|
arguments,
|
|
224
267
|
);
|
|
268
|
+
|
|
225
269
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
|
226
270
|
if (inIndex >= this.ins.length) return ONE;
|
|
271
|
+
|
|
227
272
|
// ignore OP_CODESEPARATOR
|
|
228
273
|
const ourScript = bscript.compile(
|
|
229
|
-
bscript.decompile(prevOutScript)
|
|
230
|
-
return x !==
|
|
274
|
+
bscript.decompile(prevOutScript)!.filter((x) => {
|
|
275
|
+
return x !== opcodes.OP_CODESEPARATOR;
|
|
231
276
|
}),
|
|
232
277
|
);
|
|
278
|
+
|
|
233
279
|
const txTmp = this.clone();
|
|
280
|
+
|
|
234
281
|
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
|
|
235
282
|
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
|
|
236
283
|
txTmp.outs = [];
|
|
284
|
+
|
|
237
285
|
// ignore sequence numbers (except at inIndex)
|
|
238
286
|
txTmp.ins.forEach((input, i) => {
|
|
239
287
|
if (i === inIndex) return;
|
|
288
|
+
|
|
240
289
|
input.sequence = 0;
|
|
241
290
|
});
|
|
291
|
+
|
|
242
292
|
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
|
|
243
293
|
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
|
|
244
294
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
|
|
245
295
|
if (inIndex >= this.outs.length) return ONE;
|
|
296
|
+
|
|
246
297
|
// truncate outputs after
|
|
247
298
|
txTmp.outs.length = inIndex + 1;
|
|
299
|
+
|
|
248
300
|
// "blank" outputs before
|
|
249
301
|
for (let i = 0; i < inIndex; i++) {
|
|
250
|
-
txTmp.outs[i] = BLANK_OUTPUT;
|
|
302
|
+
(txTmp.outs as any)[i] = BLANK_OUTPUT;
|
|
251
303
|
}
|
|
304
|
+
|
|
252
305
|
// ignore sequence numbers (except at inIndex)
|
|
253
306
|
txTmp.ins.forEach((input, y) => {
|
|
254
307
|
if (y === inIndex) return;
|
|
308
|
+
|
|
255
309
|
input.sequence = 0;
|
|
256
310
|
});
|
|
257
311
|
}
|
|
312
|
+
|
|
258
313
|
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
|
|
259
314
|
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
|
|
260
315
|
txTmp.ins = [txTmp.ins[inIndex]];
|
|
261
316
|
txTmp.ins[0].script = ourScript;
|
|
317
|
+
|
|
262
318
|
// SIGHASH_ALL: only ignore input scripts
|
|
263
319
|
} else {
|
|
264
320
|
// "blank" others input scripts
|
|
265
|
-
txTmp.ins.forEach(input => {
|
|
321
|
+
txTmp.ins.forEach((input) => {
|
|
266
322
|
input.script = EMPTY_BUFFER;
|
|
267
323
|
});
|
|
268
324
|
txTmp.ins[inIndex].script = ourScript;
|
|
269
325
|
}
|
|
326
|
+
|
|
270
327
|
// serialize and hash
|
|
271
|
-
const buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
|
|
328
|
+
const buffer: Buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
|
|
272
329
|
buffer.writeInt32LE(hashType, buffer.length - 4);
|
|
273
330
|
txTmp.__toBuffer(buffer, 0, false);
|
|
331
|
+
|
|
274
332
|
return bcrypto.hash256(buffer);
|
|
275
333
|
}
|
|
334
|
+
|
|
276
335
|
hashForWitnessV1(
|
|
277
|
-
inIndex,
|
|
278
|
-
prevOutScripts,
|
|
279
|
-
values,
|
|
280
|
-
hashType,
|
|
281
|
-
leafHash,
|
|
282
|
-
annex,
|
|
283
|
-
) {
|
|
336
|
+
inIndex: number,
|
|
337
|
+
prevOutScripts: Buffer[],
|
|
338
|
+
values: number[],
|
|
339
|
+
hashType: number,
|
|
340
|
+
leafHash?: Buffer,
|
|
341
|
+
annex?: Buffer,
|
|
342
|
+
): Buffer {
|
|
284
343
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#common-signature-message
|
|
285
344
|
typeforce(
|
|
286
345
|
types.tuple(
|
|
@@ -291,79 +350,76 @@ class Transaction {
|
|
|
291
350
|
),
|
|
292
351
|
arguments,
|
|
293
352
|
);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
) {
|
|
298
|
-
throw new Error(
|
|
299
|
-
'Must supply prevout script and value for all inputs',
|
|
300
|
-
);
|
|
353
|
+
|
|
354
|
+
if (values.length !== this.ins.length || prevOutScripts.length !== this.ins.length) {
|
|
355
|
+
throw new Error('Must supply prevout script and value for all inputs');
|
|
301
356
|
}
|
|
357
|
+
|
|
302
358
|
const outputType =
|
|
303
359
|
hashType === Transaction.SIGHASH_DEFAULT
|
|
304
360
|
? Transaction.SIGHASH_ALL
|
|
305
361
|
: hashType & Transaction.SIGHASH_OUTPUT_MASK;
|
|
362
|
+
|
|
306
363
|
const inputType = hashType & Transaction.SIGHASH_INPUT_MASK;
|
|
364
|
+
|
|
307
365
|
const isAnyoneCanPay = inputType === Transaction.SIGHASH_ANYONECANPAY;
|
|
308
366
|
const isNone = outputType === Transaction.SIGHASH_NONE;
|
|
309
367
|
const isSingle = outputType === Transaction.SIGHASH_SINGLE;
|
|
368
|
+
|
|
310
369
|
let hashPrevouts = EMPTY_BUFFER;
|
|
311
370
|
let hashAmounts = EMPTY_BUFFER;
|
|
312
371
|
let hashScriptPubKeys = EMPTY_BUFFER;
|
|
313
372
|
let hashSequences = EMPTY_BUFFER;
|
|
314
373
|
let hashOutputs = EMPTY_BUFFER;
|
|
374
|
+
|
|
315
375
|
if (!isAnyoneCanPay) {
|
|
316
|
-
let bufferWriter =
|
|
317
|
-
|
|
318
|
-
);
|
|
319
|
-
this.ins.forEach(txIn => {
|
|
376
|
+
let bufferWriter = BufferWriter.withCapacity(36 * this.ins.length);
|
|
377
|
+
this.ins.forEach((txIn) => {
|
|
320
378
|
bufferWriter.writeSlice(txIn.hash);
|
|
321
379
|
bufferWriter.writeUInt32(txIn.index);
|
|
322
380
|
});
|
|
323
381
|
hashPrevouts = bcrypto.sha256(bufferWriter.end());
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
);
|
|
327
|
-
values.forEach(value => bufferWriter.writeUInt64(value));
|
|
382
|
+
|
|
383
|
+
bufferWriter = BufferWriter.withCapacity(8 * this.ins.length);
|
|
384
|
+
values.forEach((value) => bufferWriter.writeUInt64(value));
|
|
328
385
|
hashAmounts = bcrypto.sha256(bufferWriter.end());
|
|
329
|
-
|
|
386
|
+
|
|
387
|
+
bufferWriter = BufferWriter.withCapacity(
|
|
330
388
|
prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
|
|
331
389
|
);
|
|
332
|
-
prevOutScripts.forEach(prevOutScript =>
|
|
333
|
-
bufferWriter.writeVarSlice(prevOutScript),
|
|
334
|
-
);
|
|
390
|
+
prevOutScripts.forEach((prevOutScript) => bufferWriter.writeVarSlice(prevOutScript));
|
|
335
391
|
hashScriptPubKeys = bcrypto.sha256(bufferWriter.end());
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
);
|
|
339
|
-
this.ins.forEach(txIn => bufferWriter.writeUInt32(txIn.sequence));
|
|
392
|
+
|
|
393
|
+
bufferWriter = BufferWriter.withCapacity(4 * this.ins.length);
|
|
394
|
+
this.ins.forEach((txIn) => bufferWriter.writeUInt32(txIn.sequence));
|
|
340
395
|
hashSequences = bcrypto.sha256(bufferWriter.end());
|
|
341
396
|
}
|
|
397
|
+
|
|
342
398
|
if (!(isNone || isSingle)) {
|
|
343
399
|
if (!this.outs.length)
|
|
344
|
-
throw new Error(
|
|
345
|
-
'Add outputs to the transaction before signing.',
|
|
346
|
-
);
|
|
400
|
+
throw new Error('Add outputs to the transaction before signing.');
|
|
347
401
|
const txOutsSize = this.outs
|
|
348
|
-
.map(output => 8 + varSliceSize(output.script))
|
|
402
|
+
.map((output) => 8 + varSliceSize(output.script))
|
|
349
403
|
.reduce((a, b) => a + b);
|
|
350
|
-
const bufferWriter =
|
|
351
|
-
|
|
352
|
-
this.outs.forEach(out => {
|
|
404
|
+
const bufferWriter = BufferWriter.withCapacity(txOutsSize);
|
|
405
|
+
|
|
406
|
+
this.outs.forEach((out) => {
|
|
353
407
|
bufferWriter.writeUInt64(out.value);
|
|
354
408
|
bufferWriter.writeVarSlice(out.script);
|
|
355
409
|
});
|
|
410
|
+
|
|
356
411
|
hashOutputs = bcrypto.sha256(bufferWriter.end());
|
|
357
412
|
} else if (isSingle && inIndex < this.outs.length) {
|
|
358
413
|
const output = this.outs[inIndex];
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
);
|
|
414
|
+
|
|
415
|
+
const bufferWriter = BufferWriter.withCapacity(8 + varSliceSize(output.script));
|
|
362
416
|
bufferWriter.writeUInt64(output.value);
|
|
363
417
|
bufferWriter.writeVarSlice(output.script);
|
|
364
418
|
hashOutputs = bcrypto.sha256(bufferWriter.end());
|
|
365
419
|
}
|
|
420
|
+
|
|
366
421
|
const spendType = (leafHash ? 2 : 0) + (annex ? 1 : 0);
|
|
422
|
+
|
|
367
423
|
// Length calculation from:
|
|
368
424
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-14
|
|
369
425
|
// With extension from:
|
|
@@ -374,8 +430,8 @@ class Transaction {
|
|
|
374
430
|
(isNone ? 32 : 0) +
|
|
375
431
|
(annex ? 32 : 0) +
|
|
376
432
|
(leafHash ? 37 : 0);
|
|
377
|
-
const sigMsgWriter =
|
|
378
|
-
|
|
433
|
+
const sigMsgWriter = BufferWriter.withCapacity(sigMsgSize);
|
|
434
|
+
|
|
379
435
|
sigMsgWriter.writeUInt8(hashType);
|
|
380
436
|
// Transaction
|
|
381
437
|
sigMsgWriter.writeInt32(this.version);
|
|
@@ -400,9 +456,7 @@ class Transaction {
|
|
|
400
456
|
sigMsgWriter.writeUInt32(inIndex);
|
|
401
457
|
}
|
|
402
458
|
if (annex) {
|
|
403
|
-
const bufferWriter =
|
|
404
|
-
varSliceSize(annex),
|
|
405
|
-
);
|
|
459
|
+
const bufferWriter = BufferWriter.withCapacity(varSliceSize(annex));
|
|
406
460
|
bufferWriter.writeVarSlice(annex);
|
|
407
461
|
sigMsgWriter.writeSlice(bcrypto.sha256(bufferWriter.end()));
|
|
408
462
|
}
|
|
@@ -416,6 +470,7 @@ class Transaction {
|
|
|
416
470
|
sigMsgWriter.writeUInt8(0);
|
|
417
471
|
sigMsgWriter.writeUInt32(0xffffffff);
|
|
418
472
|
}
|
|
473
|
+
|
|
419
474
|
// Extra zero byte because:
|
|
420
475
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-19
|
|
421
476
|
return bcrypto.taggedHash(
|
|
@@ -423,42 +478,49 @@ class Transaction {
|
|
|
423
478
|
Buffer.concat([Buffer.from([0x00]), sigMsgWriter.end()]),
|
|
424
479
|
);
|
|
425
480
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
);
|
|
436
|
-
let
|
|
437
|
-
|
|
481
|
+
|
|
482
|
+
hashForWitnessV0(
|
|
483
|
+
inIndex: number,
|
|
484
|
+
prevOutScript: Buffer,
|
|
485
|
+
value: number,
|
|
486
|
+
hashType: number,
|
|
487
|
+
): Buffer {
|
|
488
|
+
typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments);
|
|
489
|
+
|
|
490
|
+
let tbuffer: Buffer = Buffer.from([]);
|
|
491
|
+
let bufferWriter: BufferWriter;
|
|
492
|
+
|
|
438
493
|
let hashOutputs = ZERO;
|
|
439
494
|
let hashPrevouts = ZERO;
|
|
440
495
|
let hashSequence = ZERO;
|
|
496
|
+
|
|
441
497
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
|
442
498
|
tbuffer = Buffer.allocUnsafe(36 * this.ins.length);
|
|
443
|
-
bufferWriter = new
|
|
444
|
-
|
|
499
|
+
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
500
|
+
|
|
501
|
+
this.ins.forEach((txIn) => {
|
|
445
502
|
bufferWriter.writeSlice(txIn.hash);
|
|
446
503
|
bufferWriter.writeUInt32(txIn.index);
|
|
447
504
|
});
|
|
505
|
+
|
|
448
506
|
hashPrevouts = bcrypto.hash256(tbuffer);
|
|
449
507
|
}
|
|
508
|
+
|
|
450
509
|
if (
|
|
451
510
|
!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
|
452
511
|
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
453
512
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
|
|
454
513
|
) {
|
|
455
514
|
tbuffer = Buffer.allocUnsafe(4 * this.ins.length);
|
|
456
|
-
bufferWriter = new
|
|
457
|
-
|
|
515
|
+
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
516
|
+
|
|
517
|
+
this.ins.forEach((txIn) => {
|
|
458
518
|
bufferWriter.writeUInt32(txIn.sequence);
|
|
459
519
|
});
|
|
520
|
+
|
|
460
521
|
hashSequence = bcrypto.hash256(tbuffer);
|
|
461
522
|
}
|
|
523
|
+
|
|
462
524
|
if (
|
|
463
525
|
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
464
526
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
|
|
@@ -466,26 +528,30 @@ class Transaction {
|
|
|
466
528
|
const txOutsSize = this.outs.reduce((sum, output) => {
|
|
467
529
|
return sum + 8 + varSliceSize(output.script);
|
|
468
530
|
}, 0);
|
|
531
|
+
|
|
469
532
|
tbuffer = Buffer.allocUnsafe(txOutsSize);
|
|
470
|
-
bufferWriter = new
|
|
471
|
-
|
|
533
|
+
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
534
|
+
|
|
535
|
+
this.outs.forEach((out) => {
|
|
472
536
|
bufferWriter.writeUInt64(out.value);
|
|
473
537
|
bufferWriter.writeVarSlice(out.script);
|
|
474
538
|
});
|
|
539
|
+
|
|
475
540
|
hashOutputs = bcrypto.hash256(tbuffer);
|
|
476
|
-
} else if (
|
|
477
|
-
(hashType & 0x1f) === Transaction.SIGHASH_SINGLE &&
|
|
478
|
-
inIndex < this.outs.length
|
|
479
|
-
) {
|
|
541
|
+
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
|
480
542
|
const output = this.outs[inIndex];
|
|
543
|
+
|
|
481
544
|
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script));
|
|
482
|
-
bufferWriter = new
|
|
545
|
+
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
483
546
|
bufferWriter.writeUInt64(output.value);
|
|
484
547
|
bufferWriter.writeVarSlice(output.script);
|
|
548
|
+
|
|
485
549
|
hashOutputs = bcrypto.hash256(tbuffer);
|
|
486
550
|
}
|
|
551
|
+
|
|
487
552
|
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
|
|
488
|
-
bufferWriter = new
|
|
553
|
+
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
554
|
+
|
|
489
555
|
const input = this.ins[inIndex];
|
|
490
556
|
bufferWriter.writeInt32(this.version);
|
|
491
557
|
bufferWriter.writeSlice(hashPrevouts);
|
|
@@ -500,72 +566,86 @@ class Transaction {
|
|
|
500
566
|
bufferWriter.writeUInt32(hashType);
|
|
501
567
|
return bcrypto.hash256(tbuffer);
|
|
502
568
|
}
|
|
503
|
-
|
|
569
|
+
|
|
570
|
+
getHash(forWitness?: boolean): Buffer {
|
|
504
571
|
// wtxid for coinbase is always 32 bytes of 0x00
|
|
505
572
|
if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0);
|
|
506
|
-
return bcrypto.hash256(
|
|
507
|
-
this.__toBuffer(undefined, undefined, forWitness),
|
|
508
|
-
);
|
|
573
|
+
return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness));
|
|
509
574
|
}
|
|
510
|
-
|
|
575
|
+
|
|
576
|
+
getId(): string {
|
|
511
577
|
// transaction hash's are displayed in reverse order
|
|
512
|
-
return
|
|
513
|
-
'hex',
|
|
514
|
-
);
|
|
578
|
+
return reverseBuffer(this.getHash(false)).toString('hex');
|
|
515
579
|
}
|
|
516
|
-
|
|
580
|
+
|
|
581
|
+
toBuffer(buffer?: Buffer, initialOffset?: number): Buffer {
|
|
517
582
|
return this.__toBuffer(buffer, initialOffset, true);
|
|
518
583
|
}
|
|
519
|
-
|
|
584
|
+
|
|
585
|
+
toHex(): string {
|
|
520
586
|
return this.toBuffer(undefined, undefined).toString('hex');
|
|
521
587
|
}
|
|
522
|
-
|
|
588
|
+
|
|
589
|
+
setInputScript(index: number, scriptSig: Buffer): void {
|
|
523
590
|
typeforce(types.tuple(types.Number, types.Buffer), arguments);
|
|
591
|
+
|
|
524
592
|
this.ins[index].script = scriptSig;
|
|
525
593
|
}
|
|
526
|
-
|
|
594
|
+
|
|
595
|
+
setWitness(index: number, witness: Buffer[]): void {
|
|
527
596
|
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
|
|
597
|
+
|
|
528
598
|
this.ins[index].witness = witness;
|
|
529
599
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
);
|
|
600
|
+
|
|
601
|
+
private __toBuffer(
|
|
602
|
+
buffer?: Buffer,
|
|
603
|
+
initialOffset?: number,
|
|
604
|
+
_ALLOW_WITNESS: boolean = false,
|
|
605
|
+
): Buffer {
|
|
606
|
+
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS)) as Buffer;
|
|
607
|
+
|
|
608
|
+
const bufferWriter = new BufferWriter(buffer, initialOffset || 0);
|
|
609
|
+
|
|
537
610
|
bufferWriter.writeInt32(this.version);
|
|
611
|
+
|
|
538
612
|
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
|
613
|
+
|
|
539
614
|
if (hasWitnesses) {
|
|
540
615
|
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
|
|
541
616
|
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
|
|
542
617
|
}
|
|
618
|
+
|
|
543
619
|
bufferWriter.writeVarInt(this.ins.length);
|
|
544
|
-
|
|
620
|
+
|
|
621
|
+
this.ins.forEach((txIn) => {
|
|
545
622
|
bufferWriter.writeSlice(txIn.hash);
|
|
546
623
|
bufferWriter.writeUInt32(txIn.index);
|
|
547
624
|
bufferWriter.writeVarSlice(txIn.script);
|
|
548
625
|
bufferWriter.writeUInt32(txIn.sequence);
|
|
549
626
|
});
|
|
627
|
+
|
|
550
628
|
bufferWriter.writeVarInt(this.outs.length);
|
|
551
|
-
this.outs.forEach(txOut => {
|
|
629
|
+
this.outs.forEach((txOut) => {
|
|
552
630
|
if (isOutput(txOut)) {
|
|
553
631
|
bufferWriter.writeUInt64(txOut.value);
|
|
554
632
|
} else {
|
|
555
|
-
bufferWriter.writeSlice(txOut.valueBuffer);
|
|
633
|
+
bufferWriter.writeSlice((txOut as any).valueBuffer);
|
|
556
634
|
}
|
|
635
|
+
|
|
557
636
|
bufferWriter.writeVarSlice(txOut.script);
|
|
558
637
|
});
|
|
638
|
+
|
|
559
639
|
if (hasWitnesses) {
|
|
560
|
-
this.ins.forEach(input => {
|
|
640
|
+
this.ins.forEach((input) => {
|
|
561
641
|
bufferWriter.writeVector(input.witness);
|
|
562
642
|
});
|
|
563
643
|
}
|
|
644
|
+
|
|
564
645
|
bufferWriter.writeUInt32(this.locktime);
|
|
646
|
+
|
|
565
647
|
// avoid slicing unless necessary
|
|
566
|
-
if (initialOffset !== undefined)
|
|
567
|
-
return buffer.slice(initialOffset, bufferWriter.offset);
|
|
648
|
+
if (initialOffset !== undefined) return buffer.slice(initialOffset, bufferWriter.offset);
|
|
568
649
|
return buffer;
|
|
569
650
|
}
|
|
570
651
|
}
|
|
571
|
-
exports.Transaction = Transaction;
|