@alexbosworth/blockchain 3.1.0 → 4.0.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.
- package/CHANGELOG.md +13 -0
- package/README.md +74 -0
- package/addresses/bech32_type.js +52 -0
- package/addresses/decode_bech32.js +79 -0
- package/addresses/decode_bech32_address.js +86 -0
- package/addresses/index.js +2 -1
- package/addresses/modify_bech32_accumulator.js +24 -0
- package/hashes/id_for_transaction_components.js +2 -0
- package/index.js +6 -0
- package/numbers/compact_int_as_number.js +6 -7
- package/package.json +3 -3
- package/script/parse_push_bytes_count.js +0 -1
- package/test/addresses/test_bech32_type.js +29 -0
- package/test/addresses/test_decode_bech32.js +49 -0
- package/test/addresses/test_decode_bech32_address.js +188 -0
- package/test/script/test_parse_push_bytes_count.js +15 -0
- package/test/transactions/test_id_for_transaction.js +72 -0
- package/test/transactions/test_size_of_transaction.js +84 -0
- package/transactions/id_for_transaction.js +39 -0
- package/transactions/index.js +4 -0
- package/transactions/parse_transaction.js +9 -11
- package/transactions/size_of_transaction.js +58 -0
- /package/test/addresses/{decode_base58_address.js → test_decode_base58_address.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Versions
|
|
2
2
|
|
|
3
|
+
## 4.0.0
|
|
4
|
+
|
|
5
|
+
- `sizeOfTransaction`: Add method to get transaction weight and virtual size
|
|
6
|
+
|
|
7
|
+
### Breaking Changes
|
|
8
|
+
|
|
9
|
+
- End support for Node.js v20, require v22+
|
|
10
|
+
|
|
11
|
+
## 3.2.0
|
|
12
|
+
|
|
13
|
+
- `decode_bech32_address.js`: Add method to decode a bech32/bech32m address
|
|
14
|
+
- `id_for_transaction.js`: Add method to derive the standard tx id for a raw tx
|
|
15
|
+
|
|
3
16
|
## 3.1.0
|
|
4
17
|
|
|
5
18
|
- `p2pkhOutputScript`: Add method to get an output script for a p2wpkh hash
|
package/README.md
CHANGED
|
@@ -4,6 +4,27 @@ Utility methods for working with Blockchain data
|
|
|
4
4
|
|
|
5
5
|
## Methods
|
|
6
6
|
|
|
7
|
+
- [compactIntAsNumber](#compactintasnumber)
|
|
8
|
+
- [componentsOfTransaction](#componentsoftransaction)
|
|
9
|
+
- [decodeBase58Address](#decodebase58address)
|
|
10
|
+
- [decodeBech32Address](#decodebech32address)
|
|
11
|
+
- [idForBlock](#idforblock)
|
|
12
|
+
- [idForTransaction](#idfortransaction)
|
|
13
|
+
- [idForTransactionComponents](#idfortransactioncomponents)
|
|
14
|
+
- [noLocktimeIdForTransaction](#nolocktimeidfortransaction)
|
|
15
|
+
- [numberAsCompactInt](#numberascompactint)
|
|
16
|
+
- [p2pkhOutputScript](#p2pkhoutputscript)
|
|
17
|
+
- [p2shOutputScript](#p2shoutputscript)
|
|
18
|
+
- [p2trOutputScript](#p2troutputscript)
|
|
19
|
+
- [p2wpkhOutputScript](#p2wpkhoutputscript)
|
|
20
|
+
- [p2wshOutputScript](#p2wshoutputscript)
|
|
21
|
+
- [previousBlockId](#previousblockid)
|
|
22
|
+
- [queryTransactions](#querytransactions)
|
|
23
|
+
- [scriptAsScriptElements](#scriptasscriptelements)
|
|
24
|
+
- [scriptElementsAsScript](#scriptelementsasscript)
|
|
25
|
+
- [sizeOfTransaction](#sizeoftransaction)
|
|
26
|
+
- [unsignedTxFromPsbt](#unsignedtxfrompsbt)
|
|
27
|
+
|
|
7
28
|
### compactIntAsNumber
|
|
8
29
|
|
|
9
30
|
Convert a compact integer to a regular number
|
|
@@ -73,6 +94,24 @@ Derive output hash and version data from a base58 address string
|
|
|
73
94
|
version: <Script Version Byte Number>
|
|
74
95
|
}
|
|
75
96
|
|
|
97
|
+
### decodeBech32Address
|
|
98
|
+
|
|
99
|
+
Decode a bech32 address string to derive the address details
|
|
100
|
+
|
|
101
|
+
{
|
|
102
|
+
address: <Bech32 Encoded Address String>
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@throws
|
|
106
|
+
<Error>
|
|
107
|
+
|
|
108
|
+
@returns
|
|
109
|
+
{
|
|
110
|
+
data: <Output Data Buffer Object>
|
|
111
|
+
prefix: <Human Readable Prefix String>
|
|
112
|
+
version: <Witness Version Number>
|
|
113
|
+
}
|
|
114
|
+
|
|
76
115
|
### idForBlock
|
|
77
116
|
|
|
78
117
|
Get an id for a block: the double sha256 hash of the block header
|
|
@@ -89,10 +128,28 @@ Get an id for a block: the double sha256 hash of the block header
|
|
|
89
128
|
id: <Block Id Hex Encoded String>
|
|
90
129
|
}
|
|
91
130
|
|
|
131
|
+
### idForTransaction
|
|
132
|
+
|
|
133
|
+
Derive the standard transaction id for a raw serialized tx
|
|
134
|
+
|
|
135
|
+
{
|
|
136
|
+
transaction: <Hex Encoded Transaction String>
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@throws
|
|
140
|
+
<Error>
|
|
141
|
+
|
|
142
|
+
@returns
|
|
143
|
+
{
|
|
144
|
+
id: <Transaction Id Hex Encoded String>
|
|
145
|
+
}
|
|
146
|
+
|
|
92
147
|
### idForTransactionComponents
|
|
93
148
|
|
|
94
149
|
Determine a transaction id from transaction components
|
|
95
150
|
|
|
151
|
+
Note: remember the input hash is the reversed byte order of a normal tx id
|
|
152
|
+
|
|
96
153
|
{
|
|
97
154
|
inputs: [{
|
|
98
155
|
hash: <Spending Internal Byte Order Transaction Id Buffer Object>
|
|
@@ -299,6 +356,23 @@ Map array of script buffer elements to a fully formed script
|
|
|
299
356
|
script: <Script Hex String>
|
|
300
357
|
}
|
|
301
358
|
|
|
359
|
+
### sizeOfTransaction
|
|
360
|
+
|
|
361
|
+
Get the weight and virtual size of a hex-encoded transaction
|
|
362
|
+
|
|
363
|
+
{
|
|
364
|
+
transaction: <Hex Encoded Transaction String>
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
@throws
|
|
368
|
+
<Error>
|
|
369
|
+
|
|
370
|
+
@returns
|
|
371
|
+
{
|
|
372
|
+
vsize: <Transaction Virtual Size Number>
|
|
373
|
+
weight: <Transaction Weight Units Number>
|
|
374
|
+
}
|
|
375
|
+
|
|
302
376
|
### unsignedTxFromPsbt
|
|
303
377
|
|
|
304
378
|
Get the unsigned transaction out of a PSBT
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const modifyBech32Accumulator = require('./modify_bech32_accumulator');
|
|
2
|
+
|
|
3
|
+
const codeForBech32 = 1;
|
|
4
|
+
const codeForBech32m = 0x2bc830a3;
|
|
5
|
+
const {from} = Array;
|
|
6
|
+
const separator = 0;
|
|
7
|
+
|
|
8
|
+
/** Determine the type of bech32 data being represented and verify checksum
|
|
9
|
+
|
|
10
|
+
{
|
|
11
|
+
prefix: <Lowercase Human Readable Prefix String>
|
|
12
|
+
words: [<Bech32 5-Bit Data Number>]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@throws
|
|
16
|
+
<Error>
|
|
17
|
+
|
|
18
|
+
@returns
|
|
19
|
+
{
|
|
20
|
+
type: <Bech32 Type String>
|
|
21
|
+
}
|
|
22
|
+
*/
|
|
23
|
+
module.exports = ({prefix, words}) => {
|
|
24
|
+
let accumulator = 1;
|
|
25
|
+
|
|
26
|
+
// Collect the high bits
|
|
27
|
+
const highBits = from(
|
|
28
|
+
{length: prefix.length},
|
|
29
|
+
(_, i) => prefix.charCodeAt(i) >> 5
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// Collect the low bits
|
|
33
|
+
const lowBits = from(
|
|
34
|
+
{length: prefix.length},
|
|
35
|
+
(_, i) => prefix.charCodeAt(i) & 31
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
[...highBits, separator, ...lowBits, ...words].forEach(v => {
|
|
39
|
+
return accumulator = modifyBech32Accumulator(accumulator) ^ (v & 31);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
switch (accumulator) {
|
|
43
|
+
case codeForBech32:
|
|
44
|
+
return {type: 'bech32'};
|
|
45
|
+
|
|
46
|
+
case codeForBech32m:
|
|
47
|
+
return {type: 'bech32m'};
|
|
48
|
+
|
|
49
|
+
default:
|
|
50
|
+
throw new Error('UnexpectedChecksumValueForBech32DataAndPrefix');
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const bech32Type = require('./bech32_type');
|
|
2
|
+
|
|
3
|
+
const alphabet = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
|
4
|
+
const knownPrefixes = ['bc', 'tb', 'bcrt'];
|
|
5
|
+
const lengthSeparator = 1;
|
|
6
|
+
const lengthChecksum = 6;
|
|
7
|
+
const prefixDividerCharacter = '1';
|
|
8
|
+
const maximumCharactersLength = 90;
|
|
9
|
+
const minimumCharactersLength = 8;
|
|
10
|
+
const minimumDividerPosition = 1;
|
|
11
|
+
const notFoundIndex = -1;
|
|
12
|
+
const removeChecksum = words => words.slice(0, -6);
|
|
13
|
+
const split = (str, index) => [str.slice(0, index), str.slice(index + 1)];
|
|
14
|
+
|
|
15
|
+
/** Decode a Bech32/Bech32m encoded string and validate/remove checksum
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
encoded: <Bech32/Bech32m Encoded String
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@throws
|
|
22
|
+
<Error>
|
|
23
|
+
|
|
24
|
+
@returns
|
|
25
|
+
{
|
|
26
|
+
prefix: <Human Readable Part String>
|
|
27
|
+
type: <Bech32 Type String>
|
|
28
|
+
words: [<Data Words Number>]
|
|
29
|
+
}
|
|
30
|
+
*/
|
|
31
|
+
module.exports = ({encoded}) => {
|
|
32
|
+
if (typeof encoded !== 'string') {
|
|
33
|
+
throw new Error('ExpectedEncodedStringToDecodeBech32String');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (encoded.length > maximumCharactersLength) {
|
|
37
|
+
throw new Error('ExpectedShorterBech32EncodedStringToDecode');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (encoded.length < minimumCharactersLength) {
|
|
41
|
+
throw new Error('ExpectedLongerBech32EncodedStringToDecode');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const converted = encoded.toLowerCase();
|
|
45
|
+
|
|
46
|
+
if (encoded !== converted && encoded !== encoded.toUpperCase()) {
|
|
47
|
+
throw new Error('ExpectedSingleCasingStyleForBech32EncodedString');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Find the last index of the prefix divider character
|
|
51
|
+
const dividerPosition = converted.lastIndexOf(prefixDividerCharacter);
|
|
52
|
+
|
|
53
|
+
if (dividerPosition < minimumDividerPosition) {
|
|
54
|
+
throw new Error('ExpectedLaterDividerPositionForBech32String');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (converted.length - dividerPosition < lengthSeparator + lengthChecksum) {
|
|
58
|
+
throw new Error('ExpectedChecksumDataForBech32String');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Divide the data from the human readable prefix
|
|
62
|
+
const [prefix, data] = split(converted, dividerPosition);
|
|
63
|
+
|
|
64
|
+
// Make sure the prefix is a known one
|
|
65
|
+
if (!knownPrefixes.includes(prefix)) {
|
|
66
|
+
throw new Error('ExpectedKnownBech32AddressPrefixToDecodeBech32');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Convert data part into bech32 words
|
|
70
|
+
const words = [...data].map(char => alphabet.indexOf(char));
|
|
71
|
+
|
|
72
|
+
if (words.includes(notFoundIndex)) {
|
|
73
|
+
throw new Error('ExpectedAllKnownCharactersForBech32EncodedString');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const {type} = bech32Type({prefix, words});
|
|
77
|
+
|
|
78
|
+
return {prefix, type, words: removeChecksum(words)};
|
|
79
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const decodeBech32 = require('./decode_bech32');
|
|
2
|
+
|
|
3
|
+
const bitsPerBech32Word = 5;
|
|
4
|
+
const bitsPerByte = 8;
|
|
5
|
+
const byteMask = 0xff;
|
|
6
|
+
const bytesArrayAsBuffer = arr => Buffer.from(arr);
|
|
7
|
+
const knownV0ProgramLengths = [20, 32];
|
|
8
|
+
const maxProgLength = 40;
|
|
9
|
+
const maximumSegwitVersion = 16;
|
|
10
|
+
const minimumProgLength = 2;
|
|
11
|
+
const typeBech32V1 = 'bech32m';
|
|
12
|
+
const typeInitialSegwit = 'bech32';
|
|
13
|
+
const versionInitialSegwit = 0;
|
|
14
|
+
|
|
15
|
+
/** Decode a bech32 address string to derive the address details
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
address: <Bech32 Encoded Address String>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@throws
|
|
22
|
+
<Error>
|
|
23
|
+
|
|
24
|
+
@returns
|
|
25
|
+
{
|
|
26
|
+
prefix: <Human Readable Prefix String>
|
|
27
|
+
program: <Output Data Buffer Object>
|
|
28
|
+
version: <Witness Version Number>
|
|
29
|
+
}
|
|
30
|
+
*/
|
|
31
|
+
module.exports = ({address}) => {
|
|
32
|
+
const {prefix, type, words} = decodeBech32({encoded: address});
|
|
33
|
+
|
|
34
|
+
if (!words.length) {
|
|
35
|
+
throw new Error('ExpectedNonEmptyWitnessCommitmentDataInBech32Address');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const [version, ...data] = words;
|
|
39
|
+
|
|
40
|
+
if (version > maximumSegwitVersion) {
|
|
41
|
+
throw new Error('UnexpectedHighSegwitWitnessVersion');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (version === versionInitialSegwit && type !== typeInitialSegwit) {
|
|
45
|
+
throw new Error('ExpectedInitialSegwitTypeForInitialSegwitVersion');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (version !== versionInitialSegwit && type !== typeBech32V1) {
|
|
49
|
+
throw new Error('ExpectedUseOfLaterVersionBech32PostInitialSegwit');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let bits = 0;
|
|
53
|
+
let carry = 0;
|
|
54
|
+
const program = [];
|
|
55
|
+
|
|
56
|
+
// Convert the 5 bit bech32 words into 8 bit (ie bytes) data
|
|
57
|
+
data.forEach(value => {
|
|
58
|
+
carry = (carry << bitsPerBech32Word) | value;
|
|
59
|
+
|
|
60
|
+
bits += bitsPerBech32Word;
|
|
61
|
+
|
|
62
|
+
while (bits >= bitsPerByte) {
|
|
63
|
+
bits -= bitsPerByte;
|
|
64
|
+
|
|
65
|
+
program.push((carry >> bits) & byteMask);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (bits >= bitsPerBech32Word) {
|
|
70
|
+
throw new Error('ExpectedNoPaddingOnBech32Address');
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
if (((carry << (bitsPerByte - bits)) & byteMask) !== 0) {
|
|
74
|
+
throw new Error('UnexpectedNonZeroPadding');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (program.length < minimumProgLength || program.length > maxProgLength) {
|
|
78
|
+
throw new Error('UnexpectedSizeOfBech32AddressProgram');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (version === 0 && !knownV0ProgramLengths.includes(program.length)) {
|
|
82
|
+
throw new Error('UnexpectedBech32AddressProgramSizeForV0Address')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {version, prefix, program: bytesArrayAsBuffer(program)};
|
|
86
|
+
};
|
package/addresses/index.js
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const generatedModifierValues = [[0x01, 0x3b6a57b2], [0x02, 0x26508e6d], [0x04, 0x1ea119fa], [0x08, 0x3d4233dd], [0x10, 0x2a1462b3]];
|
|
2
|
+
const lowerBitsBitMask = 0x1ffffff;
|
|
3
|
+
|
|
4
|
+
/** Update the 30 bit accumulator with Bech32 precomputed values
|
|
5
|
+
|
|
6
|
+
accumulator: <Current Accumulator 32-Bit Range Integer Number>
|
|
7
|
+
|
|
8
|
+
@returns
|
|
9
|
+
<Bech32 Generator Constants Modified Number>
|
|
10
|
+
*/
|
|
11
|
+
module.exports = accumulator => {
|
|
12
|
+
// Take the top 5 bits of the accumulator value (30 - 5 = 25)
|
|
13
|
+
const topBits = accumulator >> 25;
|
|
14
|
+
|
|
15
|
+
// Shift the lower 25 bits up by 5
|
|
16
|
+
let modified = ((accumulator & lowerBitsBitMask) << 5) >>> 0;
|
|
17
|
+
|
|
18
|
+
// Based on bit masks, apply the appropriate precomputed Bech32 values
|
|
19
|
+
generatedModifierValues
|
|
20
|
+
.filter(([mask]) => topBits & mask)
|
|
21
|
+
.forEach(([, modifier]) => modified ^= modifier);
|
|
22
|
+
|
|
23
|
+
return modified;
|
|
24
|
+
};
|
|
@@ -12,6 +12,8 @@ const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
|
12
12
|
|
|
13
13
|
/** Determine a transaction id from transaction components
|
|
14
14
|
|
|
15
|
+
Note: remember the input hash is the reversed byte order of a normal tx id
|
|
16
|
+
|
|
15
17
|
{
|
|
16
18
|
inputs: [{
|
|
17
19
|
hash: <Spending Internal Byte Order Transaction Id Buffer Object>
|
package/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const {compactIntAsNumber} = require('./numbers');
|
|
2
2
|
const {componentsOfTransaction} = require('./transactions');
|
|
3
3
|
const {decodeBase58Address} = require('./addresses');
|
|
4
|
+
const {decodeBech32Address} = require('./addresses');
|
|
4
5
|
const {idForBlock} = require('./hashes');
|
|
6
|
+
const {idForTransaction} = require('./transactions');
|
|
5
7
|
const {idForTransactionComponents} = require('./hashes');
|
|
6
8
|
const {noLocktimeIdForTransaction} = require('./transactions');
|
|
7
9
|
const {numberAsCompactInt} = require('./numbers');
|
|
@@ -14,13 +16,16 @@ const {previousBlockId} = require('./hashes');
|
|
|
14
16
|
const {queryTransactions} = require('./transactions');
|
|
15
17
|
const {scriptAsScriptElements} = require('./script');
|
|
16
18
|
const {scriptElementsAsScript} = require('./script');
|
|
19
|
+
const {sizeOfTransaction} = require('./transactions');
|
|
17
20
|
const {unsignedTxFromPsbt} = require('./transactions');
|
|
18
21
|
|
|
19
22
|
module.exports = {
|
|
20
23
|
compactIntAsNumber,
|
|
21
24
|
componentsOfTransaction,
|
|
22
25
|
decodeBase58Address,
|
|
26
|
+
decodeBech32Address,
|
|
23
27
|
idForBlock,
|
|
28
|
+
idForTransaction,
|
|
24
29
|
idForTransactionComponents,
|
|
25
30
|
noLocktimeIdForTransaction,
|
|
26
31
|
numberAsCompactInt,
|
|
@@ -33,5 +38,6 @@ module.exports = {
|
|
|
33
38
|
queryTransactions,
|
|
34
39
|
scriptAsScriptElements,
|
|
35
40
|
scriptElementsAsScript,
|
|
41
|
+
sizeOfTransaction,
|
|
36
42
|
unsignedTxFromPsbt,
|
|
37
43
|
};
|
|
@@ -40,14 +40,13 @@ module.exports = ({encoded, start}) => {
|
|
|
40
40
|
|
|
41
41
|
case 5:
|
|
42
42
|
return {bytes, number: encoded.readUInt32LE(offset + lengthMarkerOffset)};
|
|
43
|
+
}
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
const number = encoded.readBigUInt64LE(offset + lengthMarkerOffset);
|
|
46
|
-
|
|
47
|
-
if (!isSafeNumber(number)) {
|
|
48
|
-
throw new Error('ExpectedSafeSizeEncodedCompactInteger');
|
|
49
|
-
}
|
|
45
|
+
const number = encoded.readBigUInt64LE(offset + lengthMarkerOffset);
|
|
50
46
|
|
|
51
|
-
|
|
47
|
+
if (!isSafeNumber(number)) {
|
|
48
|
+
throw new Error('ExpectedSafeSizeEncodedCompactInteger');
|
|
52
49
|
}
|
|
50
|
+
|
|
51
|
+
return {bytes, number: Number(number)};
|
|
53
52
|
};
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"description": "Blockchain data utility methods",
|
|
10
10
|
"engines": {
|
|
11
|
-
"node": ">=
|
|
11
|
+
"node": ">=22"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"name": "@alexbosworth/blockchain",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "https://github.com/alexbosworth/blockchain.git"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
-
"test": "
|
|
20
|
+
"test": "node --experimental-test-coverage --test test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
|
|
21
21
|
},
|
|
22
|
-
"version": "
|
|
22
|
+
"version": "4.0.0"
|
|
23
23
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const {deepEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const {throws} = require('node:assert').strict;
|
|
4
|
+
|
|
5
|
+
const bech32Type = require('./../../addresses/bech32_type');
|
|
6
|
+
|
|
7
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
+
|
|
9
|
+
const tests = [
|
|
10
|
+
{
|
|
11
|
+
args: {prefix: 'bc', words: [0, 0, 0, 0, 0, 0]},
|
|
12
|
+
description: 'The checksum must derive to a known value',
|
|
13
|
+
error: 'UnexpectedChecksumValueForBech32DataAndPrefix',
|
|
14
|
+
},
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
18
|
+
return test(description, (t, end) => {
|
|
19
|
+
if (!!error) {
|
|
20
|
+
throws(() => bech32Type(args), new Error(error), 'Err');
|
|
21
|
+
} else {
|
|
22
|
+
const res = bech32Type(args);
|
|
23
|
+
|
|
24
|
+
deepEqual(res, expected, 'Got expected result');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return end();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const {deepEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const {throws} = require('node:assert').strict;
|
|
4
|
+
|
|
5
|
+
const decodeBech32 = require('./../../addresses/decode_bech32');
|
|
6
|
+
|
|
7
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
+
|
|
9
|
+
const tests = [
|
|
10
|
+
{
|
|
11
|
+
args: {encoded: null},
|
|
12
|
+
description: 'A bech32 string is expected',
|
|
13
|
+
error: 'ExpectedEncodedStringToDecodeBech32String',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
args: {encoded: 'b'.repeat(91)},
|
|
17
|
+
description: 'A short bech32 string is expected',
|
|
18
|
+
error: 'ExpectedShorterBech32EncodedStringToDecode',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
args: {encoded: 'b'.repeat(7)},
|
|
22
|
+
description: 'A long bech32 string is expected',
|
|
23
|
+
error: 'ExpectedLongerBech32EncodedStringToDecode',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
args: {encoded: '1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P'},
|
|
27
|
+
description: 'A farther divider is expected',
|
|
28
|
+
error: 'ExpectedLaterDividerPositionForBech32String',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
args: {encoded: 'BCKJBC1QR5'},
|
|
32
|
+
description: 'A shorter divider is expected',
|
|
33
|
+
error: 'ExpectedChecksumDataForBech32String',
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
38
|
+
return test(description, (t, end) => {
|
|
39
|
+
if (!!error) {
|
|
40
|
+
throws(() => decodeBech32(args), new Error(error), 'Err');
|
|
41
|
+
} else {
|
|
42
|
+
const res = decodeBech32(args);
|
|
43
|
+
|
|
44
|
+
deepEqual(res, expected, 'Got expected result');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return end();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
const {deepEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const {throws} = require('node:assert').strict;
|
|
4
|
+
|
|
5
|
+
const {decodeBech32Address} = require('./../../');
|
|
6
|
+
|
|
7
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
+
|
|
9
|
+
const tests = [
|
|
10
|
+
{
|
|
11
|
+
args: {
|
|
12
|
+
address: 'tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut',
|
|
13
|
+
},
|
|
14
|
+
description: 'Invalid human-readable part',
|
|
15
|
+
error: 'ExpectedKnownBech32AddressPrefixToDecodeBech32',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
args: {
|
|
19
|
+
address: 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd',
|
|
20
|
+
},
|
|
21
|
+
description: 'Invalid checksum (Bech32 instead of Bech32m)',
|
|
22
|
+
error: 'ExpectedUseOfLaterVersionBech32PostInitialSegwit',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
args: {
|
|
26
|
+
address: 'tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf',
|
|
27
|
+
},
|
|
28
|
+
description: 'Invalid tb checksum (Bech32 instead of Bech32m)',
|
|
29
|
+
error: 'ExpectedUseOfLaterVersionBech32PostInitialSegwit',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
args: {
|
|
33
|
+
address: 'BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL',
|
|
34
|
+
},
|
|
35
|
+
description: 'Invalid BC checksum (Bech32 instead of Bech32m)',
|
|
36
|
+
error: 'ExpectedUseOfLaterVersionBech32PostInitialSegwit',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
args: {address: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh'},
|
|
40
|
+
description: 'Invalid checksum (Bech32m instead of Bech32)',
|
|
41
|
+
error: 'ExpectedInitialSegwitTypeForInitialSegwitVersion',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
args: {
|
|
45
|
+
address: 'tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47',
|
|
46
|
+
},
|
|
47
|
+
description: 'Invalid tb checksum (Bech32m instead of Bech32)',
|
|
48
|
+
error: 'ExpectedInitialSegwitTypeForInitialSegwitVersion',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
args: {
|
|
52
|
+
address: 'bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4',
|
|
53
|
+
},
|
|
54
|
+
description: 'Invalid character in checksum',
|
|
55
|
+
error: 'ExpectedAllKnownCharactersForBech32EncodedString',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
args: {
|
|
59
|
+
address: 'BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R',
|
|
60
|
+
},
|
|
61
|
+
description: 'Invalid witness version',
|
|
62
|
+
error: 'UnexpectedHighSegwitWitnessVersion',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
args: {address: 'bc1pw5dgrnzv'},
|
|
66
|
+
description: 'Invalid program length (1 byte)',
|
|
67
|
+
error: 'UnexpectedSizeOfBech32AddressProgram',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
args: {
|
|
71
|
+
address: 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav',
|
|
72
|
+
},
|
|
73
|
+
description: 'Invalid program length (41 bytes)',
|
|
74
|
+
error: 'UnexpectedSizeOfBech32AddressProgram',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
args: {address: 'BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P'},
|
|
78
|
+
description: 'Invalid program length for witness version 0 (per BIP141)',
|
|
79
|
+
error: 'UnexpectedBech32AddressProgramSizeForV0Address',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
args: {
|
|
83
|
+
address: 'tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq',
|
|
84
|
+
},
|
|
85
|
+
description: 'Mixed case',
|
|
86
|
+
error: 'ExpectedSingleCasingStyleForBech32EncodedString',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
args: {
|
|
90
|
+
address: 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf',
|
|
91
|
+
},
|
|
92
|
+
description: 'zero padding of more than 4 bits',
|
|
93
|
+
error: 'ExpectedNoPaddingOnBech32Address',
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
args: {
|
|
97
|
+
address: 'tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j',
|
|
98
|
+
},
|
|
99
|
+
description: 'Non-zero padding in 8-to-5 conversion',
|
|
100
|
+
error: 'UnexpectedNonZeroPadding',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
args: {address: 'bc1gmk9yu'},
|
|
104
|
+
description: 'Empty data section',
|
|
105
|
+
error: 'ExpectedNonEmptyWitnessCommitmentDataInBech32Address',
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
args: {address: 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4'},
|
|
109
|
+
description: 'Details are returned for a btc1q address',
|
|
110
|
+
expected: {
|
|
111
|
+
prefix: 'bc',
|
|
112
|
+
program: hexAsBuffer('751e76e8199196d454941c45d1b3a323f1433bd6'),
|
|
113
|
+
version: 0,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
args: {
|
|
118
|
+
address: 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7',
|
|
119
|
+
},
|
|
120
|
+
description: 'Details are returned for a tb1q address',
|
|
121
|
+
expected: {
|
|
122
|
+
prefix: 'tb',
|
|
123
|
+
program: hexAsBuffer('1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'),
|
|
124
|
+
version: 0,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
args: {
|
|
129
|
+
address: 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y',
|
|
130
|
+
},
|
|
131
|
+
description: 'Details are returned for a bc1p address',
|
|
132
|
+
expected: {
|
|
133
|
+
prefix: 'bc',
|
|
134
|
+
program: hexAsBuffer('751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6'),
|
|
135
|
+
version: 1,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
args: {address: 'BC1SW50QGDZ25J'},
|
|
140
|
+
description: 'Details are returned for a short bc1 address',
|
|
141
|
+
expected: {prefix: 'bc', program: hexAsBuffer('751e'), version: 16},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
args: {address: 'bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs'},
|
|
145
|
+
description: 'Details are returned for a midsize bc1 address',
|
|
146
|
+
expected: {
|
|
147
|
+
prefix: 'bc',
|
|
148
|
+
program: hexAsBuffer('751e76e8199196d454941c45d1b3a323'),
|
|
149
|
+
version: 2,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
args: {
|
|
154
|
+
address: 'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy',
|
|
155
|
+
},
|
|
156
|
+
description: 'Details are returned for a zero starting tb address',
|
|
157
|
+
expected: {
|
|
158
|
+
prefix: 'tb',
|
|
159
|
+
program: hexAsBuffer('000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433'),
|
|
160
|
+
version: 0,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
args: {
|
|
165
|
+
address: 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0',
|
|
166
|
+
},
|
|
167
|
+
description: 'Details are returned for a bc1 v1 address',
|
|
168
|
+
expected: {
|
|
169
|
+
prefix: 'bc',
|
|
170
|
+
program: hexAsBuffer('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
|
171
|
+
version: 1,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
177
|
+
return test(description, (t, end) => {
|
|
178
|
+
if (!!error) {
|
|
179
|
+
throws(() => decodeBech32Address(args), new Error(error), 'Err');
|
|
180
|
+
} else {
|
|
181
|
+
const res = decodeBech32Address(args);
|
|
182
|
+
|
|
183
|
+
deepEqual(res, expected, 'Got expected result');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return end();
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const {strictEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
|
|
4
|
+
const parsePushBytesCount = require('./../../script/parse_push_bytes_count');
|
|
5
|
+
|
|
6
|
+
test('An unsupported op code is not parsed as a push', (t, end) => {
|
|
7
|
+
const res = parsePushBytesCount({
|
|
8
|
+
offset: 0,
|
|
9
|
+
script: Buffer.from('4f00000000', 'hex'),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
strictEqual(res, undefined, 'Got no push bytes count');
|
|
13
|
+
|
|
14
|
+
return end();
|
|
15
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {idForTransaction} = require('./../../');
|
|
6
|
+
|
|
7
|
+
const tests = [
|
|
8
|
+
{
|
|
9
|
+
args: {transaction: ''},
|
|
10
|
+
description: 'A transaction hex is required',
|
|
11
|
+
error: 'ExpectedHexEncodedTransactionToGetComponentsOf',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
args: {
|
|
15
|
+
transaction: '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000',
|
|
16
|
+
},
|
|
17
|
+
description: 'A coinbase transaction',
|
|
18
|
+
expected: {
|
|
19
|
+
id: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
args: {
|
|
24
|
+
transaction: '01000000019ac03d5ae6a875d970128ef9086cef276a1919684a6988023cc7254691d97e6d010000006b4830450221009d41dc793ba24e65f571473d40b299b6459087cea1509f0d381740b1ac863cb6022039c425906fcaf51b2b84d8092569fb3213de43abaff2180e2a799d4fcb4dd0aa012102d5ede09a8ae667d0f855ef90325e27f6ce35bbe60a1e6e87af7f5b3c652140fdffffffff080100000000000000010101000000000000000202010100000000000000014c0100000000000000034c02010100000000000000014d0100000000000000044dffff010100000000000000014e0100000000000000064effffffff0100000000',
|
|
25
|
+
},
|
|
26
|
+
description: 'invalid script',
|
|
27
|
+
expected: {
|
|
28
|
+
id: 'ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
args: {
|
|
33
|
+
transaction: '02000000000103ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570200000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570100000000ffffffff0640420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6de91f62901000000225120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d49240420f00000000001600148729d17b2aa507ab19051a028384bc6e0ce25e455e368900000000002251200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b440420f0000000000160014d2d59a8a59f997cbc8888411010faf1658e0e3465e368900000000002251207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab2024830450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d0121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef75329602483045022100f9cde9adb00c0a6c62dae8604ca750039201288c0dafff952461da3caf05e3ae0220679c01f2518413951de3b62531b1cf36bb92562e3bd4197f0fa6e6e3e231272e0121027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d0247304402202550beec478845af2df929abf85708f9fcceaae31377f2e01d803e2acf7b426f022036c312b1e38ca333fe70aa37d3093387ac7486f08438eb8eed323699594468cb012102275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe00000000',
|
|
34
|
+
},
|
|
35
|
+
description: 'Transaction with witnesses and P2TR and P2WPKH outputs',
|
|
36
|
+
expected: {
|
|
37
|
+
id: 'ab940e8efd07f7f85f907e36f8e6509ab795de91bbd3e0fa69781549464506a4',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
args: {
|
|
42
|
+
transaction: '0100000001f709fa82596e4f908ee331cb5e0ed46ab331d7dcfaf697fe95891e73dac4ebcb000000008c20ca42095840735e89283fec298e62ac2ddea9b5f34a8cbb7097ad965b87568100201b1b01dc829177da4a14551d2fc96a9db00c6501edfa12f22cd9cefd335c227f483045022100a9df60536df5733dd0de6bc921fab0b3eee6426501b43a228afa2c90072eb5ca02201c78b74266fac7d1db5deff080d8a403743203f109fbcabf6d5a760bf87386d20100ffffffff01c075790000000000232103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6ac00000000',
|
|
43
|
+
},
|
|
44
|
+
description: 'A non-segwit transaction is read',
|
|
45
|
+
expected: {
|
|
46
|
+
id: 'cc60b1f899ec0a69b7c3f25ddf32c4524096a9c5b01cbd84c6d0312a0c478984',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
args: {
|
|
51
|
+
transaction: '0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000',
|
|
52
|
+
},
|
|
53
|
+
description: 'A P2PK transaction is read',
|
|
54
|
+
expected: {
|
|
55
|
+
id: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
61
|
+
return test(description, (t, end) => {
|
|
62
|
+
if (!!error) {
|
|
63
|
+
throws(() => idForTransaction(args), new Error(error), 'Got err');
|
|
64
|
+
} else {
|
|
65
|
+
const res = idForTransaction(args);
|
|
66
|
+
|
|
67
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return end();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {sizeOfTransaction} = require('./../../');
|
|
6
|
+
|
|
7
|
+
const legacyTransaction = [
|
|
8
|
+
'02000000',
|
|
9
|
+
'01',
|
|
10
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
11
|
+
'00000000',
|
|
12
|
+
'00',
|
|
13
|
+
'ffffffff',
|
|
14
|
+
'01',
|
|
15
|
+
'0100000000000000',
|
|
16
|
+
'01',
|
|
17
|
+
'51',
|
|
18
|
+
'00000000',
|
|
19
|
+
].join('');
|
|
20
|
+
|
|
21
|
+
const witnessTransaction = [
|
|
22
|
+
'02000000',
|
|
23
|
+
'0001',
|
|
24
|
+
'01',
|
|
25
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
26
|
+
'00000000',
|
|
27
|
+
'00',
|
|
28
|
+
'ffffffff',
|
|
29
|
+
'01',
|
|
30
|
+
'0100000000000000',
|
|
31
|
+
'01',
|
|
32
|
+
'51',
|
|
33
|
+
'01',
|
|
34
|
+
'01',
|
|
35
|
+
'00',
|
|
36
|
+
'00000000',
|
|
37
|
+
].join('');
|
|
38
|
+
|
|
39
|
+
const tests = [
|
|
40
|
+
{
|
|
41
|
+
args: {transaction: ''},
|
|
42
|
+
description: 'A transaction is required',
|
|
43
|
+
error: 'ExpectedHexEncodedTransactionToGetSize',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
args: {transaction: '00f'},
|
|
47
|
+
description: 'An even-length transaction is required',
|
|
48
|
+
error: 'ExpectedHexEncodedTransactionToGetSize',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
args: {transaction: 'not hex'},
|
|
52
|
+
description: 'A hex-encoded transaction is required',
|
|
53
|
+
error: 'ExpectedHexEncodedTransactionToGetSize',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
args: {transaction: `${legacyTransaction}00`},
|
|
57
|
+
description: 'Data after the transaction is not accepted',
|
|
58
|
+
error: 'UnexpectedDataAfterTransactionToGetSize',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
args: {transaction: legacyTransaction},
|
|
62
|
+
description: 'The size of a legacy transaction is returned',
|
|
63
|
+
expected: {vsize: 61, weight: 244},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
args: {transaction: witnessTransaction},
|
|
67
|
+
description: 'The size of a witness transaction is returned',
|
|
68
|
+
expected: {vsize: 63, weight: 249},
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
73
|
+
return test(description, (t, end) => {
|
|
74
|
+
if (!!error) {
|
|
75
|
+
throws(() => sizeOfTransaction(args), new Error(error), 'Got error');
|
|
76
|
+
} else {
|
|
77
|
+
const res = sizeOfTransaction(args);
|
|
78
|
+
|
|
79
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return end();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const componentsOfTransaction = require('./components_of_transaction');
|
|
2
|
+
const {idForTransactionComponents} = require('./../hashes');
|
|
3
|
+
|
|
4
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
5
|
+
|
|
6
|
+
/** Derive the standard transaction id for a raw serialized tx
|
|
7
|
+
|
|
8
|
+
{
|
|
9
|
+
transaction: <Hex Encoded Transaction String>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@throws
|
|
13
|
+
<Error>
|
|
14
|
+
|
|
15
|
+
@returns
|
|
16
|
+
{
|
|
17
|
+
id: <Transaction Id Hex Encoded String>
|
|
18
|
+
}
|
|
19
|
+
*/
|
|
20
|
+
module.exports = ({transaction}) => {
|
|
21
|
+
const tx = componentsOfTransaction({transaction});
|
|
22
|
+
|
|
23
|
+
const {id} = idForTransactionComponents({
|
|
24
|
+
inputs: tx.inputs.map(({id, script, sequence, vout}) => ({
|
|
25
|
+
sequence,
|
|
26
|
+
vout,
|
|
27
|
+
hash: hexAsBuffer(id).reverse(),
|
|
28
|
+
script: hexAsBuffer(script),
|
|
29
|
+
})),
|
|
30
|
+
locktime: tx.locktime,
|
|
31
|
+
outputs: tx.outputs.map(({script, tokens}) => ({
|
|
32
|
+
tokens,
|
|
33
|
+
script: hexAsBuffer(script),
|
|
34
|
+
})),
|
|
35
|
+
version: tx.version,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return {id};
|
|
39
|
+
};
|
package/transactions/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
const componentsOfTransaction = require('./components_of_transaction');
|
|
2
|
+
const idForTransaction = require('./id_for_transaction');
|
|
2
3
|
const noLocktimeIdForTransaction = require('./no_locktime_id_for_transaction');
|
|
3
4
|
const parseTransaction = require('./parse_transaction');
|
|
4
5
|
const queryTransactions = require('./query_transactions');
|
|
6
|
+
const sizeOfTransaction = require('./size_of_transaction');
|
|
5
7
|
const unsignedTxFromPsbt = require('./unsigned_tx_from_psbt');
|
|
6
8
|
|
|
7
9
|
module.exports = {
|
|
8
10
|
componentsOfTransaction,
|
|
11
|
+
idForTransaction,
|
|
9
12
|
noLocktimeIdForTransaction,
|
|
10
13
|
parseTransaction,
|
|
11
14
|
queryTransactions,
|
|
15
|
+
sizeOfTransaction,
|
|
12
16
|
unsignedTxFromPsbt,
|
|
13
17
|
};
|
|
@@ -11,8 +11,6 @@ const defaultLocktime = 0;
|
|
|
11
11
|
const defaultStartIndex = 0;
|
|
12
12
|
const defaultWitnessCount = 0;
|
|
13
13
|
const {isBuffer} = Buffer;
|
|
14
|
-
const segWitV0Marker = 0;
|
|
15
|
-
const segWitV0Flag = 1;
|
|
16
14
|
const times = n => [...Array(n).keys()];
|
|
17
15
|
|
|
18
16
|
/** Parse a raw transaction out of a buffer at a specific offset start
|
|
@@ -51,19 +49,19 @@ module.exports = args => {
|
|
|
51
49
|
let offset = start;
|
|
52
50
|
|
|
53
51
|
// Transaction version is a signed 4 byte integer
|
|
54
|
-
const version = args.buffer.readInt32LE(offset
|
|
52
|
+
const version = args.buffer.readInt32LE(offset);
|
|
55
53
|
|
|
56
54
|
offset += byteCountInt32;
|
|
57
55
|
|
|
58
56
|
// For SegWit transactions, the next 2 bytes would be a marker and flag
|
|
59
57
|
const flagSplit = offset + byteCountInt8;
|
|
60
58
|
|
|
61
|
-
const marker = args.buffer.readUInt8(offset
|
|
59
|
+
const marker = args.buffer.readUInt8(offset);
|
|
62
60
|
|
|
63
|
-
const flag = args.buffer.readUInt8(flagSplit
|
|
61
|
+
const flag = args.buffer.readUInt8(flagSplit);
|
|
64
62
|
|
|
65
63
|
// The presence of the marker and flag indicates SegWit tx encoding
|
|
66
|
-
const isSegWit = !marker && flag
|
|
64
|
+
const isSegWit = !marker && !!flag;
|
|
67
65
|
|
|
68
66
|
// When tx isn't SegWit though, the bytes are not marker and flag
|
|
69
67
|
offset += isSegWit ? byteCountMarkerFlag : byteCountNoMarkerFlag;
|
|
@@ -83,7 +81,7 @@ module.exports = args => {
|
|
|
83
81
|
offset += byteCountHash;
|
|
84
82
|
|
|
85
83
|
// The vout is the 4 byte output index of the tx being spent in this input
|
|
86
|
-
const vout = args.buffer.readUInt32LE(offset
|
|
84
|
+
const vout = args.buffer.readUInt32LE(offset);
|
|
87
85
|
|
|
88
86
|
offset += byteCountInt32;
|
|
89
87
|
|
|
@@ -98,7 +96,7 @@ module.exports = args => {
|
|
|
98
96
|
offset += scriptLength.number;
|
|
99
97
|
|
|
100
98
|
// Sequence is a 4 byte unsigned number for an input, used for CSV mainly
|
|
101
|
-
const sequence = args.buffer.readUInt32LE(offset
|
|
99
|
+
const sequence = args.buffer.readUInt32LE(offset);
|
|
102
100
|
|
|
103
101
|
offset += byteCountInt32;
|
|
104
102
|
|
|
@@ -112,7 +110,7 @@ module.exports = args => {
|
|
|
112
110
|
// Read in the outputs of the transaction
|
|
113
111
|
const outputs = times(outputsCount.number).map(i => {
|
|
114
112
|
// The value being spent
|
|
115
|
-
const value = args.buffer.readBigUInt64LE(offset
|
|
113
|
+
const value = args.buffer.readBigUInt64LE(offset);
|
|
116
114
|
|
|
117
115
|
offset += byteCountInt64;
|
|
118
116
|
|
|
@@ -165,7 +163,7 @@ module.exports = args => {
|
|
|
165
163
|
});
|
|
166
164
|
|
|
167
165
|
// The final element is the 4 byte transaction nLockTime
|
|
168
|
-
const locktime = args.buffer.readUInt32LE(offset
|
|
166
|
+
const locktime = args.buffer.readUInt32LE(offset);
|
|
169
167
|
|
|
170
168
|
offset += byteCountInt32;
|
|
171
169
|
|
|
@@ -173,4 +171,4 @@ module.exports = args => {
|
|
|
173
171
|
const bytes = args.buffer.subarray(start, offset);
|
|
174
172
|
|
|
175
173
|
return {bytes, inputs, locktime, outputs, version};
|
|
176
|
-
};
|
|
174
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const parseTransaction = require('./parse_transaction');
|
|
2
|
+
|
|
3
|
+
const byteCountLocktime = 4;
|
|
4
|
+
const byteCountMarkerFlag = 2;
|
|
5
|
+
const {ceil} = Math;
|
|
6
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
7
|
+
const isHex = n => !!n && !(n.length % 2) && /^[0-9A-F]*$/i.test(n);
|
|
8
|
+
const noFlagAndMarker = 0;
|
|
9
|
+
const witnessScaling = 4;
|
|
10
|
+
|
|
11
|
+
/** Get the weight and virtual size of a hex-encoded transaction
|
|
12
|
+
|
|
13
|
+
{
|
|
14
|
+
transaction: <Hex Encoded Transaction String>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@throws
|
|
18
|
+
<Error>
|
|
19
|
+
|
|
20
|
+
@returns
|
|
21
|
+
{
|
|
22
|
+
vsize: <Transaction Virtual Size Number>
|
|
23
|
+
weight: <Transaction Weight Units Number>
|
|
24
|
+
}
|
|
25
|
+
*/
|
|
26
|
+
module.exports = ({transaction}) => {
|
|
27
|
+
if (!isHex(transaction)) {
|
|
28
|
+
throw new Error('ExpectedHexEncodedTransactionToGetSize');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const buffer = hexAsBuffer(transaction);
|
|
32
|
+
|
|
33
|
+
const {bytes, inputs} = parseTransaction({buffer});
|
|
34
|
+
|
|
35
|
+
if (bytes.length !== buffer.length) {
|
|
36
|
+
throw new Error('UnexpectedDataAfterTransactionToGetSize');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Parse through the outputs, stopping before witness data and nLockTime
|
|
40
|
+
const txBeforeWitness = parseTransaction({
|
|
41
|
+
buffer,
|
|
42
|
+
is_terminating_after_outputs: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Before the witness, a SegWit transaction includes marker and flag bytes
|
|
46
|
+
const hasWitness = inputs.some(input => input.witness !== undefined);
|
|
47
|
+
|
|
48
|
+
const markerFlagSize = hasWitness ? byteCountMarkerFlag : noFlagAndMarker;
|
|
49
|
+
const sizeBeforeWitness = txBeforeWitness.bytes.length;
|
|
50
|
+
|
|
51
|
+
// Stripped size includes nLockTime but excludes marker, flag, and witnesses
|
|
52
|
+
const strippedSize = sizeBeforeWitness + byteCountLocktime - markerFlagSize;
|
|
53
|
+
|
|
54
|
+
// Base bytes weigh four units and witness bytes weigh one unit
|
|
55
|
+
const weight = strippedSize * witnessScaling + bytes.length - strippedSize;
|
|
56
|
+
|
|
57
|
+
return {weight, vsize: ceil(weight / witnessScaling)};
|
|
58
|
+
};
|
|
File without changes
|