@alexbosworth/blockchain 4.7.2 → 4.9.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 +8 -0
- package/README.md +51 -0
- package/addresses/decode_base58.js +119 -0
- package/addresses/decode_base58_address.js +3 -95
- package/addresses/index.js +2 -0
- package/index.js +4 -0
- package/package.json +12 -2
- package/script/conf/networks.json +22 -0
- package/script/index.js +2 -0
- package/script/output_script_for_address.js +96 -0
- package/.gitattributes +0 -1
- package/.tm_properties +0 -1
- package/test/5bit/test_bytes_as_words.js +0 -49
- package/test/5bit/test_words_as_bytes.js +0 -79
- package/test/addresses/test_bech32_type.js +0 -29
- package/test/addresses/test_decode_base58_address.js +0 -70
- package/test/addresses/test_decode_bech32.js +0 -49
- package/test/addresses/test_decode_bech32_address.js +0 -188
- package/test/addresses/test_encode_base58_address.js +0 -127
- package/test/addresses/test_encode_bech32.js +0 -69
- package/test/addresses/test_encode_bech32_address.js +0 -167
- package/test/fixtures/tx_invalid.json +0 -397
- package/test/fixtures/tx_valid.json +0 -528
- package/test/hashes/test_hash_for_p2pkh.js +0 -54
- package/test/hashes/test_hash_for_p2wpkh.js +0 -54
- package/test/hashes/test_id_for_block.js +0 -38
- package/test/hashes/test_no_locktime_id_for_transaction.js +0 -56
- package/test/hashes/test_previous_block_id.js +0 -38
- package/test/numbers/test_compact_int_as_number.js +0 -104
- package/test/numbers/test_encode_push_bytes_count.js +0 -82
- package/test/numbers/test_number_as_compact_int.js +0 -77
- package/test/numbers/test_number_as_little_endian.js +0 -77
- package/test/numbers/test_parse_push_bytes_count.js +0 -67
- package/test/script/test_p2ms_script.js +0 -106
- package/test/script/test_p2pkh_output_script.js +0 -41
- package/test/script/test_p2sh_output_script.js +0 -41
- package/test/script/test_p2tr_output_script.js +0 -43
- package/test/script/test_p2wpkh_output_script.js +0 -41
- package/test/script/test_p2wsh_output_script.js +0 -43
- package/test/script/test_script_as_script_elements.js +0 -84
- package/test/script/test_script_elements_as_script.js +0 -72
- package/test/transactions/test_components_of_transaction.js +0 -352
- package/test/transactions/test_core_tx_vectors.js +0 -70
- package/test/transactions/test_id_for_transaction.js +0 -72
- package/test/transactions/test_non_witness_hash_to_sign.js +0 -404
- package/test/transactions/test_query_transactions.js +0 -87
- package/test/transactions/test_size_of_transaction.js +0 -84
- package/test/transactions/test_transaction_from_components.js +0 -281
- package/test/transactions/test_unsigned_tx_from_psbt.js +0 -89
- package/test/transactions/test_v0_hash_to_sign.js +0 -262
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Versions
|
|
2
2
|
|
|
3
|
+
## 4.9.0
|
|
4
|
+
|
|
5
|
+
- `outputScriptForAddress`: Add method to get the output script for an address
|
|
6
|
+
|
|
7
|
+
## 4.8.0
|
|
8
|
+
|
|
9
|
+
- `decodeBase58`: Add method to decode a base58check string into its payload
|
|
10
|
+
|
|
3
11
|
## 4.7.2
|
|
4
12
|
|
|
5
13
|
- `nonWitnessHashToSign`: Add method to calculate the pre-SegWit hash to sign
|
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Utility methods for working with Blockchain data
|
|
|
6
6
|
|
|
7
7
|
- [compactIntAsNumber](#compactintasnumber)
|
|
8
8
|
- [componentsOfTransaction](#componentsoftransaction)
|
|
9
|
+
- [decodeBase58](#decodebase58)
|
|
9
10
|
- [decodeBase58Address](#decodebase58address)
|
|
10
11
|
- [decodeBech32Address](#decodebech32address)
|
|
11
12
|
- [encodeBase58Address](#encodebase58address)
|
|
@@ -18,6 +19,7 @@ Utility methods for working with Blockchain data
|
|
|
18
19
|
- [noLocktimeIdForTransaction](#nolocktimeidfortransaction)
|
|
19
20
|
- [nonWitnessHashToSign](#nonwitnesshashtosign)
|
|
20
21
|
- [numberAsCompactInt](#numberascompactint)
|
|
22
|
+
- [outputScriptForAddress](#outputscriptforaddress)
|
|
21
23
|
- [p2msScript](#p2msscript)
|
|
22
24
|
- [p2pkhOutputScript](#p2pkhoutputscript)
|
|
23
25
|
- [p2shOutputScript](#p2shoutputscript)
|
|
@@ -85,6 +87,22 @@ Get the components of a hex-encoded transaction
|
|
|
85
87
|
version: <Version Number>
|
|
86
88
|
}
|
|
87
89
|
|
|
90
|
+
### decodeBase58
|
|
91
|
+
|
|
92
|
+
Decode a base58check encoded string into its payload
|
|
93
|
+
|
|
94
|
+
{
|
|
95
|
+
encoded: <Base58Check Encoded String>
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@throws
|
|
99
|
+
<Error> when the string has invalid characters or a bad checksum
|
|
100
|
+
|
|
101
|
+
@returns
|
|
102
|
+
{
|
|
103
|
+
payload: <Decoded Payload Buffer Object>
|
|
104
|
+
}
|
|
105
|
+
|
|
88
106
|
### decodeBase58Address
|
|
89
107
|
|
|
90
108
|
Derive output hash and version data from a base58 address string
|
|
@@ -341,6 +359,39 @@ Convert a number to compact size integer serialization
|
|
|
341
359
|
encoded: <Serialized Compact Integer Buffer Object>
|
|
342
360
|
}
|
|
343
361
|
|
|
362
|
+
### outputScriptForAddress
|
|
363
|
+
|
|
364
|
+
Get the output script for an address
|
|
365
|
+
|
|
366
|
+
Supported addresses: P2PKH, P2SH, P2WPKH, P2WSH, P2TR, future segwit versions
|
|
367
|
+
|
|
368
|
+
Supported networks: btc, btcregtest, btcsignet, btctestnet
|
|
369
|
+
|
|
370
|
+
{
|
|
371
|
+
address: <Address String>
|
|
372
|
+
network: <Network Name String>
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
@throws
|
|
376
|
+
<Error>
|
|
377
|
+
|
|
378
|
+
@returns
|
|
379
|
+
{
|
|
380
|
+
script: <Output Script Hex String>
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
Example:
|
|
384
|
+
|
|
385
|
+
```node
|
|
386
|
+
const {outputScriptForAddress} = require('@alexbosworth/blockchain');
|
|
387
|
+
|
|
388
|
+
// Get the output script to pay to an address
|
|
389
|
+
const {script} = outputScriptForAddress({
|
|
390
|
+
address: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
|
|
391
|
+
network: 'btc',
|
|
392
|
+
});
|
|
393
|
+
```
|
|
394
|
+
|
|
344
395
|
### p2msScript
|
|
345
396
|
|
|
346
397
|
Get a Pay To Multisig Script and the script hash to pay to the script
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const {createHash} = require('crypto');
|
|
2
|
+
|
|
3
|
+
const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
4
|
+
const byteMask = 0xff;
|
|
5
|
+
const {ceil} = Math;
|
|
6
|
+
const countChecksumBytes = 4;
|
|
7
|
+
const leadingZeroCharCode = 0x31;
|
|
8
|
+
const notFoundIndex = -1;
|
|
9
|
+
const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
10
|
+
|
|
11
|
+
/** Decode a base58check encoded string into its payload
|
|
12
|
+
|
|
13
|
+
{
|
|
14
|
+
encoded: <Base58Check Encoded String>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@throws
|
|
18
|
+
<Error> when the string has invalid characters or a bad checksum
|
|
19
|
+
|
|
20
|
+
@returns
|
|
21
|
+
{
|
|
22
|
+
payload: <Decoded Payload Buffer Object>
|
|
23
|
+
}
|
|
24
|
+
*/
|
|
25
|
+
module.exports = ({encoded}) => {
|
|
26
|
+
if (typeof encoded !== 'string') {
|
|
27
|
+
throw new Error('ExpectedBase58CheckStringToDecode');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Ignore encoded string whitespace
|
|
31
|
+
const str = encoded.trim();
|
|
32
|
+
|
|
33
|
+
// Exit early with error on empty string, since there is nothing to decode
|
|
34
|
+
if (!str.length) {
|
|
35
|
+
throw new Error('ExpectedNonEmptyBase58CheckStringToDecode');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let bytesLength = 0;
|
|
39
|
+
let countLeadingZeros = 0;
|
|
40
|
+
let index = 0;
|
|
41
|
+
|
|
42
|
+
// Iterate to determine the number of leading zeros to preserve
|
|
43
|
+
while (index < str.length && str.charCodeAt(index) === leadingZeroCharCode) {
|
|
44
|
+
countLeadingZeros++;
|
|
45
|
+
index++;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Total bytes needed are log(58) / log(256) (256 values in a byte) = 0.732..
|
|
49
|
+
const workingSize = ceil((str.length - index) * 733 / 1000);
|
|
50
|
+
|
|
51
|
+
// Working buffer for the base256 (byte) representation, right-aligned
|
|
52
|
+
const working = new Uint8Array(workingSize);
|
|
53
|
+
|
|
54
|
+
// Iterate through the encoded characters and grab the decoded bytes
|
|
55
|
+
while (index < str.length) {
|
|
56
|
+
const value = base58Alphabet.indexOf(str[index]);
|
|
57
|
+
|
|
58
|
+
if (value === notFoundIndex) {
|
|
59
|
+
throw new Error('ExpectedAllBase58CharactersInBase58CheckString');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let carry = value;
|
|
63
|
+
let i = 0;
|
|
64
|
+
|
|
65
|
+
for (
|
|
66
|
+
let position = workingSize - 1;
|
|
67
|
+
(carry !== 0 || i < bytesLength) && position >= 0;
|
|
68
|
+
--position, ++i
|
|
69
|
+
) {
|
|
70
|
+
carry += base58Alphabet.length * working[position];
|
|
71
|
+
|
|
72
|
+
// Take the least-significant byte
|
|
73
|
+
working[position] = carry & byteMask;
|
|
74
|
+
|
|
75
|
+
// Carry forward the remaining bytes
|
|
76
|
+
carry = (carry / 256) | 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Track the number of bytes being used
|
|
80
|
+
bytesLength = i;
|
|
81
|
+
|
|
82
|
+
// Move to the next character in the encoded string
|
|
83
|
+
index++;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Meaningful bytes are right-aligned in working
|
|
87
|
+
const bytesStart = workingSize - bytesLength;
|
|
88
|
+
const meaningfulCount = workingSize - bytesStart;
|
|
89
|
+
|
|
90
|
+
// Final buffer: leading zeros plus meaningful bytes
|
|
91
|
+
const decoded = Buffer.alloc(countLeadingZeros + meaningfulCount);
|
|
92
|
+
|
|
93
|
+
// Make a view into the meaningful bytes
|
|
94
|
+
const dataBytes = working.subarray(bytesStart, bytesStart + meaningfulCount);
|
|
95
|
+
|
|
96
|
+
// Copy the meaningful bytes into the final decoded data
|
|
97
|
+
decoded.set(dataBytes, countLeadingZeros);
|
|
98
|
+
|
|
99
|
+
// Exit early with error when we don't have both a payload and checksum
|
|
100
|
+
if (decoded.length <= countChecksumBytes) {
|
|
101
|
+
throw new Error('ExpectedPayloadAndChecksumInBase58CheckString');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The last bytes of a base58check string are a double sha256 checksum
|
|
105
|
+
const checksum = decoded.subarray(decoded.length - countChecksumBytes);
|
|
106
|
+
|
|
107
|
+
// Everything before the checksum is the payload
|
|
108
|
+
const payload = decoded.subarray(0, decoded.length - countChecksumBytes);
|
|
109
|
+
|
|
110
|
+
// Calculate the checksum for the given payload
|
|
111
|
+
const payloadCheck = sha256(sha256(payload)).subarray(0, countChecksumBytes);
|
|
112
|
+
|
|
113
|
+
// Exit early with error when the double SHA256 of payload doesn't match
|
|
114
|
+
if (!checksum.equals(payloadCheck)) {
|
|
115
|
+
throw new Error('ExpectedValidPayloadChecksumInBase58CheckString');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {payload: Buffer.from(payload)};
|
|
119
|
+
};
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
const
|
|
1
|
+
const decodeBase58 = require('./decode_base58');
|
|
2
2
|
|
|
3
|
-
const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
4
|
-
const byteMask = 0xff;
|
|
5
|
-
const {ceil} = Math;
|
|
6
|
-
const countChecksumBytes = 4;
|
|
7
3
|
const countHashBytes = 20;
|
|
8
4
|
const countVersionBytes = 1;
|
|
9
|
-
const leadingZeroCharCode = 0x31;
|
|
10
|
-
const notFoundIndex = -1;
|
|
11
|
-
const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
12
5
|
|
|
13
6
|
/** Derive output hash and version data from a base58 address string
|
|
14
7
|
|
|
@@ -30,99 +23,14 @@ module.exports = ({address}) => {
|
|
|
30
23
|
throw new Error('ExpectedBase58AddressStringToDecode');
|
|
31
24
|
}
|
|
32
25
|
|
|
33
|
-
//
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
// Exit early with error on empty address, since it's not an address
|
|
37
|
-
if (!str.length) {
|
|
38
|
-
throw new Error('ExpectedNonEmptyBase58AddressToDecode');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let bytesLength = 0;
|
|
42
|
-
let countLeadingZeros = 0;
|
|
43
|
-
let index = 0;
|
|
44
|
-
|
|
45
|
-
// Iterate to determine the number of leading zeros to preserve
|
|
46
|
-
while (index < str.length && str.charCodeAt(index) === leadingZeroCharCode) {
|
|
47
|
-
countLeadingZeros++;
|
|
48
|
-
index++;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Total bytes needed are log(58) / log(256) (256 values in a byte) = 0.732..
|
|
52
|
-
const workingSize = ceil((str.length - index) * 733 / 1000);
|
|
53
|
-
|
|
54
|
-
// Working buffer for the base256 (byte) representation, right-aligned
|
|
55
|
-
const working = new Uint8Array(workingSize);
|
|
56
|
-
|
|
57
|
-
// Iterate through the address characters and grab the decoded bytes
|
|
58
|
-
while (index < str.length) {
|
|
59
|
-
const value = base58Alphabet.indexOf(str[index]);
|
|
60
|
-
|
|
61
|
-
if (value === notFoundIndex) {
|
|
62
|
-
throw new Error('ExpectedAllBase58CharactersInBase58Address');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let carry = value;
|
|
66
|
-
let i = 0;
|
|
67
|
-
|
|
68
|
-
for (
|
|
69
|
-
let position = workingSize - 1;
|
|
70
|
-
(carry !== 0 || i < bytesLength) && position >= 0;
|
|
71
|
-
--position, ++i
|
|
72
|
-
) {
|
|
73
|
-
carry += base58Alphabet.length * working[position];
|
|
74
|
-
|
|
75
|
-
// Take the least-significant byte
|
|
76
|
-
working[position] = carry & byteMask;
|
|
77
|
-
|
|
78
|
-
// Carry forward the remaining bytes
|
|
79
|
-
carry = (carry / 256) | 0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Track the number of bytes being used
|
|
83
|
-
bytesLength = i;
|
|
84
|
-
|
|
85
|
-
// Move to the next character in the address
|
|
86
|
-
index++;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Meaningful bytes are right-aligned in working
|
|
90
|
-
const bytesStart = workingSize - bytesLength;
|
|
91
|
-
const meaningfulCount = workingSize - bytesStart;
|
|
92
|
-
|
|
93
|
-
// Final buffer: leading zeros plus meaningful bytes
|
|
94
|
-
const decoded = Buffer.alloc(countLeadingZeros + meaningfulCount);
|
|
95
|
-
|
|
96
|
-
// Make a view into the meaningful bytes
|
|
97
|
-
const dataBytes = working.subarray(bytesStart, bytesStart + meaningfulCount);
|
|
98
|
-
|
|
99
|
-
// Copy the meaningful bytes into the final decoded data
|
|
100
|
-
decoded.set(dataBytes, countLeadingZeros);
|
|
101
|
-
|
|
102
|
-
// Exit early with error when we don't have both a payload and checksum
|
|
103
|
-
if (decoded.length <= countChecksumBytes) {
|
|
104
|
-
throw new Error('ExpectedPayloadAndChecksumInBase58Address');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// The last bytes of a base58 address are a double sha256 checksum
|
|
108
|
-
const checksum = decoded.subarray(decoded.length - countChecksumBytes);
|
|
109
|
-
|
|
110
|
-
// Aside from the checksum there is a version byte and then a hash
|
|
111
|
-
const payload = decoded.subarray(0, decoded.length - countChecksumBytes);
|
|
26
|
+
// Decode the base58check string and validate its checksum
|
|
27
|
+
const {payload} = decodeBase58({encoded: address});
|
|
112
28
|
|
|
113
29
|
// Make sure that we have a base58 payload that has the version and hash
|
|
114
30
|
if (payload.length !== countVersionBytes + countHashBytes) {
|
|
115
31
|
throw new Error('ExpectedVersionAnd20ByteHashInBase58AddressPayload');
|
|
116
32
|
}
|
|
117
33
|
|
|
118
|
-
// Calculate the checksum for the given payload
|
|
119
|
-
const payloadCheck = sha256(sha256(payload)).subarray(0, countChecksumBytes);
|
|
120
|
-
|
|
121
|
-
// Exit early with error when the double SHA256 of payload doesn't match
|
|
122
|
-
if (!checksum.equals(payloadCheck)) {
|
|
123
|
-
throw new Error('ExpectedValidPayloadChecksumInBase58Address');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
34
|
const [version] = payload;
|
|
127
35
|
|
|
128
36
|
return {version, hash: payload.subarray(countVersionBytes)};
|
package/addresses/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
const decodeBase58 = require('./decode_base58');
|
|
1
2
|
const decodeBase58Address = require('./decode_base58_address');
|
|
2
3
|
const decodeBech32Address = require('./decode_bech32_address');
|
|
3
4
|
const encodeBase58Address = require('./encode_base58_address');
|
|
4
5
|
const encodeBech32Address = require('./encode_bech32_address');
|
|
5
6
|
|
|
6
7
|
module.exports = {
|
|
8
|
+
decodeBase58,
|
|
7
9
|
decodeBase58Address,
|
|
8
10
|
decodeBech32Address,
|
|
9
11
|
encodeBase58Address,
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const {compactIntAsNumber} = require('./numbers');
|
|
2
2
|
const {componentsOfTransaction} = require('./transactions');
|
|
3
|
+
const {decodeBase58} = require('./addresses');
|
|
3
4
|
const {decodeBase58Address} = require('./addresses');
|
|
4
5
|
const {decodeBech32Address} = require('./addresses');
|
|
5
6
|
const {encodeBase58Address} = require('./addresses');
|
|
@@ -12,6 +13,7 @@ const {idForTransactionComponents} = require('./hashes');
|
|
|
12
13
|
const {noLocktimeIdForTransaction} = require('./transactions');
|
|
13
14
|
const {nonWitnessHashToSign} = require('./transactions');
|
|
14
15
|
const {numberAsCompactInt} = require('./numbers');
|
|
16
|
+
const {outputScriptForAddress} = require('./script');
|
|
15
17
|
const {p2msScript} = require('./script');
|
|
16
18
|
const {p2pkhOutputScript} = require('./script');
|
|
17
19
|
const {p2shOutputScript} = require('./script');
|
|
@@ -30,6 +32,7 @@ const {v0HashToSign} = require('./transactions');
|
|
|
30
32
|
module.exports = {
|
|
31
33
|
compactIntAsNumber,
|
|
32
34
|
componentsOfTransaction,
|
|
35
|
+
decodeBase58,
|
|
33
36
|
decodeBase58Address,
|
|
34
37
|
decodeBech32Address,
|
|
35
38
|
encodeBase58Address,
|
|
@@ -42,6 +45,7 @@ module.exports = {
|
|
|
42
45
|
noLocktimeIdForTransaction,
|
|
43
46
|
nonWitnessHashToSign,
|
|
44
47
|
numberAsCompactInt,
|
|
48
|
+
outputScriptForAddress,
|
|
45
49
|
p2msScript,
|
|
46
50
|
p2pkhOutputScript,
|
|
47
51
|
p2shOutputScript,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": {
|
|
3
3
|
"name": "Alex Bosworth",
|
|
4
|
-
"url": "https://
|
|
4
|
+
"url": "https://x.com/alexbosworth"
|
|
5
5
|
},
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/alexbosworth/blockchain/issues"
|
|
@@ -10,6 +10,16 @@
|
|
|
10
10
|
"engines": {
|
|
11
11
|
"node": ">=22"
|
|
12
12
|
},
|
|
13
|
+
"files": [
|
|
14
|
+
"5bit",
|
|
15
|
+
"addresses",
|
|
16
|
+
"CHANGELOG.md",
|
|
17
|
+
"hashes",
|
|
18
|
+
"index.js",
|
|
19
|
+
"numbers",
|
|
20
|
+
"script",
|
|
21
|
+
"transactions"
|
|
22
|
+
],
|
|
13
23
|
"license": "MIT",
|
|
14
24
|
"name": "@alexbosworth/blockchain",
|
|
15
25
|
"repository": {
|
|
@@ -19,5 +29,5 @@
|
|
|
19
29
|
"scripts": {
|
|
20
30
|
"test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
|
|
21
31
|
},
|
|
22
|
-
"version": "4.
|
|
32
|
+
"version": "4.9.0"
|
|
23
33
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"btc": {
|
|
3
|
+
"bech32": "bc",
|
|
4
|
+
"p2pkh": 0,
|
|
5
|
+
"p2sh": 5
|
|
6
|
+
},
|
|
7
|
+
"btcregtest": {
|
|
8
|
+
"bech32": "bcrt",
|
|
9
|
+
"p2pkh": 111,
|
|
10
|
+
"p2sh": 196
|
|
11
|
+
},
|
|
12
|
+
"btcsignet": {
|
|
13
|
+
"bech32": "tb",
|
|
14
|
+
"p2pkh": 111,
|
|
15
|
+
"p2sh": 196
|
|
16
|
+
},
|
|
17
|
+
"btctestnet": {
|
|
18
|
+
"bech32": "tb",
|
|
19
|
+
"p2pkh": 111,
|
|
20
|
+
"p2sh": 196
|
|
21
|
+
}
|
|
22
|
+
}
|
package/script/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const outputScriptForAddress = require('./output_script_for_address');
|
|
1
2
|
const p2msScript = require('./p2ms_script');
|
|
2
3
|
const p2pkhOutputScript = require('./p2pkh_output_script');
|
|
3
4
|
const p2shOutputScript = require('./p2sh_output_script');
|
|
@@ -8,6 +9,7 @@ const scriptAsScriptElements = require('./script_as_script_elements');
|
|
|
8
9
|
const scriptElementsAsScript = require('./script_elements_as_script');
|
|
9
10
|
|
|
10
11
|
module.exports = {
|
|
12
|
+
outputScriptForAddress,
|
|
11
13
|
p2msScript,
|
|
12
14
|
p2pkhOutputScript,
|
|
13
15
|
p2shOutputScript,
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const {decodeBase58Address} = require('./../addresses');
|
|
2
|
+
const {decodeBech32Address} = require('./../addresses');
|
|
3
|
+
const networks = require('./conf/networks.json');
|
|
4
|
+
const p2pkhOutputScript = require('./p2pkh_output_script');
|
|
5
|
+
const p2shOutputScript = require('./p2sh_output_script');
|
|
6
|
+
const p2wpkhOutputScript = require('./p2wpkh_output_script');
|
|
7
|
+
const p2wshOutputScript = require('./p2wsh_output_script');
|
|
8
|
+
const scriptElementsAsOutput = require('./script_elements_as_output');
|
|
9
|
+
|
|
10
|
+
const bech32Separator = '1';
|
|
11
|
+
const bufferAsHex = buffer => buffer.toString('hex');
|
|
12
|
+
const {keys} = Object;
|
|
13
|
+
const p2wpkhHashLength = 20;
|
|
14
|
+
const {values} = Object;
|
|
15
|
+
const versionAsOpCode = version => 0x50 + version;
|
|
16
|
+
const witnessVersionInitial = 0;
|
|
17
|
+
|
|
18
|
+
/** Get the output script for an address
|
|
19
|
+
|
|
20
|
+
Supported addresses: P2PKH, P2SH, P2WPKH, P2WSH, P2TR, future segwit versions
|
|
21
|
+
|
|
22
|
+
Supported networks: btc, btcregtest, btcsignet, btctestnet
|
|
23
|
+
|
|
24
|
+
{
|
|
25
|
+
address: <Address String>
|
|
26
|
+
network: <Network Name String>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@throws
|
|
30
|
+
<Error>
|
|
31
|
+
|
|
32
|
+
@returns
|
|
33
|
+
{
|
|
34
|
+
script: <Output Script Hex String>
|
|
35
|
+
}
|
|
36
|
+
*/
|
|
37
|
+
module.exports = ({address, network}) => {
|
|
38
|
+
if (typeof address !== 'string') {
|
|
39
|
+
throw new Error('ExpectedAddressStringToDeriveOutputScript');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!keys(networks).includes(network)) {
|
|
43
|
+
throw new Error('ExpectedKnownNetworkNameToDeriveOutputScript');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const {bech32, p2pkh, p2sh} = networks[network];
|
|
47
|
+
|
|
48
|
+
// Bech32 addresses start with a human readable prefix and then a separator
|
|
49
|
+
const [prefix] = address.toLowerCase().split(bech32Separator);
|
|
50
|
+
|
|
51
|
+
// Collect the bech32 prefixes that are in use across the known networks
|
|
52
|
+
const prefixes = values(networks).map(n => n.bech32);
|
|
53
|
+
|
|
54
|
+
// Exit early when the address is base58 encoded, it is a P2PKH or a P2SH
|
|
55
|
+
if (!prefixes.includes(prefix)) {
|
|
56
|
+
const {hash, version} = decodeBase58Address({address});
|
|
57
|
+
|
|
58
|
+
switch (version) {
|
|
59
|
+
case p2pkh:
|
|
60
|
+
return {script: bufferAsHex(p2pkhOutputScript({hash}).script)};
|
|
61
|
+
|
|
62
|
+
case p2sh:
|
|
63
|
+
return {script: bufferAsHex(p2shOutputScript({hash}).script)};
|
|
64
|
+
|
|
65
|
+
default:
|
|
66
|
+
throw new Error('UnexpectedBase58AddressVersionForNetwork');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const decoded = decodeBech32Address({address});
|
|
71
|
+
|
|
72
|
+
if (decoded.prefix !== bech32) {
|
|
73
|
+
throw new Error('UnexpectedBech32AddressPrefixForNetwork');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const {program, version} = decoded;
|
|
77
|
+
|
|
78
|
+
const isInitialVersion = version === witnessVersionInitial;
|
|
79
|
+
|
|
80
|
+
// Initial segwit version programs that are 20 bytes are public key hashes
|
|
81
|
+
if (isInitialVersion && program.length === p2wpkhHashLength) {
|
|
82
|
+
return {script: bufferAsHex(p2wpkhOutputScript({hash: program}).script)};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Initial segwit version programs that are 32 bytes are script hashes
|
|
86
|
+
if (isInitialVersion) {
|
|
87
|
+
return {script: bufferAsHex(p2wshOutputScript({hash: program}).script)};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Later witness versions are their OP_n and then the program as a push
|
|
91
|
+
const {output} = scriptElementsAsOutput({
|
|
92
|
+
elements: [versionAsOpCode(version), program],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return {script: bufferAsHex(output)};
|
|
96
|
+
};
|
package/.gitattributes
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
package-lock.json binary
|
package/.tm_properties
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
exclude = '{$exclude,node_modules,*~}'
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const {deepEqual} = require('node:assert').strict;
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
const {throws} = require('node:assert').strict;
|
|
4
|
-
|
|
5
|
-
const bytesAsWords = require('./../../5bit/bytes_as_words');
|
|
6
|
-
|
|
7
|
-
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
-
|
|
9
|
-
const tests = [
|
|
10
|
-
{
|
|
11
|
-
args: {},
|
|
12
|
-
description: 'Data bytes are expected',
|
|
13
|
-
error: 'ExpectedDataBytesToConvertToDataWords',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
args: {bytes: hexAsBuffer('')},
|
|
17
|
-
description: 'No bytes are mapped to no words',
|
|
18
|
-
expected: {words: []},
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
args: {bytes: hexAsBuffer('ff')},
|
|
22
|
-
description: 'A single byte is mapped to words with zero padding',
|
|
23
|
-
expected: {words: [31, 28]},
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
args: {bytes: hexAsBuffer('751e76e8199196d454941c45d1b3a323f1433bd6')},
|
|
27
|
-
description: 'Bytes are mapped to words',
|
|
28
|
-
expected: {
|
|
29
|
-
words: [
|
|
30
|
-
14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2,
|
|
31
|
-
29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22,
|
|
32
|
-
],
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
];
|
|
36
|
-
|
|
37
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
38
|
-
return test(description, (t, end) => {
|
|
39
|
-
if (!!error) {
|
|
40
|
-
throws(() => bytesAsWords(args), new Error(error), 'Err');
|
|
41
|
-
} else {
|
|
42
|
-
const res = bytesAsWords(args);
|
|
43
|
-
|
|
44
|
-
deepEqual(res, expected, 'Got expected result');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return end();
|
|
48
|
-
});
|
|
49
|
-
});
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
const {deepEqual} = require('node:assert').strict;
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
const {throws} = require('node:assert').strict;
|
|
4
|
-
|
|
5
|
-
const wordsAsBytes = require('./../../5bit/words_as_bytes');
|
|
6
|
-
|
|
7
|
-
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
-
|
|
9
|
-
const tests = [
|
|
10
|
-
{
|
|
11
|
-
args: {},
|
|
12
|
-
description: 'An array of data words is expected',
|
|
13
|
-
error: 'ExpectedArrayOfDataWordsToConvertToDataBytes',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
args: {words: [1.5]},
|
|
17
|
-
description: 'Integer data words are expected',
|
|
18
|
-
error: 'ExpectedIntegerDataWordsToConvertToDataBytes',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
args: {words: [NaN]},
|
|
22
|
-
description: 'Numeric data words are expected',
|
|
23
|
-
error: 'ExpectedIntegerDataWordsToConvertToDataBytes',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
args: {words: [32]},
|
|
27
|
-
description: 'Data words within the word value range are expected',
|
|
28
|
-
error: 'Expected5BitDataWordValuesToConvertToDataBytes',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
args: {words: [-1]},
|
|
32
|
-
description: 'Unsigned data words are expected',
|
|
33
|
-
error: 'Expected5BitDataWordValuesToConvertToDataBytes',
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
args: {words: [31, 28, 0]},
|
|
37
|
-
description: 'A full extra word of padding is not expected',
|
|
38
|
-
error: 'ExpectedNoPaddingWordsToConvertToDataBytes',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
args: {words: [31, 31]},
|
|
42
|
-
description: 'Non-zero padding bits are not expected',
|
|
43
|
-
error: 'UnexpectedNonZeroPadding',
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
args: {words: []},
|
|
47
|
-
description: 'No words are mapped to no bytes',
|
|
48
|
-
expected: {bytes: hexAsBuffer('')},
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
args: {words: [31, 28]},
|
|
52
|
-
description: 'Words are mapped to a byte, removing zero padding',
|
|
53
|
-
expected: {bytes: hexAsBuffer('ff')},
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
args: {
|
|
57
|
-
words: [
|
|
58
|
-
14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2,
|
|
59
|
-
29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22,
|
|
60
|
-
],
|
|
61
|
-
},
|
|
62
|
-
description: 'Words are mapped to bytes',
|
|
63
|
-
expected: {bytes: hexAsBuffer('751e76e8199196d454941c45d1b3a323f1433bd6')},
|
|
64
|
-
},
|
|
65
|
-
];
|
|
66
|
-
|
|
67
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
68
|
-
return test(description, (t, end) => {
|
|
69
|
-
if (!!error) {
|
|
70
|
-
throws(() => wordsAsBytes(args), new Error(error), 'Err');
|
|
71
|
-
} else {
|
|
72
|
-
const res = wordsAsBytes(args);
|
|
73
|
-
|
|
74
|
-
deepEqual(res, expected, 'Got expected result');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return end();
|
|
78
|
-
});
|
|
79
|
-
});
|