@latticexyz/protocol-parser 1.42.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/LICENSE +21 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
- package/src/common.ts +11 -0
- package/src/decodeDynamicField.test-d.ts +48 -0
- package/src/decodeDynamicField.test.ts +175 -0
- package/src/decodeDynamicField.ts +145 -0
- package/src/decodeKeyTuple.test.ts +38 -0
- package/src/decodeKeyTuple.ts +11 -0
- package/src/decodeRecord.test.ts +13 -0
- package/src/decodeRecord.ts +61 -0
- package/src/decodeStaticField.test-d.ts +40 -0
- package/src/decodeStaticField.test.ts +94 -0
- package/src/decodeStaticField.ts +145 -0
- package/src/encodeField.ts +17 -0
- package/src/encodeRecord.test.ts +18 -0
- package/src/encodeRecord.ts +30 -0
- package/src/errors.ts +64 -0
- package/src/hexToPackedCounter.test.ts +34 -0
- package/src/hexToPackedCounter.ts +31 -0
- package/src/hexToSchema.test.ts +51 -0
- package/src/hexToSchema.ts +33 -0
- package/src/hexToTableSchema.ts +12 -0
- package/src/index.ts +10 -0
- package/src/schemaToHex.test.ts +16 -0
- package/src/schemaToHex.ts +18 -0
- package/src/staticDataLength.ts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present Lattice Labs Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{hexToString as M,sliceHex as N}from"viem";import{assertExhaustive as R}from"@latticexyz/common/utils";import{arrayAbiTypeToStaticAbiType as K,staticAbiTypeToByteLength as O}from"@latticexyz/schema-type";import{getAddress as k,hexToBigInt as C,hexToBool as V}from"viem";import{assertExhaustive as U}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as j,staticAbiTypeToDefaultValue as z}from"@latticexyz/schema-type";import{MUDError as p}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as D}from"@latticexyz/schema-type";var u=class extends p{name="InvalidHexValueError";constructor(e){super(`Hex value "${e}" is an odd length (${e.length-2}). It must be an even length.`)}},T=class extends p{name="InvalidHexLengthForSchemaError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a schema.`)}},x=class extends p{name="InvalidHexLengthForPackedCounterError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a packed counter.`)}},f=class extends p{name="InvalidHexLengthForStaticFieldError";constructor(e,i){super(`Hex value "${i}" has length of ${i.length-2}, but expected length of ${D[e]*2} for ${e} type.`)}},g=class extends p{name="InvalidHexLengthForArrayFieldError";constructor(e,i){super(`Hex value "${i}" has length of ${i.length-2}, but expected a multiple of ${D[e]*2} for ${e}[] type.`)}},S=class extends p{name="SchemaStaticLengthMismatchError";constructor(e,i,s){super(`Schema "${e}" static data length (${i}) did not match the summed length of all static fields (${s}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`)}},A=class extends p{name="PackedCounterLengthMismatchError";constructor(e,i,s){super(`PackedCounter "${e}" total bytes length (${i}) did not match the summed length of all field byte lengths (${s}).`)}};function d(t,e){if(e.length>3&&e.length%2!==0)throw new u(e);if((e.length-2)/2!==j[t])throw new f(t,e);switch(t){case"uint8":case"uint16":case"uint24":case"uint32":case"uint40":case"uint48":case"uint56":case"uint64":case"uint72":case"uint80":case"uint88":case"uint96":case"uint104":case"uint112":case"uint120":case"uint128":case"uint136":case"uint144":case"uint152":case"uint160":case"uint168":case"uint176":case"uint184":case"uint192":case"uint200":case"uint208":case"uint216":case"uint224":case"uint232":case"uint240":case"uint248":case"uint256":case"int8":case"int16":case"int24":case"int32":case"int40":case"int48":case"int56":case"int64":case"int72":case"int80":case"int88":case"int96":case"int104":case"int112":case"int120":case"int128":case"int136":case"int144":case"int152":case"int160":case"int168":case"int176":case"int184":case"int192":case"int200":case"int208":case"int216":case"int224":case"int232":case"int240":case"int248":case"int256":{let s=C(e,{signed:t.startsWith("int")}),a=typeof z[t];if(a==="number")return Number(s);if(a==="bigint")return s;throw new Error(`Unexpected default value type (${a}) for ABI type (${t})`)}case"bytes1":case"bytes2":case"bytes3":case"bytes4":case"bytes5":case"bytes6":case"bytes7":case"bytes8":case"bytes9":case"bytes10":case"bytes11":case"bytes12":case"bytes13":case"bytes14":case"bytes15":case"bytes16":case"bytes17":case"bytes18":case"bytes19":case"bytes20":case"bytes21":case"bytes22":case"bytes23":case"bytes24":case"bytes25":case"bytes26":case"bytes27":case"bytes28":case"bytes29":case"bytes30":case"bytes31":case"bytes32":return e;case"bool":return V(e);case"address":return k(e)}return U(t,`Unsupported static ABI type: ${t}`)}function v(t,e){if(t==="bytes")return e;if(t==="string")return M(e);if(e.length>3&&e.length%2!==0)throw new u(e);let i=(e.length-2)/2;switch(t){case"uint8[]":case"uint16[]":case"uint24[]":case"uint32[]":case"uint40[]":case"uint48[]":case"uint56[]":case"uint64[]":case"uint72[]":case"uint80[]":case"uint88[]":case"uint96[]":case"uint104[]":case"uint112[]":case"uint120[]":case"uint128[]":case"uint136[]":case"uint144[]":case"uint152[]":case"uint160[]":case"uint168[]":case"uint176[]":case"uint184[]":case"uint192[]":case"uint200[]":case"uint208[]":case"uint216[]":case"uint224[]":case"uint232[]":case"uint240[]":case"uint248[]":case"uint256[]":case"int8[]":case"int16[]":case"int24[]":case"int32[]":case"int40[]":case"int48[]":case"int56[]":case"int64[]":case"int72[]":case"int80[]":case"int88[]":case"int96[]":case"int104[]":case"int112[]":case"int120[]":case"int128[]":case"int136[]":case"int144[]":case"int152[]":case"int160[]":case"int168[]":case"int176[]":case"int184[]":case"int192[]":case"int200[]":case"int208[]":case"int216[]":case"int224[]":case"int232[]":case"int240[]":case"int248[]":case"int256[]":case"bytes1[]":case"bytes2[]":case"bytes3[]":case"bytes4[]":case"bytes5[]":case"bytes6[]":case"bytes7[]":case"bytes8[]":case"bytes9[]":case"bytes10[]":case"bytes11[]":case"bytes12[]":case"bytes13[]":case"bytes14[]":case"bytes15[]":case"bytes16[]":case"bytes17[]":case"bytes18[]":case"bytes19[]":case"bytes20[]":case"bytes21[]":case"bytes22[]":case"bytes23[]":case"bytes24[]":case"bytes25[]":case"bytes26[]":case"bytes27[]":case"bytes28[]":case"bytes29[]":case"bytes30[]":case"bytes31[]":case"bytes32[]":case"bool[]":case"address[]":{let s=K(t),a=O[s];if(i%a!==0)throw new g(s,e);return new Array(i/a).fill(void 0).map((n,c)=>{let r=N(e,c*a,(c+1)*a);return d(s,r)})}}return R(t,`Unsupported dynamic ABI type: ${t}`)}import{decodeAbiParameters as W}from"viem";function ge(t,e){return e.map((i,s)=>W([{type:t.staticFields[s]}],i)[0])}import{staticAbiTypeToByteLength as q}from"@latticexyz/schema-type";import{sliceHex as P}from"viem";import{sliceHex as F}from"viem";function $(t){if(t.length!==66)throw new x(t);let e=d("uint56",F(t,0,7)),i=v("uint40[]",F(t,7)),s=BigInt(i.reduce((a,o)=>a+o,0));if(s!==e)throw new A(t,e,s);return{totalByteLength:e,fieldByteLengths:i}}import{staticAbiTypeToByteLength as _}from"@latticexyz/schema-type";function B(t){return t.reduce((e,i)=>e+_[i],0)}function Me(t,e){let i=[],s=0;t.staticFields.forEach(n=>{let c=q[n],r=d(n,P(e,s,s+c));s+=c,i.push(r)});let a=B(t.staticFields),o=s;if(o!==a&&console.warn("Decoded static data length does not match schema's expected static data length. Data may get corrupted. Is `getStaticByteLength` outdated?",{expectedLength:a,actualLength:o,bytesOffset:s}),t.dynamicFields.length>0){let n=$(P(e,s,s+32));s+=32,t.dynamicFields.forEach((r,H)=>{let m=n.fieldByteLengths[H],y=v(r,P(e,s,s+m));s+=m,i.push(y)});let c=s-32-o;BigInt(c)!==n.totalByteLength&&console.warn("Decoded dynamic data length does not match data layout's expected data length. Data may get corrupted. Did the data layout change?",{expectedLength:n.totalByteLength,actualLength:c,bytesOffset:s})}return i}import{arrayAbiTypeToStaticAbiType as G,isArrayAbiType as J}from"@latticexyz/schema-type";import{encodePacked as w}from"viem";function h(t,e){if(J(t)&&Array.isArray(e)){let i=G(t);return w(e.map(()=>i),e)}return w([t],[e])}function Ge(t,e){let i=e.slice(0,t.staticFields.length),s=e.slice(t.staticFields.length),a=i.map((m,y)=>h(t.staticFields[y],m).replace(/^0x/,"")).join("");if(t.dynamicFields.length===0)return`0x${a}`;let o=s.map((m,y)=>h(t.dynamicFields[y],m).replace(/^0x/,"")),n=o.map(m=>m.length/2),c=n.reduce((m,y)=>m+BigInt(y),0n),r=o.join(""),H=`${h("uint56",c).replace(/^0x/,"")}${n.map(m=>h("uint40",m).replace(/^0x/,"")).join("")}`.padEnd(64,"0");return`0x${a}${H}${r}`}import{sliceHex as E}from"viem";import{schemaAbiTypes as I,staticAbiTypeToByteLength as Q}from"@latticexyz/schema-type";import{hexToNumber as l,sliceHex as b}from"viem";function L(t){if(t.length!==66)throw new T(t);let e=l(b(t,0,2)),i=l(b(t,2,3)),s=l(b(t,3,4)),a=[],o=[];for(let c=4;c<4+i;c++){let r=l(b(t,c,c+1));a.push(I[r])}for(let c=4+i;c<4+i+s;c++){let r=l(b(t,c,c+1));o.push(I[r])}let n=a.reduce((c,r)=>c+Q[r],0);if(n!==e)throw new S(t,e,n);return{staticFields:a,dynamicFields:o}}function nt(t){let e=L(E(t,0,32));return{keySchema:L(E(t,32,64)),valueSchema:e}}export{u as InvalidHexLengthError,g as InvalidHexLengthForArrayFieldError,x as InvalidHexLengthForPackedCounterError,T as InvalidHexLengthForSchemaError,f as InvalidHexLengthForStaticFieldError,A as PackedCounterLengthMismatchError,S as SchemaStaticLengthMismatchError,v as decodeDynamicField,ge as decodeKeyTuple,Me as decodeRecord,d as decodeStaticField,h as encodeField,Ge as encodeRecord,$ as hexToPackedCounter,nt as hexToTableSchema};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeKeyTuple.ts","../src/decodeRecord.ts","../src/hexToPackedCounter.ts","../src/staticDataLength.ts","../src/encodeField.ts","../src/encodeRecord.ts","../src/hexToTableSchema.ts","../src/hexToSchema.ts"],"sourcesContent":["import { Hex, hexToString, sliceHex } from \"viem\";\nimport { assertExhaustive } from \"@latticexyz/common/utils\";\nimport {\n DynamicAbiType,\n DynamicAbiTypeToPrimitiveType,\n arrayAbiTypeToStaticAbiType,\n staticAbiTypeToByteLength,\n} from \"@latticexyz/schema-type\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { InvalidHexLengthError, InvalidHexLengthForArrayFieldError } from \"./errors\";\n\n// TODO: require size/length for dynamic fields, because hex might be longer (sometimes we just move the length pointer of arrays without updating the value)\n// this means the error should probably change from \"invalid length\" to \"too short\", which viem has that we could reuse?\n\nexport function decodeDynamicField<\n TAbiType extends DynamicAbiType,\n TPrimitiveType extends DynamicAbiTypeToPrimitiveType<TAbiType>\n>(abiType: TAbiType, data: Hex): TPrimitiveType {\n if (abiType === \"bytes\") {\n return data as TPrimitiveType;\n }\n if (abiType === \"string\") {\n return hexToString(data) as TPrimitiveType;\n }\n\n if (data.length > 3 && data.length % 2 !== 0) {\n throw new InvalidHexLengthError(data);\n }\n\n const dataSize = (data.length - 2) / 2;\n\n switch (abiType) {\n case \"uint8[]\":\n case \"uint16[]\":\n case \"uint24[]\":\n case \"uint32[]\":\n case \"uint40[]\":\n case \"uint48[]\":\n case \"uint56[]\":\n case \"uint64[]\":\n case \"uint72[]\":\n case \"uint80[]\":\n case \"uint88[]\":\n case \"uint96[]\":\n case \"uint104[]\":\n case \"uint112[]\":\n case \"uint120[]\":\n case \"uint128[]\":\n case \"uint136[]\":\n case \"uint144[]\":\n case \"uint152[]\":\n case \"uint160[]\":\n case \"uint168[]\":\n case \"uint176[]\":\n case \"uint184[]\":\n case \"uint192[]\":\n case \"uint200[]\":\n case \"uint208[]\":\n case \"uint216[]\":\n case \"uint224[]\":\n case \"uint232[]\":\n case \"uint240[]\":\n case \"uint248[]\":\n case \"uint256[]\":\n case \"int8[]\":\n case \"int16[]\":\n case \"int24[]\":\n case \"int32[]\":\n case \"int40[]\":\n case \"int48[]\":\n case \"int56[]\":\n case \"int64[]\":\n case \"int72[]\":\n case \"int80[]\":\n case \"int88[]\":\n case \"int96[]\":\n case \"int104[]\":\n case \"int112[]\":\n case \"int120[]\":\n case \"int128[]\":\n case \"int136[]\":\n case \"int144[]\":\n case \"int152[]\":\n case \"int160[]\":\n case \"int168[]\":\n case \"int176[]\":\n case \"int184[]\":\n case \"int192[]\":\n case \"int200[]\":\n case \"int208[]\":\n case \"int216[]\":\n case \"int224[]\":\n case \"int232[]\":\n case \"int240[]\":\n case \"int248[]\":\n case \"int256[]\":\n case \"bytes1[]\":\n case \"bytes2[]\":\n case \"bytes3[]\":\n case \"bytes4[]\":\n case \"bytes5[]\":\n case \"bytes6[]\":\n case \"bytes7[]\":\n case \"bytes8[]\":\n case \"bytes9[]\":\n case \"bytes10[]\":\n case \"bytes11[]\":\n case \"bytes12[]\":\n case \"bytes13[]\":\n case \"bytes14[]\":\n case \"bytes15[]\":\n case \"bytes16[]\":\n case \"bytes17[]\":\n case \"bytes18[]\":\n case \"bytes19[]\":\n case \"bytes20[]\":\n case \"bytes21[]\":\n case \"bytes22[]\":\n case \"bytes23[]\":\n case \"bytes24[]\":\n case \"bytes25[]\":\n case \"bytes26[]\":\n case \"bytes27[]\":\n case \"bytes28[]\":\n case \"bytes29[]\":\n case \"bytes30[]\":\n case \"bytes31[]\":\n case \"bytes32[]\":\n case \"bool[]\":\n case \"address[]\": {\n const staticAbiType = arrayAbiTypeToStaticAbiType(abiType);\n const itemByteLength = staticAbiTypeToByteLength[staticAbiType];\n if (dataSize % itemByteLength !== 0) {\n throw new InvalidHexLengthForArrayFieldError(staticAbiType, data);\n }\n const items = new Array(dataSize / itemByteLength).fill(undefined).map((_, i) => {\n const itemData = sliceHex(data, i * itemByteLength, (i + 1) * itemByteLength);\n return decodeStaticField(staticAbiType, itemData);\n });\n return items as TPrimitiveType;\n }\n }\n\n return assertExhaustive(abiType, `Unsupported dynamic ABI type: ${abiType}`);\n}\n","import { Hex, getAddress, hexToBigInt, hexToBool } from \"viem\";\nimport { assertExhaustive } from \"@latticexyz/common/utils\";\nimport {\n StaticAbiType,\n StaticAbiTypeToPrimitiveType,\n staticAbiTypeToByteLength,\n staticAbiTypeToDefaultValue,\n} from \"@latticexyz/schema-type\";\nimport { InvalidHexLengthError, InvalidHexLengthForStaticFieldError } from \"./errors\";\n\nexport function decodeStaticField<\n TAbiType extends StaticAbiType,\n TPrimitiveType extends StaticAbiTypeToPrimitiveType<TAbiType>\n>(abiType: TAbiType, data: Hex): TPrimitiveType {\n if (data.length > 3 && data.length % 2 !== 0) {\n throw new InvalidHexLengthError(data);\n }\n\n const dataSize = (data.length - 2) / 2;\n if (dataSize !== staticAbiTypeToByteLength[abiType]) {\n throw new InvalidHexLengthForStaticFieldError(abiType, data);\n }\n\n switch (abiType) {\n case \"uint8\":\n case \"uint16\":\n case \"uint24\":\n case \"uint32\":\n case \"uint40\":\n case \"uint48\":\n case \"uint56\":\n case \"uint64\":\n case \"uint72\":\n case \"uint80\":\n case \"uint88\":\n case \"uint96\":\n case \"uint104\":\n case \"uint112\":\n case \"uint120\":\n case \"uint128\":\n case \"uint136\":\n case \"uint144\":\n case \"uint152\":\n case \"uint160\":\n case \"uint168\":\n case \"uint176\":\n case \"uint184\":\n case \"uint192\":\n case \"uint200\":\n case \"uint208\":\n case \"uint216\":\n case \"uint224\":\n case \"uint232\":\n case \"uint240\":\n case \"uint248\":\n case \"uint256\":\n case \"int8\":\n case \"int16\":\n case \"int24\":\n case \"int32\":\n case \"int40\":\n case \"int48\":\n case \"int56\":\n case \"int64\":\n case \"int72\":\n case \"int80\":\n case \"int88\":\n case \"int96\":\n case \"int104\":\n case \"int112\":\n case \"int120\":\n case \"int128\":\n case \"int136\":\n case \"int144\":\n case \"int152\":\n case \"int160\":\n case \"int168\":\n case \"int176\":\n case \"int184\":\n case \"int192\":\n case \"int200\":\n case \"int208\":\n case \"int216\":\n case \"int224\":\n case \"int232\":\n case \"int240\":\n case \"int248\":\n case \"int256\": {\n const value = hexToBigInt(data, { signed: abiType.startsWith(\"int\") });\n const defaultValueType = typeof staticAbiTypeToDefaultValue[abiType];\n if (defaultValueType === \"number\") {\n return Number(value) as TPrimitiveType;\n }\n if (defaultValueType === \"bigint\") {\n return value as TPrimitiveType;\n }\n throw new Error(`Unexpected default value type (${defaultValueType}) for ABI type (${abiType})`);\n }\n\n case \"bytes1\":\n case \"bytes2\":\n case \"bytes3\":\n case \"bytes4\":\n case \"bytes5\":\n case \"bytes6\":\n case \"bytes7\":\n case \"bytes8\":\n case \"bytes9\":\n case \"bytes10\":\n case \"bytes11\":\n case \"bytes12\":\n case \"bytes13\":\n case \"bytes14\":\n case \"bytes15\":\n case \"bytes16\":\n case \"bytes17\":\n case \"bytes18\":\n case \"bytes19\":\n case \"bytes20\":\n case \"bytes21\":\n case \"bytes22\":\n case \"bytes23\":\n case \"bytes24\":\n case \"bytes25\":\n case \"bytes26\":\n case \"bytes27\":\n case \"bytes28\":\n case \"bytes29\":\n case \"bytes30\":\n case \"bytes31\":\n case \"bytes32\": {\n return data as TPrimitiveType;\n }\n\n case \"bool\": {\n return hexToBool(data) as TPrimitiveType;\n }\n\n case \"address\": {\n return getAddress(data) as TPrimitiveType;\n }\n }\n\n return assertExhaustive(abiType, `Unsupported static ABI type: ${abiType}`);\n}\n","import { Hex } from \"viem\";\nimport { MUDError } from \"@latticexyz/common/errors\";\nimport { StaticAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\n\nexport class InvalidHexLengthError extends MUDError {\n override name = \"InvalidHexValueError\";\n constructor(value: Hex) {\n super(`Hex value \"${value}\" is an odd length (${value.length - 2}). It must be an even length.`);\n }\n}\n\nexport class InvalidHexLengthForSchemaError extends MUDError {\n override name = \"InvalidHexLengthForSchemaError\";\n constructor(value: Hex) {\n super(`Hex value \"${value}\" has length of ${value.length - 2}, but expected length of 64 for a schema.`);\n }\n}\n\nexport class InvalidHexLengthForPackedCounterError extends MUDError {\n override name = \"InvalidHexLengthForPackedCounterError\";\n constructor(value: Hex) {\n super(`Hex value \"${value}\" has length of ${value.length - 2}, but expected length of 64 for a packed counter.`);\n }\n}\n\nexport class InvalidHexLengthForStaticFieldError extends MUDError {\n override name = \"InvalidHexLengthForStaticFieldError\";\n constructor(abiType: StaticAbiType, value: Hex) {\n super(\n `Hex value \"${value}\" has length of ${value.length - 2}, but expected length of ${\n staticAbiTypeToByteLength[abiType] * 2\n } for ${abiType} type.`\n );\n }\n}\n\nexport class InvalidHexLengthForArrayFieldError extends MUDError {\n override name = \"InvalidHexLengthForArrayFieldError\";\n constructor(abiType: StaticAbiType, value: Hex) {\n super(\n `Hex value \"${value}\" has length of ${value.length - 2}, but expected a multiple of ${\n staticAbiTypeToByteLength[abiType] * 2\n } for ${abiType}[] type.`\n );\n }\n}\n\nexport class SchemaStaticLengthMismatchError extends MUDError {\n override name = \"SchemaStaticLengthMismatchError\";\n constructor(schemaData: Hex, definedLength: number, summedLength: number) {\n super(\n `Schema \"${schemaData}\" static data length (${definedLength}) did not match the summed length of all static fields (${summedLength}). Is \\`staticAbiTypeToByteLength\\` up to date with Solidity schema types?`\n );\n }\n}\n\nexport class PackedCounterLengthMismatchError extends MUDError {\n override name = \"PackedCounterLengthMismatchError\";\n constructor(packedCounterData: Hex, definedLength: bigint, summedLength: bigint) {\n super(\n `PackedCounter \"${packedCounterData}\" total bytes length (${definedLength}) did not match the summed length of all field byte lengths (${summedLength}).`\n );\n }\n}\n","import { Hex, decodeAbiParameters } from \"viem\";\nimport { StaticPrimitiveType } from \"@latticexyz/schema-type\";\nimport { Schema } from \"./common\";\n\n// key tuples are encoded in the same way as abi.encode, so we can decode them with viem\n\nexport function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {\n return keyTuple.map(\n (key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType\n );\n}\n","import { StaticPrimitiveType, DynamicPrimitiveType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\nimport { Hex, sliceHex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { hexToPackedCounter } from \"./hexToPackedCounter\";\nimport { staticDataLength } from \"./staticDataLength\";\n\nexport function decodeRecord(schema: Schema, data: Hex): readonly (StaticPrimitiveType | DynamicPrimitiveType)[] {\n const values: (StaticPrimitiveType | DynamicPrimitiveType)[] = [];\n\n let bytesOffset = 0;\n schema.staticFields.forEach((fieldType) => {\n const fieldByteLength = staticAbiTypeToByteLength[fieldType];\n const value = decodeStaticField(fieldType, sliceHex(data, bytesOffset, bytesOffset + fieldByteLength));\n bytesOffset += fieldByteLength;\n values.push(value);\n });\n\n // Warn user if static data length doesn't match the schema, because data corruption might be possible.\n const schemaStaticDataLength = staticDataLength(schema.staticFields);\n const actualStaticDataLength = bytesOffset;\n if (actualStaticDataLength !== schemaStaticDataLength) {\n console.warn(\n \"Decoded static data length does not match schema's expected static data length. Data may get corrupted. Is `getStaticByteLength` outdated?\",\n {\n expectedLength: schemaStaticDataLength,\n actualLength: actualStaticDataLength,\n bytesOffset,\n }\n );\n }\n\n if (schema.dynamicFields.length > 0) {\n const dataLayout = hexToPackedCounter(sliceHex(data, bytesOffset, bytesOffset + 32));\n bytesOffset += 32;\n\n schema.dynamicFields.forEach((fieldType, i) => {\n const dataLength = dataLayout.fieldByteLengths[i];\n const value = decodeDynamicField(fieldType, sliceHex(data, bytesOffset, bytesOffset + dataLength));\n bytesOffset += dataLength;\n values.push(value);\n });\n\n // Warn user if dynamic data length doesn't match the schema, because data corruption might be possible.\n const actualDynamicDataLength = bytesOffset - 32 - actualStaticDataLength;\n // TODO: refactor this so we don't break for bytes offsets >UINT40\n if (BigInt(actualDynamicDataLength) !== dataLayout.totalByteLength) {\n console.warn(\n \"Decoded dynamic data length does not match data layout's expected data length. Data may get corrupted. Did the data layout change?\",\n {\n expectedLength: dataLayout.totalByteLength,\n actualLength: actualDynamicDataLength,\n bytesOffset,\n }\n );\n }\n }\n\n return values;\n}\n","import { Hex, sliceHex } from \"viem\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { InvalidHexLengthForPackedCounterError, PackedCounterLengthMismatchError } from \"./errors\";\n\n// Keep this logic in sync with PackedCounter.sol\n\n// - First 7 bytes (uint56) are used for the total byte length of the dynamic data\n// - The next 5 byte (uint40) sections are used for the byte length of each field, in the same order as the schema's dynamic fields\n\n// We use byte lengths rather than item counts so that, on chain, we can slice without having to get the schema first (and thus the field lengths of each dynamic type)\n\nexport function hexToPackedCounter(data: Hex): {\n totalByteLength: bigint;\n fieldByteLengths: readonly number[];\n} {\n if (data.length !== 66) {\n throw new InvalidHexLengthForPackedCounterError(data);\n }\n const totalByteLength = decodeStaticField(\"uint56\", sliceHex(data, 0, 7));\n\n // TODO: use schema to make sure we only parse as many as we need (rather than zeroes at the end)?\n const fieldByteLengths = decodeDynamicField(\"uint40[]\", sliceHex(data, 7));\n\n const summedLength = BigInt(fieldByteLengths.reduce((total, length) => total + length, 0));\n if (summedLength !== totalByteLength) {\n throw new PackedCounterLengthMismatchError(data, totalByteLength, summedLength);\n }\n\n return { totalByteLength, fieldByteLengths };\n}\n","import { StaticAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\n\nexport function staticDataLength(staticFields: readonly StaticAbiType[]): number {\n return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);\n}\n","import { SchemaAbiType, arrayAbiTypeToStaticAbiType, isArrayAbiType } from \"@latticexyz/schema-type\";\nimport { AbiParameterToPrimitiveType } from \"abitype\";\nimport { Hex, encodePacked } from \"viem\";\n\nexport function encodeField<TSchemaAbiType extends SchemaAbiType>(\n fieldType: TSchemaAbiType,\n value: AbiParameterToPrimitiveType<{ type: TSchemaAbiType }>\n): Hex {\n if (isArrayAbiType(fieldType) && Array.isArray(value)) {\n const staticFieldType = arrayAbiTypeToStaticAbiType(fieldType);\n return encodePacked(\n value.map(() => staticFieldType),\n value\n );\n }\n return encodePacked([fieldType], [value]);\n}\n","import { StaticPrimitiveType, DynamicPrimitiveType } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { encodeField } from \"./encodeField\";\nimport { Schema } from \"./common\";\n\nexport function encodeRecord(schema: Schema, values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[]): Hex {\n const staticValues = values.slice(0, schema.staticFields.length) as readonly StaticPrimitiveType[];\n const dynamicValues = values.slice(schema.staticFields.length) as readonly DynamicPrimitiveType[];\n\n const staticData = staticValues\n .map((value, i) => encodeField(schema.staticFields[i], value).replace(/^0x/, \"\"))\n .join(\"\");\n\n if (schema.dynamicFields.length === 0) return `0x${staticData}`;\n\n const dynamicDataItems = dynamicValues.map((value, i) =>\n encodeField(schema.dynamicFields[i], value).replace(/^0x/, \"\")\n );\n\n const dynamicFieldByteLengths = dynamicDataItems.map((value) => value.length / 2);\n const dynamicTotalByteLength = dynamicFieldByteLengths.reduce((total, length) => total + BigInt(length), 0n);\n\n const dynamicData = dynamicDataItems.join(\"\");\n\n const packedCounter = `${encodeField(\"uint56\", dynamicTotalByteLength).replace(/^0x/, \"\")}${dynamicFieldByteLengths\n .map((length) => encodeField(\"uint40\", length).replace(/^0x/, \"\"))\n .join(\"\")}`.padEnd(64, \"0\");\n\n return `0x${staticData}${packedCounter}${dynamicData}`;\n}\n","import { Hex, sliceHex } from \"viem\";\nimport { TableSchema } from \"./common\";\nimport { hexToSchema } from \"./hexToSchema\";\n\nexport function hexToTableSchema(data: Hex): TableSchema {\n const valueSchema = hexToSchema(sliceHex(data, 0, 32));\n const keySchema = hexToSchema(sliceHex(data, 32, 64));\n return {\n keySchema,\n valueSchema,\n };\n}\n","import { StaticAbiType, DynamicAbiType, schemaAbiTypes, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\nimport { Hex, hexToNumber, sliceHex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from \"./errors\";\n\nexport function hexToSchema(data: Hex): Schema {\n if (data.length !== 66) {\n throw new InvalidHexLengthForSchemaError(data);\n }\n\n const staticDataLength = hexToNumber(sliceHex(data, 0, 2));\n const numStaticFields = hexToNumber(sliceHex(data, 2, 3));\n const numDynamicFields = hexToNumber(sliceHex(data, 3, 4));\n const staticFields: StaticAbiType[] = [];\n const dynamicFields: DynamicAbiType[] = [];\n\n for (let i = 4; i < 4 + numStaticFields; i++) {\n const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));\n staticFields.push(schemaAbiTypes[schemaTypeIndex] as StaticAbiType);\n }\n for (let i = 4 + numStaticFields; i < 4 + numStaticFields + numDynamicFields; i++) {\n const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));\n dynamicFields.push(schemaAbiTypes[schemaTypeIndex] as DynamicAbiType);\n }\n\n // validate static data length\n const actualStaticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);\n if (actualStaticDataLength !== staticDataLength) {\n throw new SchemaStaticLengthMismatchError(data, staticDataLength, actualStaticDataLength);\n }\n\n return { staticFields, dynamicFields };\n}\n"],"mappings":"AAAA,OAAc,eAAAA,EAAa,YAAAC,MAAgB,OAC3C,OAAS,oBAAAC,MAAwB,2BACjC,OAGE,+BAAAC,EACA,6BAAAC,MACK,0BCPP,OAAc,cAAAC,EAAY,eAAAC,EAAa,aAAAC,MAAiB,OACxD,OAAS,oBAAAC,MAAwB,2BACjC,OAGE,6BAAAC,EACA,+BAAAC,MACK,0BCNP,OAAS,YAAAC,MAAgB,4BACzB,OAAwB,6BAAAC,MAAiC,0BAElD,IAAMC,EAAN,cAAoCF,CAAS,CACzC,KAAO,uBAChB,YAAYG,EAAY,CACtB,MAAM,cAAcA,wBAA4BA,EAAM,OAAS,gCAAgC,CACjG,CACF,EAEaC,EAAN,cAA6CJ,CAAS,CAClD,KAAO,iCAChB,YAAYG,EAAY,CACtB,MAAM,cAAcA,oBAAwBA,EAAM,OAAS,4CAA4C,CACzG,CACF,EAEaE,EAAN,cAAoDL,CAAS,CACzD,KAAO,wCAChB,YAAYG,EAAY,CACtB,MAAM,cAAcA,oBAAwBA,EAAM,OAAS,oDAAoD,CACjH,CACF,EAEaG,EAAN,cAAkDN,CAAS,CACvD,KAAO,sCAChB,YAAYO,EAAwBJ,EAAY,CAC9C,MACE,cAAcA,oBAAwBA,EAAM,OAAS,6BACnDF,EAA0BM,CAAO,E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AEpE,QAASD,EAAI,EAAIJ,EAAiBI,EAAI,EAAIJ,EAAkBC,EAAkBG,IAAK,CACjF,IAAMC,EAAkBP,EAAYC,EAASJ,EAAMS,EAAGA,EAAI,CAAC,CAAC,EAC5DD,EAAc,KAAKG,EAAeD,CAAe,CAAmB,EAItE,IAAME,EAAyBL,EAAa,OAAO,CAACM,EAAKC,IAAcD,EAAME,EAA0BD,CAAS,EAAG,CAAC,EACpH,GAAIF,IAA2BV,EAC7B,MAAM,IAAIc,EAAgChB,EAAME,EAAkBU,CAAsB,EAG1F,MAAO,CAAE,aAAAL,EAAc,cAAAC,CAAc,CACvC,CD5BO,SAASS,GAAiBC,EAAwB,CACvD,IAAMC,EAAcC,EAAYC,EAASH,EAAM,EAAG,EAAE,CAAC,EAErD,MAAO,CACL,UAFgBE,EAAYC,EAASH,EAAM,GAAI,EAAE,CAAC,EAGlD,YAAAC,CACF,CACF","names":["hexToString","sliceHex","assertExhaustive","arrayAbiTypeToStaticAbiType","staticAbiTypeToByteLength","getAddress","hexToBigInt","hexToBool","assertExhaustive","staticAbiTypeToByteLength","staticAbiTypeToDefaultValue","MUDError","staticAbiTypeToByteLength","InvalidHexLengthError","value","InvalidHexLengthForSchemaError","InvalidHexLengthForPackedCounterError","InvalidHexLengthForStaticFieldError","abiType","InvalidHexLengthForArrayFieldError","SchemaStaticLengthMismatchError","schemaData","definedLength","summedLength","PackedCounterLengthMismatchError","packedCounterData","decodeStaticField","abiType","data","InvalidHexLengthError","staticAbiTypeToByteLength","InvalidHexLengthForStaticFieldError","value","hexToBigInt","defaultValueType","staticAbiTypeToDefaultValue","hexToBool","getAddress","assertExhaustive","decodeDynamicField","abiType","data","hexToString","InvalidHexLengthError","dataSize","staticAbiType","arrayAbiTypeToStaticAbiType","itemByteLength","staticAbiTypeToByteLength","InvalidHexLengthForArrayFieldError","_","i","itemData","sliceHex","decodeStaticField","assertExhaustive","decodeAbiParameters","decodeKeyTuple","keySchema","keyTuple","key","index","staticAbiTypeToByteLength","sliceHex","sliceHex","hexToPackedCounter","data","InvalidHexLengthForPackedCounterError","totalByteLength","decodeStaticField","sliceHex","fieldByteLengths","decodeDynamicField","summedLength","total","length","PackedCounterLengthMismatchError","staticAbiTypeToByteLength","staticDataLength","staticFields","length","fieldType","decodeRecord","schema","data","values","bytesOffset","fieldType","fieldByteLength","staticAbiTypeToByteLength","value","decodeStaticField","sliceHex","schemaStaticDataLength","staticDataLength","actualStaticDataLength","dataLayout","hexToPackedCounter","i","dataLength","decodeDynamicField","actualDynamicDataLength","arrayAbiTypeToStaticAbiType","isArrayAbiType","encodePacked","encodeField","fieldType","value","staticFieldType","encodeRecord","schema","values","staticValues","dynamicValues","staticData","value","i","encodeField","dynamicDataItems","dynamicFieldByteLengths","dynamicTotalByteLength","total","length","dynamicData","packedCounter","sliceHex","schemaAbiTypes","staticAbiTypeToByteLength","hexToNumber","sliceHex","hexToSchema","data","InvalidHexLengthForSchemaError","staticDataLength","hexToNumber","sliceHex","numStaticFields","numDynamicFields","staticFields","dynamicFields","i","schemaTypeIndex","schemaAbiTypes","actualStaticDataLength","acc","fieldType","staticAbiTypeToByteLength","SchemaStaticLengthMismatchError","hexToTableSchema","data","valueSchema","hexToSchema","sliceHex"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@latticexyz/protocol-parser",
|
|
3
|
+
"version": "1.42.0",
|
|
4
|
+
"description": "Parser utilities for the MUD protocol",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/latticexyz/mud.git",
|
|
8
|
+
"directory": "packages/protocol-parser"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"types": "src/index.ts",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"abitype": "0.8.7",
|
|
18
|
+
"viem": "1.1.7",
|
|
19
|
+
"@latticexyz/common": "1.42.0",
|
|
20
|
+
"@latticexyz/schema-type": "1.42.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"tsup": "^6.7.0",
|
|
24
|
+
"vitest": "0.31.4"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "pnpm run build:js",
|
|
32
|
+
"build:js": "tsup",
|
|
33
|
+
"clean": "pnpm run clean:js",
|
|
34
|
+
"clean:js": "rimraf dist",
|
|
35
|
+
"dev": "tsup --watch",
|
|
36
|
+
"lint": "eslint .",
|
|
37
|
+
"test": "vitest typecheck --run && vitest --run"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type";
|
|
2
|
+
|
|
3
|
+
export type Schema = {
|
|
4
|
+
readonly staticFields: readonly StaticAbiType[];
|
|
5
|
+
readonly dynamicFields: readonly DynamicAbiType[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type TableSchema = {
|
|
9
|
+
readonly keySchema: Schema; // TODO: refine to set dynamicFields to []
|
|
10
|
+
readonly valueSchema: Schema;
|
|
11
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { assertType, describe, it } from "vitest";
|
|
2
|
+
import { Hex } from "viem";
|
|
3
|
+
import { decodeDynamicField } from "./decodeDynamicField";
|
|
4
|
+
|
|
5
|
+
describe("decodeDynamicField", () => {
|
|
6
|
+
it("returns a boolean array for bool[] ABI type", () => {
|
|
7
|
+
assertType<boolean[]>(decodeDynamicField("bool[]", "0x"));
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("returns a number array or bigint array for uint[] ABI types", () => {
|
|
11
|
+
assertType<number[]>(decodeDynamicField("uint8[]", "0x"));
|
|
12
|
+
assertType<number[]>(decodeDynamicField("uint16[]", "0x"));
|
|
13
|
+
assertType<number[]>(decodeDynamicField("uint32[]", "0x"));
|
|
14
|
+
assertType<number[]>(decodeDynamicField("uint48[]", "0x"));
|
|
15
|
+
assertType<bigint[]>(decodeDynamicField("uint56[]", "0x"));
|
|
16
|
+
assertType<bigint[]>(decodeDynamicField("uint128[]", "0x"));
|
|
17
|
+
assertType<bigint[]>(decodeDynamicField("uint256[]", "0x"));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns a number array or bigint array for int[] ABI types", () => {
|
|
21
|
+
assertType<number[]>(decodeDynamicField("int8[]", "0x"));
|
|
22
|
+
assertType<number[]>(decodeDynamicField("int16[]", "0x"));
|
|
23
|
+
assertType<number[]>(decodeDynamicField("int32[]", "0x"));
|
|
24
|
+
assertType<number[]>(decodeDynamicField("int48[]", "0x"));
|
|
25
|
+
assertType<bigint[]>(decodeDynamicField("int56[]", "0x"));
|
|
26
|
+
assertType<bigint[]>(decodeDynamicField("int128[]", "0x"));
|
|
27
|
+
assertType<bigint[]>(decodeDynamicField("int256[]", "0x"));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("returns a hex array for bytes[] ABI types", () => {
|
|
31
|
+
assertType<Hex[]>(decodeDynamicField("bytes1[]", "0x"));
|
|
32
|
+
assertType<Hex[]>(decodeDynamicField("bytes2[]", "0x"));
|
|
33
|
+
assertType<Hex[]>(decodeDynamicField("bytes8[]", "0x"));
|
|
34
|
+
assertType<Hex[]>(decodeDynamicField("bytes32[]", "0x"));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("returns a hex array for address ABI type", () => {
|
|
38
|
+
assertType<Hex[]>(decodeDynamicField("address[]", "0x"));
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns a hex for bytes ABI type", () => {
|
|
42
|
+
assertType<Hex>(decodeDynamicField("bytes", "0x"));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("returns a string for string ABI type", () => {
|
|
46
|
+
assertType<string>(decodeDynamicField("string", "0x"));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { decodeDynamicField } from "./decodeDynamicField";
|
|
3
|
+
|
|
4
|
+
describe("decodeDynamicField", () => {
|
|
5
|
+
it("can decode bool[]", () => {
|
|
6
|
+
expect(decodeDynamicField("bool[]", "0x00")).toStrictEqual([false]);
|
|
7
|
+
expect(decodeDynamicField("bool[]", "0x01")).toStrictEqual([true]);
|
|
8
|
+
expect(decodeDynamicField("bool[]", "0x0000")).toStrictEqual([false, false]);
|
|
9
|
+
expect(decodeDynamicField("bool[]", "0x0001")).toStrictEqual([false, true]);
|
|
10
|
+
expect(decodeDynamicField("bool[]", "0x0100")).toStrictEqual([true, false]);
|
|
11
|
+
expect(decodeDynamicField("bool[]", "0x0101")).toStrictEqual([true, true]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("can decode uint8[]", () => {
|
|
15
|
+
expect(decodeDynamicField("uint8[]", "0x00")).toStrictEqual([0]);
|
|
16
|
+
expect(decodeDynamicField("uint8[]", "0x01")).toStrictEqual([1]);
|
|
17
|
+
expect(decodeDynamicField("uint8[]", "0xff")).toStrictEqual([255]);
|
|
18
|
+
expect(decodeDynamicField("uint8[]", "0x0000")).toStrictEqual([0, 0]);
|
|
19
|
+
expect(decodeDynamicField("uint8[]", "0x0101")).toStrictEqual([1, 1]);
|
|
20
|
+
expect(decodeDynamicField("uint8[]", "0xffff")).toStrictEqual([255, 255]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("can decode uint256[]", () => {
|
|
24
|
+
expect(
|
|
25
|
+
decodeDynamicField("uint256[]", "0x0000000000000000000000000000000000000000000000000000000000000000")
|
|
26
|
+
).toStrictEqual([0n]);
|
|
27
|
+
expect(
|
|
28
|
+
decodeDynamicField("uint256[]", "0x0000000000000000000000000000000000000000000000000000000000000001")
|
|
29
|
+
).toStrictEqual([1n]);
|
|
30
|
+
expect(
|
|
31
|
+
decodeDynamicField("uint256[]", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
32
|
+
).toStrictEqual([115792089237316195423570985008687907853269984665640564039457584007913129639935n]);
|
|
33
|
+
expect(
|
|
34
|
+
decodeDynamicField("uint256[]", "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe")
|
|
35
|
+
).toStrictEqual([115792089237316195423570985008687907853269984665640564039457584007913129639934n]);
|
|
36
|
+
expect(
|
|
37
|
+
decodeDynamicField(
|
|
38
|
+
"uint256[]",
|
|
39
|
+
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001"
|
|
40
|
+
)
|
|
41
|
+
).toStrictEqual([1n, 1n]);
|
|
42
|
+
expect(
|
|
43
|
+
decodeDynamicField(
|
|
44
|
+
"uint256[]",
|
|
45
|
+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
|
46
|
+
)
|
|
47
|
+
).toStrictEqual([
|
|
48
|
+
115792089237316195423570985008687907853269984665640564039457584007913129639935n,
|
|
49
|
+
115792089237316195423570985008687907853269984665640564039457584007913129639935n,
|
|
50
|
+
]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("can decode int8[]", () => {
|
|
54
|
+
expect(decodeDynamicField("int8[]", "0x00")).toStrictEqual([0]);
|
|
55
|
+
expect(decodeDynamicField("int8[]", "0x01")).toStrictEqual([1]);
|
|
56
|
+
expect(decodeDynamicField("int8[]", "0x7f")).toStrictEqual([127]);
|
|
57
|
+
expect(decodeDynamicField("int8[]", "0x80")).toStrictEqual([-128]);
|
|
58
|
+
expect(decodeDynamicField("int8[]", "0x81")).toStrictEqual([-127]);
|
|
59
|
+
expect(decodeDynamicField("int8[]", "0xff")).toStrictEqual([-1]);
|
|
60
|
+
|
|
61
|
+
expect(decodeDynamicField("int8[]", "0x0000")).toStrictEqual([0, 0]);
|
|
62
|
+
expect(decodeDynamicField("int8[]", "0x0100")).toStrictEqual([1, 0]);
|
|
63
|
+
expect(decodeDynamicField("int8[]", "0x007f")).toStrictEqual([0, 127]);
|
|
64
|
+
expect(decodeDynamicField("int8[]", "0x8080")).toStrictEqual([-128, -128]);
|
|
65
|
+
expect(decodeDynamicField("int8[]", "0x8181")).toStrictEqual([-127, -127]);
|
|
66
|
+
expect(decodeDynamicField("int8[]", "0x00ff")).toStrictEqual([0, -1]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("can decode int256[]", () => {
|
|
70
|
+
expect(
|
|
71
|
+
decodeDynamicField("int256[]", "0x0000000000000000000000000000000000000000000000000000000000000000")
|
|
72
|
+
).toStrictEqual([0n]);
|
|
73
|
+
expect(
|
|
74
|
+
decodeDynamicField("int256[]", "0x0000000000000000000000000000000000000000000000000000000000000001")
|
|
75
|
+
).toStrictEqual([1n]);
|
|
76
|
+
expect(
|
|
77
|
+
decodeDynamicField("int256[]", "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
78
|
+
).toStrictEqual([57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
79
|
+
expect(
|
|
80
|
+
decodeDynamicField("int256[]", "0x8000000000000000000000000000000000000000000000000000000000000000")
|
|
81
|
+
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819968n]);
|
|
82
|
+
expect(
|
|
83
|
+
decodeDynamicField("int256[]", "0x8000000000000000000000000000000000000000000000000000000000000001")
|
|
84
|
+
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
85
|
+
expect(
|
|
86
|
+
decodeDynamicField("int256[]", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
87
|
+
).toStrictEqual([-1n]);
|
|
88
|
+
|
|
89
|
+
expect(
|
|
90
|
+
decodeDynamicField(
|
|
91
|
+
"int256[]",
|
|
92
|
+
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
|
93
|
+
)
|
|
94
|
+
).toStrictEqual([57896044618658097711785492504343953926634992332820282019728792003956564819967n, -1n]);
|
|
95
|
+
expect(
|
|
96
|
+
decodeDynamicField(
|
|
97
|
+
"int256[]",
|
|
98
|
+
"0x80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
99
|
+
)
|
|
100
|
+
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819968n, 0n]);
|
|
101
|
+
expect(
|
|
102
|
+
decodeDynamicField(
|
|
103
|
+
"int256[]",
|
|
104
|
+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000001"
|
|
105
|
+
)
|
|
106
|
+
).toStrictEqual([-1n, -57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("can decode bytes arrays", () => {
|
|
110
|
+
expect(decodeDynamicField("bytes1[]", "0x01")).toStrictEqual(["0x01"]);
|
|
111
|
+
expect(decodeDynamicField("bytes1[]", "0x0001")).toStrictEqual(["0x00", "0x01"]);
|
|
112
|
+
expect(decodeDynamicField("bytes2[]", "0x0001")).toStrictEqual(["0x0001"]);
|
|
113
|
+
expect(decodeDynamicField("bytes8[]", "0xff00ff00ff00ff00")).toStrictEqual(["0xff00ff00ff00ff00"]);
|
|
114
|
+
expect(decodeDynamicField("bytes1[]", "0xff00ff00ff00ff00")).toStrictEqual([
|
|
115
|
+
"0xff",
|
|
116
|
+
"0x00",
|
|
117
|
+
"0xff",
|
|
118
|
+
"0x00",
|
|
119
|
+
"0xff",
|
|
120
|
+
"0x00",
|
|
121
|
+
"0xff",
|
|
122
|
+
"0x00",
|
|
123
|
+
]);
|
|
124
|
+
expect(decodeDynamicField("bytes2[]", "0xff00ff00ff00ff00")).toStrictEqual([
|
|
125
|
+
"0xff00",
|
|
126
|
+
"0xff00",
|
|
127
|
+
"0xff00",
|
|
128
|
+
"0xff00",
|
|
129
|
+
]);
|
|
130
|
+
expect(decodeDynamicField("bytes4[]", "0xff00ff00ff00ff00")).toStrictEqual(["0xff00ff00", "0xff00ff00"]);
|
|
131
|
+
expect(
|
|
132
|
+
decodeDynamicField("bytes32[]", "0x0000000000000000000000000000000000000000000000000000000000000001")
|
|
133
|
+
).toStrictEqual(["0x0000000000000000000000000000000000000000000000000000000000000001"]);
|
|
134
|
+
expect(
|
|
135
|
+
decodeDynamicField(
|
|
136
|
+
"bytes32[]",
|
|
137
|
+
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
|
|
138
|
+
)
|
|
139
|
+
).toStrictEqual([
|
|
140
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
141
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
142
|
+
]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("can decode address[]", () => {
|
|
146
|
+
expect(decodeDynamicField("address[]", "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")).toStrictEqual([
|
|
147
|
+
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
|
|
148
|
+
]);
|
|
149
|
+
expect(decodeDynamicField("address[]", "0xffffffffffffffffffffffffffffffffffffffff")).toStrictEqual([
|
|
150
|
+
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
expect(() => decodeDynamicField("address[]", "0x00")).toThrow(
|
|
154
|
+
'Hex value "0x00" has length of 2, but expected a multiple of 40 for address[] type.'
|
|
155
|
+
);
|
|
156
|
+
expect(() => decodeDynamicField("address[]", "0xffffffffffffffffffffffffffffffffffffffffff")).toThrow(
|
|
157
|
+
'Hex value "0xffffffffffffffffffffffffffffffffffffffffff" has length of 42, but expected a multiple of 40 for address[] type.'
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("can decode bytes", () => {
|
|
162
|
+
expect(decodeDynamicField("bytes", "0x")).toBe("0x");
|
|
163
|
+
expect(decodeDynamicField("bytes", "0x01")).toBe("0x01");
|
|
164
|
+
expect(decodeDynamicField("bytes", "0x0001")).toBe("0x0001");
|
|
165
|
+
expect(decodeDynamicField("bytes", "0xff00ff00ff00ff00")).toBe("0xff00ff00ff00ff00");
|
|
166
|
+
expect(decodeDynamicField("bytes", "0x0000000000000000000000000000000000000000000000000000000000000001")).toBe(
|
|
167
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("can decode string", () => {
|
|
172
|
+
expect(decodeDynamicField("string", "0x")).toBe("");
|
|
173
|
+
expect(decodeDynamicField("string", "0x68656c6c6f")).toBe("hello");
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Hex, hexToString, sliceHex } from "viem";
|
|
2
|
+
import { assertExhaustive } from "@latticexyz/common/utils";
|
|
3
|
+
import {
|
|
4
|
+
DynamicAbiType,
|
|
5
|
+
DynamicAbiTypeToPrimitiveType,
|
|
6
|
+
arrayAbiTypeToStaticAbiType,
|
|
7
|
+
staticAbiTypeToByteLength,
|
|
8
|
+
} from "@latticexyz/schema-type";
|
|
9
|
+
import { decodeStaticField } from "./decodeStaticField";
|
|
10
|
+
import { InvalidHexLengthError, InvalidHexLengthForArrayFieldError } from "./errors";
|
|
11
|
+
|
|
12
|
+
// TODO: require size/length for dynamic fields, because hex might be longer (sometimes we just move the length pointer of arrays without updating the value)
|
|
13
|
+
// this means the error should probably change from "invalid length" to "too short", which viem has that we could reuse?
|
|
14
|
+
|
|
15
|
+
export function decodeDynamicField<
|
|
16
|
+
TAbiType extends DynamicAbiType,
|
|
17
|
+
TPrimitiveType extends DynamicAbiTypeToPrimitiveType<TAbiType>
|
|
18
|
+
>(abiType: TAbiType, data: Hex): TPrimitiveType {
|
|
19
|
+
if (abiType === "bytes") {
|
|
20
|
+
return data as TPrimitiveType;
|
|
21
|
+
}
|
|
22
|
+
if (abiType === "string") {
|
|
23
|
+
return hexToString(data) as TPrimitiveType;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (data.length > 3 && data.length % 2 !== 0) {
|
|
27
|
+
throw new InvalidHexLengthError(data);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const dataSize = (data.length - 2) / 2;
|
|
31
|
+
|
|
32
|
+
switch (abiType) {
|
|
33
|
+
case "uint8[]":
|
|
34
|
+
case "uint16[]":
|
|
35
|
+
case "uint24[]":
|
|
36
|
+
case "uint32[]":
|
|
37
|
+
case "uint40[]":
|
|
38
|
+
case "uint48[]":
|
|
39
|
+
case "uint56[]":
|
|
40
|
+
case "uint64[]":
|
|
41
|
+
case "uint72[]":
|
|
42
|
+
case "uint80[]":
|
|
43
|
+
case "uint88[]":
|
|
44
|
+
case "uint96[]":
|
|
45
|
+
case "uint104[]":
|
|
46
|
+
case "uint112[]":
|
|
47
|
+
case "uint120[]":
|
|
48
|
+
case "uint128[]":
|
|
49
|
+
case "uint136[]":
|
|
50
|
+
case "uint144[]":
|
|
51
|
+
case "uint152[]":
|
|
52
|
+
case "uint160[]":
|
|
53
|
+
case "uint168[]":
|
|
54
|
+
case "uint176[]":
|
|
55
|
+
case "uint184[]":
|
|
56
|
+
case "uint192[]":
|
|
57
|
+
case "uint200[]":
|
|
58
|
+
case "uint208[]":
|
|
59
|
+
case "uint216[]":
|
|
60
|
+
case "uint224[]":
|
|
61
|
+
case "uint232[]":
|
|
62
|
+
case "uint240[]":
|
|
63
|
+
case "uint248[]":
|
|
64
|
+
case "uint256[]":
|
|
65
|
+
case "int8[]":
|
|
66
|
+
case "int16[]":
|
|
67
|
+
case "int24[]":
|
|
68
|
+
case "int32[]":
|
|
69
|
+
case "int40[]":
|
|
70
|
+
case "int48[]":
|
|
71
|
+
case "int56[]":
|
|
72
|
+
case "int64[]":
|
|
73
|
+
case "int72[]":
|
|
74
|
+
case "int80[]":
|
|
75
|
+
case "int88[]":
|
|
76
|
+
case "int96[]":
|
|
77
|
+
case "int104[]":
|
|
78
|
+
case "int112[]":
|
|
79
|
+
case "int120[]":
|
|
80
|
+
case "int128[]":
|
|
81
|
+
case "int136[]":
|
|
82
|
+
case "int144[]":
|
|
83
|
+
case "int152[]":
|
|
84
|
+
case "int160[]":
|
|
85
|
+
case "int168[]":
|
|
86
|
+
case "int176[]":
|
|
87
|
+
case "int184[]":
|
|
88
|
+
case "int192[]":
|
|
89
|
+
case "int200[]":
|
|
90
|
+
case "int208[]":
|
|
91
|
+
case "int216[]":
|
|
92
|
+
case "int224[]":
|
|
93
|
+
case "int232[]":
|
|
94
|
+
case "int240[]":
|
|
95
|
+
case "int248[]":
|
|
96
|
+
case "int256[]":
|
|
97
|
+
case "bytes1[]":
|
|
98
|
+
case "bytes2[]":
|
|
99
|
+
case "bytes3[]":
|
|
100
|
+
case "bytes4[]":
|
|
101
|
+
case "bytes5[]":
|
|
102
|
+
case "bytes6[]":
|
|
103
|
+
case "bytes7[]":
|
|
104
|
+
case "bytes8[]":
|
|
105
|
+
case "bytes9[]":
|
|
106
|
+
case "bytes10[]":
|
|
107
|
+
case "bytes11[]":
|
|
108
|
+
case "bytes12[]":
|
|
109
|
+
case "bytes13[]":
|
|
110
|
+
case "bytes14[]":
|
|
111
|
+
case "bytes15[]":
|
|
112
|
+
case "bytes16[]":
|
|
113
|
+
case "bytes17[]":
|
|
114
|
+
case "bytes18[]":
|
|
115
|
+
case "bytes19[]":
|
|
116
|
+
case "bytes20[]":
|
|
117
|
+
case "bytes21[]":
|
|
118
|
+
case "bytes22[]":
|
|
119
|
+
case "bytes23[]":
|
|
120
|
+
case "bytes24[]":
|
|
121
|
+
case "bytes25[]":
|
|
122
|
+
case "bytes26[]":
|
|
123
|
+
case "bytes27[]":
|
|
124
|
+
case "bytes28[]":
|
|
125
|
+
case "bytes29[]":
|
|
126
|
+
case "bytes30[]":
|
|
127
|
+
case "bytes31[]":
|
|
128
|
+
case "bytes32[]":
|
|
129
|
+
case "bool[]":
|
|
130
|
+
case "address[]": {
|
|
131
|
+
const staticAbiType = arrayAbiTypeToStaticAbiType(abiType);
|
|
132
|
+
const itemByteLength = staticAbiTypeToByteLength[staticAbiType];
|
|
133
|
+
if (dataSize % itemByteLength !== 0) {
|
|
134
|
+
throw new InvalidHexLengthForArrayFieldError(staticAbiType, data);
|
|
135
|
+
}
|
|
136
|
+
const items = new Array(dataSize / itemByteLength).fill(undefined).map((_, i) => {
|
|
137
|
+
const itemData = sliceHex(data, i * itemByteLength, (i + 1) * itemByteLength);
|
|
138
|
+
return decodeStaticField(staticAbiType, itemData);
|
|
139
|
+
});
|
|
140
|
+
return items as TPrimitiveType;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return assertExhaustive(abiType, `Unsupported dynamic ABI type: ${abiType}`);
|
|
145
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { decodeKeyTuple } from "./decodeKeyTuple";
|
|
3
|
+
import { Schema } from "./common";
|
|
4
|
+
|
|
5
|
+
describe("decodeKeyTuple", () => {
|
|
6
|
+
it("can decode bool key tuple", () => {
|
|
7
|
+
expect(
|
|
8
|
+
decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
|
|
9
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
10
|
+
])
|
|
11
|
+
).toStrictEqual([false]);
|
|
12
|
+
expect(
|
|
13
|
+
decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
|
|
14
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
15
|
+
])
|
|
16
|
+
).toStrictEqual([true]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("can decode complex key tuple", () => {
|
|
20
|
+
expect(
|
|
21
|
+
decodeKeyTuple({ staticFields: ["uint256", "int32", "bytes16", "address", "bool", "int8"], dynamicFields: [] }, [
|
|
22
|
+
"0x000000000000000000000000000000000000000000000000000000000000002a",
|
|
23
|
+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6",
|
|
24
|
+
"0x1234000000000000000000000000000000000000000000000000000000000000",
|
|
25
|
+
"0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
|
|
26
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
27
|
+
"0x0000000000000000000000000000000000000000000000000000000000000003",
|
|
28
|
+
])
|
|
29
|
+
).toStrictEqual([
|
|
30
|
+
42n,
|
|
31
|
+
-42,
|
|
32
|
+
"0x12340000000000000000000000000000",
|
|
33
|
+
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
|
|
34
|
+
true,
|
|
35
|
+
3,
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Hex, decodeAbiParameters } from "viem";
|
|
2
|
+
import { StaticPrimitiveType } from "@latticexyz/schema-type";
|
|
3
|
+
import { Schema } from "./common";
|
|
4
|
+
|
|
5
|
+
// key tuples are encoded in the same way as abi.encode, so we can decode them with viem
|
|
6
|
+
|
|
7
|
+
export function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {
|
|
8
|
+
return keyTuple.map(
|
|
9
|
+
(key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { decodeRecord } from "./decodeRecord";
|
|
3
|
+
|
|
4
|
+
describe("decodeRecord", () => {
|
|
5
|
+
it("can decode hex to record values", () => {
|
|
6
|
+
const schema = { staticFields: ["uint32", "uint128"], dynamicFields: ["uint32[]", "string"] } as const;
|
|
7
|
+
const values = decodeRecord(
|
|
8
|
+
schema,
|
|
9
|
+
"0x0000000100000000000000000000000000000002000000000000130000000008000000000b0000000000000000000000000000000000000300000004736f6d6520737472696e67"
|
|
10
|
+
);
|
|
11
|
+
expect(values).toStrictEqual([1, 2n, [3, 4], "some string"]);
|
|
12
|
+
});
|
|
13
|
+
});
|