@alexbosworth/blockchain 4.7.0 → 4.7.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.
@@ -100,8 +100,52 @@ const tests = [
100
100
  args: {
101
101
  transaction: '010000000002010000000000000000000000000000000000000000000000000000000000000000ffffffff00ffffffff010100000000000000000101510000' + '0000',
102
102
  },
103
- description: 'A witness serialized tx with an unknown flag is rejected',
104
- error: 'UnexpectedWitnessFlagByteValueInTransaction',
103
+ description: 'A zero marker with unknown flag is legacy and has extra data',
104
+ error: 'UnexpectedDataAfterTransactionToGetComponentsOf',
105
+ },
106
+ {
107
+ args: {
108
+ transaction: '0100000000' + '02' + '01000000000000000151' + '020000000000000000' + '00000000',
109
+ },
110
+ description: 'A zero inputs tx with a non-witness flag byte is parsed as legacy',
111
+ expected: {
112
+ inputs: [],
113
+ locktime: 0,
114
+ outputs: [{script: '51', tokens: 1}, {script: '', tokens: 2}],
115
+ version: 1,
116
+ },
117
+ },
118
+ {
119
+ args: {
120
+ transaction: '0100000001' + '00'.repeat(32) + 'ffffffff00ffffffff' + '01' + '0000000000002000' + '00' + '00000000',
121
+ },
122
+ description: 'A tx with an output value beyond safe integer range is rejected',
123
+ error: 'UnexpectedOutputValueInTransaction',
124
+ },
125
+ {
126
+ args: {
127
+ transaction: '0100000001' + '00'.repeat(32) + 'ffffffff00ffffffff' + '01' + 'ffffffffffff1f00' + '00' + '00000000',
128
+ },
129
+ description: 'A tx with the max safe integer output value is parsed',
130
+ expected: {
131
+ inputs: [{
132
+ id: '00'.repeat(32),
133
+ script: '',
134
+ sequence: 4294967295,
135
+ vout: 4294967295,
136
+ witness: undefined,
137
+ }],
138
+ locktime: 0,
139
+ outputs: [{script: '', tokens: 9007199254740991}],
140
+ version: 1,
141
+ },
142
+ },
143
+ {
144
+ args: {
145
+ transaction: '0100000000' + '00' + '00000000',
146
+ },
147
+ description: 'A zero inputs and zero outputs legacy tx is parsed',
148
+ expected: {inputs: [], locktime: 0, outputs: [], version: 1},
105
149
  },
106
150
  {
107
151
  args: {
@@ -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,6 +12,7 @@ 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()];
@@ -85,12 +86,8 @@ module.exports = args => {
85
86
  // The presence of the marker and flag indicates SegWit tx encoding
86
87
  const isSegWit = !marker && flag === witnessFlagValue;
87
88
 
88
- // A zero marker byte requires the BIP 144 witness flag value to follow
89
- if (!marker && !isSegWit) {
90
- throw new Error('UnexpectedWitnessFlagByteValueInTransaction');
91
- }
92
-
93
- // When tx isn't SegWit though, the bytes are not marker and flag
89
+ // When tx isn't SegWit though, the bytes are not marker and flag: a zero
90
+ // marker without the witness flag is a legacy tx with an empty inputs count
94
91
  offset += isSegWit ? byteCountMarkerFlag : byteCountNoMarkerFlag;
95
92
 
96
93
  const inputsCount = decodeCompactInt(args.buffer, offset);
@@ -160,6 +157,11 @@ module.exports = args => {
160
157
 
161
158
  offset += byteCountInt64;
162
159
 
160
+ // Values beyond the safe integer range cannot be represented as a Number
161
+ if (value > maxTokens) {
162
+ throw new Error('UnexpectedOutputValueInTransaction');
163
+ }
164
+
163
165
  // The script being spent to has a variable length
164
166
  const scriptLength = decodeCompactInt(args.buffer, offset);
165
167