@latticexyz/protocol-parser 2.0.0-alpha.1.257 → 2.0.0-alpha.1.259

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/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{staticAbiTypeToByteLength as w}from"@latticexyz/schema-type";import{schemaAbiTypes as S,staticAbiTypeToByteLength as P}from"@latticexyz/schema-type";function A(t,e=[]){let s=t.reduce((i,a)=>i+P[a],0),c=t.length,n=e.length,r=t.map(i=>S.indexOf(i)),y=e.map(i=>S.indexOf(i));return`0x${[s.toString(16).padStart(4,"0"),c.toString(16).padStart(2,"0"),n.toString(16).padStart(2,"0"),...r.map(i=>i.toString(16).padStart(2,"0")),...y.map(i=>i.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}function X(t,e=[]){return{staticDataLength:t.reduce((c,n)=>c+w[n],0),staticFields:t,dynamicFields:e,isEmpty:!1,schemaData:A(t,e)}}import{hexToString as U,sliceHex as z}from"viem";import{assertExhaustive as M}from"@latticexyz/common/utils";import{arrayAbiTypeToStaticAbiType as V,staticAbiTypeToByteLength as N}from"@latticexyz/schema-type";import{getAddress as B,hexToBigInt as E,hexToBool as I}from"viem";import{assertExhaustive as F}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as k,staticAbiTypeToDefaultValue as C}from"@latticexyz/schema-type";import{MUDError as o}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as v}from"@latticexyz/schema-type";var m=class extends o{name="InvalidHexValueError";constructor(e){super(`Hex value "${e}" is an odd length (${e.length-2}). It must be an even length.`)}},b=class extends o{name="InvalidHexLengthForSchemaError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a schema.`)}},h=class extends o{name="InvalidHexLengthForPackedCounterError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a packed counter.`)}},T=class extends o{name="InvalidHexLengthForStaticFieldError";constructor(e,s){super(`Hex value "${s}" has length of ${s.length-2}, but expected length of ${v[e]*2} for ${e} type.`)}},x=class extends o{name="InvalidHexLengthForArrayFieldError";constructor(e,s){super(`Hex value "${s}" has length of ${s.length-2}, but expected a multiple of ${v[e]*2} for ${e}[] type.`)}},f=class extends o{name="SchemaStaticLengthMismatchError";constructor(e,s,c){super(`Schema "${e}" static data length (${s}) did not match the summed length of all static fields (${c}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`)}},l=class extends o{name="PackedCounterLengthMismatchError";constructor(e,s,c){super(`PackedCounter "${e}" total bytes length (${s}) did not match the summed length of all field byte lengths (${c}).`)}};function d(t,e){if(e.length>3&&e.length%2!==0)throw new m(e);if((e.length-2)/2!==k[t])throw new T(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 c=E(e,{signed:t.startsWith("int")}),n=typeof C[t];if(n==="number")return Number(c);if(n==="bigint")return c;throw new Error(`Unexpected default value type (${n}) 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 I(e);case"address":return B(e)}return F(t,`Unsupported static ABI type: ${t}`)}function H(t,e){if(t==="bytes")return e;if(t==="string")return U(e);if(e.length>3&&e.length%2!==0)throw new m(e);let s=(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 c=V(t),n=N[c];if(s%n!==0)throw new x(c,e);return new Array(s/n).fill(void 0).map((y,i)=>{let a=z(e,i*n,(i+1)*n);return d(c,a)})}}return M(t,`Unsupported dynamic ABI type: ${t}`)}import{decodeAbiParameters as O}from"viem";function Ae(t,e){return e.map((s,c)=>O([{type:t.staticFields[c]}],s)[0])}import{sliceHex as D}from"viem";function we(t){if(t.length!==66)throw new h(t);let e=d("uint56",D(t,0,7)),s=H("uint40[]",D(t,7)),c=BigInt(s.reduce((n,r)=>n+r,0));if(c!==e)throw new l(t,e,c);return{totalByteLength:e,fieldByteLengths:s}}import{hexToNumber as p,sliceHex as u}from"viem";import{staticAbiTypeToByteLength as j,schemaAbiTypes as L}from"@latticexyz/schema-type";function g(t){if(t.length!==66)throw new b(t);let e=p(u(t,0,2)),s=p(u(t,2,3)),c=p(u(t,3,4)),n=[],r=[];for(let i=4;i<4+s;i++){let a=p(u(t,i,i+1));n.push(L[a])}for(let i=4+s;i<4+s+c;i++){let a=p(u(t,i,i+1));r.push(L[a])}let y=n.reduce((i,a)=>i+j[a],0);if(y!==e)throw new f(t,e,y);return{staticDataLength:e,staticFields:n,dynamicFields:r,isEmpty:t==="0x",schemaData:t}}import{sliceHex as $}from"viem";function Oe(t){let e=g($(t,0,32));return{keySchema:g($(t,32,64)),valueSchema:e,isEmpty:t==="0x",schemaData:t}}export{m as InvalidHexLengthError,x as InvalidHexLengthForArrayFieldError,h as InvalidHexLengthForPackedCounterError,b as InvalidHexLengthForSchemaError,T as InvalidHexLengthForStaticFieldError,l as PackedCounterLengthMismatchError,f as SchemaStaticLengthMismatchError,X as abiTypesToSchema,A as abiTypesToSchemaData,H as decodeDynamicField,Ae as decodeKeyTuple,d as decodeStaticField,we as hexToPackedCounter,g as hexToSchema,Oe as hexToTableSchema};
1
+ import{hexToString as I,sliceHex as E}from"viem";import{assertExhaustive as k}from"@latticexyz/common/utils";import{arrayAbiTypeToStaticAbiType as C,staticAbiTypeToByteLength as U}from"@latticexyz/schema-type";import{getAddress as $,hexToBigInt as w,hexToBool as L}from"viem";import{assertExhaustive as B}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as F,staticAbiTypeToDefaultValue as D}from"@latticexyz/schema-type";import{MUDError as r}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as S}from"@latticexyz/schema-type";var o=class extends r{name="InvalidHexValueError";constructor(e){super(`Hex value "${e}" is an odd length (${e.length-2}). It must be an even length.`)}},p=class extends r{name="InvalidHexLengthForSchemaError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a schema.`)}},b=class extends r{name="InvalidHexLengthForPackedCounterError";constructor(e){super(`Hex value "${e}" has length of ${e.length-2}, but expected length of 64 for a packed counter.`)}},h=class extends r{name="InvalidHexLengthForStaticFieldError";constructor(e,s){super(`Hex value "${s}" has length of ${s.length-2}, but expected length of ${S[e]*2} for ${e} type.`)}},l=class extends r{name="InvalidHexLengthForArrayFieldError";constructor(e,s){super(`Hex value "${s}" has length of ${s.length-2}, but expected a multiple of ${S[e]*2} for ${e}[] type.`)}},x=class extends r{name="SchemaStaticLengthMismatchError";constructor(e,s,i){super(`Schema "${e}" static data length (${s}) did not match the summed length of all static fields (${i}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`)}},T=class extends r{name="PackedCounterLengthMismatchError";constructor(e,s,i){super(`PackedCounter "${e}" total bytes length (${s}) did not match the summed length of all field byte lengths (${i}).`)}};function f(t,e){if(e.length>3&&e.length%2!==0)throw new o(e);if((e.length-2)/2!==F[t])throw new h(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 i=w(e,{signed:t.startsWith("int")}),c=typeof D[t];if(c==="number")return Number(i);if(c==="bigint")return i;throw new Error(`Unexpected default value type (${c}) 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 L(e);case"address":return $(e)}return B(t,`Unsupported static ABI type: ${t}`)}function A(t,e){if(t==="bytes")return e;if(t==="string")return I(e);if(e.length>3&&e.length%2!==0)throw new o(e);let s=(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 i=C(t),c=U[i];if(s%c!==0)throw new l(i,e);return new Array(s/c).fill(void 0).map((d,n)=>{let a=E(e,n*c,(n+1)*c);return f(i,a)})}}return k(t,`Unsupported dynamic ABI type: ${t}`)}import{decodeAbiParameters as z}from"viem";function oe(t,e){return e.map((s,i)=>z([{type:t.staticFields[i]}],s)[0])}import{sliceHex as v}from"viem";function le(t){if(t.length!==66)throw new b(t);let e=f("uint56",v(t,0,7)),s=A("uint40[]",v(t,7)),i=BigInt(s.reduce((c,y)=>c+y,0));if(i!==e)throw new T(t,e,i);return{totalByteLength:e,fieldByteLengths:s}}import{sliceHex as P}from"viem";import{schemaAbiTypes as H,staticAbiTypeToByteLength as M}from"@latticexyz/schema-type";import{hexToNumber as m,sliceHex as u}from"viem";function g(t){if(t.length!==66)throw new p(t);let e=m(u(t,0,2)),s=m(u(t,2,3)),i=m(u(t,3,4)),c=[],y=[];for(let n=4;n<4+s;n++){let a=m(u(t,n,n+1));c.push(H[a])}for(let n=4+s;n<4+s+i;n++){let a=m(u(t,n,n+1));y.push(H[a])}let d=c.reduce((n,a)=>n+M[a],0);if(d!==e)throw new x(t,e,d);return{staticFields:c,dynamicFields:y}}function we(t){let e=g(P(t,0,32));return{keySchema:g(P(t,32,64)),valueSchema:e}}export{o as InvalidHexLengthError,l as InvalidHexLengthForArrayFieldError,b as InvalidHexLengthForPackedCounterError,p as InvalidHexLengthForSchemaError,h as InvalidHexLengthForStaticFieldError,T as PackedCounterLengthMismatchError,x as SchemaStaticLengthMismatchError,A as decodeDynamicField,oe as decodeKeyTuple,f as decodeStaticField,le as hexToPackedCounter,we as hexToTableSchema};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/abiTypesToSchema.ts","../src/abiTypesToSchemaData.ts","../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeKeyTuple.ts","../src/hexToPackedCounter.ts","../src/hexToSchema.ts","../src/hexToTableSchema.ts"],"sourcesContent":["import { StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\nimport { Schema } from \"./common\";\nimport { abiTypesToSchemaData } from \"./abiTypesToSchemaData\";\n\nexport function abiTypesToSchema(staticFields: StaticAbiType[], dynamicFields: DynamicAbiType[] = []): Schema {\n const staticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);\n return {\n staticDataLength,\n staticFields,\n dynamicFields,\n isEmpty: false,\n schemaData: abiTypesToSchemaData(staticFields, dynamicFields),\n };\n}\n","import { Hex } from \"viem\";\nimport { schemaAbiTypes, StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\n\nexport function abiTypesToSchemaData(staticFields: StaticAbiType[], dynamicFields: DynamicAbiType[] = []): Hex {\n const staticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);\n const numStaticFields = staticFields.length;\n const numDynamicFields = dynamicFields.length;\n const staticSchemaTypes = staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));\n const dynamicSchemaTypes = dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));\n return `0x${[\n staticDataLength.toString(16).padStart(4, \"0\"),\n numStaticFields.toString(16).padStart(2, \"0\"),\n numDynamicFields.toString(16).padStart(2, \"0\"),\n ...staticSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, \"0\")),\n ...dynamicSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, \"0\")),\n ]\n .join(\"\")\n .padEnd(64, \"0\")}`;\n}\n","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: Hex[]): StaticPrimitiveType[] {\n return keyTuple.map(\n (key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType\n );\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: 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((a, b) => a + b, 0));\n if (summedLength !== totalByteLength) {\n throw new PackedCounterLengthMismatchError(data, totalByteLength, summedLength);\n }\n\n return { totalByteLength, fieldByteLengths };\n}\n","import { Hex, hexToNumber, sliceHex } from \"viem\";\nimport { StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength, schemaAbiTypes } from \"@latticexyz/schema-type\";\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 {\n staticDataLength,\n staticFields,\n dynamicFields,\n isEmpty: data === \"0x\",\n schemaData: data,\n };\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 isEmpty: data === \"0x\",\n schemaData: data,\n };\n}\n"],"mappings":"AAAA,OAAwC,6BAAAA,MAAiC,0BCCzE,OAAS,kBAAAC,EAA+C,6BAAAC,MAAiC,0BAElF,SAASC,EAAqBC,EAA+BC,EAAkC,CAAC,EAAQ,CAC7G,IAAMC,EAAmBF,EAAa,OAAO,CAACG,EAAKC,IAAcD,EAAML,EAA0BM,CAAS,EAAG,CAAC,EACxGC,EAAkBL,EAAa,OAC/BM,EAAmBL,EAAc,OACjCM,EAAoBP,EAAa,IAAKQ,GAAYX,EAAe,QAAQW,CAAO,CAAC,EACjFC,EAAqBR,EAAc,IAAKO,GAAYX,EAAe,QAAQW,CAAO,CAAC,EACzF,MAAO,KAAK,CACVN,EAAiB,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAC7CG,EAAgB,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAC5CC,EAAiB,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAC7C,GAAGC,EAAkB,IAAKG,GAAeA,EAAW,SAAS,EAAE,E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,EAAM,EAAG,EAAI,CAAC,CAAC,EAC5DO,EAAa,KAAKG,EAAeD,CAAe,CAAkB,EAEpE,QAAS,EAAI,EAAIJ,EAAiB,EAAI,EAAIA,EAAkBC,EAAkB,IAAK,CACjF,IAAMG,EAAkBN,EAAYC,EAASJ,EAAM,EAAG,EAAI,CAAC,CAAC,EAC5DQ,EAAc,KAAKE,EAAeD,CAAe,CAAmB,EAItE,IAAME,EAAyBJ,EAAa,OAAO,CAACK,EAAKC,IAAcD,EAAME,EAA0BD,CAAS,EAAG,CAAC,EACpH,GAAIF,IAA2BT,EAC7B,MAAM,IAAIa,EAAgCf,EAAME,EAAkBS,CAAsB,EAG1F,MAAO,CACL,iBAAAT,EACA,aAAAK,EACA,cAAAC,EACA,QAASR,IAAS,KAClB,WAAYA,CACd,CACF,CCtCA,OAAc,YAAAgB,MAAgB,OAIvB,SAASC,GAAiBC,EAAwB,CACvD,IAAMC,EAAcC,EAAYC,EAASH,EAAM,EAAG,EAAE,CAAC,EAErD,MAAO,CACL,UAFgBE,EAAYC,EAASH,EAAM,GAAI,EAAE,CAAC,EAGlD,YAAAC,EACA,QAASD,IAAS,KAClB,WAAYA,CACd,CACF","names":["staticAbiTypeToByteLength","schemaAbiTypes","staticAbiTypeToByteLength","abiTypesToSchemaData","staticFields","dynamicFields","staticDataLength","acc","fieldType","numStaticFields","numDynamicFields","staticSchemaTypes","abiType","dynamicSchemaTypes","schemaType","abiTypesToSchema","staticFields","dynamicFields","acc","fieldType","staticAbiTypeToByteLength","abiTypesToSchemaData","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","_","itemData","sliceHex","decodeStaticField","assertExhaustive","decodeAbiParameters","decodeKeyTuple","keySchema","keyTuple","key","index","sliceHex","hexToPackedCounter","data","InvalidHexLengthForPackedCounterError","totalByteLength","decodeStaticField","sliceHex","fieldByteLengths","decodeDynamicField","summedLength","a","b","PackedCounterLengthMismatchError","hexToNumber","sliceHex","staticAbiTypeToByteLength","schemaAbiTypes","hexToSchema","data","InvalidHexLengthForSchemaError","staticDataLength","hexToNumber","sliceHex","numStaticFields","numDynamicFields","staticFields","dynamicFields","schemaTypeIndex","schemaAbiTypes","actualStaticDataLength","acc","fieldType","staticAbiTypeToByteLength","SchemaStaticLengthMismatchError","sliceHex","hexToTableSchema","data","valueSchema","hexToSchema","sliceHex"]}
1
+ {"version":3,"sources":["../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeKeyTuple.ts","../src/hexToPackedCounter.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 { 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 { 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,EAAI,SAC/BA,SACV,CACF,CACF,EAEaC,EAAN,cAAiDR,CAAS,CACtD,KAAO,qCAChB,YAAYO,EAAwBJ,EAAY,CAC9C,MACE,cAAcA,oBAAwBA,EAAM,OAAS,iCACnDF,EAA0BM,CAAO,EAAI,SAC/BA,WACV,CACF,CACF,EAEaE,EAAN,cAA8CT,CAAS,CACnD,KAAO,kCAChB,YAAYU,EAAiBC,E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,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","sliceHex","hexToPackedCounter","data","InvalidHexLengthForPackedCounterError","totalByteLength","decodeStaticField","sliceHex","fieldByteLengths","decodeDynamicField","summedLength","total","length","PackedCounterLengthMismatchError","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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latticexyz/protocol-parser",
3
- "version": "2.0.0-alpha.1.257+2be15c1b",
3
+ "version": "2.0.0-alpha.1.259+6feef632",
4
4
  "description": "Parser utilities for the MUD protocol",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,10 +23,8 @@
23
23
  "test": "vitest typecheck --run && vitest --run"
24
24
  },
25
25
  "dependencies": {
26
- "@latticexyz/common": "2.0.0-alpha.1.257+2be15c1b",
27
- "@latticexyz/config": "2.0.0-alpha.1.257+2be15c1b",
28
- "@latticexyz/schema-type": "2.0.0-alpha.1.257+2be15c1b",
29
- "@latticexyz/store": "2.0.0-alpha.1.257+2be15c1b",
26
+ "@latticexyz/common": "2.0.0-alpha.1.259+6feef632",
27
+ "@latticexyz/schema-type": "2.0.0-alpha.1.259+6feef632",
30
28
  "abitype": "0.8.7",
31
29
  "viem": "1.1.7"
32
30
  },
@@ -37,5 +35,5 @@
37
35
  "publishConfig": {
38
36
  "access": "public"
39
37
  },
40
- "gitHead": "2be15c1b61bc5d13a4003d42b640a571cd98d032"
38
+ "gitHead": "6feef63218866996a2b5469011736261f780d5c4"
41
39
  }
package/src/common.ts CHANGED
@@ -1,12 +1,11 @@
1
- import { Hex } from "viem";
2
1
  import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type";
3
2
 
4
- export type Schema = Readonly<{
5
- staticDataLength: number;
6
- staticFields: StaticAbiType[];
7
- dynamicFields: DynamicAbiType[];
8
- isEmpty: boolean;
9
- schemaData: Hex;
10
- }>;
3
+ export type Schema = {
4
+ readonly staticFields: readonly StaticAbiType[];
5
+ readonly dynamicFields: readonly DynamicAbiType[];
6
+ };
11
7
 
12
- export type TableSchema = { keySchema: Schema; valueSchema: Schema; isEmpty: boolean; schemaData: Hex };
8
+ export type TableSchema = {
9
+ readonly keySchema: Schema; // TODO: refine to set dynamicFields to []
10
+ readonly valueSchema: Schema;
11
+ };
@@ -1,20 +1,24 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { decodeKeyTuple } from "./decodeKeyTuple";
3
- import { abiTypesToSchema } from "./abiTypesToSchema";
3
+ import { Schema } from "./common";
4
4
 
5
5
  describe("decodeKeyTuple", () => {
6
6
  it("can decode bool key tuple", () => {
7
7
  expect(
8
- decodeKeyTuple(abiTypesToSchema(["bool"]), ["0x0000000000000000000000000000000000000000000000000000000000000000"])
8
+ decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
9
+ "0x0000000000000000000000000000000000000000000000000000000000000000",
10
+ ])
9
11
  ).toStrictEqual([false]);
10
12
  expect(
11
- decodeKeyTuple(abiTypesToSchema(["bool"]), ["0x0000000000000000000000000000000000000000000000000000000000000001"])
13
+ decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
14
+ "0x0000000000000000000000000000000000000000000000000000000000000001",
15
+ ])
12
16
  ).toStrictEqual([true]);
13
17
  });
14
18
 
15
19
  it("can decode complex key tuple", () => {
16
20
  expect(
17
- decodeKeyTuple(abiTypesToSchema(["uint256", "int32", "bytes16", "address", "bool", "int8"]), [
21
+ decodeKeyTuple({ staticFields: ["uint256", "int32", "bytes16", "address", "bool", "int8"], dynamicFields: [] }, [
18
22
  "0x000000000000000000000000000000000000000000000000000000000000002a",
19
23
  "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6",
20
24
  "0x1234000000000000000000000000000000000000000000000000000000000000",
@@ -4,7 +4,7 @@ import { Schema } from "./common";
4
4
 
5
5
  // key tuples are encoded in the same way as abi.encode, so we can decode them with viem
6
6
 
7
- export function decodeKeyTuple(keySchema: Schema, keyTuple: Hex[]): StaticPrimitiveType[] {
7
+ export function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {
8
8
  return keyTuple.map(
9
9
  (key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType
10
10
  );
@@ -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
+ });
@@ -0,0 +1,61 @@
1
+ import { StaticPrimitiveType, DynamicPrimitiveType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
2
+ import { Hex, sliceHex } from "viem";
3
+ import { Schema } from "./common";
4
+ import { decodeDynamicField } from "./decodeDynamicField";
5
+ import { decodeStaticField } from "./decodeStaticField";
6
+ import { hexToPackedCounter } from "./hexToPackedCounter";
7
+ import { staticDataLength } from "./staticDataLength";
8
+
9
+ export function decodeRecord(schema: Schema, data: Hex): readonly (StaticPrimitiveType | DynamicPrimitiveType)[] {
10
+ const values: (StaticPrimitiveType | DynamicPrimitiveType)[] = [];
11
+
12
+ let bytesOffset = 0;
13
+ schema.staticFields.forEach((fieldType) => {
14
+ const fieldByteLength = staticAbiTypeToByteLength[fieldType];
15
+ const value = decodeStaticField(fieldType, sliceHex(data, bytesOffset, bytesOffset + fieldByteLength));
16
+ bytesOffset += fieldByteLength;
17
+ values.push(value);
18
+ });
19
+
20
+ // Warn user if static data length doesn't match the schema, because data corruption might be possible.
21
+ const schemaStaticDataLength = staticDataLength(schema.staticFields);
22
+ const actualStaticDataLength = bytesOffset;
23
+ if (actualStaticDataLength !== schemaStaticDataLength) {
24
+ console.warn(
25
+ "Decoded static data length does not match schema's expected static data length. Data may get corrupted. Is `getStaticByteLength` outdated?",
26
+ {
27
+ expectedLength: schemaStaticDataLength,
28
+ actualLength: actualStaticDataLength,
29
+ bytesOffset,
30
+ }
31
+ );
32
+ }
33
+
34
+ if (schema.dynamicFields.length > 0) {
35
+ const dataLayout = hexToPackedCounter(sliceHex(data, bytesOffset, bytesOffset + 32));
36
+ bytesOffset += 32;
37
+
38
+ schema.dynamicFields.forEach((fieldType, i) => {
39
+ const dataLength = dataLayout.fieldByteLengths[i];
40
+ const value = decodeDynamicField(fieldType, sliceHex(data, bytesOffset, bytesOffset + dataLength));
41
+ bytesOffset += dataLength;
42
+ values.push(value);
43
+ });
44
+
45
+ // Warn user if dynamic data length doesn't match the schema, because data corruption might be possible.
46
+ const actualDynamicDataLength = bytesOffset - 32 - actualStaticDataLength;
47
+ // TODO: refactor this so we don't break for bytes offsets >UINT40
48
+ if (BigInt(actualDynamicDataLength) !== dataLayout.totalByteLength) {
49
+ console.warn(
50
+ "Decoded dynamic data length does not match data layout's expected data length. Data may get corrupted. Did the data layout change?",
51
+ {
52
+ expectedLength: dataLayout.totalByteLength,
53
+ actualLength: actualDynamicDataLength,
54
+ bytesOffset,
55
+ }
56
+ );
57
+ }
58
+ }
59
+
60
+ return values;
61
+ }
@@ -0,0 +1,17 @@
1
+ import { SchemaAbiType, arrayAbiTypeToStaticAbiType, isArrayAbiType } from "@latticexyz/schema-type";
2
+ import { AbiParameterToPrimitiveType } from "abitype";
3
+ import { Hex, encodePacked } from "viem";
4
+
5
+ export function encodeField<TSchemaAbiType extends SchemaAbiType>(
6
+ fieldType: TSchemaAbiType,
7
+ value: AbiParameterToPrimitiveType<{ type: TSchemaAbiType }>
8
+ ): Hex {
9
+ if (isArrayAbiType(fieldType) && Array.isArray(value)) {
10
+ const staticFieldType = arrayAbiTypeToStaticAbiType(fieldType);
11
+ return encodePacked(
12
+ value.map(() => staticFieldType),
13
+ value
14
+ );
15
+ }
16
+ return encodePacked([fieldType], [value]);
17
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { encodeRecord } from "./encodeRecord";
3
+
4
+ describe("encodeRecord", () => {
5
+ it("can encode a schema and record values to hex", () => {
6
+ const schema = { staticFields: ["uint32", "uint128"], dynamicFields: ["uint32[]", "string"] } as const;
7
+ const hex = encodeRecord(schema, [1, 2n, [3, 4], "some string"]);
8
+ expect(hex).toBe(
9
+ "0x0000000100000000000000000000000000000002000000000000130000000008000000000b0000000000000000000000000000000000000300000004736f6d6520737472696e67"
10
+ );
11
+ });
12
+ });
@@ -0,0 +1,28 @@
1
+ import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type";
2
+ import { Hex } from "viem";
3
+ import { encodeField } from "./encodeField";
4
+ import { Schema } from "./common";
5
+
6
+ export function encodeRecord(schema: Schema, values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[]): Hex {
7
+ const staticValues = values.slice(0, schema.staticFields.length) as readonly StaticPrimitiveType[];
8
+ const dynamicValues = values.slice(schema.staticFields.length) as readonly DynamicPrimitiveType[];
9
+
10
+ const staticData = staticValues
11
+ .map((value, i) => encodeField(schema.staticFields[i], value).replace(/^0x/, ""))
12
+ .join("");
13
+
14
+ const dynamicDataItems = dynamicValues.map((value, i) =>
15
+ encodeField(schema.dynamicFields[i], value).replace(/^0x/, "")
16
+ );
17
+
18
+ const dynamicFieldByteLengths = dynamicDataItems.map((value) => value.length / 2);
19
+ const dynamicTotalByteLength = dynamicFieldByteLengths.reduce((total, length) => total + BigInt(length), 0n);
20
+
21
+ const dynamicData = dynamicDataItems.join("");
22
+
23
+ const packedCounter = `${encodeField("uint56", dynamicTotalByteLength).replace(/^0x/, "")}${dynamicFieldByteLengths
24
+ .map((length) => encodeField("uint40", length).replace(/^0x/, ""))
25
+ .join("")}`.padEnd(64, "0");
26
+
27
+ return `0x${staticData}${packedCounter}${dynamicData}`;
28
+ }
@@ -12,7 +12,7 @@ import { InvalidHexLengthForPackedCounterError, PackedCounterLengthMismatchError
12
12
 
13
13
  export function hexToPackedCounter(data: Hex): {
14
14
  totalByteLength: bigint;
15
- fieldByteLengths: number[];
15
+ fieldByteLengths: readonly number[];
16
16
  } {
17
17
  if (data.length !== 66) {
18
18
  throw new InvalidHexLengthForPackedCounterError(data);
@@ -22,7 +22,7 @@ export function hexToPackedCounter(data: Hex): {
22
22
  // TODO: use schema to make sure we only parse as many as we need (rather than zeroes at the end)?
23
23
  const fieldByteLengths = decodeDynamicField("uint40[]", sliceHex(data, 7));
24
24
 
25
- const summedLength = BigInt(fieldByteLengths.reduce((a, b) => a + b, 0));
25
+ const summedLength = BigInt(fieldByteLengths.reduce((total, length) => total + length, 0));
26
26
  if (summedLength !== totalByteLength) {
27
27
  throw new PackedCounterLengthMismatchError(data, totalByteLength, summedLength);
28
28
  }
@@ -2,28 +2,39 @@ import { describe, expect, it } from "vitest";
2
2
  import { hexToSchema } from "./hexToSchema";
3
3
 
4
4
  describe("hexToSchema", () => {
5
- it("decodes schema hex data to schema", () => {
6
- expect(hexToSchema("0x0001010060000000000000000000000000000000000000000000000000000000")).toStrictEqual({
7
- staticDataLength: 1,
8
- staticFields: ["bool"],
9
- dynamicFields: [],
10
- isEmpty: false,
11
- schemaData: "0x0001010060000000000000000000000000000000000000000000000000000000",
12
- });
13
- expect(hexToSchema("0x0001010160c20000000000000000000000000000000000000000000000000000")).toStrictEqual({
14
- staticDataLength: 1,
15
- staticFields: ["bool"],
16
- dynamicFields: ["bool[]"],
17
- isEmpty: false,
18
- schemaData: "0x0001010160c20000000000000000000000000000000000000000000000000000",
19
- });
20
- expect(hexToSchema("0x002402045f2381c3c4c500000000000000000000000000000000000000000000")).toStrictEqual({
21
- staticDataLength: 36,
22
- staticFields: ["bytes32", "int32"],
23
- dynamicFields: ["uint256[]", "address[]", "bytes", "string"],
24
- isEmpty: false,
25
- schemaData: "0x002402045f2381c3c4c500000000000000000000000000000000000000000000",
26
- });
5
+ it("converts hex to schema", () => {
6
+ expect(hexToSchema("0x0001010060000000000000000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
7
+ {
8
+ "dynamicFields": [],
9
+ "staticFields": [
10
+ "bool",
11
+ ],
12
+ }
13
+ `);
14
+ expect(hexToSchema("0x0001010160c20000000000000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
15
+ {
16
+ "dynamicFields": [
17
+ "bool[]",
18
+ ],
19
+ "staticFields": [
20
+ "bool",
21
+ ],
22
+ }
23
+ `);
24
+ expect(hexToSchema("0x002402045f2381c3c4c500000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
25
+ {
26
+ "dynamicFields": [
27
+ "uint256[]",
28
+ "address[]",
29
+ "bytes",
30
+ "string",
31
+ ],
32
+ "staticFields": [
33
+ "bytes32",
34
+ "int32",
35
+ ],
36
+ }
37
+ `);
27
38
  });
28
39
 
29
40
  it("throws if schema hex data is not bytes32", () => {
@@ -1,5 +1,5 @@
1
+ import { StaticAbiType, DynamicAbiType, schemaAbiTypes, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
1
2
  import { Hex, hexToNumber, sliceHex } from "viem";
2
- import { StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength, schemaAbiTypes } from "@latticexyz/schema-type";
3
3
  import { Schema } from "./common";
4
4
  import { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from "./errors";
5
5
 
@@ -29,11 +29,5 @@ export function hexToSchema(data: Hex): Schema {
29
29
  throw new SchemaStaticLengthMismatchError(data, staticDataLength, actualStaticDataLength);
30
30
  }
31
31
 
32
- return {
33
- staticDataLength,
34
- staticFields,
35
- dynamicFields,
36
- isEmpty: data === "0x",
37
- schemaData: data,
38
- };
32
+ return { staticFields, dynamicFields };
39
33
  }
@@ -8,7 +8,5 @@ export function hexToTableSchema(data: Hex): TableSchema {
8
8
  return {
9
9
  keySchema,
10
10
  valueSchema,
11
- isEmpty: data === "0x",
12
- schemaData: data,
13
11
  };
14
12
  }
package/src/index.ts CHANGED
@@ -1,10 +1,7 @@
1
- export * from "./abiTypesToSchema";
2
- export * from "./abiTypesToSchemaData";
3
1
  export * from "./common";
4
2
  export * from "./decodeDynamicField";
5
3
  export * from "./decodeKeyTuple";
6
4
  export * from "./decodeStaticField";
7
5
  export * from "./errors";
8
6
  export * from "./hexToPackedCounter";
9
- export * from "./hexToSchema";
10
7
  export * from "./hexToTableSchema";
@@ -0,0 +1,16 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { schemaToHex } from "./schemaToHex";
3
+
4
+ describe("schemaToHex", () => {
5
+ it("converts schema to hex", () => {
6
+ expect(schemaToHex({ staticFields: ["bool"], dynamicFields: [] })).toBe(
7
+ "0x0001010060000000000000000000000000000000000000000000000000000000"
8
+ );
9
+ expect(schemaToHex({ staticFields: ["bool"], dynamicFields: ["bool[]"] })).toBe(
10
+ "0x0001010160c20000000000000000000000000000000000000000000000000000"
11
+ );
12
+ expect(
13
+ schemaToHex({ staticFields: ["bytes32", "int32"], dynamicFields: ["uint256[]", "address[]", "bytes", "string"] })
14
+ ).toBe("0x002402045f2381c3c4c500000000000000000000000000000000000000000000");
15
+ });
16
+ });
@@ -0,0 +1,18 @@
1
+ import { schemaAbiTypes } from "@latticexyz/schema-type";
2
+ import { Hex } from "viem";
3
+ import { Schema } from "./common";
4
+ import { staticDataLength } from "./staticDataLength";
5
+
6
+ export function schemaToHex(schema: Schema): Hex {
7
+ const staticSchemaTypes = schema.staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
8
+ const dynamicSchemaTypes = schema.dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
9
+ return `0x${[
10
+ staticDataLength(schema.staticFields).toString(16).padStart(4, "0"),
11
+ schema.staticFields.length.toString(16).padStart(2, "0"),
12
+ schema.dynamicFields.length.toString(16).padStart(2, "0"),
13
+ ...staticSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
14
+ ...dynamicSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
15
+ ]
16
+ .join("")
17
+ .padEnd(64, "0")}`;
18
+ }
@@ -0,0 +1,5 @@
1
+ import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
2
+
3
+ export function staticDataLength(staticFields: readonly StaticAbiType[]): number {
4
+ return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);
5
+ }
@@ -1,28 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { abiTypesToSchema } from "./abiTypesToSchema";
3
-
4
- describe("abiTypesToSchema", () => {
5
- it("encodes schema from ABI types", () => {
6
- expect(abiTypesToSchema(["bool"])).toStrictEqual({
7
- staticDataLength: 1,
8
- staticFields: ["bool"],
9
- dynamicFields: [],
10
- isEmpty: false,
11
- schemaData: "0x0001010060000000000000000000000000000000000000000000000000000000",
12
- });
13
- expect(abiTypesToSchema(["bool"], ["bool[]"])).toStrictEqual({
14
- staticDataLength: 1,
15
- staticFields: ["bool"],
16
- dynamicFields: ["bool[]"],
17
- isEmpty: false,
18
- schemaData: "0x0001010160c20000000000000000000000000000000000000000000000000000",
19
- });
20
- expect(abiTypesToSchema(["bytes32", "int32"], ["uint256[]", "address[]", "bytes", "string"])).toStrictEqual({
21
- staticDataLength: 36,
22
- staticFields: ["bytes32", "int32"],
23
- dynamicFields: ["uint256[]", "address[]", "bytes", "string"],
24
- isEmpty: false,
25
- schemaData: "0x002402045f2381c3c4c500000000000000000000000000000000000000000000",
26
- });
27
- });
28
- });
@@ -1,14 +0,0 @@
1
- import { StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
2
- import { Schema } from "./common";
3
- import { abiTypesToSchemaData } from "./abiTypesToSchemaData";
4
-
5
- export function abiTypesToSchema(staticFields: StaticAbiType[], dynamicFields: DynamicAbiType[] = []): Schema {
6
- const staticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);
7
- return {
8
- staticDataLength,
9
- staticFields,
10
- dynamicFields,
11
- isEmpty: false,
12
- schemaData: abiTypesToSchemaData(staticFields, dynamicFields),
13
- };
14
- }
@@ -1,14 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { abiTypesToSchemaData } from "./abiTypesToSchemaData";
3
-
4
- describe("abiTypesToSchemaData", () => {
5
- it("encodes schema data from ABI types", () => {
6
- expect(abiTypesToSchemaData(["bool"])).toBe("0x0001010060000000000000000000000000000000000000000000000000000000");
7
- expect(abiTypesToSchemaData(["bool"], ["bool[]"])).toBe(
8
- "0x0001010160c20000000000000000000000000000000000000000000000000000"
9
- );
10
- expect(abiTypesToSchemaData(["bytes32", "int32"], ["uint256[]", "address[]", "bytes", "string"])).toBe(
11
- "0x002402045f2381c3c4c500000000000000000000000000000000000000000000"
12
- );
13
- });
14
- });
@@ -1,19 +0,0 @@
1
- import { Hex } from "viem";
2
- import { schemaAbiTypes, StaticAbiType, DynamicAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
3
-
4
- export function abiTypesToSchemaData(staticFields: StaticAbiType[], dynamicFields: DynamicAbiType[] = []): Hex {
5
- const staticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);
6
- const numStaticFields = staticFields.length;
7
- const numDynamicFields = dynamicFields.length;
8
- const staticSchemaTypes = staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
9
- const dynamicSchemaTypes = dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
10
- return `0x${[
11
- staticDataLength.toString(16).padStart(4, "0"),
12
- numStaticFields.toString(16).padStart(2, "0"),
13
- numDynamicFields.toString(16).padStart(2, "0"),
14
- ...staticSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
15
- ...dynamicSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
16
- ]
17
- .join("")
18
- .padEnd(64, "0")}`;
19
- }