solidity-scale-codec 0.1.3 → 0.3.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 (104) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/DEFINITIONS.md +132 -0
  3. package/README.md +97 -1
  4. package/package.json +16 -4
  5. package/src/LittleEndian/LittleEndianU128.sol +4 -2
  6. package/src/LittleEndian/LittleEndianU16.sol +2 -2
  7. package/src/LittleEndian/LittleEndianU256.sol +4 -2
  8. package/src/LittleEndian/LittleEndianU32.sol +4 -2
  9. package/src/LittleEndian/LittleEndianU64.sol +4 -2
  10. package/src/LittleEndian/LittleEndianU8.sol +2 -2
  11. package/src/Scale/Address/Address.sol +55 -0
  12. package/src/Scale/Address.sol +4 -0
  13. package/src/Scale/Array/BoolArr.sol +16 -2
  14. package/src/Scale/Array/I128Arr.sol +16 -2
  15. package/src/Scale/Array/I16Arr.sol +16 -2
  16. package/src/Scale/Array/I256Arr.sol +16 -2
  17. package/src/Scale/Array/I32Arr.sol +16 -2
  18. package/src/Scale/Array/I64Arr.sol +16 -2
  19. package/src/Scale/Array/I8Arr.sol +16 -2
  20. package/src/Scale/Array/U128Arr.sol +16 -2
  21. package/src/Scale/Array/U16Arr.sol +16 -2
  22. package/src/Scale/Array/U256Arr.sol +16 -2
  23. package/src/Scale/Array/U32Arr.sol +16 -2
  24. package/src/Scale/Array/U64Arr.sol +16 -2
  25. package/src/Scale/Array/U8Arr.sol +16 -2
  26. package/src/Scale/Bool/Bool.sol +12 -0
  27. package/src/Scale/Bytes/Bytes.sol +63 -0
  28. package/src/Scale/Bytes/Bytes16.sol +53 -0
  29. package/src/Scale/Bytes/Bytes2.sol +53 -0
  30. package/src/Scale/Bytes/Bytes32.sol +53 -0
  31. package/src/Scale/Bytes/Bytes4.sol +53 -0
  32. package/src/Scale/Bytes/Bytes8.sol +53 -0
  33. package/src/Scale/Bytes.sol +8 -0
  34. package/src/Scale/Compact/Compact.sol +37 -3
  35. package/src/Scale/Signed/I128.sol +11 -0
  36. package/src/Scale/Signed/I16.sol +11 -0
  37. package/src/Scale/Signed/I256.sol +11 -0
  38. package/src/Scale/Signed/I32.sol +11 -0
  39. package/src/Scale/Signed/I64.sol +11 -0
  40. package/src/Scale/Signed/I8.sol +11 -0
  41. package/src/Scale/Unsigned/U128.sol +16 -2
  42. package/src/Scale/Unsigned/U16.sol +16 -2
  43. package/src/Scale/Unsigned/U256.sol +16 -2
  44. package/src/Scale/Unsigned/U32.sol +16 -2
  45. package/src/Scale/Unsigned/U64.sol +16 -2
  46. package/src/Scale/Unsigned/U8.sol +16 -2
  47. package/src/Xcm/Types/Version.sol +5 -0
  48. package/src/Xcm/VersionedXcm/VersionedXcm.sol +27 -0
  49. package/src/Xcm/VersionedXcm/VersionedXcmCodec.sol +70 -0
  50. package/src/Xcm/v3/Constants.sol +5 -0
  51. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCode.sol +73 -0
  52. package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCodeCodec.sol +94 -0
  53. package/src/Xcm/v5/Asset/Asset.sol +13 -0
  54. package/src/Xcm/v5/Asset/AssetCodec.sol +72 -0
  55. package/src/Xcm/v5/AssetFilter/AssetFilter.sol +61 -0
  56. package/src/Xcm/v5/AssetFilter/AssetFilterCodec.sol +105 -0
  57. package/src/Xcm/v5/AssetId/AssetId.sol +10 -0
  58. package/src/Xcm/v5/AssetId/AssetIdCodec.sol +57 -0
  59. package/src/Xcm/v5/AssetInstance/AssetInstance.sol +139 -0
  60. package/src/Xcm/v5/AssetInstance/AssetInstanceCodec.sol +174 -0
  61. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilter.sol +82 -0
  62. package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilterCodec.sol +95 -0
  63. package/src/Xcm/v5/Assets/Assets.sol +14 -0
  64. package/src/Xcm/v5/Assets/AssetsCodec.sol +94 -0
  65. package/src/Xcm/v5/BodyId/BodyId.sol +121 -0
  66. package/src/Xcm/v5/BodyId/BodyIdCodec.sol +128 -0
  67. package/src/Xcm/v5/BodyPart/BodyPart.sol +105 -0
  68. package/src/Xcm/v5/BodyPart/BodyPartCodec.sol +134 -0
  69. package/src/Xcm/v5/Constants.sol +15 -0
  70. package/src/Xcm/v5/Fungibility/Fungibility.sol +60 -0
  71. package/src/Xcm/v5/Fungibility/FungibilityCodec.sol +128 -0
  72. package/src/Xcm/v5/Hint/Hint.sol +40 -0
  73. package/src/Xcm/v5/Hint/HintCodec.sol +82 -0
  74. package/src/Xcm/v5/Instruction/Instruction.sol +1217 -0
  75. package/src/Xcm/v5/Instruction/InstructionCodec.sol +321 -0
  76. package/src/Xcm/v5/Junction/Junction.sol +258 -0
  77. package/src/Xcm/v5/Junction/JunctionCodec.sol +329 -0
  78. package/src/Xcm/v5/Junctions/Junctions.sol +12 -0
  79. package/src/Xcm/v5/Junctions/JunctionsCodec.sol +120 -0
  80. package/src/Xcm/v5/Location/Location.sol +12 -0
  81. package/src/Xcm/v5/Location/LocationCodec.sol +68 -0
  82. package/src/Xcm/v5/NetworkId/NetworkId.sol +115 -0
  83. package/src/Xcm/v5/NetworkId/NetworkIdCodec.sol +114 -0
  84. package/src/Xcm/v5/OriginKind/OriginKind.sol +14 -0
  85. package/src/Xcm/v5/OriginKind/OriginKindCodec.sol +66 -0
  86. package/src/Xcm/v5/PalletInfo/PalletInfo.sol +18 -0
  87. package/src/Xcm/v5/PalletInfo/PalletInfoCodec.sol +119 -0
  88. package/src/Xcm/v5/QueryResponseInfo/QueryResponseInfo.sol +16 -0
  89. package/src/Xcm/v5/QueryResponseInfo/QueryResponseInfoCodec.sol +91 -0
  90. package/src/Xcm/v5/Response/Response.sol +145 -0
  91. package/src/Xcm/v5/Response/ResponseCodec.sol +174 -0
  92. package/src/Xcm/v5/Types/QueryId.sol +5 -0
  93. package/src/Xcm/v5/Weight/Weight.sol +10 -0
  94. package/src/Xcm/v5/Weight/WeightCodec.sol +87 -0
  95. package/src/Xcm/v5/WeightLimit/WeightLimit.sol +48 -0
  96. package/src/Xcm/v5/WeightLimit/WeightLimitCodec.sol +88 -0
  97. package/src/Xcm/v5/WildAsset/WildAsset.sol +112 -0
  98. package/src/Xcm/v5/WildAsset/WildAssetCodec.sol +166 -0
  99. package/src/Xcm/v5/WildFungibility/WildFungibility.sol +10 -0
  100. package/src/Xcm/v5/WildFungibility/WildFungibilityCodec.sol +74 -0
  101. package/src/Xcm/v5/Xcm/Xcm.sol +27 -0
  102. package/src/Xcm/v5/Xcm/XcmCodec.sol +83 -0
  103. package/src/Xcm/v5/XcmError/XcmError.sol +122 -0
  104. package/src/Xcm/v5/XcmError/XcmErrorCodec.sol +85 -0
@@ -0,0 +1,82 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.28;
3
+
4
+ import {Location} from "../Location/Location.sol";
5
+ import {LocationCodec} from "../Location/LocationCodec.sol";
6
+ import {Hint, HintType} from "./Hint.sol";
7
+
8
+ /// @title SCALE Codec for XCM v5 `Hint`
9
+ /// @notice SCALE-compliant encoder/decoder for the `Hint` type.
10
+ /// @dev SCALE reference: https://docs.polkadot.com/polkadot-protocol/basics/data-encoding
11
+ /// @dev XCM v5 reference: https://paritytech.github.io/polkadot-sdk/master/staging_xcm/v5/enum.Hint.html
12
+ library HintCodec {
13
+ error InvalidHintLength();
14
+ error InvalidHintType(uint8 hType);
15
+
16
+ /// @notice Encodes a `Hint` struct into SCALE bytes.
17
+ /// @param hint The `Hint` struct to encode.
18
+ /// @return SCALE-encoded bytes representing the `Hint`.
19
+ function encode(Hint memory hint) internal pure returns (bytes memory) {
20
+ return abi.encodePacked(uint8(hint.hType), hint.payload);
21
+ }
22
+
23
+ /// @notice Returns the number of bytes that a `Hint` would occupy when SCALE-encoded.
24
+ /// @param data The byte sequence containing the encoded `Hint`.
25
+ /// @param offset The starting index in `data` from which to calculate the encoded size.
26
+ /// @return The number of bytes occupied by the encoded `Hint`.
27
+ function encodedSizeAt(
28
+ bytes memory data,
29
+ uint256 offset
30
+ ) internal pure returns (uint256) {
31
+ if (data.length < offset + 1) revert InvalidHintLength();
32
+ uint8 hType = uint8(data[offset]);
33
+ if (hType == uint8(HintType.AssetClaimer)) {
34
+ return 1 + LocationCodec.encodedSizeAt(data, offset + 1);
35
+ }
36
+ revert InvalidHintType(hType);
37
+ }
38
+
39
+ /// @notice Decodes a `Hint` from SCALE bytes starting at the beginning.
40
+ /// @param data The byte sequence containing the encoded `Hint`.
41
+ /// @return hint The decoded `Hint` struct.
42
+ /// @return bytesRead The number of bytes read.
43
+ function decode(
44
+ bytes memory data
45
+ ) internal pure returns (Hint memory hint, uint256 bytesRead) {
46
+ return decodeAt(data, 0);
47
+ }
48
+
49
+ /// @notice Decodes a `Hint` from SCALE bytes starting at a given offset.
50
+ /// @param data The byte sequence containing the encoded `Hint`.
51
+ /// @param offset The starting index in `data` from which to decode.
52
+ /// @return hint The decoded `Hint` struct.
53
+ /// @return bytesRead The number of bytes read.
54
+ function decodeAt(
55
+ bytes memory data,
56
+ uint256 offset
57
+ ) internal pure returns (Hint memory hint, uint256 bytesRead) {
58
+ if (data.length < offset + 1) revert InvalidHintLength();
59
+ uint8 hType = uint8(data[offset]);
60
+ if (hType > uint8(HintType.AssetClaimer)) revert InvalidHintType(hType);
61
+ uint256 size = encodedSizeAt(data, offset);
62
+ uint256 payloadLength = size - 1;
63
+ bytes memory payload = new bytes(payloadLength);
64
+ for (uint256 i = 0; i < payloadLength; ++i) {
65
+ payload[i] = data[offset + 1 + i];
66
+ }
67
+ hint = Hint({hType: HintType(hType), payload: payload});
68
+ bytesRead = size;
69
+ }
70
+
71
+ /// @notice Decodes the `Location` from an `AssetClaimer` hint.
72
+ /// @param hint The `Hint` struct. Must be of type `AssetClaimer`.
73
+ /// @return The claimer `Location`.
74
+ function asAssetClaimer(
75
+ Hint memory hint
76
+ ) internal pure returns (Location memory) {
77
+ if (hint.hType != HintType.AssetClaimer)
78
+ revert InvalidHintType(uint8(hint.hType));
79
+ (Location memory location, ) = LocationCodec.decode(hint.payload);
80
+ return location;
81
+ }
82
+ }