@alexbosworth/blockchain 1.5.0 → 1.6.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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## 1.6.0
4
+
5
+ - `previousBlockId`: Add method to get the previous block id from a block
6
+
3
7
  ## 1.5.0
4
8
 
5
9
  - `scriptAsScriptElements`: Convert an encoded script into script elements
package/README.md CHANGED
@@ -105,6 +105,22 @@ Convert a number to compact size integer serialization
105
105
  encoded: <Serialized Compact Integer Buffer Object>
106
106
  }
107
107
 
108
+ ### previousBlockId
109
+
110
+ Given a raw block, return the previous block id
111
+
112
+ {
113
+ block: <Hex Encoded Block String>
114
+ }
115
+
116
+ @throws
117
+ <Error>
118
+
119
+ @returns
120
+ {
121
+ previous: <Previous Block Id Hex String>
122
+ }
123
+
108
124
  ### scriptAsScriptElements
109
125
 
110
126
  Map a serialized script into an array of script elements
package/hashes/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const idForBlock = require('./id_for_block');
2
2
  const noLocktimeIdForTransaction = require('./no_locktime_id_for_transaction');
3
+ const previousBlockId = require('./previous_block_id');
3
4
 
4
- module.exports = {idForBlock, noLocktimeIdForTransaction};
5
+ module.exports = {idForBlock, noLocktimeIdForTransaction, previousBlockId};
@@ -0,0 +1,28 @@
1
+ const hashBytesHexCount = 32 * 2;
2
+ const reverseByteOrder = n => Buffer.from(n, 'hex').reverse().toString('hex');
3
+ const versionBytesHexCount = 4 * 2;
4
+
5
+ /** Given a raw block, return the previous block id
6
+
7
+ {
8
+ block: <Hex Encoded Block String>
9
+ }
10
+
11
+ @throws
12
+ <Error>
13
+
14
+ @returns
15
+ {
16
+ previous: <Previous Block Id Hex String>
17
+ }
18
+ */
19
+ module.exports = ({block}) => {
20
+ if (!block) {
21
+ throw new Error('ExpectedHexEncodedBlockToDerivePreviousBlockId');
22
+ }
23
+
24
+ const end = hashBytesHexCount + versionBytesHexCount;
25
+ const start = versionBytesHexCount;
26
+
27
+ return {previous: reverseByteOrder(block.substring(start, end))};
28
+ };
package/index.js CHANGED
@@ -3,6 +3,7 @@ const {componentsOfTransaction} = require('./transactions');
3
3
  const {idForBlock} = require('./hashes');
4
4
  const {noLocktimeIdForTransaction} = require('./hashes');
5
5
  const {numberAsCompactInt} = require('./numbers');
6
+ const {previousBlockId} = require('./hashes');
6
7
  const {scriptAsScriptElements} = require('./script');
7
8
  const {scriptElementsAsScript} = require('./script');
8
9
 
@@ -12,6 +13,7 @@ module.exports = {
12
13
  idForBlock,
13
14
  noLocktimeIdForTransaction,
14
15
  numberAsCompactInt,
16
+ previousBlockId,
15
17
  scriptAsScriptElements,
16
18
  scriptElementsAsScript,
17
19
  };
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "scripts": {
20
20
  "test": "npx nyc@latest node --experimental-test-coverage --test test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
21
21
  },
22
- "version": "1.5.0"
22
+ "version": "1.6.0"
23
23
  }
@@ -1,6 +1,6 @@
1
1
  const {deepStrictEqual} = require('node:assert').strict;
2
- const {throws} = require('node:assert').strict;
3
2
  const test = require('node:test');
3
+ const {throws} = require('node:assert').strict;
4
4
 
5
5
  const {idForBlock} = require('./../../');
6
6
 
@@ -0,0 +1,38 @@
1
+ const {deepEqual} = require('node:assert').strict;
2
+ const test = require('node:test');
3
+ const {throws} = require('node:assert').strict;
4
+
5
+ const {previousBlockId} = require('./../../');
6
+
7
+ const hexAsBuffer = hex => Buffer.from(hex, 'hex');
8
+
9
+ const tests = [
10
+ {
11
+ args: {block: ''},
12
+ description: 'A hex encoded block is required',
13
+ error: 'ExpectedHexEncodedBlockToDerivePreviousBlockId',
14
+ },
15
+ {
16
+ args: {
17
+ block: '00609c32519912209ce3196c09d7a409035874057780d4072fa31adc1f0000000000000090882aac112a7353ffc0cf13ebc8196620bc83c810b03bd5d6678b536a203c4b1a45d264f4422e193e7ea2ab01010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1a03ceb625012013090909200909200904631e00a3290000000000ffffffff02be40250000000000160014820d4a343a44e915c36494995c2899abe37418930000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000',
18
+ },
19
+ description: 'The previous block id is returned',
20
+ expected: {
21
+ previous: '000000000000001fdc1aa32f07d480770574580309a4d7096c19e39c20129951',
22
+ },
23
+ },
24
+ ];
25
+
26
+ tests.forEach(({args, description, error, expected}) => {
27
+ return test(description, (t, end) => {
28
+ if (!!error) {
29
+ throws(() => previousBlockId(args), new Error(error), 'Error returned');
30
+ } else {
31
+ const res = previousBlockId(args);
32
+
33
+ deepEqual(res, expected, 'Got expected result');
34
+ }
35
+
36
+ return end();
37
+ });
38
+ });