@alexbosworth/blockchain 4.7.0 → 4.7.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.
- package/CHANGELOG.md +1 -1
- package/package.json +1 -1
- package/test/fixtures/tx_invalid.json +397 -0
- package/test/fixtures/tx_valid.json +528 -0
- package/test/transactions/test_components_of_transaction.js +39 -0
- package/test/transactions/test_core_tx_vectors.js +70 -0
- package/transactions/parse_transaction.js +11 -2
|
@@ -103,6 +103,45 @@ const tests = [
|
|
|
103
103
|
description: 'A witness serialized tx with an unknown flag is rejected',
|
|
104
104
|
error: 'UnexpectedWitnessFlagByteValueInTransaction',
|
|
105
105
|
},
|
|
106
|
+
{
|
|
107
|
+
args: {
|
|
108
|
+
transaction: '0100000000' + '02' + '01000000000000000151' + '020000000000000000' + '00000000',
|
|
109
|
+
},
|
|
110
|
+
description: 'A zero marker with a non-witness non-zero flag is rejected',
|
|
111
|
+
error: 'UnexpectedWitnessFlagByteValueInTransaction',
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
args: {
|
|
115
|
+
transaction: '0100000001' + '00'.repeat(32) + 'ffffffff00ffffffff' + '01' + '0000000000002000' + '00' + '00000000',
|
|
116
|
+
},
|
|
117
|
+
description: 'A tx with an output value beyond safe integer range is rejected',
|
|
118
|
+
error: 'UnexpectedOutputValueInTransaction',
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
args: {
|
|
122
|
+
transaction: '0100000001' + '00'.repeat(32) + 'ffffffff00ffffffff' + '01' + 'ffffffffffff1f00' + '00' + '00000000',
|
|
123
|
+
},
|
|
124
|
+
description: 'A tx with the max safe integer output value is parsed',
|
|
125
|
+
expected: {
|
|
126
|
+
inputs: [{
|
|
127
|
+
id: '00'.repeat(32),
|
|
128
|
+
script: '',
|
|
129
|
+
sequence: 4294967295,
|
|
130
|
+
vout: 4294967295,
|
|
131
|
+
witness: undefined,
|
|
132
|
+
}],
|
|
133
|
+
locktime: 0,
|
|
134
|
+
outputs: [{script: '', tokens: 9007199254740991}],
|
|
135
|
+
version: 1,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
args: {
|
|
140
|
+
transaction: '0100000000' + '00' + '00000000',
|
|
141
|
+
},
|
|
142
|
+
description: 'A zero inputs and zero outputs legacy tx is parsed',
|
|
143
|
+
expected: {inputs: [], locktime: 0, outputs: [], version: 1},
|
|
144
|
+
},
|
|
106
145
|
{
|
|
107
146
|
args: {
|
|
108
147
|
transaction: '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000',
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const {equal} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const {readFileSync} = require('node:fs');
|
|
4
|
+
const {join} = require('node:path');
|
|
5
|
+
const test = require('node:test');
|
|
6
|
+
|
|
7
|
+
const {componentsOfTransaction} = require('./../../');
|
|
8
|
+
const {sizeOfTransaction} = require('./../../');
|
|
9
|
+
const {transactionFromComponents} = require('./../../');
|
|
10
|
+
|
|
11
|
+
// Vectors that Core deserializes but that cannot be represented as Numbers
|
|
12
|
+
const expectedErrors = {
|
|
13
|
+
'tx_invalid #24: Negative output': 'UnexpectedOutputValueInTransaction',
|
|
14
|
+
};
|
|
15
|
+
const fixtures = ['tx_valid', 'tx_invalid'];
|
|
16
|
+
const fixturesDir = join(__dirname, '..', 'fixtures');
|
|
17
|
+
const isVector = entry => entry.length > 1;
|
|
18
|
+
const readJson = name => JSON.parse(readFileSync(join(fixturesDir, name)));
|
|
19
|
+
const vectorTx = vector => vector[1];
|
|
20
|
+
|
|
21
|
+
// Bitcoin Core test vectors: every entry, valid or invalid at the consensus
|
|
22
|
+
// level, is required to deserialize, so every entry is expected to parse and
|
|
23
|
+
// to re-serialize back to the exact same bytes.
|
|
24
|
+
fixtures.forEach(fixture => {
|
|
25
|
+
const entries = readJson(`${fixture}.json`);
|
|
26
|
+
|
|
27
|
+
let comment = fixture;
|
|
28
|
+
|
|
29
|
+
return entries.forEach((entry, i) => {
|
|
30
|
+
// Single string entries are comments describing the following vectors
|
|
31
|
+
if (!isVector(entry)) {
|
|
32
|
+
comment = entry[0];
|
|
33
|
+
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const transaction = vectorTx(entry);
|
|
38
|
+
|
|
39
|
+
const description = `${fixture} #${i}: ${comment}`;
|
|
40
|
+
|
|
41
|
+
return test(description, (t, end) => {
|
|
42
|
+
if (!!expectedErrors[description]) {
|
|
43
|
+
const error = new Error(expectedErrors[description]);
|
|
44
|
+
|
|
45
|
+
throws(() => componentsOfTransaction({transaction}), error, 'Got err');
|
|
46
|
+
|
|
47
|
+
return end();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const components = componentsOfTransaction({transaction});
|
|
51
|
+
|
|
52
|
+
const roundTrip = transactionFromComponents(components).transaction;
|
|
53
|
+
|
|
54
|
+
equal(roundTrip, transaction, 'Transaction round trips through components');
|
|
55
|
+
|
|
56
|
+
const {vsize, weight} = sizeOfTransaction({transaction});
|
|
57
|
+
|
|
58
|
+
const size = transaction.length / 2;
|
|
59
|
+
|
|
60
|
+
const hasWitness = components.inputs.some(input => !!input.witness);
|
|
61
|
+
|
|
62
|
+
// A legacy tx has weight of 4x its size, a witness tx weighs less
|
|
63
|
+
equal(weight, hasWitness ? weight : size * 4, 'Legacy weight is 4x size');
|
|
64
|
+
equal(weight <= size * 4, true, 'Weight is bounded by 4x the size');
|
|
65
|
+
equal(vsize, Math.ceil(weight / 4), 'Virtual size is weight over 4');
|
|
66
|
+
|
|
67
|
+
return end();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -12,10 +12,12 @@ const defaultLocktime = 0;
|
|
|
12
12
|
const defaultStartIndex = 0;
|
|
13
13
|
const defaultWitnessCount = 0;
|
|
14
14
|
const {isBuffer} = Buffer;
|
|
15
|
+
const maxTokens = BigInt(Number.MAX_SAFE_INTEGER);
|
|
15
16
|
const minByteLengthInput = 41;
|
|
16
17
|
const minByteLengthOutput = 9;
|
|
17
18
|
const times = n => [...Array(n).keys()];
|
|
18
19
|
const witnessFlagValue = 1;
|
|
20
|
+
const zeroFlagValue = 0;
|
|
19
21
|
|
|
20
22
|
/** Parse a raw transaction out of a buffer at a specific offset start
|
|
21
23
|
|
|
@@ -85,8 +87,10 @@ module.exports = args => {
|
|
|
85
87
|
// The presence of the marker and flag indicates SegWit tx encoding
|
|
86
88
|
const isSegWit = !marker && flag === witnessFlagValue;
|
|
87
89
|
|
|
88
|
-
// A zero marker
|
|
89
|
-
|
|
90
|
+
// A zero marker with a zero flag is the legacy encoding of an empty tx: the
|
|
91
|
+
// zero marker is an empty inputs count and the zero flag is an empty outputs
|
|
92
|
+
// count. Any other flag value following a zero marker is unknown data.
|
|
93
|
+
if (!marker && !isSegWit && flag !== zeroFlagValue) {
|
|
90
94
|
throw new Error('UnexpectedWitnessFlagByteValueInTransaction');
|
|
91
95
|
}
|
|
92
96
|
|
|
@@ -160,6 +164,11 @@ module.exports = args => {
|
|
|
160
164
|
|
|
161
165
|
offset += byteCountInt64;
|
|
162
166
|
|
|
167
|
+
// Values beyond the safe integer range cannot be represented as a Number
|
|
168
|
+
if (value > maxTokens) {
|
|
169
|
+
throw new Error('UnexpectedOutputValueInTransaction');
|
|
170
|
+
}
|
|
171
|
+
|
|
163
172
|
// The script being spent to has a variable length
|
|
164
173
|
const scriptLength = decodeCompactInt(args.buffer, offset);
|
|
165
174
|
|