solidity-scale-codec 0.1.0 → 0.1.2

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.
Files changed (43) hide show
  1. package/README.md +3 -1
  2. package/package.json +5 -2
  3. package/src/LittleEndian/LittleEndianU128.sol +49 -0
  4. package/src/LittleEndian/LittleEndianU16.sol +29 -0
  5. package/src/LittleEndian/LittleEndianU256.sol +101 -0
  6. package/src/LittleEndian/LittleEndianU32.sol +42 -0
  7. package/src/LittleEndian/LittleEndianU64.sol +45 -0
  8. package/src/LittleEndian/LittleEndianU8.sol +26 -0
  9. package/src/Scale/Array/BoolArr.sol +49 -35
  10. package/src/Scale/Array/I128Arr.sol +49 -35
  11. package/src/Scale/Array/I16Arr.sol +49 -35
  12. package/src/Scale/Array/I256Arr.sol +49 -35
  13. package/src/Scale/Array/I32Arr.sol +49 -35
  14. package/src/Scale/Array/I64Arr.sol +49 -35
  15. package/src/Scale/Array/I8Arr.sol +49 -35
  16. package/src/Scale/Array/U128Arr.sol +49 -35
  17. package/src/Scale/Array/U16Arr.sol +49 -35
  18. package/src/Scale/Array/U256Arr.sol +49 -35
  19. package/src/Scale/Array/U32Arr.sol +49 -35
  20. package/src/Scale/Array/U64Arr.sol +49 -35
  21. package/src/Scale/Array/U8Arr.sol +49 -35
  22. package/src/Scale/Array.sol +14 -15
  23. package/src/Scale/Bool/Bool.sol +1 -1
  24. package/src/Scale/Bool.sol +1 -1
  25. package/src/Scale/Compact/Compact.sol +89 -460
  26. package/src/Scale/Compact.sol +1 -1
  27. package/src/Scale/Signed/I128.sol +13 -6
  28. package/src/Scale/Signed/I16.sol +10 -5
  29. package/src/Scale/Signed/I256.sol +13 -6
  30. package/src/Scale/Signed/I32.sol +10 -5
  31. package/src/Scale/Signed/I64.sol +10 -5
  32. package/src/Scale/Signed/I8.sol +10 -5
  33. package/src/Scale/Signed.sol +7 -8
  34. package/src/Scale/Unsigned/U128.sol +15 -9
  35. package/src/Scale/Unsigned/U16.sol +15 -9
  36. package/src/Scale/Unsigned/U256.sol +15 -9
  37. package/src/Scale/Unsigned/U32.sol +15 -9
  38. package/src/Scale/Unsigned/U64.sol +15 -9
  39. package/src/Scale/Unsigned/U8.sol +13 -9
  40. package/src/Scale/Unsigned.sol +7 -8
  41. package/src/Scale/Compact/Compact.t.sol +0 -326
  42. package/src/Utils/LittleEndian/LittleEndian.sol +0 -354
  43. package/src/Utils/LittleEndian/LittleEndian.t.sol +0 -542
@@ -1,44 +1,58 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  import {Compact} from "../Compact/Compact.sol";
5
- import { U256 } from "../Unsigned.sol";
5
+ import {U256} from "../Unsigned.sol";
6
6
 
7
7
  /// @title Scale Codec for the `uint256[]` type.
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint256[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U256Arr {
11
- using U256 for uint256;
12
-
13
- /// @notice Encodes an `uint256[]` into SCALE format.
14
- function encode(uint256[] memory arr) internal pure returns (bytes memory) {
15
- bytes memory result = Compact.encode(arr.length);
16
- for (uint256 i = 0; i < arr.length; i++) {
17
- result = bytes.concat(result, arr[i].encode());
18
- }
19
- return result;
20
- }
21
-
22
- /// @notice Decodes an `uint256[]` from SCALE format.
23
- function decode(bytes memory data)
24
- internal pure returns (uint256[] memory arr, uint256 bytesRead)
25
- {
26
- return decodeAt(data, 0);
27
- }
28
-
29
- /// @notice Decodes an `uint256[]` from SCALE format.
30
- function decodeAt(bytes memory data, uint256 offset)
31
- internal pure returns (uint256[] memory arr, uint256 bytesRead)
32
- {
33
- (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
34
- uint256 pos = offset + compactBytes;
35
-
36
- arr = new uint256[](length);
37
- for (uint256 i = 0; i < length; i++) {
38
- arr[i] = U256.decodeAt(data, pos);
39
- pos += 32;
40
- }
41
-
42
- bytesRead = pos - offset;
43
- }
11
+ error InvalidU256ArrLenght();
12
+
13
+ using U256 for uint256;
14
+
15
+ /// @notice Encodes an `uint256[]` into SCALE format.
16
+ /// @param arr The array of `uint256` to encode.
17
+ /// @return SCALE-encoded byte sequence.
18
+ function encode(uint256[] memory arr) internal pure returns (bytes memory) {
19
+ bytes memory result = Compact.encode(arr.length);
20
+ for (uint256 i = 0; i < arr.length; ++i) {
21
+ result = bytes.concat(result, arr[i].encode());
22
+ }
23
+ return result;
24
+ }
25
+
26
+ /// @notice Decodes an `uint256[]` from SCALE format.
27
+ /// @param data The SCALE-encoded byte sequence.
28
+ /// @return arr The decoded array of `uint256`.
29
+ /// @return bytesRead The total number of bytes read during decoding.
30
+ function decode(
31
+ bytes memory data
32
+ ) internal pure returns (uint256[] memory arr, uint256 bytesRead) {
33
+ return decodeAt(data, 0);
34
+ }
35
+
36
+ /// @notice Decodes an `uint256[]` from SCALE format at the specified offset.
37
+ /// @param data The SCALE-encoded byte sequence.
38
+ /// @param offset The byte offset to start decoding from.
39
+ /// @return arr The decoded array of `uint256`.
40
+ /// @return bytesRead The total number of bytes read during decoding.
41
+ function decodeAt(
42
+ bytes memory data,
43
+ uint256 offset
44
+ ) internal pure returns (uint256[] memory arr, uint256 bytesRead) {
45
+ (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
+ uint256 pos = offset + compactBytes;
47
+
48
+ if (pos + (length * 32) > data.length) revert InvalidU256ArrLenght();
49
+
50
+ arr = new uint256[](length);
51
+ for (uint256 i = 0; i < length; ++i) {
52
+ arr[i] = U256.decodeAt(data, pos);
53
+ pos += 32;
54
+ }
55
+
56
+ bytesRead = pos - offset;
57
+ }
44
58
  }
@@ -1,44 +1,58 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  import {Compact} from "../Compact/Compact.sol";
5
- import { U32 } from "../Unsigned.sol";
5
+ import {U32} from "../Unsigned.sol";
6
6
 
7
7
  /// @title Scale Codec for the `uint32[]` type.
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint32[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U32Arr {
11
- using U32 for uint32;
12
-
13
- /// @notice Encodes an `uint32[]` into SCALE format.
14
- function encode(uint32[] memory arr) internal pure returns (bytes memory) {
15
- bytes memory result = Compact.encode(arr.length);
16
- for (uint256 i = 0; i < arr.length; i++) {
17
- result = bytes.concat(result, arr[i].encode());
18
- }
19
- return result;
20
- }
21
-
22
- /// @notice Decodes an `uint32[]` from SCALE format.
23
- function decode(bytes memory data)
24
- internal pure returns (uint32[] memory arr, uint256 bytesRead)
25
- {
26
- return decodeAt(data, 0);
27
- }
28
-
29
- /// @notice Decodes an `uint32[]` from SCALE format.
30
- function decodeAt(bytes memory data, uint256 offset)
31
- internal pure returns (uint32[] memory arr, uint256 bytesRead)
32
- {
33
- (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
34
- uint256 pos = offset + compactBytes;
35
-
36
- arr = new uint32[](length);
37
- for (uint256 i = 0; i < length; i++) {
38
- arr[i] = U32.decodeAt(data, pos);
39
- pos += 4;
40
- }
41
-
42
- bytesRead = pos - offset;
43
- }
11
+ error InvalidU32ArrLenght();
12
+
13
+ using U32 for uint32;
14
+
15
+ /// @notice Encodes an `uint32[]` into SCALE format.
16
+ /// @param arr The array of `uint32` to encode.
17
+ /// @return SCALE-encoded byte sequence.
18
+ function encode(uint32[] memory arr) internal pure returns (bytes memory) {
19
+ bytes memory result = Compact.encode(arr.length);
20
+ for (uint256 i = 0; i < arr.length; ++i) {
21
+ result = bytes.concat(result, arr[i].encode());
22
+ }
23
+ return result;
24
+ }
25
+
26
+ /// @notice Decodes an `uint32[]` from SCALE format.
27
+ /// @param data The SCALE-encoded byte sequence.
28
+ /// @return arr The decoded array of `uint32`.
29
+ /// @return bytesRead The total number of bytes read during decoding.
30
+ function decode(
31
+ bytes memory data
32
+ ) internal pure returns (uint32[] memory arr, uint256 bytesRead) {
33
+ return decodeAt(data, 0);
34
+ }
35
+
36
+ /// @notice Decodes an `uint32[]` from SCALE format at the specified offset.
37
+ /// @param data The SCALE-encoded byte sequence.
38
+ /// @param offset The byte offset to start decoding from.
39
+ /// @return arr The decoded array of `uint32`.
40
+ /// @return bytesRead The total number of bytes read during decoding.
41
+ function decodeAt(
42
+ bytes memory data,
43
+ uint256 offset
44
+ ) internal pure returns (uint32[] memory arr, uint256 bytesRead) {
45
+ (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
+ uint256 pos = offset + compactBytes;
47
+
48
+ if (pos + (length * 4) > data.length) revert InvalidU32ArrLenght();
49
+
50
+ arr = new uint32[](length);
51
+ for (uint256 i = 0; i < length; ++i) {
52
+ arr[i] = U32.decodeAt(data, pos);
53
+ pos += 4;
54
+ }
55
+
56
+ bytesRead = pos - offset;
57
+ }
44
58
  }
@@ -1,44 +1,58 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  import {Compact} from "../Compact/Compact.sol";
5
- import { U64 } from "../Unsigned.sol";
5
+ import {U64} from "../Unsigned.sol";
6
6
 
7
7
  /// @title Scale Codec for the `uint64[]` type.
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint64[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U64Arr {
11
- using U64 for uint64;
12
-
13
- /// @notice Encodes an `uint64[]` into SCALE format.
14
- function encode(uint64[] memory arr) internal pure returns (bytes memory) {
15
- bytes memory result = Compact.encode(arr.length);
16
- for (uint256 i = 0; i < arr.length; i++) {
17
- result = bytes.concat(result, arr[i].encode());
18
- }
19
- return result;
20
- }
21
-
22
- /// @notice Decodes an `uint64[]` from SCALE format.
23
- function decode(bytes memory data)
24
- internal pure returns (uint64[] memory arr, uint256 bytesRead)
25
- {
26
- return decodeAt(data, 0);
27
- }
28
-
29
- /// @notice Decodes an `uint64[]` from SCALE format.
30
- function decodeAt(bytes memory data, uint256 offset)
31
- internal pure returns (uint64[] memory arr, uint256 bytesRead)
32
- {
33
- (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
34
- uint256 pos = offset + compactBytes;
35
-
36
- arr = new uint64[](length);
37
- for (uint256 i = 0; i < length; i++) {
38
- arr[i] = U64.decodeAt(data, pos);
39
- pos += 8;
40
- }
41
-
42
- bytesRead = pos - offset;
43
- }
11
+ error InvalidU64ArrLenght();
12
+
13
+ using U64 for uint64;
14
+
15
+ /// @notice Encodes an `uint64[]` into SCALE format.
16
+ /// @param arr The array of `uint64` to encode.
17
+ /// @return SCALE-encoded byte sequence.
18
+ function encode(uint64[] memory arr) internal pure returns (bytes memory) {
19
+ bytes memory result = Compact.encode(arr.length);
20
+ for (uint256 i = 0; i < arr.length; ++i) {
21
+ result = bytes.concat(result, arr[i].encode());
22
+ }
23
+ return result;
24
+ }
25
+
26
+ /// @notice Decodes an `uint64[]` from SCALE format.
27
+ /// @param data The SCALE-encoded byte sequence.
28
+ /// @return arr The decoded array of `uint64`.
29
+ /// @return bytesRead The total number of bytes read during decoding.
30
+ function decode(
31
+ bytes memory data
32
+ ) internal pure returns (uint64[] memory arr, uint256 bytesRead) {
33
+ return decodeAt(data, 0);
34
+ }
35
+
36
+ /// @notice Decodes an `uint64[]` from SCALE format at the specified offset.
37
+ /// @param data The SCALE-encoded byte sequence.
38
+ /// @param offset The byte offset to start decoding from.
39
+ /// @return arr The decoded array of `uint64`.
40
+ /// @return bytesRead The total number of bytes read during decoding.
41
+ function decodeAt(
42
+ bytes memory data,
43
+ uint256 offset
44
+ ) internal pure returns (uint64[] memory arr, uint256 bytesRead) {
45
+ (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
+ uint256 pos = offset + compactBytes;
47
+
48
+ if (pos + (length * 8) > data.length) revert InvalidU64ArrLenght();
49
+
50
+ arr = new uint64[](length);
51
+ for (uint256 i = 0; i < length; ++i) {
52
+ arr[i] = U64.decodeAt(data, pos);
53
+ pos += 8;
54
+ }
55
+
56
+ bytesRead = pos - offset;
57
+ }
44
58
  }
@@ -1,44 +1,58 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  import {Compact} from "../Compact/Compact.sol";
5
- import { U8 } from "../Unsigned.sol";
5
+ import {U8} from "../Unsigned.sol";
6
6
 
7
7
  /// @title Scale Codec for the `uint8[]` type.
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint8[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U8Arr {
11
- using U8 for uint8;
12
-
13
- /// @notice Encodes an `uint8[]` into SCALE format.
14
- function encode(uint8[] memory arr) internal pure returns (bytes memory) {
15
- bytes memory result = Compact.encode(arr.length);
16
- for (uint256 i = 0; i < arr.length; i++) {
17
- result = bytes.concat(result, arr[i].encode());
18
- }
19
- return result;
20
- }
21
-
22
- /// @notice Decodes an `uint8[]` from SCALE format.
23
- function decode(bytes memory data)
24
- internal pure returns (uint8[] memory arr, uint256 bytesRead)
25
- {
26
- return decodeAt(data, 0);
27
- }
28
-
29
- /// @notice Decodes an `uint8[]` from SCALE format.
30
- function decodeAt(bytes memory data, uint256 offset)
31
- internal pure returns (uint8[] memory arr, uint256 bytesRead)
32
- {
33
- (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
34
- uint256 pos = offset + compactBytes;
35
-
36
- arr = new uint8[](length);
37
- for (uint256 i = 0; i < length; i++) {
38
- arr[i] = U8.decodeAt(data, pos);
39
- pos += 1;
40
- }
41
-
42
- bytesRead = pos - offset;
43
- }
11
+ error InvalidU8ArrLenght();
12
+
13
+ using U8 for uint8;
14
+
15
+ /// @notice Encodes an `uint8[]` into SCALE format.
16
+ /// @param arr The array of `uint8` to encode.
17
+ /// @return SCALE-encoded byte sequence.
18
+ function encode(uint8[] memory arr) internal pure returns (bytes memory) {
19
+ bytes memory result = Compact.encode(arr.length);
20
+ for (uint256 i = 0; i < arr.length; ++i) {
21
+ result = bytes.concat(result, arr[i].encode());
22
+ }
23
+ return result;
24
+ }
25
+
26
+ /// @notice Decodes an `uint8[]` from SCALE format.
27
+ /// @param data The SCALE-encoded byte sequence.
28
+ /// @return arr The decoded array of `uint8`.
29
+ /// @return bytesRead The total number of bytes read during decoding.
30
+ function decode(
31
+ bytes memory data
32
+ ) internal pure returns (uint8[] memory arr, uint256 bytesRead) {
33
+ return decodeAt(data, 0);
34
+ }
35
+
36
+ /// @notice Decodes an `uint8[]` from SCALE format at the specified offset.
37
+ /// @param data The SCALE-encoded byte sequence.
38
+ /// @param offset The byte offset to start decoding from.
39
+ /// @return arr The decoded array of `uint8`.
40
+ /// @return bytesRead The total number of bytes read during decoding.
41
+ function decodeAt(
42
+ bytes memory data,
43
+ uint256 offset
44
+ ) internal pure returns (uint8[] memory arr, uint256 bytesRead) {
45
+ (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
+ uint256 pos = offset + compactBytes;
47
+
48
+ if (pos + (length * 1) > data.length) revert InvalidU8ArrLenght();
49
+
50
+ arr = new uint8[](length);
51
+ for (uint256 i = 0; i < length; ++i) {
52
+ arr[i] = U8.decodeAt(data, pos);
53
+ pos += 1;
54
+ }
55
+
56
+ bytesRead = pos - offset;
57
+ }
44
58
  }
@@ -1,17 +1,16 @@
1
-
2
1
  // SPDX-License-Identifier: Apache-2.0
3
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
4
3
 
5
- import { U8Arr } from "./Array/U8Arr.sol";
6
- import { U16Arr } from "./Array/U16Arr.sol";
7
- import { U32Arr } from "./Array/U32Arr.sol";
8
- import { U64Arr } from "./Array/U64Arr.sol";
9
- import { U128Arr } from "./Array/U128Arr.sol";
10
- import { U256Arr } from "./Array/U256Arr.sol";
11
- import { I8Arr } from "./Array/I8Arr.sol";
12
- import { I16Arr } from "./Array/I16Arr.sol";
13
- import { I32Arr } from "./Array/I32Arr.sol";
14
- import { I64Arr } from "./Array/I64Arr.sol";
15
- import { I128Arr } from "./Array/I128Arr.sol";
16
- import { I256Arr } from "./Array/I256Arr.sol";
17
- import { BoolArr } from "./Array/BoolArr.sol";
4
+ import {U8Arr} from "./Array/U8Arr.sol";
5
+ import {U16Arr} from "./Array/U16Arr.sol";
6
+ import {U32Arr} from "./Array/U32Arr.sol";
7
+ import {U64Arr} from "./Array/U64Arr.sol";
8
+ import {U128Arr} from "./Array/U128Arr.sol";
9
+ import {U256Arr} from "./Array/U256Arr.sol";
10
+ import {I8Arr} from "./Array/I8Arr.sol";
11
+ import {I16Arr} from "./Array/I16Arr.sol";
12
+ import {I32Arr} from "./Array/I32Arr.sol";
13
+ import {I64Arr} from "./Array/I64Arr.sol";
14
+ import {I128Arr} from "./Array/I128Arr.sol";
15
+ import {I256Arr} from "./Array/I256Arr.sol";
16
+ import {BoolArr} from "./Array/BoolArr.sol";
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  /// @title Scale Codec for the `bool` type.
5
5
  /// @notice SCALE-compliant encoder/decoder for `bool`.
@@ -1,4 +1,4 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
4
  import {Bool} from "./Bool/Bool.sol";