solidity-scale-codec 2.0.0 → 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 CHANGED
@@ -7,6 +7,16 @@ 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
+
10
20
  ## Version 2.0.0
11
21
 
12
22
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-scale-codec",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Solidity implementation of scale-codec.",
5
5
  "keywords": [
6
6
  "solidity",
@@ -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 = 4;
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
- params.length == 0 ||
208
- params.length > 32 ||
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
- payloadLength = 1 + length; // 1 byte for the length + the key bytes
78
- } else if (
79
- variant == uint8(JunctionVariant.OnlyChild) ||
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 (variant < 4) {
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