solidity-scale-codec 1.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/package.json +1 -1
- package/src/Scale/Bytes.sol +1 -0
- package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCode.sol +5 -5
- package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCodeCodec.sol +34 -13
- package/src/Xcm/v5/BodyId/BodyIdCodec.sol +1 -1
- package/src/Xcm/v5/Junction/Junction.sol +22 -5
- package/src/Xcm/v5/Junction/JunctionCodec.sol +17 -6
- package/src/Xcm/v5/NetworkId/NetworkIdCodec.sol +7 -1
- package/src/Xcm/v5/PalletInfo/PalletInfo.sol +2 -2
- package/src/Xcm/v5/PalletInfo/PalletInfoCodec.sol +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
[Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Missing `GlobalConsensus` junction variant and related helper function in `Junction.sol` and `JunctionCodec.sol`. - [#5592427](https://github.com/LucasGrasso/solidity-scale-codec/commit/5592427)
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- Variant checking in `NetworkId`. - [#7e9309d](https://github.com/LucasGrasso/solidity-scale-codec/commit/7e9309d)
|
|
17
|
+
- Decoding of `Index` variant in `BodyId`. - [#75ead59](https://github.com/LucasGrasso/solidity-scale-codec/commit/75ead59)
|
|
18
|
+
- Incorrect encoding/decoding of `GeneralKey` junction variant. - [#4e0f795](https://github.com/LucasGrasso/solidity-scale-codec/commit/4e0f795)
|
|
19
|
+
|
|
20
|
+
## Version 2.0.0
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- Add `Bytes.sol` to barrel import - [#2dbc1ab](https://github.com/LucasGrasso/solidity-scale-codec/commit/2dbc1ab)
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
|
|
28
|
+
- Change all `uint8[]` to `bytes` for better ergonomics and consistency with typical byte handling in Solidity. - [#ab6c226](https://github.com/LucasGrasso/solidity-scale-codec/commit/ab6c226)
|
|
29
|
+
|
|
10
30
|
## Version 1.0.1
|
|
11
31
|
|
|
12
32
|
### Added
|
package/package.json
CHANGED
package/src/Scale/Bytes.sol
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {Bytes} from "../../../Scale/Bytes.sol";
|
|
5
5
|
import {MAX_DISPATCH_ERROR_LEN} from "../Constants.sol";
|
|
6
6
|
|
|
7
7
|
/// @notice Discriminant for the `MaybeErrorCode` enum.
|
|
@@ -25,13 +25,13 @@ struct MaybeErrorCode {
|
|
|
25
25
|
/// @notice Parameters for the `Error` variant.
|
|
26
26
|
struct ErrorParams {
|
|
27
27
|
/// @custom:property Dispatch error bytes.
|
|
28
|
-
|
|
28
|
+
bytes errorBytes;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/// @notice Parameters for the `TruncatedError` variant.
|
|
32
32
|
struct TruncatedErrorParams {
|
|
33
33
|
/// @custom:property Truncated dispatch error bytes.
|
|
34
|
-
|
|
34
|
+
bytes errorBytes;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// ============ Factory Functions ============
|
|
@@ -52,7 +52,7 @@ function error(ErrorParams memory params) pure returns (MaybeErrorCode memory) {
|
|
|
52
52
|
return
|
|
53
53
|
MaybeErrorCode({
|
|
54
54
|
variant: MaybeErrorCodeVariant.Error,
|
|
55
|
-
payload:
|
|
55
|
+
payload: Bytes.encode(params.errorBytes)
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -67,7 +67,7 @@ function truncatedError(
|
|
|
67
67
|
return
|
|
68
68
|
MaybeErrorCode({
|
|
69
69
|
variant: MaybeErrorCodeVariant.TruncatedError,
|
|
70
|
-
payload:
|
|
70
|
+
payload: Bytes.encode(params.errorBytes)
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import {Bytes} from "../../../Scale/Bytes.sol";
|
|
5
|
+
import {
|
|
6
|
+
MaybeErrorCode,
|
|
7
|
+
MaybeErrorCodeVariant,
|
|
8
|
+
ErrorParams,
|
|
9
|
+
TruncatedErrorParams
|
|
10
|
+
} from "./MaybeErrorCode.sol";
|
|
6
11
|
import {BytesUtils} from "../../../Utils/BytesUtils.sol";
|
|
7
12
|
|
|
8
13
|
/// @title SCALE Codec for XCM v3 `MaybeErrorCode`
|
|
@@ -38,7 +43,7 @@ library MaybeErrorCodeCodec {
|
|
|
38
43
|
variant == uint8(MaybeErrorCodeVariant.Error) ||
|
|
39
44
|
variant == uint8(MaybeErrorCodeVariant.TruncatedError)
|
|
40
45
|
) {
|
|
41
|
-
return 1 +
|
|
46
|
+
return 1 + Bytes.encodedSizeAt(data, offset + 1);
|
|
42
47
|
} else {
|
|
43
48
|
revert InvalidMaybeErrorCodeVariant(variant);
|
|
44
49
|
}
|
|
@@ -78,16 +83,32 @@ library MaybeErrorCodeCodec {
|
|
|
78
83
|
bytesRead = size;
|
|
79
84
|
}
|
|
80
85
|
|
|
81
|
-
/// @notice
|
|
82
|
-
/// @param me The `MaybeErrorCode` struct to
|
|
83
|
-
/// @return
|
|
84
|
-
function
|
|
86
|
+
/// @notice Extracts the `Error` parameters from a `MaybeErrorCode` struct. Reverts if the variant is not `Error`.
|
|
87
|
+
/// @param me The `MaybeErrorCode` struct to extract from. Must be of type `Error`.
|
|
88
|
+
/// @return params An `ErrorParams` struct containing the decoded dispatch error
|
|
89
|
+
function asError(
|
|
85
90
|
MaybeErrorCode memory me
|
|
86
|
-
) internal pure returns (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
) internal pure returns (ErrorParams memory params) {
|
|
92
|
+
_assertVariant(me, MaybeErrorCodeVariant.Error);
|
|
93
|
+
(params.errorBytes, ) = Bytes.decode(me.payload);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// @notice Extracts the `TruncatedError` parameters from a `MaybeErrorCode` struct. Reverts if the variant is not `TruncatedError`.
|
|
97
|
+
/// @param me The `MaybeErrorCode` struct to extract from. Must be of type `TruncatedError`.
|
|
98
|
+
/// @return params A `TruncatedErrorParams` struct containing the decoded truncated dispatch error
|
|
99
|
+
function asTruncatedError(
|
|
100
|
+
MaybeErrorCode memory me
|
|
101
|
+
) internal pure returns (TruncatedErrorParams memory params) {
|
|
102
|
+
_assertVariant(me, MaybeErrorCodeVariant.TruncatedError);
|
|
103
|
+
(params.errorBytes, ) = Bytes.decode(me.payload);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function _assertVariant(
|
|
107
|
+
MaybeErrorCode memory me,
|
|
108
|
+
MaybeErrorCodeVariant expected
|
|
109
|
+
) private pure {
|
|
110
|
+
if (me.variant != expected) {
|
|
111
|
+
revert InvalidMaybeErrorCodeVariant(uint8(me.variant));
|
|
112
|
+
}
|
|
92
113
|
}
|
|
93
114
|
}
|
|
@@ -50,7 +50,7 @@ library BodyIdCodec {
|
|
|
50
50
|
} else if (variant == BodyIdVariant.Moniker) {
|
|
51
51
|
payloadLength = 4;
|
|
52
52
|
} else if (variant == BodyIdVariant.Index) {
|
|
53
|
-
payloadLength =
|
|
53
|
+
payloadLength = Compact.encodedSizeAt(data, offset + 1);
|
|
54
54
|
} else {
|
|
55
55
|
revert InvalidBodyIdVariant(variantValue);
|
|
56
56
|
}
|
|
@@ -99,6 +99,12 @@ struct GeneralIndexParams {
|
|
|
99
99
|
uint128 index;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/// @notice Parameters for a `GlobalConsensus` junction.
|
|
103
|
+
struct GlobalConsensusParams {
|
|
104
|
+
/// @custom:property The `NetworkId` associated with the global consensus, See `NetworkId` struct for details.
|
|
105
|
+
NetworkId network;
|
|
106
|
+
}
|
|
107
|
+
|
|
102
108
|
/// @notice A single item in a path to describe the relative location of a consensus system. Each item assumes a pre-existing location as its context and is defined in terms of it.
|
|
103
109
|
struct Junction {
|
|
104
110
|
/// @custom:property variant The type of the junction, determining how to interpret the payload. See `JunctionVariant` enum for possible values.
|
|
@@ -203,11 +209,9 @@ function generalIndex(
|
|
|
203
209
|
function generalKey(
|
|
204
210
|
GeneralKeyParams memory params
|
|
205
211
|
) pure returns (Junction memory) {
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
params.key.length != params.length
|
|
210
|
-
) revert InvalidJunctionPayload();
|
|
212
|
+
if (params.length == 0 || params.length > 32) {
|
|
213
|
+
revert InvalidJunctionPayload();
|
|
214
|
+
}
|
|
211
215
|
return
|
|
212
216
|
Junction({
|
|
213
217
|
variant: JunctionVariant.GeneralKey,
|
|
@@ -221,6 +225,19 @@ function onlyChild() pure returns (Junction memory) {
|
|
|
221
225
|
return Junction({variant: JunctionVariant.OnlyChild, payload: ""});
|
|
222
226
|
}
|
|
223
227
|
|
|
228
|
+
/// @notice Creates a `GlobalConsensus` junction, which represents a global network capable of externalizing its own consensus.
|
|
229
|
+
/// @param params Parameters for the global consensus variant.
|
|
230
|
+
/// @return A `Junction` struct representing the `GlobalConsensus` junction, with the encoded network payload.
|
|
231
|
+
function globalConsensus(
|
|
232
|
+
GlobalConsensusParams memory params
|
|
233
|
+
) pure returns (Junction memory) {
|
|
234
|
+
return
|
|
235
|
+
Junction({
|
|
236
|
+
variant: JunctionVariant.GlobalConsensus,
|
|
237
|
+
payload: params.network.encode()
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
224
241
|
/// @notice Creates a `Plurality` junction with the specified body ID and body part.
|
|
225
242
|
/// @param params Parameters for the plurality variant.
|
|
226
243
|
/// @return A `Junction` struct representing the `Plurality` junction with the provided parameters.
|
|
@@ -20,7 +20,8 @@ import {
|
|
|
20
20
|
AccountKey20Params,
|
|
21
21
|
GeneralKeyParams,
|
|
22
22
|
PalletInstanceParams,
|
|
23
|
-
GeneralIndexParams
|
|
23
|
+
GeneralIndexParams,
|
|
24
|
+
GlobalConsensusParams
|
|
24
25
|
} from "./Junction.sol";
|
|
25
26
|
import {BytesUtils} from "../../../Utils/BytesUtils.sol";
|
|
26
27
|
import {UnsignedUtils} from "../../../Utils/UnsignedUtils.sol";
|
|
@@ -74,12 +75,12 @@ library JunctionCodec {
|
|
|
74
75
|
} else if (variant == uint8(JunctionVariant.GeneralKey)) {
|
|
75
76
|
if (offset >= data.length) revert InvalidJunctionLength();
|
|
76
77
|
uint8 length = uint8(data[offset]);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
variant == uint8(JunctionVariant.GlobalConsensus)
|
|
81
|
-
) {
|
|
78
|
+
if (length == 0 || length > 32) revert InvalidJunctionPayload();
|
|
79
|
+
payloadLength = 1 + 32; // 1 byte for the length + the fixed key bytes
|
|
80
|
+
} else if (variant == uint8(JunctionVariant.OnlyChild)) {
|
|
82
81
|
payloadLength = 0;
|
|
82
|
+
} else if (variant == uint8(JunctionVariant.GlobalConsensus)) {
|
|
83
|
+
payloadLength = NetworkIdCodec.encodedSizeAt(data, offset);
|
|
83
84
|
} else if (variant == uint8(JunctionVariant.Plurality)) {
|
|
84
85
|
uint256 innerLength = BodyIdCodec.encodedSizeAt(data, offset);
|
|
85
86
|
payloadLength =
|
|
@@ -242,6 +243,16 @@ library JunctionCodec {
|
|
|
242
243
|
);
|
|
243
244
|
}
|
|
244
245
|
|
|
246
|
+
/// @notice Decodes a `GlobalConsensus` junction from a given `Junction` struct, extracting the network information.
|
|
247
|
+
/// @param junction The `Junction` struct to decode, which should represent a `GlobalConsensus` junction.
|
|
248
|
+
/// @return params A `GlobalConsensusParams` struct containing the decoded
|
|
249
|
+
function asGlobalConsensus(
|
|
250
|
+
Junction memory junction
|
|
251
|
+
) internal pure returns (GlobalConsensusParams memory params) {
|
|
252
|
+
_assertVariant(junction, JunctionVariant.GlobalConsensus);
|
|
253
|
+
(params.network, ) = NetworkIdCodec.decode(junction.payload);
|
|
254
|
+
}
|
|
255
|
+
|
|
245
256
|
function _innerNetworkIdSize(
|
|
246
257
|
bytes memory data,
|
|
247
258
|
uint256 offset
|
|
@@ -49,7 +49,13 @@ library NetworkIdCodec {
|
|
|
49
49
|
payloadLen = 40; // 8 (u64) + 32 (bytes32)
|
|
50
50
|
} else if (variant == uint8(NetworkIdVariant.Ethereum)) {
|
|
51
51
|
payloadLen = Compact.encodedSizeAt(data, offset + 1);
|
|
52
|
-
} else if (
|
|
52
|
+
} else if (
|
|
53
|
+
variant == uint8(NetworkIdVariant.Polkadot) ||
|
|
54
|
+
variant == uint8(NetworkIdVariant.Kusama) ||
|
|
55
|
+
variant == uint8(NetworkIdVariant.BitcoinCore) ||
|
|
56
|
+
variant == uint8(NetworkIdVariant.BitcoinCash) ||
|
|
57
|
+
variant == uint8(NetworkIdVariant.PolkadotBulletin)
|
|
58
|
+
) {
|
|
53
59
|
payloadLen = 0; // Static variants
|
|
54
60
|
} else {
|
|
55
61
|
// Reserved or unknown types are invalid
|
|
@@ -6,9 +6,9 @@ struct PalletInfo {
|
|
|
6
6
|
/// @custom:property The index which identifies the pallet.
|
|
7
7
|
uint32 index;
|
|
8
8
|
/// @custom:property The name of the pallet. Max length is `MAX_PALLET_NAME_LEN`.
|
|
9
|
-
|
|
9
|
+
bytes name;
|
|
10
10
|
/// @custom:property The module name of the pallet. Max length is `MAX_PALLET_NAME_LEN`.
|
|
11
|
-
|
|
11
|
+
bytes moduleName;
|
|
12
12
|
/// @custom:property The major version of the crate which implements the pallet.
|
|
13
13
|
uint32 major;
|
|
14
14
|
/// @custom:property The minor version of the crate which implements the pallet.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {Compact} from "../../../Scale/Compact.sol";
|
|
5
|
-
import {
|
|
5
|
+
import {Bytes} from "../../../Scale/Bytes.sol";
|
|
6
6
|
import {MAX_PALLET_NAME_LEN} from "../Constants.sol";
|
|
7
7
|
import {PalletInfo} from "./PalletInfo.sol";
|
|
8
8
|
|
|
@@ -27,8 +27,8 @@ library PalletInfoCodec {
|
|
|
27
27
|
return
|
|
28
28
|
abi.encodePacked(
|
|
29
29
|
Compact.encode(info.index),
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
Bytes.encode(info.name),
|
|
31
|
+
Bytes.encode(info.moduleName),
|
|
32
32
|
Compact.encode(info.major),
|
|
33
33
|
Compact.encode(info.minor),
|
|
34
34
|
Compact.encode(info.patch)
|
|
@@ -46,8 +46,8 @@ library PalletInfoCodec {
|
|
|
46
46
|
if (data.length < offset + 1) revert InvalidPalletInfoLength();
|
|
47
47
|
uint256 pos = offset;
|
|
48
48
|
pos += Compact.encodedSizeAt(data, pos); // index
|
|
49
|
-
pos +=
|
|
50
|
-
pos +=
|
|
49
|
+
pos += Bytes.encodedSizeAt(data, pos); // name
|
|
50
|
+
pos += Bytes.encodedSizeAt(data, pos); // moduleName
|
|
51
51
|
pos += Compact.encodedSizeAt(data, pos); // major
|
|
52
52
|
pos += Compact.encodedSizeAt(data, pos); // minor
|
|
53
53
|
pos += Compact.encodedSizeAt(data, pos); // patch
|
|
@@ -80,12 +80,12 @@ library PalletInfoCodec {
|
|
|
80
80
|
(index, read) = Compact.decodeAt(data, pos);
|
|
81
81
|
pos += read;
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
(name, read) =
|
|
83
|
+
bytes memory name;
|
|
84
|
+
(name, read) = Bytes.decodeAt(data, pos);
|
|
85
85
|
pos += read;
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
(moduleName, read) =
|
|
87
|
+
bytes memory moduleName;
|
|
88
|
+
(moduleName, read) = Bytes.decodeAt(data, pos);
|
|
89
89
|
pos += read;
|
|
90
90
|
|
|
91
91
|
uint256 major;
|