solidity-scale-codec 0.3.3 → 1.0.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 (48) hide show
  1. package/CHANGELOG.md +36 -2
  2. package/README.md +1 -1
  3. package/package.json +2 -2
  4. package/src/Utils/BytesUtils.sol +26 -0
  5. package/src/Utils/UnsignedUtils.sol +52 -0
  6. package/src/Xcm/VersionedXcm/VersionedXcm.sol +4 -1
  7. package/src/Xcm/VersionedXcm/VersionedXcmCodec.sol +11 -0
  8. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCode.sol +8 -7
  9. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCodeCodec.sol +18 -19
  10. package/src/Xcm/v5/AssetFilter/AssetFilter.sol +8 -8
  11. package/src/Xcm/v5/AssetFilter/AssetFilterCodec.sol +47 -34
  12. package/src/Xcm/v5/AssetInstance/AssetInstance.sol +13 -12
  13. package/src/Xcm/v5/AssetInstance/AssetInstanceCodec.sol +53 -56
  14. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilter.sol +12 -12
  15. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilterCodec.sol +48 -20
  16. package/src/Xcm/v5/Assets/Assets.sol +16 -0
  17. package/src/Xcm/v5/Assets/AssetsCodec.sol +3 -3
  18. package/src/Xcm/v5/BodyId/BodyId.sol +24 -24
  19. package/src/Xcm/v5/BodyId/BodyIdCodec.sol +41 -48
  20. package/src/Xcm/v5/BodyPart/BodyPart.sol +44 -28
  21. package/src/Xcm/v5/BodyPart/BodyPartCodec.sol +70 -37
  22. package/src/Xcm/v5/Constants.sol +2 -2
  23. package/src/Xcm/v5/Fungibility/Fungibility.sol +6 -6
  24. package/src/Xcm/v5/Fungibility/FungibilityCodec.sol +40 -36
  25. package/src/Xcm/v5/Hint/Hint.sol +5 -5
  26. package/src/Xcm/v5/Hint/HintCodec.sol +24 -20
  27. package/src/Xcm/v5/Instruction/Instruction.sol +81 -55
  28. package/src/Xcm/v5/Instruction/InstructionCodec.sol +1047 -73
  29. package/src/Xcm/v5/Junction/Junction.sol +55 -69
  30. package/src/Xcm/v5/Junction/JunctionCodec.sol +72 -135
  31. package/src/Xcm/v5/Junctions/Junctions.sol +34 -0
  32. package/src/Xcm/v5/Junctions/JunctionsCodec.sol +0 -18
  33. package/src/Xcm/v5/Location/Location.sol +8 -0
  34. package/src/Xcm/v5/NetworkId/NetworkId.sol +15 -16
  35. package/src/Xcm/v5/NetworkId/NetworkIdCodec.sol +57 -34
  36. package/src/Xcm/v5/OriginKind/OriginKindCodec.sol +1 -1
  37. package/src/Xcm/v5/Response/Response.sol +49 -40
  38. package/src/Xcm/v5/Response/ResponseCodec.sol +64 -54
  39. package/src/Xcm/v5/Weight/WeightCodec.sol +3 -2
  40. package/src/Xcm/v5/WeightLimit/WeightLimit.sol +6 -6
  41. package/src/Xcm/v5/WeightLimit/WeightLimitCodec.sol +32 -23
  42. package/src/Xcm/v5/WildAsset/WildAsset.sol +17 -25
  43. package/src/Xcm/v5/WildAsset/WildAssetCodec.sol +35 -38
  44. package/src/Xcm/v5/WildFungibility/WildFungibilityCodec.sol +6 -6
  45. package/src/Xcm/v5/Xcm/XcmBuilder.sol +689 -0
  46. package/src/Xcm/v5/XcmError/XcmError.sol +7 -7
  47. package/src/Xcm/v5/XcmError/XcmErrorCodec.sol +25 -22
  48. package/DEFINITIONS.md +0 -132
@@ -4,7 +4,7 @@ pragma solidity ^0.8.28;
4
4
  import {LittleEndianU64} from "../../../LittleEndian/LittleEndianU64.sol";
5
5
 
6
6
  /// @notice Error codes used in XCM.
7
- enum XcmErrorType {
7
+ enum XcmErrorVariant {
8
8
  /// @custom:variant An arithmetic overflow happened.
9
9
  Overflow,
10
10
  /// @custom:variant The instruction is intentionally unsupported.
@@ -81,16 +81,16 @@ enum XcmErrorType {
81
81
 
82
82
  /// @notice XCM v5 error, containing the error type and an optional payload for `Trap`.
83
83
  struct XcmError {
84
- /// @custom:property The type of the error. See `XcmErrorType` enum for possible values.
85
- XcmErrorType eType;
86
- /// @custom:property The trap code. Only meaningful when `eType` is `Trap`.
84
+ /// @custom:property The type of the error. See `XcmErrorVariant` enum for possible values.
85
+ XcmErrorVariant variant;
86
+ /// @custom:property The trap code. Only meaningful when `variant` is `Trap`.
87
87
  bytes payload;
88
88
  }
89
89
 
90
90
  /// @notice Parameters for unit (payload-less) XCM errors.
91
91
  struct UnitParams {
92
92
  /// @custom:property The non-trap error discriminant.
93
- XcmErrorType eType;
93
+ XcmErrorVariant variant;
94
94
  }
95
95
 
96
96
  /// @notice Parameters for the `Trap` error variant.
@@ -107,7 +107,7 @@ using LittleEndianU64 for uint64;
107
107
  /// @param params Parameters for the unit error.
108
108
  /// @return The `XcmError` struct.
109
109
  function unit(UnitParams memory params) pure returns (XcmError memory) {
110
- return XcmError({eType: params.eType, payload: ""});
110
+ return XcmError({variant: params.variant, payload: ""});
111
111
  }
112
112
 
113
113
  /// @notice Creates a `Trap` error with the given u64 code.
@@ -116,7 +116,7 @@ function unit(UnitParams memory params) pure returns (XcmError memory) {
116
116
  function trap(TrapParams memory params) pure returns (XcmError memory) {
117
117
  return
118
118
  XcmError({
119
- eType: XcmErrorType.Trap,
119
+ variant: XcmErrorVariant.Trap,
120
120
  payload: abi.encodePacked(params.code.toLittleEndian())
121
121
  });
122
122
  }
@@ -2,7 +2,8 @@
2
2
  pragma solidity ^0.8.28;
3
3
 
4
4
  import {LittleEndianU64} from "../../../LittleEndian/LittleEndianU64.sol";
5
- import {XcmError, XcmErrorType} from "./XcmError.sol";
5
+ import {XcmError, XcmErrorVariant, TrapParams} from "./XcmError.sol";
6
+ import {BytesUtils} from "../../../Utils/BytesUtils.sol";
6
7
 
7
8
  /// @title SCALE Codec for XCM v5 `Error`
8
9
  /// @notice SCALE-compliant encoder/decoder for the XCM v5 `Error` type.
@@ -10,14 +11,14 @@ import {XcmError, XcmErrorType} from "./XcmError.sol";
10
11
  /// @dev XCM v5 reference: https://paritytech.github.io/polkadot-sdk/master/staging_xcm/v5/traits.rs.html
11
12
  library XcmErrorCodec {
12
13
  error InvalidXcmErrorLength();
13
- error InvalidXcmError(uint8 e);
14
+ error InvalidXcmErrorVariant(uint8 variant);
14
15
  error InvalidXcmErrorPayload();
15
16
 
16
17
  /// @notice Encodes an `XcmError` into SCALE bytes.
17
18
  /// @param e The `XcmError` struct to encode.
18
19
  /// @return SCALE-encoded bytes representing the error.
19
20
  function encode(XcmError memory e) internal pure returns (bytes memory) {
20
- return abi.encodePacked(uint8(e.eType), e.payload);
21
+ return abi.encodePacked(uint8(e.variant), e.payload);
21
22
  }
22
23
 
23
24
  /// @notice Returns the number of bytes that an `XcmError` would occupy when SCALE-encoded.
@@ -30,9 +31,9 @@ library XcmErrorCodec {
30
31
  ) internal pure returns (uint256) {
31
32
  if (data.length < offset + 1) revert InvalidXcmErrorLength();
32
33
  uint8 raw = uint8(data[offset]);
33
- if (raw > uint8(XcmErrorType.TooManyAssets))
34
- revert InvalidXcmError(raw);
35
- if (raw == uint8(XcmErrorType.Trap)) {
34
+ if (raw > uint8(type(XcmErrorVariant).max))
35
+ revert InvalidXcmErrorVariant(raw);
36
+ if (raw == uint8(XcmErrorVariant.Trap)) {
36
37
  if (data.length < offset + 9) revert InvalidXcmErrorLength();
37
38
  return 1 + 8; // 1 byte for the error type and 8 bytes for the u64 trap code
38
39
  }
@@ -58,28 +59,30 @@ library XcmErrorCodec {
58
59
  bytes memory data,
59
60
  uint256 offset
60
61
  ) internal pure returns (XcmError memory e, uint256 bytesRead) {
61
- if (data.length < offset + 1) revert InvalidXcmErrorLength();
62
- uint8 raw = uint8(data[offset]);
63
- if (raw > uint8(XcmErrorType.TooManyAssets))
64
- revert InvalidXcmError(raw);
65
62
  uint256 size = encodedSizeAt(data, offset);
63
+ uint8 raw = uint8(data[offset]);
66
64
  uint256 payloadLength = size - 1;
67
- bytes memory payload = new bytes(payloadLength);
68
- for (uint256 i = 0; i < payloadLength; ++i) {
69
- payload[i] = data[offset + 1 + i];
70
- }
71
- e = XcmError({eType: XcmErrorType(raw), payload: payload});
65
+ bytes memory payload = BytesUtils.copy(data, offset + 1, payloadLength);
66
+ e = XcmError({variant: XcmErrorVariant(raw), payload: payload});
72
67
  bytesRead = size;
73
68
  }
74
69
 
75
70
  /// @notice Decodes the trap code from a `Trap` error.
76
71
  /// @param e The `XcmError` struct to decode, which must be of type `Trap`.
77
- /// @return code The decoded u64 trap code.
78
- function asTrap(XcmError memory e) internal pure returns (uint64 code) {
79
- if (e.eType != XcmErrorType.Trap)
80
- revert InvalidXcmError(uint8(e.eType));
81
- if (e.payload.length != 8) revert InvalidXcmErrorPayload();
82
- uint256 decoded = LittleEndianU64.fromLittleEndian(e.payload, 0);
83
- code = uint64(decoded);
72
+ /// @return params A `TrapParams` struct containing the decoded trap code.
73
+ function asTrap(
74
+ XcmError memory e
75
+ ) internal pure returns (TrapParams memory params) {
76
+ _assertVariant(e, XcmErrorVariant.Trap);
77
+ params.code = LittleEndianU64.fromLittleEndian(e.payload, 0);
78
+ }
79
+
80
+ function _assertVariant(
81
+ XcmError memory e,
82
+ XcmErrorVariant expected
83
+ ) internal pure {
84
+ if (e.variant != expected) {
85
+ revert InvalidXcmErrorVariant(uint8(e.variant));
86
+ }
84
87
  }
85
88
  }
package/DEFINITIONS.md DELETED
@@ -1,132 +0,0 @@
1
- > Note: These definitions were taken from https://github.com/w3f/polkadot-spec/blob/main/docs. They are provided here for reference. All credits to respective authors.
2
-
3
- # Notation
4
-
5
- - Let $\mathbb{{B}}$ be the set of all byte sequences.
6
- - For $x \in \mathbb{{B}}$, $x_i$ denotes the $i$-th byte of $x$, and $x_i^j$ denotes the $j$-th bit of the $i$-th byte of $x$.
7
-
8
- # Definitions
9
-
10
- ## Little Endian
11
-
12
- By the **little-endian** representation of a non-negative integer, ${I}$, represented as
13
-
14
- $$
15
- {I}={\left({B}_{{n}}\ldots{B}_{{0}}\right)}_{{256}}
16
- $$
17
-
18
- in base 256, we refer to a byte array ${B}={\left({b}_{{0}},{b}_{{1}},\ldots,{b}_{{n}}\right)}$ such that
19
-
20
- $$
21
- {b}_{{i}}\:={B}_{{i}}
22
- $$
23
-
24
- Accordingly, we define the function ${\mathsf{\text{Enc}}}_{{{\mathsf{\text{LE}}}}}$:
25
-
26
- $$
27
- {\mathsf{\text{Enc}}}_{{{\mathsf{\text{LE}}}}}:{\mathbb{{Z}}}^{+}\rightarrow{\mathbb{{B}}};{\left({B}_{{n}}\ldots{B}_{{0}}\right)}_{{256}}{\mid}\rightarrow{\left({B}_{{{0},}}{B}_{{1}},\ldots,{B}_{{n}}\right)}
28
- $$
29
-
30
- ## Scale Types
31
-
32
- ### Fixed Length Integers
33
-
34
- The SCALE codec, $\text{Enc}_{{\text{SC}}}$, for fixed length integers not defined here otherwise, is equal to the little-endian encoding of those values.
35
-
36
- ### Varying Data Type
37
-
38
- > This library does not provide means for encoding/decoding varying data types, but the definitions are provided here for completeness and reference. The implementation is left to the user of the library.
39
-
40
- We define a **varying data** type to be an ordered set of data types.
41
-
42
- $$
43
- {\mathcal{{T}}}={\left\lbrace{T}_{{1}},\ldots,{T}_{{n}}\right\rbrace}
44
- $$
45
-
46
- A value ${A}$ of varying data type is a pair ${\left({A}_{{\text{Type}}},{A}_{{\text{Value}}}\right)}$ where ${A}_{{\text{Type}}}={T}_{{i}}$ for some ${T}_{{i}}\in{\mathcal{{T}}}$ and ${A}_{{\text{Value}}}$ is its value of type ${T}_{{i}}$, which can be empty. We define $\text{idx}{\left({T}_{{i}}\right)}={i}-{1}$, unless it is explicitly defined as another value in the definition of a particular varying data type.
47
-
48
- The SCALE codec for value ${A}={\left({A}_{{\text{Type}}},{A}_{{\text{Value}}}\right)}$ of varying data type ${\mathcal{{T}}}={\left\lbrace{T}_{{i}},\ldots{T}_{{n}}\right\rbrace}$, formally referred to as $\text{Enc}_{{\text{SC}}}{\left({A}\right)}$ is defined as follows:
49
-
50
- $$
51
- \text{Enc}_{{\text{SC}}}{\left({A}\right)}\:=\text{Enc}_{{\text{SC}}}{\left(\text{idx}{\left({A}_{{\text{Type}}}\right)}\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{\text{Value}}}\right)}\right)}
52
- $$
53
-
54
- The SCALE codec does not encode the correspondence between the value and the data type it represents; the decoder needs prior knowledge of such correspondence to decode the data.
55
-
56
- ### Boolean
57
-
58
- The SCALE codec for a **boolean value** ${b}$ defined as a byte as follows:
59
-
60
- $$
61
- \text{Enc}_{{\text{SC}}}:{\left\lbrace\text{False},\text{True}\right\rbrace}\rightarrow{\mathbb{{B}}}_{{1}}
62
- $$
63
-
64
- $$
65
- {b}\rightarrow{\left\lbrace\begin{matrix}{0}&{b}=\text{False}\\{1}&{b}=\text{True}\end{matrix}\right.}
66
- $$
67
-
68
- ### Compact
69
-
70
- **SCALE Length encoding** ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}$, also known as a _compact encoding_, of a non-negative number ${n}$ is defined as follows:
71
-
72
- $$
73
- {\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}:{\mathbb{{N}}}\rightarrow{\mathbb{{B}}}
74
- $$
75
-
76
- $$
77
- {n}\rightarrow{b}\:={\left\lbrace\begin{matrix}{l}_{{1}}&{0}\le{n}<{2}^{{6}}\\{i}_{{1}}{i}_{{2}}&{2}^{{6}}\le{n}<{2}^{{14}}\\{j}_{{1}}{j}_{{2}}{j}_{{3}}{j}_{{4}}&{2}^{{14}}\le{n}<{2}^{{30}}\\{k}_{{1}}{k}_{{2}}\ldots{k}_{{m}+{1}}&{2}^{{30}}\le{n}\end{matrix}\right.}
78
- $$
79
-
80
- $$
81
- {{l}_{{1}}^{{1}}}{{l}_{{1}}^{{0}}}={00}
82
- $$
83
-
84
- $$
85
- {{i}_{{1}}^{{1}}}{{i}_{{1}}^{{0}}}={01}
86
- $$
87
-
88
- $$
89
- {{j}_{{1}}^{{1}}}{{j}_{{1}}^{{0}}}={10}
90
- $$
91
-
92
- $$
93
- {{k}_{{1}}^{{1}}}{{k}_{{1}}^{{0}}}={11}
94
- $$
95
-
96
- and the rest of the bits of ${b}$ store the value of ${n}$ in little-endian format in base-2 as follows:
97
-
98
- $$
99
- {n}\:={\left\lbrace\begin{matrix}{{l}_{{1}}^{{7}}}\ldots{{l}_{{1}}^{{3}}}{{l}_{{1}}^{{2}}}&{n}<{2}^{{6}}\\{{i}_{{2}}^{{7}}}\ldots{{i}_{{2}}^{{0}}}{{i}_{{1}}^{{7}}}..{{i}_{{1}}^{{2}}}&{2}^{{6}}\le{n}<{2}^{{14}}\\{{j}_{{4}}^{{7}}}\ldots{{j}_{{4}}^{{0}}}{{j}_{{3}}^{{7}}}\ldots{{j}_{{1}}^{{7}}}\ldots{{j}_{{1}}^{{2}}}&{2}^{{14}}\le{n}<{2}^{{30}}\\{k}_{{2}}+{k}_{{3}}{2}^{{8}}+{k}_{{4}}{2}^{{{2}\times{8}}}+\ldots+{k}_{{m}+{1}}{2}^{{{\left({m}-{1}\right)}{8}}}&{2}^{{30}}\le{n}\end{matrix}\right.}
100
- $$
101
-
102
- such that:
103
-
104
- $$
105
- {{k}_{{1}}^{{7}}}\ldots{{k}_{{1}}^{{3}}}{{k}_{{1}}^{{2}}}\:={m}-{4}
106
- $$
107
-
108
- Note that ${m}$ denotes the length of the original integer being encoded and does not include the extra byte describing the length. The encoding can be used for integers up to: $$2^{(63+4)8} -1 = 2^{536} -1$$
109
-
110
- ### Sequence
111
-
112
- The **SCALE codec** for **sequence** ${S}$ such that:
113
-
114
- $$
115
- {S}\:={A}_{{1}},\ldots{A}_{{n}}
116
- $$
117
-
118
- where ${A}_{{i}}$’s are values of **the same type** (and the decoder is unable to infer value of ${n}$ from the context) is defined as:
119
-
120
- $$
121
- \text{Enc}_{{\text{SC}}}{\left({S}\right)}\:={\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}{\left({\left|{{S}}\right|}\right)}\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{2}}\right)}\text{||}\ldots\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{n}}\right)}
122
- $$
123
-
124
- where ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}$ is defined [here](#compact).
125
-
126
- In some cases, the length indicator ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}{\left({\left|{{S}}\right|}\right)}$ is omitted if the length of the sequence is fixed and known by the decoder upfront. Such cases are explicitly stated by the definition of the corresponding type.
127
-
128
- ### String
129
-
130
- The SCALE codec for a **string value** is an [encoded sequence](#sequence) consisting of UTF-8 encoded bytes.
131
-
132
- > This can be achieved via encoding the UTF-8 sequence as a `uint8[]` array, which is supported by this library.