@alexbosworth/blockchain 1.1.0 → 1.2.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
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const {createHash} = require('crypto');
|
|
2
|
+
|
|
3
|
+
const bufferAsHex = buffer => buffer.toString('hex');
|
|
4
|
+
const headerEndIndex = 160;
|
|
5
|
+
const headerStartIndex = 0;
|
|
6
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
7
|
+
const isHex = n => !!n && !(n.length % 2) && /^[0-9A-F]*$/i.test(n);
|
|
8
|
+
const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
9
|
+
|
|
10
|
+
/** Get an id for a block
|
|
11
|
+
|
|
12
|
+
{
|
|
13
|
+
block: <Hex Encoded Block Data String>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@throws
|
|
17
|
+
<Error>
|
|
18
|
+
|
|
19
|
+
@returns
|
|
20
|
+
{
|
|
21
|
+
id: <Block Hex String>
|
|
22
|
+
}
|
|
23
|
+
*/
|
|
24
|
+
module.exports = ({block}) => {
|
|
25
|
+
if (!isHex(block)) {
|
|
26
|
+
throw new Error('ExpectedHexEncodedBlockToGetIdFor');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const header = block.substring(headerStartIndex, headerEndIndex);
|
|
30
|
+
|
|
31
|
+
return {id: bufferAsHex(sha256(sha256(hexAsBuffer(header))).reverse())};
|
|
32
|
+
};
|
package/hashes/index.js
CHANGED
package/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
const {compactIntAsNumber} = require('./numbers');
|
|
2
|
+
const {idForBlock} = require('./hashes');
|
|
2
3
|
const {noLocktimeIdForTransaction} = require('./hashes');
|
|
3
4
|
const {numberAsCompactInt} = require('./numbers');
|
|
4
5
|
|
|
5
6
|
module.exports = {
|
|
6
7
|
compactIntAsNumber,
|
|
8
|
+
idForBlock,
|
|
7
9
|
noLocktimeIdForTransaction,
|
|
8
10
|
numberAsCompactInt,
|
|
9
11
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const {test} = require('@alexbosworth/tap');
|
|
2
|
+
|
|
3
|
+
const {idForBlock} = require('./../../');
|
|
4
|
+
|
|
5
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
6
|
+
|
|
7
|
+
const tests = [
|
|
8
|
+
{
|
|
9
|
+
args: {block: ''},
|
|
10
|
+
description: 'A hex encoded block is required',
|
|
11
|
+
error: 'ExpectedHexEncodedBlockToGetIdFor',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
args: {
|
|
15
|
+
block: '0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f6ebc0169bfbbf66c1f36b5f203e724805717d5e045579851d3a9cd26c89c6e460ca36364ffff7f200000000001020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0200f2052a0100000017a914a9974100aeee974a20cda9a2f545704a0ab54fdc870000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000',
|
|
16
|
+
},
|
|
17
|
+
description: 'A block id is returned',
|
|
18
|
+
expected: {
|
|
19
|
+
id: '77a525d242a5bb062983a852658c4b917d623e49ef1bdf66e94c9c79d2f2e8fe',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
25
|
+
return test(description, ({end, strictSame, throws}) => {
|
|
26
|
+
if (!!error) {
|
|
27
|
+
throws(() => idForBlock(args), new Error(error), 'Err');
|
|
28
|
+
} else {
|
|
29
|
+
const res = idForBlock(args);
|
|
30
|
+
|
|
31
|
+
strictSame(res, expected, 'Got expected result');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return end();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -11,20 +11,32 @@ const tests = [
|
|
|
11
11
|
error: 'ExpectedDataBufferToGetNoLocktimeIdForTransaction',
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
|
-
args: {
|
|
14
|
+
args: {
|
|
15
|
+
buffer: hexAsBuffer('02000000000103ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570200000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570100000000ffffffff0640420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6de91f62901000000225120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d49240420f00000000001600148729d17b2aa507ab19051a028384bc6e0ce25e455e368900000000002251200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b440420f0000000000160014d2d59a8a59f997cbc8888411010faf1658e0e3465e368900000000002251207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab2024830450221008ac71eff4d7e298941be012fc14f0ac9bf62ae6ffeac13522bb27b5b4108d3aa0220192a69ad6fdb86b1e09c7fdcaaafeb58d25060e44199c734dc0d7d385b5d800d0121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef75329602483045022100f9cde9adb00c0a6c62dae8604ca750039201288c0dafff952461da3caf05e3ae0220679c01f2518413951de3b62531b1cf36bb92562e3bd4197f0fa6e6e3e231272e0121027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d0247304402202550beec478845af2df929abf85708f9fcceaae31377f2e01d803e2acf7b426f022036c312b1e38ca333fe70aa37d3093387ac7486f08438eb8eed323699594468cb012102275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe00000000'),
|
|
16
|
+
},
|
|
15
17
|
description: 'No locktime id converted at zero locktime',
|
|
16
|
-
expected: {
|
|
18
|
+
expected: {
|
|
19
|
+
id: 'e0041a8488ac2a4b80da897ce88e95b054988c6907c0a9355354133fe86b90ef',
|
|
20
|
+
},
|
|
17
21
|
},
|
|
18
22
|
{
|
|
19
|
-
args: {
|
|
23
|
+
args: {
|
|
24
|
+
buffer: hexAsBuffer('02000000000103ae8caf3aad8861142597a29d6377f524a55f4542d5294c8292ce123a956d3e520000000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570200000000ffffffff9cbb1c5531a677b90c489d75a198a14f02d49e554ab9cdaa17bd956b127724570100000000ffffffff0640420f0000000000160014eaa9b76637b1ad340b6efadf773a73b53637d5b6de91f62901000000225120f4c82416bcb08422c195af995e1d248d1378d8b48dafa9f45bc213b83101d49240420f00000000001600148729d17b2aa507ab19051a028384bc6e0ce25e455e368900000000002251200249ccc5af06fa5642f12d42d2a34bfbb08688d54a9b99d07b98619b35df03b440420f0000000000160014d2d59a8a59f997cbc8888411010faf1658e0e3465e368900000000002251207febd720c78518b52aa1a2443823cc8f55e373910f616e112d5d7bd622fe1ab20247304402204fcc9416a9e908cba10595b15c7f455dcae87a7201f984fb79812de0ea7f562c02203acda03ae03a8c40ab314820e99ff09b77cf103131f73225cd228d74a4946e990121029943eaccd3987fa495a6b4f47f2fafeb0521e4e12f39498d9465a564ef7532960247304402204cb0db22579d5e2f9aafaaf9c99e3a96fe5f85b39065c9007506bfa686f311b402206f213de9e805010d84b64f31d65e0619114044dac6bb2c17aa7742f7e98d5f400121027326b48c9f2729597e328ab6d05f5af75866e1ffa203fadf78387a3b202ff80d02483045022100cc54a394ccb687a74f463412349a0f2defb7ec139819530ce06ff5392b4094f40220783fbad9afaa90459fa10f2af8a4225bc1027b6b06d7b886c75a836cad603017012102275a197f7ccfece19cf0532b068b6e38ceceda146e791875ecbdc55500bb7efe37000000'),
|
|
25
|
+
},
|
|
20
26
|
description: 'No locktime id converted at non zero locktime',
|
|
21
|
-
expected: {
|
|
27
|
+
expected: {
|
|
28
|
+
id: 'e0041a8488ac2a4b80da897ce88e95b054988c6907c0a9355354133fe86b90ef',
|
|
29
|
+
},
|
|
22
30
|
},
|
|
23
31
|
{
|
|
24
|
-
args: {
|
|
32
|
+
args: {
|
|
33
|
+
buffer: hexAsBuffer('010000000148c84b39d73d315d9688062359c93bb8bdff77c1cd5875d683f6fa80ae1839651a0000006a4730440220174ba43251b22da52004c0fff73a9f2137b6451c1d2086f0b6a7b7cea893c17f02200e7c36d341432e93abe80e69eed13d918197decdb0a904f245b9ece66ba01bec012102247749b00e239c8be840125258a0e76a82dd7af10615b7675a06d7c6f5c3125dffffffff019e7f05000000000017a914fe5feb6a940b2c302cad971e2c4173ebbbe0ec108700000000'),
|
|
34
|
+
},
|
|
25
35
|
description: 'A non-segwit transaction is converted',
|
|
26
|
-
expected: {
|
|
27
|
-
|
|
36
|
+
expected: {
|
|
37
|
+
id: '4bc29bad70d01853a167507beffd38a03db5da565de4645ac576824ce87c9934',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
28
40
|
];
|
|
29
41
|
|
|
30
42
|
tests.forEach(({args, description, error, expected}) => {
|