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.
- package/README.md +2 -0
- package/package.json +3 -1
- package/src/LittleEndian/LittleEndianU128.sol +49 -0
- package/src/LittleEndian/LittleEndianU16.sol +29 -0
- package/src/LittleEndian/LittleEndianU256.sol +101 -0
- package/src/LittleEndian/LittleEndianU32.sol +42 -0
- package/src/LittleEndian/LittleEndianU64.sol +45 -0
- package/src/LittleEndian/LittleEndianU8.sol +26 -0
- package/src/Scale/Array/BoolArr.sol +49 -35
- package/src/Scale/Array/I128Arr.sol +49 -35
- package/src/Scale/Array/I16Arr.sol +49 -35
- package/src/Scale/Array/I256Arr.sol +49 -35
- package/src/Scale/Array/I32Arr.sol +49 -35
- package/src/Scale/Array/I64Arr.sol +49 -35
- package/src/Scale/Array/I8Arr.sol +49 -35
- package/src/Scale/Array/U128Arr.sol +49 -35
- package/src/Scale/Array/U16Arr.sol +49 -35
- package/src/Scale/Array/U256Arr.sol +49 -35
- package/src/Scale/Array/U32Arr.sol +49 -35
- package/src/Scale/Array/U64Arr.sol +49 -35
- package/src/Scale/Array/U8Arr.sol +49 -35
- package/src/Scale/Array.sol +14 -15
- package/src/Scale/Bool/Bool.sol +1 -1
- package/src/Scale/Bool.sol +1 -1
- package/src/Scale/Compact/Compact.sol +89 -460
- package/src/Scale/Compact.sol +1 -1
- package/src/Scale/Signed/I128.sol +13 -6
- package/src/Scale/Signed/I16.sol +10 -5
- package/src/Scale/Signed/I256.sol +13 -6
- package/src/Scale/Signed/I32.sol +10 -5
- package/src/Scale/Signed/I64.sol +10 -5
- package/src/Scale/Signed/I8.sol +10 -5
- package/src/Scale/Signed.sol +7 -8
- package/src/Scale/Unsigned/U128.sol +15 -9
- package/src/Scale/Unsigned/U16.sol +15 -9
- package/src/Scale/Unsigned/U256.sol +15 -9
- package/src/Scale/Unsigned/U32.sol +15 -9
- package/src/Scale/Unsigned/U64.sol +15 -9
- package/src/Scale/Unsigned/U8.sol +13 -9
- package/src/Scale/Unsigned.sol +7 -8
- package/src/Utils/LittleEndian/LittleEndian.sol +0 -354
|
@@ -1,44 +1,58 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {Compact} from "../Compact/Compact.sol";
|
|
5
|
-
import {
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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.
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {Compact} from "../Compact/Compact.sol";
|
|
5
|
-
import {
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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.
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {Compact} from "../Compact/Compact.sol";
|
|
5
|
-
import {
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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.
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {Compact} from "../Compact/Compact.sol";
|
|
5
|
-
import {
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
}
|
package/src/Scale/Array.sol
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
|
|
2
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
4
3
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
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";
|
package/src/Scale/Bool/Bool.sol
CHANGED
package/src/Scale/Bool.sol
CHANGED