solidity-scale-codec 0.1.3 → 0.3.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 (104) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/DEFINITIONS.md +132 -0
  3. package/README.md +97 -1
  4. package/package.json +16 -4
  5. package/src/LittleEndian/LittleEndianU128.sol +4 -2
  6. package/src/LittleEndian/LittleEndianU16.sol +2 -2
  7. package/src/LittleEndian/LittleEndianU256.sol +4 -2
  8. package/src/LittleEndian/LittleEndianU32.sol +4 -2
  9. package/src/LittleEndian/LittleEndianU64.sol +4 -2
  10. package/src/LittleEndian/LittleEndianU8.sol +2 -2
  11. package/src/Scale/Address/Address.sol +55 -0
  12. package/src/Scale/Address.sol +4 -0
  13. package/src/Scale/Array/BoolArr.sol +16 -2
  14. package/src/Scale/Array/I128Arr.sol +16 -2
  15. package/src/Scale/Array/I16Arr.sol +16 -2
  16. package/src/Scale/Array/I256Arr.sol +16 -2
  17. package/src/Scale/Array/I32Arr.sol +16 -2
  18. package/src/Scale/Array/I64Arr.sol +16 -2
  19. package/src/Scale/Array/I8Arr.sol +16 -2
  20. package/src/Scale/Array/U128Arr.sol +16 -2
  21. package/src/Scale/Array/U16Arr.sol +16 -2
  22. package/src/Scale/Array/U256Arr.sol +16 -2
  23. package/src/Scale/Array/U32Arr.sol +16 -2
  24. package/src/Scale/Array/U64Arr.sol +16 -2
  25. package/src/Scale/Array/U8Arr.sol +16 -2
  26. package/src/Scale/Bool/Bool.sol +12 -0
  27. package/src/Scale/Bytes/Bytes.sol +63 -0
  28. package/src/Scale/Bytes/Bytes16.sol +53 -0
  29. package/src/Scale/Bytes/Bytes2.sol +53 -0
  30. package/src/Scale/Bytes/Bytes32.sol +53 -0
  31. package/src/Scale/Bytes/Bytes4.sol +53 -0
  32. package/src/Scale/Bytes/Bytes8.sol +53 -0
  33. package/src/Scale/Bytes.sol +8 -0
  34. package/src/Scale/Compact/Compact.sol +37 -3
  35. package/src/Scale/Signed/I128.sol +11 -0
  36. package/src/Scale/Signed/I16.sol +11 -0
  37. package/src/Scale/Signed/I256.sol +11 -0
  38. package/src/Scale/Signed/I32.sol +11 -0
  39. package/src/Scale/Signed/I64.sol +11 -0
  40. package/src/Scale/Signed/I8.sol +11 -0
  41. package/src/Scale/Unsigned/U128.sol +16 -2
  42. package/src/Scale/Unsigned/U16.sol +16 -2
  43. package/src/Scale/Unsigned/U256.sol +16 -2
  44. package/src/Scale/Unsigned/U32.sol +16 -2
  45. package/src/Scale/Unsigned/U64.sol +16 -2
  46. package/src/Scale/Unsigned/U8.sol +16 -2
  47. package/src/Xcm/Types/Version.sol +5 -0
  48. package/src/Xcm/VersionedXcm/VersionedXcm.sol +27 -0
  49. package/src/Xcm/VersionedXcm/VersionedXcmCodec.sol +70 -0
  50. package/src/Xcm/v3/Constants.sol +5 -0
  51. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCode.sol +73 -0
  52. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCodeCodec.sol +94 -0
  53. package/src/Xcm/v5/Asset/Asset.sol +13 -0
  54. package/src/Xcm/v5/Asset/AssetCodec.sol +72 -0
  55. package/src/Xcm/v5/AssetFilter/AssetFilter.sol +61 -0
  56. package/src/Xcm/v5/AssetFilter/AssetFilterCodec.sol +105 -0
  57. package/src/Xcm/v5/AssetId/AssetId.sol +10 -0
  58. package/src/Xcm/v5/AssetId/AssetIdCodec.sol +57 -0
  59. package/src/Xcm/v5/AssetInstance/AssetInstance.sol +139 -0
  60. package/src/Xcm/v5/AssetInstance/AssetInstanceCodec.sol +174 -0
  61. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilter.sol +82 -0
  62. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilterCodec.sol +95 -0
  63. package/src/Xcm/v5/Assets/Assets.sol +14 -0
  64. package/src/Xcm/v5/Assets/AssetsCodec.sol +94 -0
  65. package/src/Xcm/v5/BodyId/BodyId.sol +121 -0
  66. package/src/Xcm/v5/BodyId/BodyIdCodec.sol +128 -0
  67. package/src/Xcm/v5/BodyPart/BodyPart.sol +105 -0
  68. package/src/Xcm/v5/BodyPart/BodyPartCodec.sol +134 -0
  69. package/src/Xcm/v5/Constants.sol +15 -0
  70. package/src/Xcm/v5/Fungibility/Fungibility.sol +60 -0
  71. package/src/Xcm/v5/Fungibility/FungibilityCodec.sol +128 -0
  72. package/src/Xcm/v5/Hint/Hint.sol +40 -0
  73. package/src/Xcm/v5/Hint/HintCodec.sol +82 -0
  74. package/src/Xcm/v5/Instruction/Instruction.sol +1217 -0
  75. package/src/Xcm/v5/Instruction/InstructionCodec.sol +321 -0
  76. package/src/Xcm/v5/Junction/Junction.sol +258 -0
  77. package/src/Xcm/v5/Junction/JunctionCodec.sol +329 -0
  78. package/src/Xcm/v5/Junctions/Junctions.sol +12 -0
  79. package/src/Xcm/v5/Junctions/JunctionsCodec.sol +120 -0
  80. package/src/Xcm/v5/Location/Location.sol +12 -0
  81. package/src/Xcm/v5/Location/LocationCodec.sol +68 -0
  82. package/src/Xcm/v5/NetworkId/NetworkId.sol +115 -0
  83. package/src/Xcm/v5/NetworkId/NetworkIdCodec.sol +114 -0
  84. package/src/Xcm/v5/OriginKind/OriginKind.sol +14 -0
  85. package/src/Xcm/v5/OriginKind/OriginKindCodec.sol +66 -0
  86. package/src/Xcm/v5/PalletInfo/PalletInfo.sol +18 -0
  87. package/src/Xcm/v5/PalletInfo/PalletInfoCodec.sol +119 -0
  88. package/src/Xcm/v5/QueryResponseInfo/QueryResponseInfo.sol +16 -0
  89. package/src/Xcm/v5/QueryResponseInfo/QueryResponseInfoCodec.sol +91 -0
  90. package/src/Xcm/v5/Response/Response.sol +145 -0
  91. package/src/Xcm/v5/Response/ResponseCodec.sol +174 -0
  92. package/src/Xcm/v5/Types/QueryId.sol +5 -0
  93. package/src/Xcm/v5/Weight/Weight.sol +10 -0
  94. package/src/Xcm/v5/Weight/WeightCodec.sol +87 -0
  95. package/src/Xcm/v5/WeightLimit/WeightLimit.sol +48 -0
  96. package/src/Xcm/v5/WeightLimit/WeightLimitCodec.sol +88 -0
  97. package/src/Xcm/v5/WildAsset/WildAsset.sol +112 -0
  98. package/src/Xcm/v5/WildAsset/WildAssetCodec.sol +166 -0
  99. package/src/Xcm/v5/WildFungibility/WildFungibility.sol +10 -0
  100. package/src/Xcm/v5/WildFungibility/WildFungibilityCodec.sol +74 -0
  101. package/src/Xcm/v5/Xcm/Xcm.sol +27 -0
  102. package/src/Xcm/v5/Xcm/XcmCodec.sol +83 -0
  103. package/src/Xcm/v5/XcmError/XcmError.sol +122 -0
  104. package/src/Xcm/v5/XcmError/XcmErrorCodec.sol +85 -0
@@ -8,7 +8,7 @@ import {I32} from "../Signed.sol";
8
8
  /// @notice SCALE-compliant encoder/decoder for the `int32[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library I32Arr {
11
- error InvalidI32ArrLenght();
11
+ error InvalidI32ArrLength();
12
12
 
13
13
  using I32 for int32;
14
14
 
@@ -23,6 +23,20 @@ library I32Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `int32[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `int32[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `int32[]`.
29
+ /// @return The number of bytes that the `int32[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 4);
36
+ if (offset + totalSize > data.length) revert InvalidI32ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `int32[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `int32`.
@@ -45,7 +59,7 @@ library I32Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 4) > data.length) revert InvalidI32ArrLenght();
62
+ if (pos + (length * 4) > data.length) revert InvalidI32ArrLength();
49
63
 
50
64
  arr = new int32[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {I64} from "../Signed.sol";
8
8
  /// @notice SCALE-compliant encoder/decoder for the `int64[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library I64Arr {
11
- error InvalidI64ArrLenght();
11
+ error InvalidI64ArrLength();
12
12
 
13
13
  using I64 for int64;
14
14
 
@@ -23,6 +23,20 @@ library I64Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `int64[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `int64[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `int64[]`.
29
+ /// @return The number of bytes that the `int64[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 8);
36
+ if (offset + totalSize > data.length) revert InvalidI64ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `int64[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `int64`.
@@ -45,7 +59,7 @@ library I64Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 8) > data.length) revert InvalidI64ArrLenght();
62
+ if (pos + (length * 8) > data.length) revert InvalidI64ArrLength();
49
63
 
50
64
  arr = new int64[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {I8} from "../Signed.sol";
8
8
  /// @notice SCALE-compliant encoder/decoder for the `int8[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library I8Arr {
11
- error InvalidI8ArrLenght();
11
+ error InvalidI8ArrLength();
12
12
 
13
13
  using I8 for int8;
14
14
 
@@ -23,6 +23,20 @@ library I8Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `int8[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `int8[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `int8[]`.
29
+ /// @return The number of bytes that the `int8[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 1);
36
+ if (offset + totalSize > data.length) revert InvalidI8ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `int8[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `int8`.
@@ -45,7 +59,7 @@ library I8Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 1) > data.length) revert InvalidI8ArrLenght();
62
+ if (pos + (length * 1) > data.length) revert InvalidI8ArrLength();
49
63
 
50
64
  arr = new int8[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U128} from "../Unsigned.sol";
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint128[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U128Arr {
11
- error InvalidU128ArrLenght();
11
+ error InvalidU128ArrLength();
12
12
 
13
13
  using U128 for uint128;
14
14
 
@@ -23,6 +23,20 @@ library U128Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint128[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint128[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint128[]`.
29
+ /// @return The number of bytes that the `uint128[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 16);
36
+ if (offset + totalSize > data.length) revert InvalidU128ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint128[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint128`.
@@ -45,7 +59,7 @@ library U128Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 16) > data.length) revert InvalidU128ArrLenght();
62
+ if (pos + (length * 16) > data.length) revert InvalidU128ArrLength();
49
63
 
50
64
  arr = new uint128[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U16} from "../Unsigned.sol";
8
8
  /// @notice SCALE-compliant encoder/decoder for the `uint16[]` type.
9
9
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
10
10
  library U16Arr {
11
- error InvalidU16ArrLenght();
11
+ error InvalidU16ArrLength();
12
12
 
13
13
  using U16 for uint16;
14
14
 
@@ -23,6 +23,20 @@ library U16Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint16[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint16[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint16[]`.
29
+ /// @return The number of bytes that the `uint16[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 2);
36
+ if (offset + totalSize > data.length) revert InvalidU16ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint16[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint16`.
@@ -45,7 +59,7 @@ library U16Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 2) > data.length) revert InvalidU16ArrLenght();
62
+ if (pos + (length * 2) > data.length) revert InvalidU16ArrLength();
49
63
 
50
64
  arr = new uint16[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U256} from "../Unsigned.sol";
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
- error InvalidU256ArrLenght();
11
+ error InvalidU256ArrLength();
12
12
 
13
13
  using U256 for uint256;
14
14
 
@@ -23,6 +23,20 @@ library U256Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint256[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint256[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint256[]`.
29
+ /// @return The number of bytes that the `uint256[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 32);
36
+ if (offset + totalSize > data.length) revert InvalidU256ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint256[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint256`.
@@ -45,7 +59,7 @@ library U256Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 32) > data.length) revert InvalidU256ArrLenght();
62
+ if (pos + (length * 32) > data.length) revert InvalidU256ArrLength();
49
63
 
50
64
  arr = new uint256[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U32} from "../Unsigned.sol";
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
- error InvalidU32ArrLenght();
11
+ error InvalidU32ArrLength();
12
12
 
13
13
  using U32 for uint32;
14
14
 
@@ -23,6 +23,20 @@ library U32Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint32[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint32[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint32[]`.
29
+ /// @return The number of bytes that the `uint32[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 4);
36
+ if (offset + totalSize > data.length) revert InvalidU32ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint32[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint32`.
@@ -45,7 +59,7 @@ library U32Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 4) > data.length) revert InvalidU32ArrLenght();
62
+ if (pos + (length * 4) > data.length) revert InvalidU32ArrLength();
49
63
 
50
64
  arr = new uint32[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U64} from "../Unsigned.sol";
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
- error InvalidU64ArrLenght();
11
+ error InvalidU64ArrLength();
12
12
 
13
13
  using U64 for uint64;
14
14
 
@@ -23,6 +23,20 @@ library U64Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint64[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint64[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint64[]`.
29
+ /// @return The number of bytes that the `uint64[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 8);
36
+ if (offset + totalSize > data.length) revert InvalidU64ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint64[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint64`.
@@ -45,7 +59,7 @@ library U64Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 8) > data.length) revert InvalidU64ArrLenght();
62
+ if (pos + (length * 8) > data.length) revert InvalidU64ArrLength();
49
63
 
50
64
  arr = new uint64[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -8,7 +8,7 @@ import {U8} from "../Unsigned.sol";
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
- error InvalidU8ArrLenght();
11
+ error InvalidU8ArrLength();
12
12
 
13
13
  using U8 for uint8;
14
14
 
@@ -23,6 +23,20 @@ library U8Arr {
23
23
  return result;
24
24
  }
25
25
 
26
+ /// @notice Returns the number of bytes that a `uint8[]` would occupy when SCALE-encoded.
27
+ /// @param data The byte sequence containing the encoded `uint8[]`.
28
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `uint8[]`.
29
+ /// @return The number of bytes that the `uint8[]` would occupy when SCALE-encoded.
30
+ function encodedSizeAt(
31
+ bytes memory data,
32
+ uint256 offset
33
+ ) internal pure returns (uint256) {
34
+ (uint256 count, uint256 prefixSize) = Compact.decodeAt(data, offset);
35
+ uint256 totalSize = prefixSize + (count * 1);
36
+ if (offset + totalSize > data.length) revert InvalidU8ArrLength();
37
+ return totalSize;
38
+ }
39
+
26
40
  /// @notice Decodes an `uint8[]` from SCALE format.
27
41
  /// @param data The SCALE-encoded byte sequence.
28
42
  /// @return arr The decoded array of `uint8`.
@@ -45,7 +59,7 @@ library U8Arr {
45
59
  (uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
46
60
  uint256 pos = offset + compactBytes;
47
61
 
48
- if (pos + (length * 1) > data.length) revert InvalidU8ArrLenght();
62
+ if (pos + (length * 1) > data.length) revert InvalidU8ArrLength();
49
63
 
50
64
  arr = new uint8[](length);
51
65
  for (uint256 i = 0; i < length; ++i) {
@@ -14,6 +14,18 @@ library Bool {
14
14
  return abi.encodePacked(value ? bytes1(0x01) : bytes1(0x00));
15
15
  }
16
16
 
17
+ /// @notice Returns the number of bytes that a `bool` would occupy when SCALE-encoded.
18
+ /// @param data The byte sequence containing the encoded `bool`.
19
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bool`.
20
+ /// @return The number of bytes that the `bool` would occupy when SCALE-encoded.
21
+ function encodedSizeAt(
22
+ bytes memory data,
23
+ uint256 offset
24
+ ) internal pure returns (uint256) {
25
+ if (data.length < offset + 1) revert InvalidLength();
26
+ return 1;
27
+ }
28
+
17
29
  /// @notice Decodes SCALE-encoded bytes into a `bool`.
18
30
  /// @param data The SCALE-encoded byte sequence.
19
31
  /// @return The decoded boolean.
@@ -0,0 +1,63 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {Compact} from "../../Scale/Compact.sol";
5
+
6
+ /// @title Scale Codec for the `bytes` type.
7
+ /// @notice SCALE-compliant encoder/decoder for the `bytes` type.
8
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
+ library Bytes {
10
+ error InvalidBytesLength();
11
+
12
+ /// @notice Encodes an `bytes` into SCALE format.
13
+ /// @param value The `bytes` to encode.
14
+ /// @return SCALE-encoded byte sequence.
15
+ function encode(bytes memory value) internal pure returns (bytes memory) {
16
+ return abi.encodePacked(Compact.encode(value.length), value);
17
+ }
18
+
19
+ /// @notice Returns the number of bytes that a `bytes` would occupy when SCALE-encoded.
20
+ /// @param data The byte sequence containing the encoded `bytes`.
21
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bytes`.
22
+ /// @return The number of bytes that the `bytes` would occupy when SCALE-encoded.
23
+ function encodedSizeAt(
24
+ bytes memory data,
25
+ uint256 offset
26
+ ) internal pure returns (uint256) {
27
+ (uint256 length, uint256 lengthBytes) = Compact.decodeAt(data, offset);
28
+ if (data.length < offset + lengthBytes + length) {
29
+ revert InvalidBytesLength();
30
+ }
31
+ return lengthBytes + length;
32
+ }
33
+
34
+ /// @notice Decodes SCALE-encoded bytes into an `bytes`.
35
+ /// @param data The SCALE-encoded byte sequence.
36
+ /// @return value The decoded `bytes`.
37
+ /// @return bytesRead The number of bytes read from the input.
38
+ function decode(
39
+ bytes memory data
40
+ ) internal pure returns (bytes memory value, uint256 bytesRead) {
41
+ return decodeAt(data, 0);
42
+ }
43
+
44
+ /// @notice Decodes an `bytes` from SCALE format at the specified offset.
45
+ /// @param data The SCALE-encoded byte sequence.
46
+ /// @param offset The byte offset to start decoding from.
47
+ /// @return value The decoded `bytes`.
48
+ /// @return bytesRead The number of bytes read from the input.
49
+ function decodeAt(
50
+ bytes memory data,
51
+ uint256 offset
52
+ ) internal pure returns (bytes memory value, uint256 bytesRead) {
53
+ (uint256 length, uint256 lengthBytes) = Compact.decodeAt(data, offset);
54
+ if (data.length < offset + lengthBytes + length) {
55
+ revert InvalidBytesLength();
56
+ }
57
+ value = new bytes(length);
58
+ bytesRead = lengthBytes + length;
59
+ for (uint256 i = 0; i < length; i++) {
60
+ value[i] = data[offset + lengthBytes + i];
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,53 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ /// @title Scale Codec for the `bytes16` type.
5
+ /// @notice SCALE-compliant encoder/decoder for the `bytes16` type.
6
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
+ library Bytes16 {
8
+ error InvalidBytes16Length();
9
+
10
+ /// @notice Encodes an `bytes16` into SCALE format.
11
+ /// @param value The `bytes16` to encode.
12
+ /// @return SCALE-encoded byte sequence.
13
+ function encode(bytes16 value) internal pure returns (bytes memory) {
14
+ return abi.encodePacked(value);
15
+ }
16
+
17
+ /// @notice Returns the number of bytes that a `bytes16` would occupy when SCALE-encoded.
18
+ /// @param data The byte sequence containing the encoded `bytes16`.
19
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bytes16`.
20
+ /// @return The number of bytes that the `bytes16` would occupy when SCALE-encoded.
21
+ function encodedSizeAt(
22
+ bytes memory data,
23
+ uint256 offset
24
+ ) internal pure returns (uint256) {
25
+ if (data.length < offset + 16) {
26
+ revert InvalidBytes16Length();
27
+ }
28
+ return 16;
29
+ }
30
+
31
+ /// @notice Decodes SCALE-encoded bytes into an `bytes16`.
32
+ /// @param data The SCALE-encoded byte sequence.
33
+ /// @return The decoded `bytes16`.
34
+ function decode(bytes memory data) internal pure returns (bytes16) {
35
+ return decodeAt(data, 0);
36
+ }
37
+
38
+ /// @notice Decodes an `bytes16` from SCALE format at the specified offset.
39
+ /// @param data The SCALE-encoded byte sequence.
40
+ /// @param offset The byte offset to start decoding from.
41
+ /// @return value The decoded `bytes16`.
42
+ function decodeAt(
43
+ bytes memory data,
44
+ uint256 offset
45
+ ) internal pure returns (bytes16 value) {
46
+ if (data.length < offset + 16) {
47
+ revert InvalidBytes16Length();
48
+ }
49
+ assembly {
50
+ value := mload(add(add(data, 32), offset))
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,53 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ /// @title Scale Codec for the `bytes2` type.
5
+ /// @notice SCALE-compliant encoder/decoder for the `bytes2` type.
6
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
+ library Bytes2 {
8
+ error InvalidBytes2Length();
9
+
10
+ /// @notice Encodes an `bytes2` into SCALE format.
11
+ /// @param value The `bytes2` to encode.
12
+ /// @return SCALE-encoded byte sequence.
13
+ function encode(bytes2 value) internal pure returns (bytes memory) {
14
+ return abi.encodePacked(value);
15
+ }
16
+
17
+ /// @notice Returns the number of bytes that a `bytes2` would occupy when SCALE-encoded.
18
+ /// @param data The byte sequence containing the encoded `bytes2`.
19
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bytes2`.
20
+ /// @return The number of bytes that the `bytes2` would occupy when SCALE-encoded.
21
+ function encodedSizeAt(
22
+ bytes memory data,
23
+ uint256 offset
24
+ ) internal pure returns (uint256) {
25
+ if (data.length < offset + 2) {
26
+ revert InvalidBytes2Length();
27
+ }
28
+ return 2;
29
+ }
30
+
31
+ /// @notice Decodes SCALE-encoded bytes into an `bytes2`.
32
+ /// @param data The SCALE-encoded byte sequence.
33
+ /// @return The decoded `bytes2`.
34
+ function decode(bytes memory data) internal pure returns (bytes2) {
35
+ return decodeAt(data, 0);
36
+ }
37
+
38
+ /// @notice Decodes an `bytes2` from SCALE format at the specified offset.
39
+ /// @param data The SCALE-encoded byte sequence.
40
+ /// @param offset The byte offset to start decoding from.
41
+ /// @return value The decoded `bytes2`.
42
+ function decodeAt(
43
+ bytes memory data,
44
+ uint256 offset
45
+ ) internal pure returns (bytes2 value) {
46
+ if (data.length < offset + 2) {
47
+ revert InvalidBytes2Length();
48
+ }
49
+ assembly {
50
+ value := mload(add(add(data, 32), offset))
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,53 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ /// @title Scale Codec for the `bytes32` type.
5
+ /// @notice SCALE-compliant encoder/decoder for the `bytes32` type.
6
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
+ library Bytes32 {
8
+ error InvalidBytes32Length();
9
+
10
+ /// @notice Encodes an `bytes32` into SCALE format.
11
+ /// @param value The `bytes32` to encode.
12
+ /// @return SCALE-encoded byte sequence.
13
+ function encode(bytes32 value) internal pure returns (bytes memory) {
14
+ return abi.encodePacked(value);
15
+ }
16
+
17
+ /// @notice Returns the number of bytes that a `bytes32` would occupy when SCALE-encoded.
18
+ /// @param data The byte sequence containing the encoded `bytes32`.
19
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bytes32`.
20
+ /// @return The number of bytes that the `bytes32` would occupy when SCALE-encoded.
21
+ function encodedSizeAt(
22
+ bytes memory data,
23
+ uint256 offset
24
+ ) internal pure returns (uint256) {
25
+ if (data.length < offset + 32) {
26
+ revert InvalidBytes32Length();
27
+ }
28
+ return 32;
29
+ }
30
+
31
+ /// @notice Decodes SCALE-encoded bytes into an `bytes32`.
32
+ /// @param data The SCALE-encoded byte sequence.
33
+ /// @return The decoded `bytes32`.
34
+ function decode(bytes memory data) internal pure returns (bytes32) {
35
+ return decodeAt(data, 0);
36
+ }
37
+
38
+ /// @notice Decodes an `bytes32` from SCALE format at the specified offset.
39
+ /// @param data The SCALE-encoded byte sequence.
40
+ /// @param offset The byte offset to start decoding from.
41
+ /// @return value The decoded `bytes32`.
42
+ function decodeAt(
43
+ bytes memory data,
44
+ uint256 offset
45
+ ) internal pure returns (bytes32 value) {
46
+ if (data.length < offset + 32) {
47
+ revert InvalidBytes32Length();
48
+ }
49
+ assembly {
50
+ value := mload(add(add(data, 32), offset))
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,53 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ /// @title Scale Codec for the `bytes4` type.
5
+ /// @notice SCALE-compliant encoder/decoder for the `bytes4` type.
6
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
+ library Bytes4 {
8
+ error InvalidBytes4Length();
9
+
10
+ /// @notice Encodes an `bytes4` into SCALE format.
11
+ /// @param value The `bytes4` to encode.
12
+ /// @return SCALE-encoded byte sequence.
13
+ function encode(bytes4 value) internal pure returns (bytes memory) {
14
+ return abi.encodePacked(value);
15
+ }
16
+
17
+ /// @notice Returns the number of bytes that a `bytes4` would occupy when SCALE-encoded.
18
+ /// @param data The byte sequence containing the encoded `bytes4`.
19
+ /// @param offset The starting index in `data` from which to calculate the encoded size of the `bytes4`.
20
+ /// @return The number of bytes that the `bytes4` would occupy when SCALE-encoded.
21
+ function encodedSizeAt(
22
+ bytes memory data,
23
+ uint256 offset
24
+ ) internal pure returns (uint256) {
25
+ if (data.length < offset + 4) {
26
+ revert InvalidBytes4Length();
27
+ }
28
+ return 4;
29
+ }
30
+
31
+ /// @notice Decodes SCALE-encoded bytes into an `bytes4`.
32
+ /// @param data The SCALE-encoded byte sequence.
33
+ /// @return The decoded `bytes4`.
34
+ function decode(bytes memory data) internal pure returns (bytes4) {
35
+ return decodeAt(data, 0);
36
+ }
37
+
38
+ /// @notice Decodes an `bytes4` from SCALE format at the specified offset.
39
+ /// @param data The SCALE-encoded byte sequence.
40
+ /// @param offset The byte offset to start decoding from.
41
+ /// @return value The decoded `bytes4`.
42
+ function decodeAt(
43
+ bytes memory data,
44
+ uint256 offset
45
+ ) internal pure returns (bytes4 value) {
46
+ if (data.length < offset + 4) {
47
+ revert InvalidBytes4Length();
48
+ }
49
+ assembly {
50
+ value := mload(add(add(data, 32), offset))
51
+ }
52
+ }
53
+ }