solidity-scale-codec 0.1.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.
Files changed (38) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +48 -0
  3. package/package.json +41 -0
  4. package/src/Scale/Array/BoolArr.sol +44 -0
  5. package/src/Scale/Array/I128Arr.sol +44 -0
  6. package/src/Scale/Array/I16Arr.sol +44 -0
  7. package/src/Scale/Array/I256Arr.sol +44 -0
  8. package/src/Scale/Array/I32Arr.sol +44 -0
  9. package/src/Scale/Array/I64Arr.sol +44 -0
  10. package/src/Scale/Array/I8Arr.sol +44 -0
  11. package/src/Scale/Array/U128Arr.sol +44 -0
  12. package/src/Scale/Array/U16Arr.sol +44 -0
  13. package/src/Scale/Array/U256Arr.sol +44 -0
  14. package/src/Scale/Array/U32Arr.sol +44 -0
  15. package/src/Scale/Array/U64Arr.sol +44 -0
  16. package/src/Scale/Array/U8Arr.sol +44 -0
  17. package/src/Scale/Array.sol +17 -0
  18. package/src/Scale/Bool/Bool.sol +40 -0
  19. package/src/Scale/Bool.sol +4 -0
  20. package/src/Scale/Compact/Compact.sol +543 -0
  21. package/src/Scale/Compact/Compact.t.sol +326 -0
  22. package/src/Scale/Compact.sol +4 -0
  23. package/src/Scale/Signed/I128.sol +39 -0
  24. package/src/Scale/Signed/I16.sol +39 -0
  25. package/src/Scale/Signed/I256.sol +39 -0
  26. package/src/Scale/Signed/I32.sol +39 -0
  27. package/src/Scale/Signed/I64.sol +39 -0
  28. package/src/Scale/Signed/I8.sol +39 -0
  29. package/src/Scale/Signed.sol +10 -0
  30. package/src/Scale/Unsigned/U128.sol +40 -0
  31. package/src/Scale/Unsigned/U16.sol +40 -0
  32. package/src/Scale/Unsigned/U256.sol +40 -0
  33. package/src/Scale/Unsigned/U32.sol +40 -0
  34. package/src/Scale/Unsigned/U64.sol +40 -0
  35. package/src/Scale/Unsigned/U8.sol +40 -0
  36. package/src/Scale/Unsigned.sol +10 -0
  37. package/src/Utils/LittleEndian/LittleEndian.sol +354 -0
  38. package/src/Utils/LittleEndian/LittleEndian.t.sol +542 -0
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Compact} from "../Compact/Compact.sol";
5
+ import { U16 } from "../Unsigned.sol";
6
+
7
+ /// @title Scale Codec for the `uint16[]` type.
8
+ /// @notice SCALE-compliant encoder/decoder for the `uint16[]` type.
9
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
+ library U16Arr {
11
+ using U16 for uint16;
12
+
13
+ /// @notice Encodes an `uint16[]` into SCALE format.
14
+ function encode(uint16[] 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 `uint16[]` from SCALE format.
23
+ function decode(bytes memory data)
24
+ internal pure returns (uint16[] memory arr, uint256 bytesRead)
25
+ {
26
+ return decodeAt(data, 0);
27
+ }
28
+
29
+ /// @notice Decodes an `uint16[]` from SCALE format.
30
+ function decodeAt(bytes memory data, uint256 offset)
31
+ internal pure returns (uint16[] memory arr, uint256 bytesRead)
32
+ {
33
+ (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
34
+ uint256 pos = offset + compactBytes;
35
+
36
+ arr = new uint16[](length);
37
+ for (uint256 i = 0; i < length; i++) {
38
+ arr[i] = U16.decodeAt(data, pos);
39
+ pos += 2;
40
+ }
41
+
42
+ bytesRead = pos - offset;
43
+ }
44
+ }
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Compact} from "../Compact/Compact.sol";
5
+ import { U256 } from "../Unsigned.sol";
6
+
7
+ /// @title Scale Codec for the `uint256[]` type.
8
+ /// @notice SCALE-compliant encoder/decoder for the `uint256[]` type.
9
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
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
+ }
44
+ }
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Compact} from "../Compact/Compact.sol";
5
+ import { U32 } from "../Unsigned.sol";
6
+
7
+ /// @title Scale Codec for the `uint32[]` type.
8
+ /// @notice SCALE-compliant encoder/decoder for the `uint32[]` type.
9
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
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
+ }
44
+ }
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Compact} from "../Compact/Compact.sol";
5
+ import { U64 } from "../Unsigned.sol";
6
+
7
+ /// @title Scale Codec for the `uint64[]` type.
8
+ /// @notice SCALE-compliant encoder/decoder for the `uint64[]` type.
9
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
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
+ }
44
+ }
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Compact} from "../Compact/Compact.sol";
5
+ import { U8 } from "../Unsigned.sol";
6
+
7
+ /// @title Scale Codec for the `uint8[]` type.
8
+ /// @notice SCALE-compliant encoder/decoder for the `uint8[]` type.
9
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
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
+ }
44
+ }
@@ -0,0 +1,17 @@
1
+
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ pragma solidity ^0.8.20;
4
+
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";
@@ -0,0 +1,40 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ /// @title Scale Codec for the `bool` type.
5
+ /// @notice SCALE-compliant encoder/decoder for `bool`.
6
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
+ library Bool {
8
+ error InvalidLength();
9
+
10
+ /// @notice Encodes a `bool` into SCALE format (1-byte).
11
+ /// @param value The boolean to encode.
12
+ /// @return SCALE-encoded byte sequence.
13
+ function encode(bool value) internal pure returns (bytes memory) {
14
+ return abi.encodePacked(value ? bytes1(0x01) : bytes1(0x00));
15
+ }
16
+
17
+ /// @notice Decodes SCALE-encoded bytes into a `bool`.
18
+ /// @param data The SCALE-encoded byte sequence.
19
+ /// @return The decoded boolean.
20
+ function decode(bytes memory data) internal pure returns (bool) {
21
+ if (data.length < 1) revert InvalidLength();
22
+ return data[0] != 0x00;
23
+ }
24
+
25
+ /// @notice Decodes a boolean at the specified offset.
26
+ /// @param data The SCALE-encoded byte sequence.
27
+ /// @param offset The byte offset to start decoding from.
28
+ /// @return value The decoded `bool`.
29
+ function decodeAt(
30
+ bytes memory data,
31
+ uint256 offset
32
+ ) internal pure returns (bool value) {
33
+ if (data.length < offset + 1) revert InvalidLength();
34
+ assembly {
35
+ value := iszero(
36
+ iszero(and(mload(add(add(data, 32), offset)), 0xFF))
37
+ )
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,4 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {Bool} from "./Bool/Bool.sol";