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 {I256} from "../Signed.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `int256[]` type.
|
|
8
8
|
/// @notice SCALE-compliant encoder/decoder for the `int256[]` type.
|
|
9
9
|
/// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
|
|
10
10
|
library I256Arr {
|
|
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 InvalidI256ArrLenght();
|
|
12
|
+
|
|
13
|
+
using I256 for int256;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `int256[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `int256` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(int256[] 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 `int256[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `int256`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (int256[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `int256[]` 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 `int256`.
|
|
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 (int256[] 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 InvalidI256ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new int256[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = I256.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 {I32} from "../Signed.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `int32[]` type.
|
|
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
|
-
|
|
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 InvalidI32ArrLenght();
|
|
12
|
+
|
|
13
|
+
using I32 for int32;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `int32[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `int32` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(int32[] 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 `int32[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `int32`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (int32[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `int32[]` 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 `int32`.
|
|
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 (int32[] 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 InvalidI32ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new int32[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = I32.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 {I64} from "../Signed.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `int64[]` type.
|
|
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
|
-
|
|
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 InvalidI64ArrLenght();
|
|
12
|
+
|
|
13
|
+
using I64 for int64;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `int64[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `int64` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(int64[] 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 `int64[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `int64`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (int64[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `int64[]` 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 `int64`.
|
|
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 (int64[] 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 InvalidI64ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new int64[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = I64.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 {I8} from "../Signed.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `int8[]` type.
|
|
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
|
-
|
|
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 InvalidI8ArrLenght();
|
|
12
|
+
|
|
13
|
+
using I8 for int8;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `int8[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `int8` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(int8[] 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 `int8[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `int8`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (int8[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `int8[]` 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 `int8`.
|
|
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 (int8[] 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 InvalidI8ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new int8[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = I8.decodeAt(data, pos);
|
|
53
|
+
pos += 1;
|
|
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 {U128} from "../Unsigned.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `uint128[]` type.
|
|
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
|
-
|
|
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 InvalidU128ArrLenght();
|
|
12
|
+
|
|
13
|
+
using U128 for uint128;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `uint128[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `uint128` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(uint128[] 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 `uint128[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `uint128`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (uint128[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `uint128[]` 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 `uint128`.
|
|
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 (uint128[] memory arr, uint256 bytesRead) {
|
|
45
|
+
(uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
|
|
46
|
+
uint256 pos = offset + compactBytes;
|
|
47
|
+
|
|
48
|
+
if (pos + (length * 16) > data.length) revert InvalidU128ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new uint128[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = U128.decodeAt(data, pos);
|
|
53
|
+
pos += 16;
|
|
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 {U16} from "../Unsigned.sol";
|
|
6
6
|
|
|
7
7
|
/// @title Scale Codec for the `uint16[]` type.
|
|
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
|
-
|
|
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 InvalidU16ArrLenght();
|
|
12
|
+
|
|
13
|
+
using U16 for uint16;
|
|
14
|
+
|
|
15
|
+
/// @notice Encodes an `uint16[]` into SCALE format.
|
|
16
|
+
/// @param arr The array of `uint16` to encode.
|
|
17
|
+
/// @return SCALE-encoded byte sequence.
|
|
18
|
+
function encode(uint16[] 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 `uint16[]` from SCALE format.
|
|
27
|
+
/// @param data The SCALE-encoded byte sequence.
|
|
28
|
+
/// @return arr The decoded array of `uint16`.
|
|
29
|
+
/// @return bytesRead The total number of bytes read during decoding.
|
|
30
|
+
function decode(
|
|
31
|
+
bytes memory data
|
|
32
|
+
) internal pure returns (uint16[] memory arr, uint256 bytesRead) {
|
|
33
|
+
return decodeAt(data, 0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @notice Decodes an `uint16[]` 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 `uint16`.
|
|
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 (uint16[] memory arr, uint256 bytesRead) {
|
|
45
|
+
(uint256 length, uint256 compactBytes) = Compact.decodeAt(data, offset);
|
|
46
|
+
uint256 pos = offset + compactBytes;
|
|
47
|
+
|
|
48
|
+
if (pos + (length * 2) > data.length) revert InvalidU16ArrLenght();
|
|
49
|
+
|
|
50
|
+
arr = new uint16[](length);
|
|
51
|
+
for (uint256 i = 0; i < length; ++i) {
|
|
52
|
+
arr[i] = U16.decodeAt(data, pos);
|
|
53
|
+
pos += 2;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
bytesRead = pos - offset;
|
|
57
|
+
}
|
|
44
58
|
}
|