@alexbosworth/blockchain 4.4.0 → 4.5.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/5bit/words_as_bytes.js +2 -2
- package/CHANGELOG.md +4 -0
- package/README.md +30 -0
- package/addresses/encode_base58_address.js +108 -0
- package/addresses/encode_bech32.js +2 -2
- package/addresses/index.js +2 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/script/p2ms_script.js +1 -1
- package/test/5bit/test_words_as_bytes.js +5 -0
- package/test/addresses/test_encode_base58_address.js +127 -0
- package/test/addresses/test_encode_bech32.js +5 -0
- package/test/addresses/test_encode_bech32_address.js +22 -0
- package/test/script/test_p2ms_script.js +5 -0
- package/test/transactions/test_transaction_from_components.js +55 -0
- package/transactions/transaction_from_components.js +2 -2
package/5bit/words_as_bytes.js
CHANGED
|
@@ -27,11 +27,11 @@ module.exports = ({words}) => {
|
|
|
27
27
|
throw new Error('ExpectedArrayOfDataWordsToConvertToDataBytes');
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
if (
|
|
30
|
+
if (words.some(n => !isInteger(n))) {
|
|
31
31
|
throw new Error('ExpectedIntegerDataWordsToConvertToDataBytes');
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
if (
|
|
34
|
+
if (words.some(n => n < minimumWordValue || n > maximumWordValue)) {
|
|
35
35
|
throw new Error('Expected5BitDataWordValuesToConvertToDataBytes');
|
|
36
36
|
}
|
|
37
37
|
|
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ Utility methods for working with Blockchain data
|
|
|
8
8
|
- [componentsOfTransaction](#componentsoftransaction)
|
|
9
9
|
- [decodeBase58Address](#decodebase58address)
|
|
10
10
|
- [decodeBech32Address](#decodebech32address)
|
|
11
|
+
- [encodeBase58Address](#encodebase58address)
|
|
11
12
|
- [encodeBech32Address](#encodebech32address)
|
|
12
13
|
- [hashForP2pkh](#hashforp2pkh)
|
|
13
14
|
- [hashForP2wpkh](#hashforp2wpkh)
|
|
@@ -117,6 +118,35 @@ Decode a bech32 address string to derive the address details
|
|
|
117
118
|
version: <Witness Version Number>
|
|
118
119
|
}
|
|
119
120
|
|
|
121
|
+
### encodeBase58Address
|
|
122
|
+
|
|
123
|
+
Encode output hash and version data as a base58 address string
|
|
124
|
+
|
|
125
|
+
{
|
|
126
|
+
hash: <Output Hash Buffer Object>
|
|
127
|
+
version: <Script Version Byte Number>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@throws
|
|
131
|
+
<Error>
|
|
132
|
+
|
|
133
|
+
@returns
|
|
134
|
+
{
|
|
135
|
+
address: <Base58 Encoded Address String>
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
Example:
|
|
139
|
+
|
|
140
|
+
```node
|
|
141
|
+
const {encodeBase58Address, hashForP2pkh} = require('@alexbosworth/blockchain');
|
|
142
|
+
|
|
143
|
+
// The P2PKH output hash is the hash160 of the compressed public key
|
|
144
|
+
const {hash} = hashForP2pkh({key: publicKey});
|
|
145
|
+
|
|
146
|
+
// Encode the P2PKH address paying to the public key hash
|
|
147
|
+
const {address} = encodeBase58Address({hash, version: 0});
|
|
148
|
+
```
|
|
149
|
+
|
|
120
150
|
### encodeBech32Address
|
|
121
151
|
|
|
122
152
|
Encode address details as a bech32 address string
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const {createHash} = require('crypto');
|
|
2
|
+
|
|
3
|
+
const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
4
|
+
const byteBase = 256;
|
|
5
|
+
const {ceil} = Math;
|
|
6
|
+
const countChecksumBytes = 4;
|
|
7
|
+
const countHashBytes = 20;
|
|
8
|
+
const {isBuffer} = Buffer;
|
|
9
|
+
const {isInteger} = Number;
|
|
10
|
+
const leadingZeroChar = '1';
|
|
11
|
+
const maximumVersion = 0xff;
|
|
12
|
+
const minimumVersion = 0;
|
|
13
|
+
const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
14
|
+
|
|
15
|
+
/** Encode output hash and version data as a base58 address string
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
hash: <Output Hash Buffer Object>
|
|
19
|
+
version: <Script Version Byte Number>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@throws
|
|
23
|
+
<Error>
|
|
24
|
+
|
|
25
|
+
@returns
|
|
26
|
+
{
|
|
27
|
+
address: <Base58 Encoded Address String>
|
|
28
|
+
}
|
|
29
|
+
*/
|
|
30
|
+
module.exports = ({hash, version}) => {
|
|
31
|
+
if (!isBuffer(hash)) {
|
|
32
|
+
throw new Error('ExpectedOutputHashToEncodeBase58Address');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (hash.length !== countHashBytes) {
|
|
36
|
+
throw new Error('Expected20ByteHashToEncodeBase58Address');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!isInteger(version) || version < minimumVersion) {
|
|
40
|
+
throw new Error('ExpectedScriptVersionNumberToEncodeBase58Address');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (version > maximumVersion) {
|
|
44
|
+
throw new Error('UnexpectedHighScriptVersionToEncodeBase58Address');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The payload of a base58 address is a version byte and then the hash
|
|
48
|
+
const payload = Buffer.concat([Buffer.from([version]), hash]);
|
|
49
|
+
|
|
50
|
+
// The last bytes of a base58 address are a double sha256 checksum
|
|
51
|
+
const checksum = sha256(sha256(payload)).subarray(0, countChecksumBytes);
|
|
52
|
+
|
|
53
|
+
// Data to encode is the payload followed by its checksum
|
|
54
|
+
const data = Buffer.concat([payload, checksum]);
|
|
55
|
+
|
|
56
|
+
let countLeadingZeros = 0;
|
|
57
|
+
|
|
58
|
+
// Iterate to determine the number of leading zeros to preserve
|
|
59
|
+
while (countLeadingZeros < data.length && !data[countLeadingZeros]) {
|
|
60
|
+
countLeadingZeros++;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Total chars needed are log(256) / log(58) per byte = 1.365..
|
|
64
|
+
const workingSize = ceil((data.length - countLeadingZeros) * 1366 / 1000);
|
|
65
|
+
|
|
66
|
+
// Working buffer for the base58 digit representation, right-aligned
|
|
67
|
+
const working = new Uint8Array(workingSize);
|
|
68
|
+
|
|
69
|
+
let charsLength = 0;
|
|
70
|
+
let index = countLeadingZeros;
|
|
71
|
+
|
|
72
|
+
// Iterate through the data bytes and grab the base58 digits
|
|
73
|
+
while (index < data.length) {
|
|
74
|
+
let carry = data[index];
|
|
75
|
+
let i = 0;
|
|
76
|
+
|
|
77
|
+
for (
|
|
78
|
+
let position = workingSize - 1;
|
|
79
|
+
(carry !== 0 || i < charsLength) && position >= 0;
|
|
80
|
+
--position, ++i
|
|
81
|
+
) {
|
|
82
|
+
carry += byteBase * working[position];
|
|
83
|
+
|
|
84
|
+
// Take the least-significant base58 digit
|
|
85
|
+
working[position] = carry % base58Alphabet.length;
|
|
86
|
+
|
|
87
|
+
// Carry forward the remaining digits
|
|
88
|
+
carry = (carry / base58Alphabet.length) | 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Track the number of digits being used
|
|
92
|
+
charsLength = i;
|
|
93
|
+
|
|
94
|
+
// Move to the next byte in the data
|
|
95
|
+
index++;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Meaningful digits are right-aligned in working
|
|
99
|
+
const digits = working.subarray(workingSize - charsLength);
|
|
100
|
+
|
|
101
|
+
// Leading zero bytes are preserved as leading zero characters
|
|
102
|
+
const leadingZeros = leadingZeroChar.repeat(countLeadingZeros);
|
|
103
|
+
|
|
104
|
+
// Map the base58 digits to their alphabet characters
|
|
105
|
+
const encoded = [...digits].map(digit => base58Alphabet[digit]).join('');
|
|
106
|
+
|
|
107
|
+
return {address: `${leadingZeros}${encoded}`};
|
|
108
|
+
};
|
|
@@ -48,11 +48,11 @@ module.exports = ({prefix, type, words}) => {
|
|
|
48
48
|
throw new Error('ExpectedArrayOfDataWordsToEncodeBech32String');
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
if (
|
|
51
|
+
if (words.some(n => !isInteger(n))) {
|
|
52
52
|
throw new Error('ExpectedIntegerDataWordsToEncodeBech32String');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
if (
|
|
55
|
+
if (words.some(n => n < minimumWordValue || n > maximumWordValue)) {
|
|
56
56
|
throw new Error('Expected5BitDataWordValuesToEncodeBech32String');
|
|
57
57
|
}
|
|
58
58
|
|
package/addresses/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
const decodeBase58Address = require('./decode_base58_address');
|
|
2
2
|
const decodeBech32Address = require('./decode_bech32_address');
|
|
3
|
+
const encodeBase58Address = require('./encode_base58_address');
|
|
3
4
|
const encodeBech32Address = require('./encode_bech32_address');
|
|
4
5
|
|
|
5
6
|
module.exports = {
|
|
6
7
|
decodeBase58Address,
|
|
7
8
|
decodeBech32Address,
|
|
9
|
+
encodeBase58Address,
|
|
8
10
|
encodeBech32Address,
|
|
9
11
|
};
|
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const {compactIntAsNumber} = require('./numbers');
|
|
|
2
2
|
const {componentsOfTransaction} = require('./transactions');
|
|
3
3
|
const {decodeBase58Address} = require('./addresses');
|
|
4
4
|
const {decodeBech32Address} = require('./addresses');
|
|
5
|
+
const {encodeBase58Address} = require('./addresses');
|
|
5
6
|
const {encodeBech32Address} = require('./addresses');
|
|
6
7
|
const {hashForP2pkh} = require('./hashes');
|
|
7
8
|
const {hashForP2wpkh} = require('./hashes');
|
|
@@ -29,6 +30,7 @@ module.exports = {
|
|
|
29
30
|
componentsOfTransaction,
|
|
30
31
|
decodeBase58Address,
|
|
31
32
|
decodeBech32Address,
|
|
33
|
+
encodeBase58Address,
|
|
32
34
|
encodeBech32Address,
|
|
33
35
|
hashForP2pkh,
|
|
34
36
|
hashForP2wpkh,
|
package/package.json
CHANGED
package/script/p2ms_script.js
CHANGED
|
@@ -38,7 +38,7 @@ module.exports = ({keys, required}) => {
|
|
|
38
38
|
throw new Error('ExpectedFewerPublicKeysToDeriveP2msScript');
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
if (
|
|
41
|
+
if (keys.some(key => !isBuffer(key) || key.length !== publicKeyLength)) {
|
|
42
42
|
throw new Error('ExpectedCompressedPublicKeysToDeriveP2msScript');
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -17,6 +17,11 @@ const tests = [
|
|
|
17
17
|
description: 'Integer data words are expected',
|
|
18
18
|
error: 'ExpectedIntegerDataWordsToConvertToDataBytes',
|
|
19
19
|
},
|
|
20
|
+
{
|
|
21
|
+
args: {words: [NaN]},
|
|
22
|
+
description: 'Numeric data words are expected',
|
|
23
|
+
error: 'ExpectedIntegerDataWordsToConvertToDataBytes',
|
|
24
|
+
},
|
|
20
25
|
{
|
|
21
26
|
args: {words: [32]},
|
|
22
27
|
description: 'Data words within the word value range are expected',
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const {deepEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const {throws} = require('node:assert').strict;
|
|
4
|
+
|
|
5
|
+
const {encodeBase58Address} = require('./../../');
|
|
6
|
+
|
|
7
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
8
|
+
|
|
9
|
+
const tests = [
|
|
10
|
+
{
|
|
11
|
+
args: {},
|
|
12
|
+
description: 'A hash is expected',
|
|
13
|
+
error: 'ExpectedOutputHashToEncodeBase58Address',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
args: {hash: hexAsBuffer('00')},
|
|
17
|
+
description: 'A 20 byte hash is expected',
|
|
18
|
+
error: 'Expected20ByteHashToEncodeBase58Address',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
args: {hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d')},
|
|
22
|
+
description: 'A version number is expected',
|
|
23
|
+
error: 'ExpectedScriptVersionNumberToEncodeBase58Address',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
args: {
|
|
27
|
+
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
28
|
+
version: -1,
|
|
29
|
+
},
|
|
30
|
+
description: 'A non-negative version number is expected',
|
|
31
|
+
error: 'ExpectedScriptVersionNumberToEncodeBase58Address',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
args: {
|
|
35
|
+
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
36
|
+
version: 1.5,
|
|
37
|
+
},
|
|
38
|
+
description: 'An integer version number is expected',
|
|
39
|
+
error: 'ExpectedScriptVersionNumberToEncodeBase58Address',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
args: {
|
|
43
|
+
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
44
|
+
version: 256,
|
|
45
|
+
},
|
|
46
|
+
description: 'A single byte version number is expected',
|
|
47
|
+
error: 'UnexpectedHighScriptVersionToEncodeBase58Address',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
args: {
|
|
51
|
+
hash: hexAsBuffer('404371705fa9bd789a2fcd52d2c580b65d35549d'),
|
|
52
|
+
version: 0,
|
|
53
|
+
},
|
|
54
|
+
description: 'An address is returned for a p2pkh hash and version',
|
|
55
|
+
expected: {address: '16ro3Jptwo4asSevZnsRX6vfRS24TGE6uK'},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
args: {
|
|
59
|
+
hash: hexAsBuffer('e9c3dd0c07aac76179ebc76a6c78d4d67c6c160a'),
|
|
60
|
+
version: 5,
|
|
61
|
+
},
|
|
62
|
+
description: 'An address is returned for a p2sh hash and version',
|
|
63
|
+
expected: {address: '3P14159f73E4gFr7JterCCQh9QjiTjiZrG'},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
args: {
|
|
67
|
+
hash: hexAsBuffer('243f1394f44554f4ce3fd68649c19adc483ce924'),
|
|
68
|
+
version: 111,
|
|
69
|
+
},
|
|
70
|
+
description: 'An address is returned for a testnet p2pkh hash and version',
|
|
71
|
+
expected: {address: 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn'},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
args: {
|
|
75
|
+
hash: hexAsBuffer('243f1394f44554f4ce3fd68649c19adc483ce924'),
|
|
76
|
+
version: 196,
|
|
77
|
+
},
|
|
78
|
+
description: 'An address is returned for a testnet p2sh hash and version',
|
|
79
|
+
expected: {address: '2MvYsspbAW1kBiXyNQPSz4VmZJSomMgYyUb'},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
args: {
|
|
83
|
+
hash: hexAsBuffer('0000000000000000000000000000000000000000'),
|
|
84
|
+
version: 0,
|
|
85
|
+
},
|
|
86
|
+
description: 'Leading zero bytes are preserved as leading ones',
|
|
87
|
+
expected: {address: '1111111111111111111114oLvT2'},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
args: {
|
|
91
|
+
hash: hexAsBuffer('ffffffffffffffffffffffffffffffffffffffff'),
|
|
92
|
+
version: 0,
|
|
93
|
+
},
|
|
94
|
+
description: 'A maximum value hash is encoded with a zero version',
|
|
95
|
+
expected: {address: '1QLbz7JHiBTspS962RLKV8GndWFwi5j6Qr'},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
args: {
|
|
99
|
+
hash: hexAsBuffer('0000000000000000000000000000000000000000'),
|
|
100
|
+
version: 255,
|
|
101
|
+
},
|
|
102
|
+
description: 'A minimum value hash is encoded with the maximum version',
|
|
103
|
+
expected: {address: '2mcBp5h1U41ARYXZ6EywGeyQcVdhYP7dFob'},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
args: {
|
|
107
|
+
hash: hexAsBuffer('ffffffffffffffffffffffffffffffffffffffff'),
|
|
108
|
+
version: 255,
|
|
109
|
+
},
|
|
110
|
+
description: 'A maximum value hash is encoded with the maximum version',
|
|
111
|
+
expected: {address: '2n1XR4oJkmBdJMxhBGQGb96gQ88xUyGML1i'},
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
116
|
+
return test(description, (t, end) => {
|
|
117
|
+
if (!!error) {
|
|
118
|
+
throws(() => encodeBase58Address(args), new Error(error), 'Err');
|
|
119
|
+
} else {
|
|
120
|
+
const res = encodeBase58Address(args);
|
|
121
|
+
|
|
122
|
+
deepEqual(res, expected, 'Got expected result');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return end();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -25,6 +25,11 @@ const tests = [
|
|
|
25
25
|
description: 'Integer data words are expected',
|
|
26
26
|
error: 'ExpectedIntegerDataWordsToEncodeBech32String',
|
|
27
27
|
},
|
|
28
|
+
{
|
|
29
|
+
args: {prefix: 'bc', type: 'bech32', words: [NaN]},
|
|
30
|
+
description: 'Numeric data words are expected',
|
|
31
|
+
error: 'ExpectedIntegerDataWordsToEncodeBech32String',
|
|
32
|
+
},
|
|
28
33
|
{
|
|
29
34
|
args: {prefix: 'bc', type: 'bech32', words: [32]},
|
|
30
35
|
description: 'Data words within the word value range are expected',
|
|
@@ -117,6 +117,17 @@ const tests = [
|
|
|
117
117
|
address: 'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy',
|
|
118
118
|
},
|
|
119
119
|
},
|
|
120
|
+
{
|
|
121
|
+
args: {
|
|
122
|
+
prefix: 'tb',
|
|
123
|
+
program: hexAsBuffer('000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433'),
|
|
124
|
+
version: 1,
|
|
125
|
+
},
|
|
126
|
+
description: 'An address is encoded for a zero starting tb1 v1 output',
|
|
127
|
+
expected: {
|
|
128
|
+
address: 'tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
120
131
|
{
|
|
121
132
|
args: {
|
|
122
133
|
prefix: 'bc',
|
|
@@ -128,6 +139,17 @@ const tests = [
|
|
|
128
139
|
address: 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0',
|
|
129
140
|
},
|
|
130
141
|
},
|
|
142
|
+
{
|
|
143
|
+
args: {
|
|
144
|
+
prefix: 'bcrt',
|
|
145
|
+
program: hexAsBuffer('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
|
146
|
+
version: 1,
|
|
147
|
+
},
|
|
148
|
+
description: 'An address is encoded for a bcrt1 regtest v1 output',
|
|
149
|
+
expected: {
|
|
150
|
+
address: 'bcrt1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqc8gma6',
|
|
151
|
+
},
|
|
152
|
+
},
|
|
131
153
|
];
|
|
132
154
|
|
|
133
155
|
tests.forEach(({args, description, error, expected}) => {
|
|
@@ -45,6 +45,11 @@ const tests = [
|
|
|
45
45
|
description: 'Compressed public keys are required',
|
|
46
46
|
error: 'ExpectedCompressedPublicKeysToDeriveP2msScript',
|
|
47
47
|
},
|
|
48
|
+
{
|
|
49
|
+
args: {keys: [null], required: 1},
|
|
50
|
+
description: 'Defined public keys are required',
|
|
51
|
+
error: 'ExpectedCompressedPublicKeysToDeriveP2msScript',
|
|
52
|
+
},
|
|
48
53
|
{
|
|
49
54
|
args: {keys: bip143MultisigKeys.map(bufferFromHex)},
|
|
50
55
|
description: 'A required keys count is required',
|
|
@@ -16,6 +16,11 @@ const tests = [
|
|
|
16
16
|
description: 'Inputs are required to have spending transaction ids',
|
|
17
17
|
error: 'ExpectedSpendingTransactionIdsToFormTransaction',
|
|
18
18
|
},
|
|
19
|
+
{
|
|
20
|
+
args: {inputs: [null]},
|
|
21
|
+
description: 'Inputs are required to be defined',
|
|
22
|
+
error: 'ExpectedSpendingTransactionIdsToFormTransaction',
|
|
23
|
+
},
|
|
19
24
|
{
|
|
20
25
|
args: {
|
|
21
26
|
inputs: [{
|
|
@@ -50,6 +55,24 @@ const tests = [
|
|
|
50
55
|
description: 'Outputs are required to have hex encoded scripts',
|
|
51
56
|
error: 'ExpectedHexEncodedOutputScriptsToFormTransaction',
|
|
52
57
|
},
|
|
58
|
+
{
|
|
59
|
+
args: {
|
|
60
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
61
|
+
locktime: 0,
|
|
62
|
+
outputs: [{tokens: 1}, {script: 'not hex', tokens: 1}],
|
|
63
|
+
},
|
|
64
|
+
description: 'Outputs with absent scripts are checked for hex scripts',
|
|
65
|
+
error: 'ExpectedHexEncodedOutputScriptsToFormTransaction',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
args: {
|
|
69
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
70
|
+
locktime: 0,
|
|
71
|
+
outputs: [null],
|
|
72
|
+
},
|
|
73
|
+
description: 'Outputs are required to be defined',
|
|
74
|
+
error: 'ExpectedHexEncodedOutputScriptsToFormTransaction',
|
|
75
|
+
},
|
|
53
76
|
{
|
|
54
77
|
args: {
|
|
55
78
|
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
@@ -147,6 +170,38 @@ const tests = [
|
|
|
147
170
|
transaction: '02000000000103ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570200000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570100000000ffffffff0640420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6de91f62901000000225120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d49240420f00000000001600148729d17b2aa507ab19051a028384bc6e0ce25e455e368900000000002251200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b440420f0000000000160014d2d59a8a59f997cbc8888411010faf1658e0e3465e368900000000002251207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab2024830450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d0121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef75329602483045022100f9cde9adb00c0a6c62dae8604ca750039201288c0dafff952461da3caf05e3ae0220679c01f2518413951de3b62531b1cf36bb92562e3bd4197f0fa6e6e3e231272e0121027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d0247304402202550beec478845af2df929abf85708f9fcceaae31377f2e01d803e2acf7b426f022036c312b1e38ca333fe70aa37d3093387ac7486f08438eb8eed323699594468cb012102275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe00000000',
|
|
148
171
|
},
|
|
149
172
|
},
|
|
173
|
+
{
|
|
174
|
+
args: {
|
|
175
|
+
inputs: [
|
|
176
|
+
{
|
|
177
|
+
id: '523e6d953a12ce92824c29d542455fa524f577639da29725146188ad3aaf8cae',
|
|
178
|
+
script: '',
|
|
179
|
+
sequence: 4294967295,
|
|
180
|
+
vout: 0,
|
|
181
|
+
witness: [
|
|
182
|
+
'30450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d01',
|
|
183
|
+
'029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef753296',
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
id: 'cbebc4da731e8995fe97f6fadcd731b36ad40e5ecb31e38e904f6e5982fa09f7',
|
|
188
|
+
script: '2103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6ac',
|
|
189
|
+
sequence: 4294967294,
|
|
190
|
+
vout: 1,
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
locktime: 0,
|
|
194
|
+
outputs: [{
|
|
195
|
+
script: '0014eaa9b76637b1ad340b6efadf773a73b53637d5b6',
|
|
196
|
+
tokens: 1000000,
|
|
197
|
+
}],
|
|
198
|
+
version: 2,
|
|
199
|
+
},
|
|
200
|
+
description: 'A transaction with mixed witness and non-witness inputs is formed',
|
|
201
|
+
expected: {
|
|
202
|
+
transaction: '02000000000102ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000fffffffff709fa82596e4f908ee331cb5e0ed46ab331d7dcfaf697fe95891e73dac4ebcb01000000232103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6acfeffffff0140420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6024830450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d0121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef7532960000000000',
|
|
203
|
+
},
|
|
204
|
+
},
|
|
150
205
|
{
|
|
151
206
|
args: {
|
|
152
207
|
inputs: [{
|
|
@@ -44,7 +44,7 @@ module.exports = ({inputs, locktime, outputs, version}) => {
|
|
|
44
44
|
throw new Error('ExpectedArrayOfInputsToFormTransactionFromComponents');
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
if (
|
|
47
|
+
if (inputs.some(input => !input || !isTxId(input.id))) {
|
|
48
48
|
throw new Error('ExpectedSpendingTransactionIdsToFormTransaction');
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -60,7 +60,7 @@ module.exports = ({inputs, locktime, outputs, version}) => {
|
|
|
60
60
|
throw new Error('ExpectedArrayOfOutputsToFormTransactionFromComponents');
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
if (
|
|
63
|
+
if (outputs.some(output => !output || !isHex(output.script || ''))) {
|
|
64
64
|
throw new Error('ExpectedHexEncodedOutputScriptsToFormTransaction');
|
|
65
65
|
}
|
|
66
66
|
|