@alexbosworth/blockchain 4.8.0 → 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 +4 -0
- package/README.md +34 -0
- package/index.js +2 -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.js +0 -109
- package/test/addresses/test_decode_base58_address.js +0 -94
- 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
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ Utility methods for working with Blockchain data
|
|
|
19
19
|
- [noLocktimeIdForTransaction](#nolocktimeidfortransaction)
|
|
20
20
|
- [nonWitnessHashToSign](#nonwitnesshashtosign)
|
|
21
21
|
- [numberAsCompactInt](#numberascompactint)
|
|
22
|
+
- [outputScriptForAddress](#outputscriptforaddress)
|
|
22
23
|
- [p2msScript](#p2msscript)
|
|
23
24
|
- [p2pkhOutputScript](#p2pkhoutputscript)
|
|
24
25
|
- [p2shOutputScript](#p2shoutputscript)
|
|
@@ -358,6 +359,39 @@ Convert a number to compact size integer serialization
|
|
|
358
359
|
encoded: <Serialized Compact Integer Buffer Object>
|
|
359
360
|
}
|
|
360
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
|
+
|
|
361
395
|
### p2msScript
|
|
362
396
|
|
|
363
397
|
Get a Pay To Multisig Script and the script hash to pay to the script
|
package/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const {idForTransactionComponents} = require('./hashes');
|
|
|
13
13
|
const {noLocktimeIdForTransaction} = require('./transactions');
|
|
14
14
|
const {nonWitnessHashToSign} = require('./transactions');
|
|
15
15
|
const {numberAsCompactInt} = require('./numbers');
|
|
16
|
+
const {outputScriptForAddress} = require('./script');
|
|
16
17
|
const {p2msScript} = require('./script');
|
|
17
18
|
const {p2pkhOutputScript} = require('./script');
|
|
18
19
|
const {p2shOutputScript} = require('./script');
|
|
@@ -44,6 +45,7 @@ module.exports = {
|
|
|
44
45
|
noLocktimeIdForTransaction,
|
|
45
46
|
nonWitnessHashToSign,
|
|
46
47
|
numberAsCompactInt,
|
|
48
|
+
outputScriptForAddress,
|
|
47
49
|
p2msScript,
|
|
48
50
|
p2pkhOutputScript,
|
|
49
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
|
-
});
|
|
@@ -1,29 +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 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
|
-
});
|
|
@@ -1,109 +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 {decodeBase58} = require('./../../');
|
|
6
|
-
|
|
7
|
-
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
-
|
|
9
|
-
const tests = [
|
|
10
|
-
{
|
|
11
|
-
args: {},
|
|
12
|
-
description: 'An encoded string is expected',
|
|
13
|
-
error: 'ExpectedBase58CheckStringToDecode',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
args: {encoded: Buffer.alloc(1)},
|
|
17
|
-
description: 'A string type is expected',
|
|
18
|
-
error: 'ExpectedBase58CheckStringToDecode',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
args: {encoded: ' '},
|
|
22
|
-
description: 'A non-empty encoded string is expected',
|
|
23
|
-
error: 'ExpectedNonEmptyBase58CheckStringToDecode',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
args: {encoded: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6u!'},
|
|
27
|
-
description: 'Characters must be base58 set',
|
|
28
|
-
error: 'ExpectedAllBase58CharactersInBase58CheckString',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
args: {encoded: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uO'},
|
|
32
|
-
description: 'Ambiguous characters excluded from base58 are rejected',
|
|
33
|
-
error: 'ExpectedAllBase58CharactersInBase58CheckString',
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
args: {encoded: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6ul'},
|
|
37
|
-
description: 'Lowercase L is not a base58 character',
|
|
38
|
-
error: 'ExpectedAllBase58CharactersInBase58CheckString',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
args: {encoded: '16ro3Jptwo4asSev ZnsRX6vfRS24TGE6uK'},
|
|
42
|
-
description: 'Interior whitespace is not allowed',
|
|
43
|
-
error: 'ExpectedAllBase58CharactersInBase58CheckString',
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
args: {encoded: '1'},
|
|
47
|
-
description: 'Payload and checksum must be present',
|
|
48
|
-
error: 'ExpectedPayloadAndChecksumInBase58CheckString',
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
args: {encoded: '1111'},
|
|
52
|
-
description: 'A checksum alone with no payload is rejected',
|
|
53
|
-
error: 'ExpectedPayloadAndChecksumInBase58CheckString',
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
args: {encoded: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uP'},
|
|
57
|
-
description: 'Checksum must match',
|
|
58
|
-
error: 'ExpectedValidPayloadChecksumInBase58CheckString',
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
args: {encoded: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uK'},
|
|
62
|
-
description: 'A p2pkh address payload is decoded',
|
|
63
|
-
expected: {
|
|
64
|
-
payload: hexAsBuffer('00404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
args: {encoded: ' 3P14159f73E4gFr7JterCCQh9QjiTjiZrG '},
|
|
69
|
-
description: 'Whitespace is ignored and a p2sh address payload is decoded',
|
|
70
|
-
expected: {
|
|
71
|
-
payload: hexAsBuffer('05e9c3dd0c07aac76179ebc76a6c78d4d67c6c160a'),
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
args: {encoded: '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'},
|
|
76
|
-
description: 'A WIF private key payload is decoded',
|
|
77
|
-
expected: {
|
|
78
|
-
payload: hexAsBuffer('800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d'),
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
args: {
|
|
83
|
-
encoded: 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8',
|
|
84
|
-
},
|
|
85
|
-
description: 'An extended public key payload is decoded',
|
|
86
|
-
expected: {
|
|
87
|
-
payload: hexAsBuffer('0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d5080339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2'),
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
args: {encoded: '11146EAsf'},
|
|
92
|
-
description: 'An all zero bytes payload is preserved',
|
|
93
|
-
expected: {payload: hexAsBuffer('000000')},
|
|
94
|
-
},
|
|
95
|
-
];
|
|
96
|
-
|
|
97
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
98
|
-
return test(description, (t, end) => {
|
|
99
|
-
if (!!error) {
|
|
100
|
-
throws(() => decodeBase58(args), new Error(error), 'Err');
|
|
101
|
-
} else {
|
|
102
|
-
const res = decodeBase58(args);
|
|
103
|
-
|
|
104
|
-
deepEqual(res, expected, 'Got expected result');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return end();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
@@ -1,94 +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 {decodeBase58Address} = require('./../../');
|
|
6
|
-
|
|
7
|
-
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
-
|
|
9
|
-
const tests = [
|
|
10
|
-
{
|
|
11
|
-
args: {},
|
|
12
|
-
description: 'An address is expected',
|
|
13
|
-
error: 'ExpectedBase58AddressStringToDecode',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
args: {address: ' '},
|
|
17
|
-
description: 'A non-empty address is expected',
|
|
18
|
-
error: 'ExpectedNonEmptyBase58CheckStringToDecode',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
args: {address: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uP'},
|
|
22
|
-
description: 'Address checksum must match',
|
|
23
|
-
error: 'ExpectedValidPayloadChecksumInBase58CheckString',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
args: {address: '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'},
|
|
27
|
-
description: 'Address payload must be a version and a 20 byte hash',
|
|
28
|
-
error: 'ExpectedVersionAnd20ByteHashInBase58AddressPayload',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
args: {address: '1'},
|
|
32
|
-
description: 'Address payload and checksum must be present',
|
|
33
|
-
error: 'ExpectedPayloadAndChecksumInBase58CheckString',
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
args: {address: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6u!'},
|
|
37
|
-
description: 'Characters must be base58 set',
|
|
38
|
-
error: 'ExpectedAllBase58CharactersInBase58CheckString',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
args: {address: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uK'},
|
|
42
|
-
description: 'A hash and version are returned for a p2pkh address',
|
|
43
|
-
expected: {
|
|
44
|
-
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
45
|
-
version: 0,
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
args: {address: ' 16ro3Jptwo4asSevZnsRX6vfRS24TGE6uK\n'},
|
|
50
|
-
description: 'Surrounding whitespace is ignored',
|
|
51
|
-
expected: {
|
|
52
|
-
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
53
|
-
version: 0,
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
args: {address: 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn'},
|
|
58
|
-
description: 'A hash and version are returned for a testnet p2pkh address',
|
|
59
|
-
expected: {
|
|
60
|
-
hash: hexAsBuffer('243f1394f44554f4ce3fd68649c19adc483ce924'),
|
|
61
|
-
version: 111,
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
args: {address: '2N3oefVeg6stiTb5Kh3ozCSkaqmx91FDbsm'},
|
|
66
|
-
description: 'A hash and version are returned for a testnet p2sh address',
|
|
67
|
-
expected: {
|
|
68
|
-
hash: hexAsBuffer('73d32ac9e4330a071ee1b3a9ccf3997bdd4174d0'),
|
|
69
|
-
version: 196,
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
args: {address: '3P14159f73E4gFr7JterCCQh9QjiTjiZrG'},
|
|
74
|
-
description: 'A hash and version are returned for a p2sh address',
|
|
75
|
-
expected: {
|
|
76
|
-
hash: hexAsBuffer('e9c3dd0c07aac76179ebc76a6c78d4d67c6c160a'),
|
|
77
|
-
version: 5,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
];
|
|
81
|
-
|
|
82
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
83
|
-
return test(description, (t, end) => {
|
|
84
|
-
if (!!error) {
|
|
85
|
-
throws(() => decodeBase58Address(args), new Error(error), 'Err');
|
|
86
|
-
} else {
|
|
87
|
-
const res = decodeBase58Address(args);
|
|
88
|
-
|
|
89
|
-
deepEqual(res, expected, 'Got expected result');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return end();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
@@ -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 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
|
-
});
|