@alexbosworth/blockchain 4.10.0 → 4.12.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 +9 -0
- package/README.md +55 -1
- package/index.js +2 -0
- package/keys/conf/networks.json +17 -0
- package/keys/decode_wif.js +99 -0
- package/keys/index.js +3 -0
- package/package.json +3 -2
- package/transactions/query_transactions.js +69 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Versions
|
|
2
2
|
|
|
3
|
+
## 4.12.0
|
|
4
|
+
|
|
5
|
+
- `queryTransactions`: Add `ids` argument to find transactions by their id
|
|
6
|
+
- `queryTransactions`: Add `transactions` result with raw matching transactions
|
|
7
|
+
|
|
8
|
+
## 4.11.0
|
|
9
|
+
|
|
10
|
+
- `decodeWif`: Add method to decode a WIF encoded private key
|
|
11
|
+
|
|
3
12
|
## 4.10.0
|
|
4
13
|
|
|
5
14
|
- `outputScriptForAddress`: Add support for `btctestnet4` network
|
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ Utility methods for working with Blockchain data
|
|
|
9
9
|
- [decodeBase58](#decodebase58)
|
|
10
10
|
- [decodeBase58Address](#decodebase58address)
|
|
11
11
|
- [decodeBech32Address](#decodebech32address)
|
|
12
|
+
- [decodeWif](#decodewif)
|
|
12
13
|
- [encodeBase58Address](#encodebase58address)
|
|
13
14
|
- [encodeBech32Address](#encodebech32address)
|
|
14
15
|
- [hashForP2pkh](#hashforp2pkh)
|
|
@@ -138,6 +139,54 @@ Decode a bech32 address string to derive the address details
|
|
|
138
139
|
version: <Witness Version Number>
|
|
139
140
|
}
|
|
140
141
|
|
|
142
|
+
### decodeWif
|
|
143
|
+
|
|
144
|
+
Decode a WIF encoded private key
|
|
145
|
+
|
|
146
|
+
Derive the public key in compressed form when `is_compressed` is true
|
|
147
|
+
|
|
148
|
+
When a network is not specified, a WIF for any known network is accepted
|
|
149
|
+
|
|
150
|
+
Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
|
|
151
|
+
|
|
152
|
+
{
|
|
153
|
+
[network]: <Network Name String>
|
|
154
|
+
wif: <WIF Encoded Private Key String>
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@throws
|
|
158
|
+
<Error>
|
|
159
|
+
|
|
160
|
+
@returns
|
|
161
|
+
{
|
|
162
|
+
is_compressed: <Public Key Is Compressed Bool>
|
|
163
|
+
private_key: <Private Key Buffer Object>
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
Example:
|
|
167
|
+
|
|
168
|
+
```node
|
|
169
|
+
const {createECDH} = require('node:crypto');
|
|
170
|
+
|
|
171
|
+
const {decodeWif} = require('@alexbosworth/blockchain');
|
|
172
|
+
|
|
173
|
+
// Decode the private key and the public key compression from a mainnet WIF
|
|
174
|
+
const {is_compressed, private_key} = decodeWif({
|
|
175
|
+
network: 'btc',
|
|
176
|
+
wif: 'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617',
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// The WIF signals if the public key is compressed or uncompressed
|
|
180
|
+
const format = is_compressed ? 'compressed' : 'uncompressed';
|
|
181
|
+
|
|
182
|
+
const ecdh = createECDH('secp256k1');
|
|
183
|
+
|
|
184
|
+
// Derive the public key from the private key
|
|
185
|
+
ecdh.setPrivateKey(private_key);
|
|
186
|
+
|
|
187
|
+
const publicKey = ecdh.getPublicKey(null, format);
|
|
188
|
+
```
|
|
189
|
+
|
|
141
190
|
### encodeBase58Address
|
|
142
191
|
|
|
143
192
|
Encode output hash and version data as a base58 address string
|
|
@@ -526,7 +575,8 @@ Find matching transactions within a block
|
|
|
526
575
|
|
|
527
576
|
{
|
|
528
577
|
block: <Hex Encoded Raw Block String>
|
|
529
|
-
|
|
578
|
+
[ids]: [<Transaction Id Hex String>]
|
|
579
|
+
[outputs]: [<Output Script Hex String>]
|
|
530
580
|
}
|
|
531
581
|
|
|
532
582
|
@throws
|
|
@@ -540,6 +590,10 @@ Find matching transactions within a block
|
|
|
540
590
|
transaction_id: <Transaction Id Hex String>
|
|
541
591
|
transaction_vout: <Transaction Output Index Number>
|
|
542
592
|
}]
|
|
593
|
+
transactions: [{
|
|
594
|
+
id: <Transaction Id Hex String>
|
|
595
|
+
transaction: <Hex Encoded Raw Transaction String>
|
|
596
|
+
}]
|
|
543
597
|
}
|
|
544
598
|
|
|
545
599
|
### scriptAsScriptElements
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const {componentsOfTransaction} = require('./transactions');
|
|
|
3
3
|
const {decodeBase58} = require('./addresses');
|
|
4
4
|
const {decodeBase58Address} = require('./addresses');
|
|
5
5
|
const {decodeBech32Address} = require('./addresses');
|
|
6
|
+
const {decodeWif} = require('./keys');
|
|
6
7
|
const {encodeBase58Address} = require('./addresses');
|
|
7
8
|
const {encodeBech32Address} = require('./addresses');
|
|
8
9
|
const {hashForP2pkh} = require('./hashes');
|
|
@@ -35,6 +36,7 @@ module.exports = {
|
|
|
35
36
|
decodeBase58,
|
|
36
37
|
decodeBase58Address,
|
|
37
38
|
decodeBech32Address,
|
|
39
|
+
decodeWif,
|
|
38
40
|
encodeBase58Address,
|
|
39
41
|
encodeBech32Address,
|
|
40
42
|
hashForP2pkh,
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const {decodeBase58} = require('./../addresses');
|
|
2
|
+
const networks = require('./conf/networks.json');
|
|
3
|
+
|
|
4
|
+
const compressedFlag = 0x01;
|
|
5
|
+
const copyOf = bytes => Buffer.alloc(bytes.length, bytes);
|
|
6
|
+
const countFlagBytes = 1;
|
|
7
|
+
const countPrivateKeyBytes = 32;
|
|
8
|
+
const countVersionBytes = 1;
|
|
9
|
+
const curveOrder = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 'hex');
|
|
10
|
+
const isDefined = n => n !== undefined && n !== null;
|
|
11
|
+
const isZero = bytes => bytes.every(byte => !byte);
|
|
12
|
+
const {keys} = Object;
|
|
13
|
+
const maxWifLength = 52;
|
|
14
|
+
const {values} = Object;
|
|
15
|
+
const wipe = bytes => bytes.fill(0);
|
|
16
|
+
|
|
17
|
+
/** Decode a WIF encoded private key
|
|
18
|
+
|
|
19
|
+
Derive the public key in compressed form when `is_compressed` is true
|
|
20
|
+
|
|
21
|
+
When a network is not specified, a WIF for any known network is accepted
|
|
22
|
+
|
|
23
|
+
Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
|
|
24
|
+
|
|
25
|
+
{
|
|
26
|
+
[network]: <Network Name String>
|
|
27
|
+
wif: <WIF Encoded Private Key String>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@throws
|
|
31
|
+
<Error>
|
|
32
|
+
|
|
33
|
+
@returns
|
|
34
|
+
{
|
|
35
|
+
is_compressed: <Public Key Is Compressed Bool>
|
|
36
|
+
private_key: <Private Key Buffer Object>
|
|
37
|
+
}
|
|
38
|
+
*/
|
|
39
|
+
module.exports = ({network, wif}) => {
|
|
40
|
+
if (isDefined(network) && !keys(networks).includes(network)) {
|
|
41
|
+
throw new Error('ExpectedKnownNetworkNameToDecodeWif');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof wif !== 'string') {
|
|
45
|
+
throw new Error('ExpectedWifEncodedPrivateKeyStringToDecode');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Exit early with error when the string is too long to be a WIF to decode
|
|
49
|
+
if (wif.trim().length > maxWifLength) {
|
|
50
|
+
throw new Error('ExpectedShorterWifEncodedPrivateKeyString');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Decode the base58check string and validate its checksum
|
|
54
|
+
const decoded = decodeBase58({encoded: wif});
|
|
55
|
+
|
|
56
|
+
// Copy the payload into its own memory, it was decoded into shared memory
|
|
57
|
+
const payload = copyOf(decoded.payload);
|
|
58
|
+
|
|
59
|
+
// Wipe the shared memory copy of the payload since it has the private key
|
|
60
|
+
wipe(decoded.payload);
|
|
61
|
+
|
|
62
|
+
// The payload is a version byte and then the private key, maybe with a flag
|
|
63
|
+
const keyEnd = countVersionBytes + countPrivateKeyBytes;
|
|
64
|
+
|
|
65
|
+
// A compressed public key is signaled with a flag byte after the private key
|
|
66
|
+
const isCompressed = payload.length === keyEnd + countFlagBytes;
|
|
67
|
+
|
|
68
|
+
// Make sure that the payload has the version and the private key
|
|
69
|
+
if (payload.length !== keyEnd && !isCompressed) {
|
|
70
|
+
throw new Error('ExpectedVersionAnd32BytePrivateKeyInWifPayload');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The compressed public key flag byte only has a single valid value
|
|
74
|
+
if (isCompressed && payload[keyEnd] !== compressedFlag) {
|
|
75
|
+
throw new Error('UnexpectedCompressionFlagValueInWifPayload');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const [version] = payload;
|
|
79
|
+
|
|
80
|
+
// Exit early with error when the version is not for the specified network
|
|
81
|
+
if (isDefined(network) && networks[network].wif !== version) {
|
|
82
|
+
throw new Error('UnexpectedWifVersionForNetwork');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Exit early with error when the version is not for any known network
|
|
86
|
+
if (!values(networks).some(n => n.wif === version)) {
|
|
87
|
+
throw new Error('UnexpectedWifVersionForKnownNetworks');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The private key gets its own memory, apart from the rest of the payload
|
|
91
|
+
const privateKey = copyOf(payload.subarray(countVersionBytes, keyEnd));
|
|
92
|
+
|
|
93
|
+
// A private key must be greater than zero and less than the curve order
|
|
94
|
+
if (isZero(privateKey) || privateKey.compare(curveOrder) >= 0) {
|
|
95
|
+
throw new Error('ExpectedValidSecp256k1PrivateKeyInWifPayload');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {is_compressed: isCompressed, private_key: privateKey};
|
|
99
|
+
};
|
package/keys/index.js
ADDED
package/package.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"CHANGELOG.md",
|
|
17
17
|
"hashes",
|
|
18
18
|
"index.js",
|
|
19
|
+
"keys",
|
|
19
20
|
"numbers",
|
|
20
21
|
"script",
|
|
21
22
|
"transactions"
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
"url": "git+https://github.com/alexbosworth/blockchain.git"
|
|
28
29
|
},
|
|
29
30
|
"scripts": {
|
|
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"
|
|
31
|
+
"test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/keys/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
|
|
31
32
|
},
|
|
32
|
-
"version": "4.
|
|
33
|
+
"version": "4.12.0"
|
|
33
34
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const {createHash} = require('crypto');
|
|
2
|
-
|
|
3
1
|
const {compactIntAsNumber} = require('./../numbers');
|
|
4
2
|
|
|
5
3
|
const {idForTransactionComponents} = require('./../hashes');
|
|
@@ -8,6 +6,7 @@ const parseTransaction = require('./parse_transaction');
|
|
|
8
6
|
const bufferAsHex = buffer => buffer.toString('hex');
|
|
9
7
|
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
10
8
|
const {isArray} = Array;
|
|
9
|
+
const isTxId = n => typeof n === 'string' && /^[0-9A-F]{64}$/i.test(n);
|
|
11
10
|
const minTxByteLength = 60;
|
|
12
11
|
const transactionsStartIndex = 80;
|
|
13
12
|
|
|
@@ -15,7 +14,8 @@ const transactionsStartIndex = 80;
|
|
|
15
14
|
|
|
16
15
|
{
|
|
17
16
|
block: <Hex Encoded Raw Block String>
|
|
18
|
-
|
|
17
|
+
[ids]: [<Transaction Id Hex String>]
|
|
18
|
+
[outputs]: [<Output Script Hex String>]
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
@throws
|
|
@@ -29,17 +29,33 @@ const transactionsStartIndex = 80;
|
|
|
29
29
|
transaction_id: <Transaction Id Hex String>
|
|
30
30
|
transaction_vout: <Transaction Output Index Number>
|
|
31
31
|
}]
|
|
32
|
+
transactions: [{
|
|
33
|
+
id: <Transaction Id Hex String>
|
|
34
|
+
transaction: <Hex Encoded Raw Transaction String>
|
|
35
|
+
}]
|
|
32
36
|
}
|
|
33
37
|
*/
|
|
34
|
-
module.exports = ({block, outputs}) => {
|
|
38
|
+
module.exports = ({block, ids, outputs}) => {
|
|
35
39
|
if (!block) {
|
|
36
40
|
throw new Error('ExpectedHexEncodedBlockToQueryTransactions');
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
if (
|
|
43
|
+
if (ids === undefined && outputs === undefined) {
|
|
44
|
+
throw new Error('ExpectedTransactionIdsOrOutputsToQueryTransactions');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (outputs !== undefined && !isArray(outputs)) {
|
|
40
48
|
throw new Error('ExpectedArrayOfOutputsToQueryTransactionsInBlock');
|
|
41
49
|
}
|
|
42
50
|
|
|
51
|
+
if (ids !== undefined && !isArray(ids)) {
|
|
52
|
+
throw new Error('ExpectedArrayOfIdsToQueryTransactionsInBlock');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!!ids && !ids.every(isTxId)) {
|
|
56
|
+
throw new Error('ExpectedTransactionIdHexStringToQueryTransactions');
|
|
57
|
+
}
|
|
58
|
+
|
|
43
59
|
const buffer = hexAsBuffer(block);
|
|
44
60
|
|
|
45
61
|
// Exit early with error when the block header data is cut short
|
|
@@ -54,8 +70,12 @@ module.exports = ({block, outputs}) => {
|
|
|
54
70
|
});
|
|
55
71
|
|
|
56
72
|
let cursor = transactionsStartIndex + txCount.bytes;
|
|
73
|
+
const found = new Set();
|
|
57
74
|
const hits = [];
|
|
75
|
+
const pendingIds = new Set((ids || []).map(id => id.toLowerCase()));
|
|
58
76
|
let remaining = txCount.number;
|
|
77
|
+
const scripts = outputs || [];
|
|
78
|
+
const transactions = [];
|
|
59
79
|
|
|
60
80
|
// Confirm the data could possibly contain the claimed count of txs
|
|
61
81
|
if (txCount.number * minTxByteLength > buffer.length - cursor) {
|
|
@@ -66,31 +86,61 @@ module.exports = ({block, outputs}) => {
|
|
|
66
86
|
while (remaining--) {
|
|
67
87
|
const tx = parseTransaction({buffer, start: cursor});
|
|
68
88
|
|
|
89
|
+
cursor += tx.bytes.length;
|
|
90
|
+
|
|
91
|
+
const matches = [];
|
|
92
|
+
|
|
69
93
|
// Look at the transaction outputs to see if there is a match
|
|
70
|
-
|
|
94
|
+
tx.outputs.forEach((output, vout) => {
|
|
95
|
+
const script = bufferAsHex(output.script);
|
|
96
|
+
|
|
71
97
|
// Exit early when the script isn't included in the outputs
|
|
72
|
-
if (!
|
|
98
|
+
if (!scripts.includes(script)) {
|
|
73
99
|
return;
|
|
74
100
|
}
|
|
75
101
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
102
|
+
return matches.push({script, vout, tokens: output.tokens});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Exit early when there are no output hits and no more ids to look for
|
|
106
|
+
if (!matches.length && !pendingIds.size) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Calculate the transaction id
|
|
111
|
+
const {id} = idForTransactionComponents({
|
|
112
|
+
inputs: tx.inputs,
|
|
113
|
+
locktime: tx.locktime,
|
|
114
|
+
outputs: tx.outputs,
|
|
115
|
+
version: tx.version,
|
|
116
|
+
});
|
|
83
117
|
|
|
84
|
-
|
|
118
|
+
// Add the matching outputs to found hits
|
|
119
|
+
matches.forEach(({script, tokens, vout}) => {
|
|
85
120
|
return hits.push({
|
|
86
|
-
script
|
|
87
|
-
tokens
|
|
121
|
+
script,
|
|
122
|
+
tokens,
|
|
88
123
|
transaction_id: id,
|
|
89
124
|
transaction_vout: vout,
|
|
90
125
|
});
|
|
91
126
|
});
|
|
92
127
|
|
|
93
|
-
|
|
128
|
+
// Exit early when the transaction was already found in the block
|
|
129
|
+
if (found.has(id)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Exit early when the transaction matches neither an output nor an id
|
|
134
|
+
if (!matches.length && !pendingIds.has(id)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
found.add(id);
|
|
139
|
+
|
|
140
|
+
pendingIds.delete(id);
|
|
141
|
+
|
|
142
|
+
// Add the transaction to found transactions
|
|
143
|
+
transactions.push({id, transaction: bufferAsHex(tx.bytes)});
|
|
94
144
|
}
|
|
95
145
|
|
|
96
146
|
// Exit early with error when there is extra data after the transactions
|
|
@@ -98,5 +148,5 @@ module.exports = ({block, outputs}) => {
|
|
|
98
148
|
throw new Error('UnexpectedDataAfterBlockTransactions');
|
|
99
149
|
}
|
|
100
150
|
|
|
101
|
-
return {outputs: hits};
|
|
151
|
+
return {transactions, outputs: hits};
|
|
102
152
|
};
|