@alexbosworth/blockchain 4.11.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 +5 -0
- package/README.md +6 -1
- package/package.json +1 -1
- package/transactions/query_transactions.js +69 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
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
|
+
|
|
3
8
|
## 4.11.0
|
|
4
9
|
|
|
5
10
|
- `decodeWif`: Add method to decode a WIF encoded private key
|
package/README.md
CHANGED
|
@@ -575,7 +575,8 @@ Find matching transactions within a block
|
|
|
575
575
|
|
|
576
576
|
{
|
|
577
577
|
block: <Hex Encoded Raw Block String>
|
|
578
|
-
|
|
578
|
+
[ids]: [<Transaction Id Hex String>]
|
|
579
|
+
[outputs]: [<Output Script Hex String>]
|
|
579
580
|
}
|
|
580
581
|
|
|
581
582
|
@throws
|
|
@@ -589,6 +590,10 @@ Find matching transactions within a block
|
|
|
589
590
|
transaction_id: <Transaction Id Hex String>
|
|
590
591
|
transaction_vout: <Transaction Output Index Number>
|
|
591
592
|
}]
|
|
593
|
+
transactions: [{
|
|
594
|
+
id: <Transaction Id Hex String>
|
|
595
|
+
transaction: <Hex Encoded Raw Transaction String>
|
|
596
|
+
}]
|
|
592
597
|
}
|
|
593
598
|
|
|
594
599
|
### scriptAsScriptElements
|
package/package.json
CHANGED
|
@@ -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
|
};
|