solidity-scale-codec 0.1.1 → 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 (41) hide show
  1. package/README.md +2 -0
  2. package/package.json +3 -1
  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/Utils/LittleEndian/LittleEndian.sol +0 -354
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
- import { U16 } from "../Unsigned/U16.sol";
4
+ import {U16} from "../Unsigned/U16.sol";
5
5
 
6
6
  /// @title Scale Codec for the `int16` type.
7
7
  /// @notice SCALE-compliant encoder/decoder for the `int16` type.
8
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
9
  library I16 {
10
- /// @notice Encodes an `int16` into SCALE format (2-byte two's-complement little-endian).
10
+ error OffsetOutOfBounds();
11
+
12
+ /// @notice Encodes an `int16` into SCALE format (2-byte two's-complement little-endian).
11
13
  /// @param value The signed 16-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(int16 value) internal pure returns (bytes memory) {
@@ -29,11 +31,14 @@ library I16 {
29
31
  bytes memory data,
30
32
  uint256 offset
31
33
  ) internal pure returns (int16 value) {
34
+ // Safety Check is done in the unsigned decoder.
32
35
  return int16(U16.decodeAt(data, offset));
33
36
  }
34
37
 
35
- /// @notice Converts an int16 to little-endian bytes2 (two's complement)
38
+ /// @notice Converts an int16 to little-endian bytes2 (two's complement)
39
+ /// @param value The signed 16-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
36
41
  function toLittleEndian(int16 value) internal pure returns (bytes2 result) {
37
42
  return U16.toLittleEndian(uint16(value));
38
43
  }
39
- }
44
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
- import { U256 } from "../Unsigned/U256.sol";
4
+ import {U256} from "../Unsigned/U256.sol";
5
5
 
6
6
  /// @title Scale Codec for the `int256` type.
7
7
  /// @notice SCALE-compliant encoder/decoder for the `int256` type.
8
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
9
  library I256 {
10
- /// @notice Encodes an `int256` into SCALE format (32-byte two's-complement little-endian).
10
+ error OffsetOutOfBounds();
11
+
12
+ /// @notice Encodes an `int256` into SCALE format (32-byte two's-complement little-endian).
11
13
  /// @param value The signed 256-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(int256 value) internal pure returns (bytes memory) {
@@ -29,11 +31,16 @@ library I256 {
29
31
  bytes memory data,
30
32
  uint256 offset
31
33
  ) internal pure returns (int256 value) {
34
+ // Safety Check is done in the unsigned decoder.
32
35
  return int256(U256.decodeAt(data, offset));
33
36
  }
34
37
 
35
- /// @notice Converts an int256 to little-endian bytes32 (two's complement)
36
- function toLittleEndian(int256 value) internal pure returns (bytes32 result) {
38
+ /// @notice Converts an int256 to little-endian bytes32 (two's complement)
39
+ /// @param value The signed 256-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ int256 value
43
+ ) internal pure returns (bytes32 result) {
37
44
  return U256.toLittleEndian(uint256(value));
38
45
  }
39
- }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
- import { U32 } from "../Unsigned/U32.sol";
4
+ import {U32} from "../Unsigned/U32.sol";
5
5
 
6
6
  /// @title Scale Codec for the `int32` type.
7
7
  /// @notice SCALE-compliant encoder/decoder for the `int32` type.
8
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
9
  library I32 {
10
- /// @notice Encodes an `int32` into SCALE format (4-byte two's-complement little-endian).
10
+ error OffsetOutOfBounds();
11
+
12
+ /// @notice Encodes an `int32` into SCALE format (4-byte two's-complement little-endian).
11
13
  /// @param value The signed 32-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(int32 value) internal pure returns (bytes memory) {
@@ -29,11 +31,14 @@ library I32 {
29
31
  bytes memory data,
30
32
  uint256 offset
31
33
  ) internal pure returns (int32 value) {
34
+ // Safety Check is done in the unsigned decoder.
32
35
  return int32(U32.decodeAt(data, offset));
33
36
  }
34
37
 
35
- /// @notice Converts an int32 to little-endian bytes4 (two's complement)
38
+ /// @notice Converts an int32 to little-endian bytes4 (two's complement)
39
+ /// @param value The signed 32-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
36
41
  function toLittleEndian(int32 value) internal pure returns (bytes4 result) {
37
42
  return U32.toLittleEndian(uint32(value));
38
43
  }
39
- }
44
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
- import { U64 } from "../Unsigned/U64.sol";
4
+ import {U64} from "../Unsigned/U64.sol";
5
5
 
6
6
  /// @title Scale Codec for the `int64` type.
7
7
  /// @notice SCALE-compliant encoder/decoder for the `int64` type.
8
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
9
  library I64 {
10
- /// @notice Encodes an `int64` into SCALE format (8-byte two's-complement little-endian).
10
+ error OffsetOutOfBounds();
11
+
12
+ /// @notice Encodes an `int64` into SCALE format (8-byte two's-complement little-endian).
11
13
  /// @param value The signed 64-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(int64 value) internal pure returns (bytes memory) {
@@ -29,11 +31,14 @@ library I64 {
29
31
  bytes memory data,
30
32
  uint256 offset
31
33
  ) internal pure returns (int64 value) {
34
+ // Safety Check is done in the unsigned decoder.
32
35
  return int64(U64.decodeAt(data, offset));
33
36
  }
34
37
 
35
- /// @notice Converts an int64 to little-endian bytes8 (two's complement)
38
+ /// @notice Converts an int64 to little-endian bytes8 (two's complement)
39
+ /// @param value The signed 64-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
36
41
  function toLittleEndian(int64 value) internal pure returns (bytes8 result) {
37
42
  return U64.toLittleEndian(uint64(value));
38
43
  }
39
- }
44
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
3
 
4
- import { U8 } from "../Unsigned/U8.sol";
4
+ import {U8} from "../Unsigned/U8.sol";
5
5
 
6
6
  /// @title Scale Codec for the `int8` type.
7
7
  /// @notice SCALE-compliant encoder/decoder for the `int8` type.
8
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
9
9
  library I8 {
10
- /// @notice Encodes an `int8` into SCALE format (1-byte two's-complement little-endian).
10
+ error OffsetOutOfBounds();
11
+
12
+ /// @notice Encodes an `int8` into SCALE format (1-byte two's-complement little-endian).
11
13
  /// @param value The signed 8-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(int8 value) internal pure returns (bytes memory) {
@@ -29,11 +31,14 @@ library I8 {
29
31
  bytes memory data,
30
32
  uint256 offset
31
33
  ) internal pure returns (int8 value) {
34
+ // Safety Check is done in the unsigned decoder.
32
35
  return int8(U8.decodeAt(data, offset));
33
36
  }
34
37
 
35
- /// @notice Converts an int8 to little-endian bytes1 (two's complement)
38
+ /// @notice Converts an int8 to little-endian bytes1 (two's complement)
39
+ /// @param value The signed 8-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
36
41
  function toLittleEndian(int8 value) internal pure returns (bytes1 result) {
37
42
  return U8.toLittleEndian(uint8(value));
38
43
  }
39
- }
44
+ }
@@ -1,10 +1,9 @@
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 { I8 } from "./Signed/I8.sol";
6
- import { I16 } from "./Signed/I16.sol";
7
- import { I32 } from "./Signed/I32.sol";
8
- import { I64 } from "./Signed/I64.sol";
9
- import { I128 } from "./Signed/I128.sol";
10
- import { I256 } from "./Signed/I256.sol";
4
+ import {I8} from "./Signed/I8.sol";
5
+ import {I16} from "./Signed/I16.sol";
6
+ import {I32} from "./Signed/I32.sol";
7
+ import {I64} from "./Signed/I64.sol";
8
+ import {I128} from "./Signed/I128.sol";
9
+ import {I256} from "./Signed/I256.sol";
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU128} from "../../LittleEndian/LittleEndianU128.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint128` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint128` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U128 {
8
- error InvalidU128Length();
10
+ error InvalidU128Length();
9
11
 
10
- /// @notice Encodes an `uint128` into SCALE format (16-byte little-endian).
12
+ /// @notice Encodes an `uint128` into SCALE format (16-byte little-endian).
11
13
  /// @param value The unsigned 128-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint128 value) internal pure returns (bytes memory) {
@@ -30,11 +32,15 @@ library U128 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint128 value) {
32
34
  if (data.length < offset + 16) revert InvalidU128Length();
33
- assembly { let ptr := add(add(data, 32), offset) value := 0 for { let i := 0 } lt(i, 16) { i := add(i, 1) } { let b := and(mload(add(ptr, i)), 0xFF) value := or(value, shl(mul(i, 8), b)) } }
35
+ return LittleEndianU128.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint128` to little-endian bytes16
37
- function toLittleEndian(uint128 value) internal pure returns (bytes16 result) {
38
- assembly { let v := value v := or(shl(8, and(v, 0x00FF00FF00FF00FF00FF00FF00FF00FF)), shr(8, and(v, 0xFF00FF00FF00FF00FF00FF00FF00FF00))) v := or(shl(16, and(v, 0x0000FFFF0000FFFF0000FFFF0000FFFF)), shr(16, and(v, 0xFFFF0000FFFF0000FFFF0000FFFF0000))) v := or(shl(32, and(v, 0x00000000FFFFFFFF00000000FFFFFFFF)), shr(32, and(v, 0xFFFFFFFF00000000FFFFFFFF00000000))) v := or(shl(64, v), shr(64, v)) result := shl(128, v) }
39
- }
40
- }
38
+ /// @notice Converts an `uint128` to little-endian bytes16
39
+ /// @param value The unsigned 128-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ uint128 value
43
+ ) internal pure returns (bytes16 result) {
44
+ return LittleEndianU128.toLE(value);
45
+ }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU16} from "../../LittleEndian/LittleEndianU16.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint16` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint16` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U16 {
8
- error InvalidU16Length();
10
+ error InvalidU16Length();
9
11
 
10
- /// @notice Encodes an `uint16` into SCALE format (2-byte little-endian).
12
+ /// @notice Encodes an `uint16` into SCALE format (2-byte little-endian).
11
13
  /// @param value The unsigned 16-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint16 value) internal pure returns (bytes memory) {
@@ -30,11 +32,15 @@ library U16 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint16 value) {
32
34
  if (data.length < offset + 2) revert InvalidU16Length();
33
- assembly { let ptr := add(add(data, 32), offset) let b0 := and(mload(ptr), 0xFF) let b1 := and(mload(add(ptr, 1)), 0xFF) value := or(b0, shl(8, b1)) }
35
+ return LittleEndianU16.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint16` to little-endian bytes2
37
- function toLittleEndian(uint16 value) internal pure returns (bytes2 result) {
38
- return bytes2(uint16((value >> 8) | (value << 8)));
39
- }
40
- }
38
+ /// @notice Converts an `uint16` to little-endian bytes2
39
+ /// @param value The unsigned 16-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ uint16 value
43
+ ) internal pure returns (bytes2 result) {
44
+ return LittleEndianU16.toLE(value);
45
+ }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU256} from "../../LittleEndian/LittleEndianU256.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint256` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint256` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U256 {
8
- error InvalidU256Length();
10
+ error InvalidU256Length();
9
11
 
10
- /// @notice Encodes an `uint256` into SCALE format (32-byte little-endian).
12
+ /// @notice Encodes an `uint256` into SCALE format (32-byte little-endian).
11
13
  /// @param value The unsigned 256-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint256 value) internal pure returns (bytes memory) {
@@ -30,11 +32,15 @@ library U256 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint256 value) {
32
34
  if (data.length < offset + 32) revert InvalidU256Length();
33
- assembly { let ptr := add(add(data, 32), offset) value := 0 for { let i := 0 } lt(i, 32) { i := add(i, 1) } { let b := and(mload(add(ptr, i)), 0xFF) value := or(value, shl(mul(i, 8), b)) } }
35
+ return LittleEndianU256.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint256` to little-endian bytes32
37
- function toLittleEndian(uint256 value) internal pure returns (bytes32 result) {
38
- assembly { let v := value v := or(shl(8, and(v, 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF)), shr(8, and(v, 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00))) v := or(shl(16, and(v, 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF)), shr(16, and(v, 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000))) v := or(shl(32, and(v, 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF)), shr(32, and(v, 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000))) v := or(shl(64, and(v, 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF)), shr(64, and(v, 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000))) v := or(shl(128, v), shr(128, v)) result := v }
39
- }
40
- }
38
+ /// @notice Converts an `uint256` to little-endian bytes32
39
+ /// @param value The unsigned 256-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ uint256 value
43
+ ) internal pure returns (bytes32 result) {
44
+ return LittleEndianU256.toLE(value);
45
+ }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU32} from "../../LittleEndian/LittleEndianU32.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint32` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint32` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U32 {
8
- error InvalidU32Length();
10
+ error InvalidU32Length();
9
11
 
10
- /// @notice Encodes an `uint32` into SCALE format (4-byte little-endian).
12
+ /// @notice Encodes an `uint32` into SCALE format (4-byte little-endian).
11
13
  /// @param value The unsigned 32-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint32 value) internal pure returns (bytes memory) {
@@ -30,11 +32,15 @@ library U32 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint32 value) {
32
34
  if (data.length < offset + 4) revert InvalidU32Length();
33
- assembly { let ptr := add(add(data, 32), offset) let b0 := and(mload(ptr), 0xFF) let b1 := and(mload(add(ptr, 1)), 0xFF) let b2 := and(mload(add(ptr, 2)), 0xFF) let b3 := and(mload(add(ptr, 3)), 0xFF) value := or(or(b0, shl(8, b1)), or(shl(16, b2), shl(24, b3))) }
35
+ return LittleEndianU32.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint32` to little-endian bytes4
37
- function toLittleEndian(uint32 value) internal pure returns (bytes4 result) {
38
- assembly { let v := or(or(shl(24, and(value, 0xff)), shl(16, and(shr(8, value), 0xff))), or(shl(8, and(shr(16, value), 0xff)), and(shr(24, value), 0xff))) result := shl(224, v) }
39
- }
40
- }
38
+ /// @notice Converts an `uint32` to little-endian bytes4
39
+ /// @param value The unsigned 32-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ uint32 value
43
+ ) internal pure returns (bytes4 result) {
44
+ return LittleEndianU32.toLE(value);
45
+ }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU64} from "../../LittleEndian/LittleEndianU64.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint64` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint64` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U64 {
8
- error InvalidU64Length();
10
+ error InvalidU64Length();
9
11
 
10
- /// @notice Encodes an `uint64` into SCALE format (8-byte little-endian).
12
+ /// @notice Encodes an `uint64` into SCALE format (8-byte little-endian).
11
13
  /// @param value The unsigned 64-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint64 value) internal pure returns (bytes memory) {
@@ -30,11 +32,15 @@ library U64 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint64 value) {
32
34
  if (data.length < offset + 8) revert InvalidU64Length();
33
- assembly { let ptr := add(add(data, 32), offset) value := 0 for { let i := 0 } lt(i, 8) { i := add(i, 1) } { let b := and(mload(add(ptr, i)), 0xFF) value := or(value, shl(mul(i, 8), b)) } }
35
+ return LittleEndianU64.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint64` to little-endian bytes8
37
- function toLittleEndian(uint64 value) internal pure returns (bytes8 result) {
38
- assembly { let v := value v := or(shl(8, and(v, 0x00FF00FF00FF00FF)), shr(8, and(v, 0xFF00FF00FF00FF00))) v := or(shl(16, and(v, 0x0000FFFF0000FFFF)), shr(16, and(v, 0xFFFF0000FFFF0000))) v := or(shl(32, v), shr(32, v)) result := shl(192, v) }
39
- }
40
- }
38
+ /// @notice Converts an `uint64` to little-endian bytes8
39
+ /// @param value The unsigned 64-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(
42
+ uint64 value
43
+ ) internal pure returns (bytes8 result) {
44
+ return LittleEndianU64.toLE(value);
45
+ }
46
+ }
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {LittleEndianU8} from "../../LittleEndian/LittleEndianU8.sol";
3
5
 
4
6
  /// @title Scale Codec for the `uint8` type.
5
7
  /// @notice SCALE-compliant encoder/decoder for the `uint8` type.
6
8
  /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
7
9
  library U8 {
8
- error InvalidU8Length();
10
+ error InvalidU8Length();
9
11
 
10
- /// @notice Encodes an `uint8` into SCALE format (1-byte little-endian).
12
+ /// @notice Encodes an `uint8` into SCALE format (1-byte little-endian).
11
13
  /// @param value The unsigned 8-bit integer to encode.
12
14
  /// @return SCALE-encoded byte sequence.
13
15
  function encode(uint8 value) internal pure returns (bytes memory) {
@@ -30,11 +32,13 @@ library U8 {
30
32
  uint256 offset
31
33
  ) internal pure returns (uint8 value) {
32
34
  if (data.length < offset + 1) revert InvalidU8Length();
33
- assembly { value := and(mload(add(add(data, 32), offset)), 0xFF) }
35
+ return LittleEndianU8.fromLE(data, offset);
34
36
  }
35
37
 
36
- /// @notice Converts an `uint8` to little-endian bytes1
37
- function toLittleEndian(uint8 value) internal pure returns (bytes1 result) {
38
- return bytes1(value);
39
- }
40
- }
38
+ /// @notice Converts an `uint8` to little-endian bytes1
39
+ /// @param value The unsigned 8-bit integer to convert.
40
+ /// @return result Little-endian byte representation of the input value.
41
+ function toLittleEndian(uint8 value) internal pure returns (bytes1 result) {
42
+ return LittleEndianU8.toLE(value);
43
+ }
44
+ }
@@ -1,10 +1,9 @@
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 { U8 } from "./Unsigned/U8.sol";
6
- import { U16 } from "./Unsigned/U16.sol";
7
- import { U32 } from "./Unsigned/U32.sol";
8
- import { U64 } from "./Unsigned/U64.sol";
9
- import { U128 } from "./Unsigned/U128.sol";
10
- import { U256 } from "./Unsigned/U256.sol";
4
+ import {U8} from "./Unsigned/U8.sol";
5
+ import {U16} from "./Unsigned/U16.sol";
6
+ import {U32} from "./Unsigned/U32.sol";
7
+ import {U64} from "./Unsigned/U64.sol";
8
+ import {U128} from "./Unsigned/U128.sol";
9
+ import {U256} from "./Unsigned/U256.sol";