@alexbosworth/blockchain 4.3.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 +8 -0
- package/README.md +59 -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 +4 -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 +247 -0
- package/transactions/index.js +2 -0
- package/transactions/transaction_from_components.js +156 -0
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
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Versions
|
|
2
2
|
|
|
3
|
+
## 4.5.0
|
|
4
|
+
|
|
5
|
+
- `encodeBase58Address`: Add method to convert hash and version to b58 address
|
|
6
|
+
|
|
7
|
+
## 4.4.0
|
|
8
|
+
|
|
9
|
+
- `transactionFromComponents`: Add method to form a raw tx from tx components
|
|
10
|
+
|
|
3
11
|
## 4.3.0
|
|
4
12
|
|
|
5
13
|
- `hashForP2pkh`: Add method to get the p2pkh hash160 for a public key
|
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)
|
|
@@ -27,6 +28,7 @@ Utility methods for working with Blockchain data
|
|
|
27
28
|
- [scriptAsScriptElements](#scriptasscriptelements)
|
|
28
29
|
- [scriptElementsAsScript](#scriptelementsasscript)
|
|
29
30
|
- [sizeOfTransaction](#sizeoftransaction)
|
|
31
|
+
- [transactionFromComponents](#transactionfromcomponents)
|
|
30
32
|
- [unsignedTxFromPsbt](#unsignedtxfrompsbt)
|
|
31
33
|
|
|
32
34
|
### compactIntAsNumber
|
|
@@ -116,6 +118,35 @@ Decode a bech32 address string to derive the address details
|
|
|
116
118
|
version: <Witness Version Number>
|
|
117
119
|
}
|
|
118
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
|
+
|
|
119
150
|
### encodeBech32Address
|
|
120
151
|
|
|
121
152
|
Encode address details as a bech32 address string
|
|
@@ -479,6 +510,34 @@ Get the weight and virtual size of a hex-encoded transaction
|
|
|
479
510
|
weight: <Transaction Weight Units Number>
|
|
480
511
|
}
|
|
481
512
|
|
|
513
|
+
### transactionFromComponents
|
|
514
|
+
|
|
515
|
+
Form a hex-encoded transaction from its component elements
|
|
516
|
+
|
|
517
|
+
{
|
|
518
|
+
inputs: [{
|
|
519
|
+
id: <Spending Transaction Id Hex String>
|
|
520
|
+
script: <ScriptSig Script Hex String>
|
|
521
|
+
sequence: <Sequence Number>
|
|
522
|
+
vout: <Spending Transaction Output Index Number>
|
|
523
|
+
[witness]: [<Script Stack Element Hex String>]
|
|
524
|
+
}]
|
|
525
|
+
locktime: <Timelock nLockTime Number>
|
|
526
|
+
outputs: [{
|
|
527
|
+
script: <ScriptPub Script Hex String>
|
|
528
|
+
tokens: <Tokens Count Number>
|
|
529
|
+
}]
|
|
530
|
+
version: <Version Number>
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
@throws
|
|
534
|
+
<Error>
|
|
535
|
+
|
|
536
|
+
@returns
|
|
537
|
+
{
|
|
538
|
+
transaction: <Hex Encoded Transaction String>
|
|
539
|
+
}
|
|
540
|
+
|
|
482
541
|
### unsignedTxFromPsbt
|
|
483
542
|
|
|
484
543
|
Get the unsigned transaction out of a PSBT
|
|
@@ -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');
|
|
@@ -21,6 +22,7 @@ const {queryTransactions} = require('./transactions');
|
|
|
21
22
|
const {scriptAsScriptElements} = require('./script');
|
|
22
23
|
const {scriptElementsAsScript} = require('./script');
|
|
23
24
|
const {sizeOfTransaction} = require('./transactions');
|
|
25
|
+
const {transactionFromComponents} = require('./transactions');
|
|
24
26
|
const {unsignedTxFromPsbt} = require('./transactions');
|
|
25
27
|
|
|
26
28
|
module.exports = {
|
|
@@ -28,6 +30,7 @@ module.exports = {
|
|
|
28
30
|
componentsOfTransaction,
|
|
29
31
|
decodeBase58Address,
|
|
30
32
|
decodeBech32Address,
|
|
33
|
+
encodeBase58Address,
|
|
31
34
|
encodeBech32Address,
|
|
32
35
|
hashForP2pkh,
|
|
33
36
|
hashForP2wpkh,
|
|
@@ -47,5 +50,6 @@ module.exports = {
|
|
|
47
50
|
scriptAsScriptElements,
|
|
48
51
|
scriptElementsAsScript,
|
|
49
52
|
sizeOfTransaction,
|
|
53
|
+
transactionFromComponents,
|
|
50
54
|
unsignedTxFromPsbt,
|
|
51
55
|
};
|
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',
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {componentsOfTransaction} = require('./../../');
|
|
6
|
+
const {transactionFromComponents} = require('./../../');
|
|
7
|
+
|
|
8
|
+
const tests = [
|
|
9
|
+
{
|
|
10
|
+
args: {},
|
|
11
|
+
description: 'An array of inputs is required',
|
|
12
|
+
error: 'ExpectedArrayOfInputsToFormTransactionFromComponents',
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
args: {inputs: [{id: '00'}]},
|
|
16
|
+
description: 'Inputs are required to have spending transaction ids',
|
|
17
|
+
error: 'ExpectedSpendingTransactionIdsToFormTransaction',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
args: {inputs: [null]},
|
|
21
|
+
description: 'Inputs are required to be defined',
|
|
22
|
+
error: 'ExpectedSpendingTransactionIdsToFormTransaction',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
args: {
|
|
26
|
+
inputs: [{
|
|
27
|
+
id: Buffer.alloc(32).toString('hex'),
|
|
28
|
+
script: 'not hex',
|
|
29
|
+
}],
|
|
30
|
+
},
|
|
31
|
+
description: 'Inputs are required to have hex encoded scripts',
|
|
32
|
+
error: 'ExpectedHexEncodedInputScriptsToFormTransaction',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
args: {
|
|
36
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
37
|
+
},
|
|
38
|
+
description: 'A locktime is required',
|
|
39
|
+
error: 'ExpectedLocktimeToFormTransactionFromComponents',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
args: {
|
|
43
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
44
|
+
locktime: 0,
|
|
45
|
+
},
|
|
46
|
+
description: 'An array of outputs is required',
|
|
47
|
+
error: 'ExpectedArrayOfOutputsToFormTransactionFromComponents',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
args: {
|
|
51
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
52
|
+
locktime: 0,
|
|
53
|
+
outputs: [{script: 'not hex', tokens: 1}],
|
|
54
|
+
},
|
|
55
|
+
description: 'Outputs are required to have hex encoded scripts',
|
|
56
|
+
error: 'ExpectedHexEncodedOutputScriptsToFormTransaction',
|
|
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
|
+
},
|
|
76
|
+
{
|
|
77
|
+
args: {
|
|
78
|
+
inputs: [{id: Buffer.alloc(32).toString('hex'), script: ''}],
|
|
79
|
+
locktime: 0,
|
|
80
|
+
outputs: [],
|
|
81
|
+
},
|
|
82
|
+
description: 'A version number is required',
|
|
83
|
+
error: 'ExpectedVersionNumberToFormTransactionFromComponents',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
args: {
|
|
87
|
+
inputs: [{
|
|
88
|
+
id: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
89
|
+
script: '04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73',
|
|
90
|
+
sequence: 4294967295,
|
|
91
|
+
vout: 4294967295,
|
|
92
|
+
}],
|
|
93
|
+
locktime: 0,
|
|
94
|
+
outputs: [{
|
|
95
|
+
script: '4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac',
|
|
96
|
+
tokens: 5000000000,
|
|
97
|
+
}],
|
|
98
|
+
version: 1,
|
|
99
|
+
},
|
|
100
|
+
description: 'A coinbase transaction is formed',
|
|
101
|
+
expected: {
|
|
102
|
+
transaction: '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
args: {
|
|
107
|
+
inputs: [
|
|
108
|
+
{
|
|
109
|
+
id: '523e6d953a12ce92824c29d542455fa524f577639da29725146188ad3aaf8cae',
|
|
110
|
+
script: '',
|
|
111
|
+
sequence: 4294967295,
|
|
112
|
+
vout: 0,
|
|
113
|
+
witness: [
|
|
114
|
+
'30450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d01',
|
|
115
|
+
'029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef753296',
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
id: '572477126b95bd17aacdb94a559ed4024fa198a1759d480cb977a631551cbb9c',
|
|
120
|
+
script: '',
|
|
121
|
+
sequence: 4294967295,
|
|
122
|
+
vout: 2,
|
|
123
|
+
witness: [
|
|
124
|
+
'3045022100f9cde9adb00c0a6c62dae8604ca750039201288c0dafff952461da3caf05e3ae0220679c01f2518413951de3b62531b1cf36bb92562e3bd4197f0fa6e6e3e231272e01',
|
|
125
|
+
'027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d',
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: '572477126b95bd17aacdb94a559ed4024fa198a1759d480cb977a631551cbb9c',
|
|
130
|
+
script: '',
|
|
131
|
+
sequence: 4294967295,
|
|
132
|
+
vout: 1,
|
|
133
|
+
witness: [
|
|
134
|
+
'304402202550beec478845af2df929abf85708f9fcceaae31377f2e01d803e2acf7b426f022036c312b1e38ca333fe70aa37d3093387ac7486f08438eb8eed323699594468cb01',
|
|
135
|
+
'02275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe',
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
locktime: 0,
|
|
140
|
+
outputs: [
|
|
141
|
+
{
|
|
142
|
+
script: '0014eaa9b76637b1ad340b6efadf773a73b53637d5b6',
|
|
143
|
+
tokens: 1000000,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
script: '5120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d492',
|
|
147
|
+
tokens: 4998992350,
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
script: '00148729d17b2aa507ab19051a028384bc6e0ce25e45',
|
|
151
|
+
tokens: 1000000,
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
script: '51200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b4',
|
|
155
|
+
tokens: 8992350,
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
script: '0014d2d59a8a59f997cbc8888411010faf1658e0e346',
|
|
159
|
+
tokens: 1000000,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
script: '51207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab2',
|
|
163
|
+
tokens: 8992350,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
version: 2,
|
|
167
|
+
},
|
|
168
|
+
description: 'A transaction with witnesses is formed',
|
|
169
|
+
expected: {
|
|
170
|
+
transaction: '02000000000103ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570200000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570100000000ffffffff0640420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6de91f62901000000225120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d49240420f00000000001600148729d17b2aa507ab19051a028384bc6e0ce25e455e368900000000002251200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b440420f0000000000160014d2d59a8a59f997cbc8888411010faf1658e0e3465e368900000000002251207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab2024830450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d0121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef75329602483045022100f9cde9adb00c0a6c62dae8604ca750039201288c0dafff952461da3caf05e3ae0220679c01f2518413951de3b62531b1cf36bb92562e3bd4197f0fa6e6e3e231272e0121027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d0247304402202550beec478845af2df929abf85708f9fcceaae31377f2e01d803e2acf7b426f022036c312b1e38ca333fe70aa37d3093387ac7486f08438eb8eed323699594468cb012102275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe00000000',
|
|
171
|
+
},
|
|
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
|
+
},
|
|
205
|
+
{
|
|
206
|
+
args: {
|
|
207
|
+
inputs: [{
|
|
208
|
+
id: 'cbebc4da731e8995fe97f6fadcd731b36ad40e5ecb31e38e904f6e5982fa09f7',
|
|
209
|
+
script: '20ca42095840735e89283fec298e62ac2ddea9b5f34a8cbb7097ad965b87568100201b1b01dc829177da4a14551d2fc96a9db00c6501edfa12f22cd9cefd335c227f483045022100a9df60536df5733dd0de6bc921fab0b3eee6426501b43a228afa2c90072eb5ca02201c78b74266fac7d1db5deff080d8a403743203f109fbcabf6d5a760bf87386d20100',
|
|
210
|
+
sequence: 4294967295,
|
|
211
|
+
vout: 0,
|
|
212
|
+
}],
|
|
213
|
+
locktime: 0,
|
|
214
|
+
outputs: [{
|
|
215
|
+
script: '2103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6ac',
|
|
216
|
+
tokens: 7960000,
|
|
217
|
+
}],
|
|
218
|
+
version: 1,
|
|
219
|
+
},
|
|
220
|
+
description: 'A non-segwit transaction is formed',
|
|
221
|
+
expected: {
|
|
222
|
+
transaction: '0100000001f709fa82596e4f908ee331cb5e0ed46ab331d7dcfaf697fe95891e73dac4ebcb000000008c20ca42095840735e89283fec298e62ac2ddea9b5f34a8cbb7097ad965b87568100201b1b01dc829177da4a14551d2fc96a9db00c6501edfa12f22cd9cefd335c227f483045022100a9df60536df5733dd0de6bc921fab0b3eee6426501b43a228afa2c90072eb5ca02201c78b74266fac7d1db5deff080d8a403743203f109fbcabf6d5a760bf87386d20100ffffffff01c075790000000000232103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6ac00000000',
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
228
|
+
return test(description, (t, end) => {
|
|
229
|
+
if (!!error) {
|
|
230
|
+
throws(() => transactionFromComponents(args), new Error(error), 'Err');
|
|
231
|
+
} else {
|
|
232
|
+
const res = transactionFromComponents(args);
|
|
233
|
+
|
|
234
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
235
|
+
|
|
236
|
+
const components = componentsOfTransaction(res);
|
|
237
|
+
|
|
238
|
+
deepStrictEqual(
|
|
239
|
+
transactionFromComponents(components),
|
|
240
|
+
expected,
|
|
241
|
+
'Components round trip back to the transaction'
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return end();
|
|
246
|
+
});
|
|
247
|
+
});
|
package/transactions/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const noLocktimeIdForTransaction = require('./no_locktime_id_for_transaction');
|
|
|
4
4
|
const parseTransaction = require('./parse_transaction');
|
|
5
5
|
const queryTransactions = require('./query_transactions');
|
|
6
6
|
const sizeOfTransaction = require('./size_of_transaction');
|
|
7
|
+
const transactionFromComponents = require('./transaction_from_components');
|
|
7
8
|
const unsignedTxFromPsbt = require('./unsigned_tx_from_psbt');
|
|
8
9
|
|
|
9
10
|
module.exports = {
|
|
@@ -13,5 +14,6 @@ module.exports = {
|
|
|
13
14
|
parseTransaction,
|
|
14
15
|
queryTransactions,
|
|
15
16
|
sizeOfTransaction,
|
|
17
|
+
transactionFromComponents,
|
|
16
18
|
unsignedTxFromPsbt,
|
|
17
19
|
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const {numberAsCompactInt} = require('./../numbers');
|
|
2
|
+
|
|
3
|
+
const {alloc} = Buffer;
|
|
4
|
+
const bufferAsHex = buffer => buffer.toString('hex');
|
|
5
|
+
const byteCountInt32 = 4;
|
|
6
|
+
const byteCountInt64 = 8;
|
|
7
|
+
const {concat} = Buffer;
|
|
8
|
+
const encodeCount = number => numberAsCompactInt({number}).encoded;
|
|
9
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
10
|
+
const {isArray} = Array;
|
|
11
|
+
const isHex = n => !(n.length % 2) && /^[0-9A-F]*$/i.test(n);
|
|
12
|
+
const isTxId = n => /^[0-9A-F]{64}$/i.test(n);
|
|
13
|
+
const markerAndFlag = Buffer.from([0x00, 0x01]);
|
|
14
|
+
const txIdAsTxHash = id => hexAsBuffer(id).reverse();
|
|
15
|
+
|
|
16
|
+
/** Form a hex-encoded transaction from its component elements
|
|
17
|
+
|
|
18
|
+
{
|
|
19
|
+
inputs: [{
|
|
20
|
+
id: <Spending Transaction Id Hex String>
|
|
21
|
+
script: <ScriptSig Script Hex String>
|
|
22
|
+
sequence: <Sequence Number>
|
|
23
|
+
vout: <Spending Transaction Output Index Number>
|
|
24
|
+
[witness]: [<Script Stack Element Hex String>]
|
|
25
|
+
}]
|
|
26
|
+
locktime: <Timelock nLockTime Number>
|
|
27
|
+
outputs: [{
|
|
28
|
+
script: <ScriptPub Script Hex String>
|
|
29
|
+
tokens: <Tokens Count Number>
|
|
30
|
+
}]
|
|
31
|
+
version: <Version Number>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@throws
|
|
35
|
+
<Error>
|
|
36
|
+
|
|
37
|
+
@returns
|
|
38
|
+
{
|
|
39
|
+
transaction: <Hex Encoded Transaction String>
|
|
40
|
+
}
|
|
41
|
+
*/
|
|
42
|
+
module.exports = ({inputs, locktime, outputs, version}) => {
|
|
43
|
+
if (!isArray(inputs)) {
|
|
44
|
+
throw new Error('ExpectedArrayOfInputsToFormTransactionFromComponents');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (inputs.some(input => !input || !isTxId(input.id))) {
|
|
48
|
+
throw new Error('ExpectedSpendingTransactionIdsToFormTransaction');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!!inputs.find(input => !isHex(input.script || ''))) {
|
|
52
|
+
throw new Error('ExpectedHexEncodedInputScriptsToFormTransaction');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (locktime === undefined) {
|
|
56
|
+
throw new Error('ExpectedLocktimeToFormTransactionFromComponents');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!isArray(outputs)) {
|
|
60
|
+
throw new Error('ExpectedArrayOfOutputsToFormTransactionFromComponents');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (outputs.some(output => !output || !isHex(output.script || ''))) {
|
|
64
|
+
throw new Error('ExpectedHexEncodedOutputScriptsToFormTransaction');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (version === undefined) {
|
|
68
|
+
throw new Error('ExpectedVersionNumberToFormTransactionFromComponents');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// The presence of any input witness indicates SegWit tx encoding
|
|
72
|
+
const isSegWit = !!inputs.find(input => isArray(input.witness));
|
|
73
|
+
|
|
74
|
+
const elements = [];
|
|
75
|
+
|
|
76
|
+
// Write the signed transaction version number
|
|
77
|
+
const ver = alloc(byteCountInt32);
|
|
78
|
+
|
|
79
|
+
ver.writeInt32LE(version);
|
|
80
|
+
|
|
81
|
+
elements.push(ver);
|
|
82
|
+
|
|
83
|
+
// SegWit transactions lead with marker and flag bytes
|
|
84
|
+
if (isSegWit) {
|
|
85
|
+
elements.push(markerAndFlag);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Write how many inputs there are
|
|
89
|
+
elements.push(encodeCount(inputs.length));
|
|
90
|
+
|
|
91
|
+
// Write the inputs
|
|
92
|
+
elements.push(concat(inputs.map(input => {
|
|
93
|
+
const script = hexAsBuffer(input.script);
|
|
94
|
+
|
|
95
|
+
// Encode the sequence number
|
|
96
|
+
const sequence = alloc(byteCountInt32);
|
|
97
|
+
|
|
98
|
+
sequence.writeUInt32LE(input.sequence);
|
|
99
|
+
|
|
100
|
+
// Encode the spend output index
|
|
101
|
+
const vout = alloc(byteCountInt32);
|
|
102
|
+
|
|
103
|
+
vout.writeUInt32LE(input.vout);
|
|
104
|
+
|
|
105
|
+
// Write the spend outpoint, script sig, sequence number
|
|
106
|
+
return concat([
|
|
107
|
+
txIdAsTxHash(input.id),
|
|
108
|
+
vout,
|
|
109
|
+
encodeCount(script.length),
|
|
110
|
+
script,
|
|
111
|
+
sequence,
|
|
112
|
+
]);
|
|
113
|
+
})));
|
|
114
|
+
|
|
115
|
+
// Write the outputs count
|
|
116
|
+
elements.push(encodeCount(outputs.length));
|
|
117
|
+
|
|
118
|
+
// Write the outputs
|
|
119
|
+
elements.push(concat(outputs.map(output => {
|
|
120
|
+
const script = hexAsBuffer(output.script);
|
|
121
|
+
|
|
122
|
+
// Encode the output value
|
|
123
|
+
const value = alloc(byteCountInt64);
|
|
124
|
+
|
|
125
|
+
value.writeBigUInt64LE(BigInt(output.tokens));
|
|
126
|
+
|
|
127
|
+
// Write the output value and output script
|
|
128
|
+
return concat([value, encodeCount(script.length), script]);
|
|
129
|
+
})));
|
|
130
|
+
|
|
131
|
+
// SegWit places the input witnesses after the outputs
|
|
132
|
+
if (isSegWit) {
|
|
133
|
+
elements.push(concat(inputs.map(input => {
|
|
134
|
+
const witness = input.witness || [];
|
|
135
|
+
|
|
136
|
+
// Write the witness stack element count and the stack elements
|
|
137
|
+
return concat([
|
|
138
|
+
encodeCount(witness.length),
|
|
139
|
+
concat(witness.map(element => {
|
|
140
|
+
const item = hexAsBuffer(element);
|
|
141
|
+
|
|
142
|
+
return concat([encodeCount(item.length), item]);
|
|
143
|
+
})),
|
|
144
|
+
]);
|
|
145
|
+
})));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The final element is the 4 byte transaction nLockTime
|
|
149
|
+
const timelock = alloc(byteCountInt32);
|
|
150
|
+
|
|
151
|
+
timelock.writeUInt32LE(locktime);
|
|
152
|
+
|
|
153
|
+
elements.push(timelock);
|
|
154
|
+
|
|
155
|
+
return {transaction: bufferAsHex(concat(elements))};
|
|
156
|
+
};
|