@alexbosworth/blockchain 4.5.2 → 4.5.3

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,6 +1,6 @@
1
1
  # Versions
2
2
 
3
- ## 4.5.2
3
+ ## 4.5.3
4
4
 
5
5
  - `encodeBase58Address`: Add method to convert hash and version to b58 address
6
6
 
@@ -0,0 +1,65 @@
1
+ const {alloc} = Buffer;
2
+ const byteCountUInt16 = 2;
3
+ const byteCountUInt32 = 4;
4
+ const {concat} = Buffer;
5
+ const {from} = Buffer;
6
+ const {isInteger} = Number;
7
+ const maxDirectPushByteLength = 75;
8
+ const maxUInt8 = 255;
9
+ const maxUInt16 = 65535;
10
+ const maxUInt32 = 4294967295;
11
+ const minimumCount = 0;
12
+ const OP_PUSHDATA1 = 76;
13
+ const OP_PUSHDATA2 = 77;
14
+ const OP_PUSHDATA4 = 78;
15
+
16
+ /** Encode the byte count of a script data push
17
+
18
+ A count up to 75 is a direct single byte, larger counts use OP_PUSHDATA
19
+
20
+ {
21
+ count: <Pushed Data Byte Count Number>
22
+ }
23
+
24
+ @throws
25
+ <Error>
26
+
27
+ @returns
28
+ {
29
+ encoded: <Push Byte Count Encoding Buffer Object>
30
+ }
31
+ */
32
+ module.exports = ({count}) => {
33
+ if (!isInteger(count) || count < minimumCount) {
34
+ throw new Error('ExpectedPushBytesCountNumberToEncode');
35
+ }
36
+
37
+ if (count > maxUInt32) {
38
+ throw new Error('UnexpectedlyLargePushBytesCountToEncode');
39
+ }
40
+
41
+ // Exit early when the byte count fits in a direct single byte push
42
+ if (count <= maxDirectPushByteLength) {
43
+ return {encoded: from([count])};
44
+ }
45
+
46
+ // Exit early when the byte count fits in an OP_PUSHDATA1 push
47
+ if (count <= maxUInt8) {
48
+ return {encoded: from([OP_PUSHDATA1, count])};
49
+ }
50
+
51
+ // Exit early when the byte count fits in an OP_PUSHDATA2 push
52
+ if (count <= maxUInt16) {
53
+ const encodedCount = alloc(byteCountUInt16);
54
+
55
+ encodedCount.writeUInt16LE(count);
56
+
57
+ return {encoded: concat([from([OP_PUSHDATA2]), encodedCount])};
58
+ }
59
+
60
+ const encodedCount = alloc(byteCountUInt32);
61
+
62
+ encodedCount.writeUInt32LE(count);
63
+
64
+ return {encoded: concat([from([OP_PUSHDATA4]), encodedCount])};
65
+ };
package/numbers/index.js CHANGED
@@ -1,4 +1,11 @@
1
1
  const compactIntAsNumber = require('./compact_int_as_number');
2
+ const encodePushBytesCount = require('./encode_push_bytes_count');
2
3
  const numberAsCompactInt = require('./number_as_compact_int');
4
+ const parsePushBytesCount = require('./parse_push_bytes_count');
3
5
 
4
- module.exports = {compactIntAsNumber, numberAsCompactInt};
6
+ module.exports = {
7
+ compactIntAsNumber,
8
+ encodePushBytesCount,
9
+ numberAsCompactInt,
10
+ parsePushBytesCount,
11
+ };
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "scripts": {
20
20
  "test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
21
21
  },
22
- "version": "4.5.2"
22
+ "version": "4.5.3"
23
23
  }
@@ -1,4 +1,4 @@
1
- const parsePushBytesCount = require('./parse_push_bytes_count');
1
+ const {parsePushBytesCount} = require('./../numbers');
2
2
 
3
3
  const hexAsBuffer = hex => Buffer.from(hex, 'hex');
4
4
  const isHex = n => n !== undefined && !(n.length%2) && /^[0-9A-F]*$/i.test(n);
@@ -1,11 +1,14 @@
1
- const {numberAsCompactInt} = require('./../numbers');
1
+ const {encodePushBytesCount} = require('./../numbers');
2
2
 
3
+ const {alloc} = Buffer;
3
4
  const {concat} = Buffer;
4
- const encode = number => numberAsCompactInt({number}).encoded;
5
- const flatten = arr => arr.reduce((sum, n) => Buffer.concat([sum, n]));
5
+ const encode = count => encodePushBytesCount({count}).encoded;
6
+ const flatten = arr => arr.reduce((s, n) => concat([s, n]), Buffer.alloc(0));
6
7
  const {from} = Buffer;
7
8
  const {isArray} = Array;
8
9
  const {isBuffer} = Buffer;
10
+ const {isInteger} = Number;
11
+ const isOpCode = code => code >= 0 && code <= 255;
9
12
 
10
13
  /** Map array of script buffer elements to an output script
11
14
 
@@ -26,6 +29,16 @@ module.exports = ({elements}) => {
26
29
  throw new Error('ExpectedArrayOfScriptElementsToEncodeScript');
27
30
  }
28
31
 
32
+ // Elements are either data pushes or op code numbers
33
+ if (elements.some(n => !isBuffer(n) && !isInteger(n))) {
34
+ throw new Error('ExpectedDataOrOpCodeScriptElementsToEncodeScript');
35
+ }
36
+
37
+ // Op code elements must be single byte values
38
+ if (elements.some(n => isInteger(n) && !isOpCode(n))) {
39
+ throw new Error('ExpectedSingleByteOpCodeElementToFormOutputScript');
40
+ }
41
+
29
42
  // Convert numbers to buffers and hex data to pushdata
30
43
  const fullScript = elements.map(element => {
31
44
  // Exit early when element is a data push
@@ -0,0 +1,82 @@
1
+ const {deepStrictEqual} = require('node:assert').strict;
2
+ const {throws} = require('node:assert').strict;
3
+ const test = require('node:test');
4
+
5
+ const {encodePushBytesCount} = require('./../../numbers');
6
+
7
+ const tests = [
8
+ {
9
+ args: {},
10
+ description: 'A count number is required',
11
+ error: 'ExpectedPushBytesCountNumberToEncode',
12
+ },
13
+ {
14
+ args: {count: -1},
15
+ description: 'A non-negative count number is required',
16
+ error: 'ExpectedPushBytesCountNumberToEncode',
17
+ },
18
+ {
19
+ args: {count: 1.5},
20
+ description: 'An integer count number is required',
21
+ error: 'ExpectedPushBytesCountNumberToEncode',
22
+ },
23
+ {
24
+ args: {count: 4294967296},
25
+ description: 'A count beyond OP_PUSHDATA4 size is rejected',
26
+ error: 'UnexpectedlyLargePushBytesCountToEncode',
27
+ },
28
+ {
29
+ args: {count: 0},
30
+ description: 'A zero count is encoded as a direct byte',
31
+ expected: {encoded: Buffer.from('00', 'hex')},
32
+ },
33
+ {
34
+ args: {count: 75},
35
+ description: 'The largest direct push count is encoded as a direct byte',
36
+ expected: {encoded: Buffer.from('4b', 'hex')},
37
+ },
38
+ {
39
+ args: {count: 76},
40
+ description: 'The smallest OP_PUSHDATA1 count is encoded',
41
+ expected: {encoded: Buffer.from('4c4c', 'hex')},
42
+ },
43
+ {
44
+ args: {count: 255},
45
+ description: 'The largest OP_PUSHDATA1 count is encoded',
46
+ expected: {encoded: Buffer.from('4cff', 'hex')},
47
+ },
48
+ {
49
+ args: {count: 256},
50
+ description: 'The smallest OP_PUSHDATA2 count is encoded',
51
+ expected: {encoded: Buffer.from('4d0001', 'hex')},
52
+ },
53
+ {
54
+ args: {count: 65535},
55
+ description: 'The largest OP_PUSHDATA2 count is encoded',
56
+ expected: {encoded: Buffer.from('4dffff', 'hex')},
57
+ },
58
+ {
59
+ args: {count: 65536},
60
+ description: 'The smallest OP_PUSHDATA4 count is encoded',
61
+ expected: {encoded: Buffer.from('4e00000100', 'hex')},
62
+ },
63
+ {
64
+ args: {count: 4294967295},
65
+ description: 'The largest OP_PUSHDATA4 count is encoded',
66
+ expected: {encoded: Buffer.from('4effffffff', 'hex')},
67
+ },
68
+ ];
69
+
70
+ tests.forEach(({args, description, error, expected}) => {
71
+ return test(description, (t, end) => {
72
+ if (!!error) {
73
+ throws(() => encodePushBytesCount(args), new Error(error), 'Got err');
74
+ } else {
75
+ const res = encodePushBytesCount(args);
76
+
77
+ deepStrictEqual(res, expected, 'Got expected result');
78
+ }
79
+
80
+ return end();
81
+ });
82
+ });
@@ -0,0 +1,67 @@
1
+ const {deepStrictEqual} = require('node:assert').strict;
2
+ const test = require('node:test');
3
+
4
+ const {parsePushBytesCount} = require('./../../numbers');
5
+
6
+ const hexAsBuffer = hex => Buffer.from(hex, 'hex');
7
+
8
+ const tests = [
9
+ {
10
+ args: {offset: 0, script: hexAsBuffer('4f00000000')},
11
+ description: 'An unsupported op code is not parsed as a push',
12
+ expected: undefined,
13
+ },
14
+ {
15
+ args: {offset: 0, script: hexAsBuffer('00')},
16
+ description: 'A zero byte count is parsed as a direct push',
17
+ expected: {bytes: hexAsBuffer('00'), count: 0},
18
+ },
19
+ {
20
+ args: {offset: 0, script: hexAsBuffer('4b' + 'ff'.repeat(75))},
21
+ description: 'The largest direct push byte count is parsed',
22
+ expected: {bytes: hexAsBuffer('4b'), count: 75},
23
+ },
24
+ {
25
+ args: {offset: 0, script: hexAsBuffer('4c4c' + 'ff'.repeat(76))},
26
+ description: 'An OP_PUSHDATA1 byte count is parsed',
27
+ expected: {bytes: hexAsBuffer('4c4c'), count: 76},
28
+ },
29
+ {
30
+ args: {offset: 0, script: hexAsBuffer('4c')},
31
+ description: 'An OP_PUSHDATA1 without a length byte is not parsed',
32
+ expected: {},
33
+ },
34
+ {
35
+ args: {offset: 0, script: hexAsBuffer('4d0001' + 'ff'.repeat(256))},
36
+ description: 'An OP_PUSHDATA2 byte count is parsed',
37
+ expected: {bytes: hexAsBuffer('4d0001'), count: 256},
38
+ },
39
+ {
40
+ args: {offset: 0, script: hexAsBuffer('4dff')},
41
+ description: 'An OP_PUSHDATA2 with a short length is not parsed',
42
+ expected: {},
43
+ },
44
+ {
45
+ args: {offset: 0, script: hexAsBuffer('4e70110100')},
46
+ description: 'An OP_PUSHDATA4 byte count is parsed',
47
+ expected: {bytes: hexAsBuffer('4e70110100'), count: 70000},
48
+ },
49
+ {
50
+ args: {offset: 0, script: hexAsBuffer('4e112233')},
51
+ description: 'An OP_PUSHDATA4 with a short length is not parsed',
52
+ expected: {},
53
+ },
54
+ {
55
+ args: {offset: 2, script: hexAsBuffer('ffff4c4c')},
56
+ description: 'A push byte count is parsed at an offset',
57
+ expected: {bytes: hexAsBuffer('4c4c'), count: 76},
58
+ },
59
+ ];
60
+
61
+ tests.forEach(({args, description, expected}) => {
62
+ return test(description, (t, end) => {
63
+ deepStrictEqual(parsePushBytesCount(args), expected, 'Got expected');
64
+
65
+ return end();
66
+ });
67
+ });
@@ -10,6 +10,46 @@ const tests = [
10
10
  description: 'An array of elements is required',
11
11
  error: 'ExpectedArrayOfScriptElementsToEncodeScript',
12
12
  },
13
+ {
14
+ args: {elements: []},
15
+ description: 'No elements are mapped to an empty script',
16
+ expected: {script: ''},
17
+ },
18
+ {
19
+ args: {elements: [Buffer.alloc(76)]},
20
+ description: 'A data element beyond direct push uses OP_PUSHDATA1',
21
+ expected: {script: '4c4c' + '00'.repeat(76)},
22
+ },
23
+ {
24
+ args: {elements: [Buffer.alloc(256)]},
25
+ description: 'A data element beyond one byte length uses OP_PUSHDATA2',
26
+ expected: {script: '4d0001' + '00'.repeat(256)},
27
+ },
28
+ {
29
+ args: {elements: [Buffer.alloc(70000)]},
30
+ description: 'A data element beyond two byte length uses OP_PUSHDATA4',
31
+ expected: {script: '4e70110100' + '00'.repeat(70000)},
32
+ },
33
+ {
34
+ args: {elements: [300, Buffer.alloc(20)]},
35
+ description: 'Op code elements above a single byte value are rejected',
36
+ error: 'ExpectedSingleByteOpCodeElementToFormOutputScript',
37
+ },
38
+ {
39
+ args: {elements: [-1, Buffer.alloc(20)]},
40
+ description: 'Negative op code elements are rejected',
41
+ error: 'ExpectedSingleByteOpCodeElementToFormOutputScript',
42
+ },
43
+ {
44
+ args: {elements: ['76', Buffer.alloc(20)]},
45
+ description: 'Non number op code elements are rejected',
46
+ error: 'ExpectedDataOrOpCodeScriptElementsToEncodeScript',
47
+ },
48
+ {
49
+ args: {elements: [0, Buffer.alloc(75)]},
50
+ description: 'A maximum length direct data push is encoded',
51
+ expected: {script: '004b' + '00'.repeat(75)},
52
+ },
13
53
  {
14
54
  args: {elements: [118, 169, Buffer.alloc(20), 136, 172]},
15
55
  description: 'Elements are mapped to an output script',
@@ -1,15 +0,0 @@
1
- const {strictEqual} = require('node:assert').strict;
2
- const test = require('node:test');
3
-
4
- const parsePushBytesCount = require('./../../script/parse_push_bytes_count');
5
-
6
- test('An unsupported op code is not parsed as a push', (t, end) => {
7
- const res = parsePushBytesCount({
8
- offset: 0,
9
- script: Buffer.from('4f00000000', 'hex'),
10
- });
11
-
12
- strictEqual(res, undefined, 'Got no push bytes count');
13
-
14
- return end();
15
- });
File without changes