@alexbosworth/blockchain 4.5.0 → 4.5.1

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.0
3
+ ## 4.5.1
4
4
 
5
5
  - `encodeBase58Address`: Add method to convert hash and version to b58 address
6
6
 
@@ -25,12 +25,22 @@ module.exports = ({encoded, start}) => {
25
25
 
26
26
  const offset = start || defaultOffset;
27
27
 
28
+ // Exit early with error when there is no size byte to read
29
+ if (encoded.length <= offset) {
30
+ throw new Error('ExpectedCompactIntegerSizeByteToConvertToNumber');
31
+ }
32
+
28
33
  // The total byte count of the compact integer is given by the first byte
29
34
  const size = encoded.readUInt8(offset);
30
35
 
31
36
  // Convert the first byte into the byte length of the encoding
32
37
  const bytes = byteLength(size);
33
38
 
39
+ // Exit early with error when the encoded number is cut short
40
+ if (encoded.length < offset + bytes) {
41
+ throw new Error('ExpectedFullEncodedCompactIntegerToConvertToNumber');
42
+ }
43
+
34
44
  switch (bytes) {
35
45
  case 1:
36
46
  return {bytes, number: size};
@@ -7,6 +7,7 @@ const limitBytes4 = 4294967295;
7
7
  const markerBytes2 = 253;
8
8
  const markerBytes4 = 254;
9
9
  const markerBytes8 = 255;
10
+ const minimumNumber = 0;
10
11
  const rightShift = 0;
11
12
 
12
13
  /** Convert a number to compact size integer serialization
@@ -28,6 +29,10 @@ module.exports = ({number}) => {
28
29
  throw new Error('ExpectedEncodeSafeNumberToEncodeAsCompactInteger');
29
30
  }
30
31
 
32
+ if (number < minimumNumber) {
33
+ throw new Error('ExpectedNonNegativeNumberToEncodeAsCompactInteger');
34
+ }
35
+
31
36
  const encoded = Buffer.alloc(byteLength(number));
32
37
 
33
38
  switch (encoded.length) {
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.0"
22
+ "version": "4.5.1"
23
23
  }
@@ -15,6 +15,16 @@ const tests = [
15
15
  description: 'Too large numbers are not supported',
16
16
  error: 'ExpectedSafeSizeEncodedCompactInteger',
17
17
  },
18
+ {
19
+ args: {encoded: '00', start: 1},
20
+ description: 'A size byte within the encoded bytes is expected',
21
+ error: 'ExpectedCompactIntegerSizeByteToConvertToNumber',
22
+ },
23
+ {
24
+ args: {encoded: 'fdff'},
25
+ description: 'A complete encoded number is expected',
26
+ error: 'ExpectedFullEncodedCompactIntegerToConvertToNumber',
27
+ },
18
28
  {
19
29
  args: {encoded: '00'},
20
30
  description: 'Smallest possible one byte number decoded',
@@ -10,6 +10,11 @@ const tests = [
10
10
  description: 'Fractional numbers are not allowed',
11
11
  error: 'ExpectedEncodeSafeNumberToEncodeAsCompactInteger',
12
12
  },
13
+ {
14
+ args: {number: -1},
15
+ description: 'Negative numbers are not allowed',
16
+ error: 'ExpectedNonNegativeNumberToEncodeAsCompactInteger',
17
+ },
13
18
  {
14
19
  args: {number: Number.MAX_SAFE_INTEGER + 1},
15
20
  description: 'Large numbers are not allowed',