@latticexyz/protocol-parser 2.0.0-alpha.1.268 → 2.0.0-main-0d434816

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{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};
1
+ import{isDynamicAbiType as C}from"@latticexyz/schema-type";function ae(e){let t=[],c=[];for(let i of e)C(i)?c.push(i):t.push(i);return{staticFields:t,dynamicFields:c}}import{hexToString as K,sliceHex as N}from"viem";import{assertExhaustive as R}from"@latticexyz/common/utils";import{arrayAbiTypeToStaticAbiType as W,staticAbiTypeToByteLength as _}from"@latticexyz/schema-type";import{getAddress as V,hexToBigInt as j,hexToBool as U}from"viem";import{assertExhaustive as z}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as M,staticAbiTypeToDefaultValue as O}from"@latticexyz/schema-type";import{MUDError as u}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as L}from"@latticexyz/schema-type";var d=class extends u{name="InvalidHexValueError";constructor(t){super(`Hex value "${t}" is an odd length (${t.length-2}). It must be an even length.`)}},f=class extends u{name="InvalidHexLengthForSchemaError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for a schema.`)}},x=class extends u{name="InvalidHexLengthForPackedCounterError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for a packed counter.`)}},g=class extends u{name="InvalidHexLengthForStaticFieldError";constructor(t,c){super(`Hex value "${c}" has length of ${c.length-2}, but expected length of ${L[t]*2} for ${t} type.`)}},S=class extends u{name="InvalidHexLengthForArrayFieldError";constructor(t,c){super(`Hex value "${c}" has length of ${c.length-2}, but expected a multiple of ${L[t]*2} for ${t}[] type.`)}},A=class extends u{name="SchemaStaticLengthMismatchError";constructor(t,c,i){super(`Schema "${t}" static data length (${c}) did not match the summed length of all static fields (${i}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`)}},v=class extends u{name="PackedCounterLengthMismatchError";constructor(t,c,i){super(`PackedCounter "${t}" total bytes length (${c}) did not match the summed length of all field byte lengths (${i}).`)}};function p(e,t){if(t.length>3&&t.length%2!==0)throw new d(t);if((t.length-2)/2!==M[e])throw new g(e,t);switch(e){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=j(t,{signed:e.startsWith("int")}),a=typeof O[e];if(a==="number")return Number(i);if(a==="bigint")return i;throw new Error(`Unexpected default value type (${a}) for ABI type (${e})`)}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 t;case"bool":return U(t);case"address":return V(t)}return z(e,`Unsupported static ABI type: ${e}`)}function h(e,t){if(e==="bytes")return t;if(e==="string")return K(t);if(t.length>3&&t.length%2!==0)throw new d(t);let c=(t.length-2)/2;switch(e){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=W(e),a=_[i];if(c%a!==0)throw new S(i,t);return new Array(c/a).fill(void 0).map((n,s)=>{let r=N(t,s*a,(s+1)*a);return p(i,r)})}}return R(e,`Unsupported dynamic ABI type: ${e}`)}import{isDynamicAbiType as q}from"@latticexyz/schema-type";function Ee(e,t){return q(e)?h(e,t):p(e,t)}import{decodeAbiParameters as G}from"viem";function je(e,t){return t.map((c,i)=>G([{type:e.staticFields[i]}],c)[0])}import{staticAbiTypeToByteLength as Q}from"@latticexyz/schema-type";import{sliceHex as F}from"viem";import{sliceHex as $}from"viem";function B(e){if(e.length!==66)throw new x(e);let t=p("uint56",$(e,0,7)),c=h("uint40[]",$(e,7)),i=BigInt(c.reduce((a,o)=>a+o,0));if(i!==t)throw new v(e,t,i);return{totalByteLength:t,fieldByteLengths:c}}import{staticAbiTypeToByteLength as J}from"@latticexyz/schema-type";function P(e){return e.reduce((t,c)=>t+J[c],0)}function ct(e,t){let c=[],i=0;e.staticFields.forEach(n=>{let s=Q[n],r=p(n,F(t,i,i+s));i+=s,c.push(r)});let a=P(e.staticFields),o=i;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:i}),e.dynamicFields.length>0){let n=B(F(t,i,i+32));i+=32,e.dynamicFields.forEach((r,H)=>{let m=n.fieldByteLengths[H],y=h(r,F(t,i,i+m));i+=m,c.push(y)});let s=i-32-o;BigInt(s)!==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:s,bytesOffset:i})}return c}import{arrayAbiTypeToStaticAbiType as X,isArrayAbiType as Y}from"@latticexyz/schema-type";import{encodePacked as w}from"viem";function l(e,t){if(Y(e)&&Array.isArray(t)){let c=X(e);return w(t.map(()=>c),t)}return w([e],[t])}import{encodeAbiParameters as Z}from"viem";function ut(e,t){return t.map((c,i)=>Z([{type:e.staticFields[i]}],[c]))}function lt(e,t){let c=t.slice(0,e.staticFields.length),i=t.slice(e.staticFields.length),a=c.map((m,y)=>l(e.staticFields[y],m).replace(/^0x/,"")).join("");if(e.dynamicFields.length===0)return`0x${a}`;let o=i.map((m,y)=>l(e.dynamicFields[y],m).replace(/^0x/,"")),n=o.map(m=>m.length/2),s=n.reduce((m,y)=>m+BigInt(y),0n),r=o.join(""),H=`${l("uint56",s).replace(/^0x/,"")}${n.map(m=>l("uint40",m).replace(/^0x/,"")).join("")}`.padEnd(64,"0");return`0x${a}${H}${r}`}import{schemaAbiTypes as I,staticAbiTypeToByteLength as ee}from"@latticexyz/schema-type";import{hexToNumber as T,sliceHex as b}from"viem";function D(e){if(e.length!==66)throw new f(e);let t=T(b(e,0,2)),c=T(b(e,2,3)),i=T(b(e,3,4)),a=[],o=[];for(let s=4;s<4+c;s++){let r=T(b(e,s,s+1));a.push(I[r])}for(let s=4+c;s<4+c+i;s++){let r=T(b(e,s,s+1));o.push(I[r])}let n=a.reduce((s,r)=>s+ee[r],0);if(n!==t)throw new A(e,t,n);return{staticFields:a,dynamicFields:o}}import{sliceHex as E}from"viem";function Dt(e){let t=D(E(e,0,32));return{keySchema:D(E(e,32,64)),valueSchema:t}}function $t(e,t){return t<e.staticFields.length?e.staticFields[t]:e.dynamicFields[t-e.staticFields.length]}import{schemaAbiTypes as k}from"@latticexyz/schema-type";function Et(e){let t=e.staticFields.map(i=>k.indexOf(i)),c=e.dynamicFields.map(i=>k.indexOf(i));return`0x${[P(e.staticFields).toString(16).padStart(4,"0"),e.staticFields.length.toString(16).padStart(2,"0"),e.dynamicFields.length.toString(16).padStart(2,"0"),...t.map(i=>i.toString(16).padStart(2,"0")),...c.map(i=>i.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}export{d as InvalidHexLengthError,S as InvalidHexLengthForArrayFieldError,x as InvalidHexLengthForPackedCounterError,f as InvalidHexLengthForSchemaError,g as InvalidHexLengthForStaticFieldError,v as PackedCounterLengthMismatchError,A as SchemaStaticLengthMismatchError,ae as abiTypesToSchema,h as decodeDynamicField,Ee as decodeField,je as decodeKeyTuple,ct as decodeRecord,p as decodeStaticField,l as encodeField,ut as encodeKeyTuple,lt as encodeRecord,B as hexToPackedCounter,D as hexToSchema,Dt as hexToTableSchema,$t as schemaIndexToAbiType,Et as schemaToHex,P as staticDataLength};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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,CACz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OAIpC,SAASC,EAAYC,EAAmB,CAC7C,GAAIA,EAAK,SAAW,GAClB,MAAM,IAAIC,EAA+BD,CAAI,EAG/C,IAAME,EAAmBC,EAAYC,EAASJ,EAAM,EAAG,CAAC,CAAC,EACnDK,EAAkBF,EAAYC,EAASJ,EAAM,EAAG,CAAC,CAAC,EAClDM,EAAmBH,EAAYC,EAASJ,EAAM,EAAG,CAAC,CAAC,EACnDO,EAAgC,CAAC,EACjCC,EAAkC,CAAC,EAEzC,QAASC,EAAI,EAAGA,EAAI,EAAIJ,EAAiBI,IAAK,CAC5C,IAAMC,EAAkBP,EAAYC,EAASJ,EAAMS,EAAGA,EAAI,CAAC,CAAC,EAC5DF,EAAa,KAAKI,EAAeD,CAAe,CAAkB,EAEpE,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"]}
1
+ {"version":3,"sources":["../src/abiTypesToSchema.ts","../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeField.ts","../src/decodeKeyTuple.ts","../src/decodeRecord.ts","../src/hexToPackedCounter.ts","../src/staticDataLength.ts","../src/encodeField.ts","../src/encodeKeyTuple.ts","../src/encodeRecord.ts","../src/hexToSchema.ts","../src/hexToTableSchema.ts","../src/schemaIndexToAbiType.ts","../src/schemaToHex.ts"],"sourcesContent":["import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from \"@latticexyz/schema-type\";\nimport { Schema } from \"./common\";\n\nexport function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema {\n const staticFields: StaticAbiType[] = [];\n const dynamicFields: DynamicAbiType[] = [];\n for (const abiType of abiTypes) {\n if (isDynamicAbiType(abiType)) dynamicFields.push(abiType);\n else staticFields.push(abiType);\n }\n return { staticFields, dynamicFields };\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 } from \"viem\";\nimport { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from \"@latticexyz/schema-type\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { decodeStaticField } from \"./decodeStaticField\";\n\nexport function decodeField<\n TAbiType extends SchemaAbiType,\n TPrimitiveType extends SchemaAbiTypeToPrimitiveType<TAbiType>\n>(abiType: TAbiType, data: Hex): TPrimitiveType {\n return (\n isDynamicAbiType(abiType) ? decodeDynamicField(abiType, data) : decodeStaticField(abiType, data)\n ) as TPrimitiveType;\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 } from \"@latticexyz/schema-type\";\nimport { Hex, encodeAbiParameters } from \"viem\";\nimport { Schema } from \"./common\";\n\nexport function encodeKeyTuple(keySchema: Schema, keyTuple: StaticPrimitiveType[]): Hex[] {\n return keyTuple.map((key, index) => encodeAbiParameters([{ type: keySchema.staticFields[index] }], [key]));\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 { 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","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 { SchemaAbiType } from \"@latticexyz/schema-type\";\nimport { Schema } from \"./common\";\n\nexport function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType {\n if (schemaIndex < schema.staticFields.length) {\n return schema.staticFields[schemaIndex];\n }\n return schema.dynamicFields[schemaIndex - schema.staticFields.length];\n}\n","import { schemaAbiTypes } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { staticDataLength } from \"./staticDataLength\";\n\nexport function schemaToHex(schema: Schema): Hex {\n const staticSchemaTypes = schema.staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));\n const dynamicSchemaTypes = schema.dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));\n return `0x${[\n staticDataLength(schema.staticFields).toString(16).padStart(4, \"0\"),\n schema.staticFields.length.toString(16).padStart(2, \"0\"),\n schema.dynamicFields.length.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"],"mappings":"AAAA,OAAuD,oBAAAA,MAAwB,0BAGxE,SAASC,GAAiBC,EAAmC,CAClE,IAAMC,EAAgC,CAAC,EACjCC,EAAkC,CAAC,EACzC,QAAWC,KAAWH,EAChBF,EAAiBK,CAAO,EAAGD,EAAc,KAAKC,CAAO,EACpDF,EAAa,KAAKE,CAAO,EAEhC,MAAO,CAAE,aAAAF,EAAc,cAAAC,CAAc,CACvC,CCXA,OAAc,eAAAE,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,cAAi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G,MAAO,KAAK,CACVG,EAAiBL,EAAO,YAAY,EAAE,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAClEA,EAAO,aAAa,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EACvDA,EAAO,cAAc,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EACxD,GAAGC,EAAkB,IAAKK,GAAeA,EAAW,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,EACjF,GAAGF,EAAmB,IAAKE,GAAeA,EAAW,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,CACpF,EACG,KAAK,EAAE,EACP,OAAO,GAAI,GAAG,GACnB","names":["isDynamicAbiType","abiTypesToSchema","abiTypes","staticFields","dynamicFields","abiType","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","isDynamicAbiType","decodeField","abiType","data","isDynamicAbiType","decodeDynamicField","decodeStaticField","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","encodeAbiParameters","encodeKeyTuple","keySchema","keyTuple","key","index","encodeRecord","schema","values","staticValues","dynamicValues","staticData","value","i","encodeField","dynamicDataItems","dynamicFieldByteLengths","dynamicTotalByteLength","total","length","dynamicData","packedCounter","schemaAbiTypes","staticAbiTypeToByteLength","hexToNumber","sliceHex","hexToSchema","data","InvalidHexLengthForSchemaError","staticDataLength","hexToNumber","sliceHex","numStaticFields","numDynamicFields","staticFields","dynamicFields","i","schemaTypeIndex","schemaAbiTypes","actualStaticDataLength","acc","fieldType","staticAbiTypeToByteLength","SchemaStaticLengthMismatchError","sliceHex","hexToTableSchema","data","valueSchema","hexToSchema","sliceHex","schemaIndexToAbiType","schema","schemaIndex","schemaAbiTypes","schemaToHex","schema","staticSchemaTypes","abiType","schemaAbiTypes","dynamicSchemaTypes","staticDataLength","schemaType"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latticexyz/protocol-parser",
3
- "version": "2.0.0-alpha.1.268+15e65311",
3
+ "version": "2.0.0-main-0d434816",
4
4
  "description": "Parser utilities for the MUD protocol",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,20 +13,11 @@
13
13
  ".": "./dist/index.js"
14
14
  },
15
15
  "types": "src/index.ts",
16
- "scripts": {
17
- "build": "pnpm run build:js",
18
- "build:js": "tsup",
19
- "clean": "pnpm run clean:js",
20
- "clean:js": "rimraf dist",
21
- "dev": "tsup --watch",
22
- "lint": "eslint .",
23
- "test": "vitest typecheck --run && vitest --run"
24
- },
25
16
  "dependencies": {
26
- "@latticexyz/common": "2.0.0-alpha.1.268+15e65311",
27
- "@latticexyz/schema-type": "2.0.0-alpha.1.268+15e65311",
28
17
  "abitype": "0.8.7",
29
- "viem": "1.1.7"
18
+ "viem": "1.1.7",
19
+ "@latticexyz/common": "2.0.0-main-0d434816",
20
+ "@latticexyz/schema-type": "2.0.0-main-0d434816"
30
21
  },
31
22
  "devDependencies": {
32
23
  "tsup": "^6.7.0",
@@ -35,5 +26,14 @@
35
26
  "publishConfig": {
36
27
  "access": "public"
37
28
  },
38
- "gitHead": "15e65311feda89e8d3dc3f9c0326252065c424b9"
39
- }
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
+ }
@@ -0,0 +1,39 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { abiTypesToSchema } from "./abiTypesToSchema";
3
+
4
+ describe("hexToSchema", () => {
5
+ it("converts hex to schema", () => {
6
+ expect(abiTypesToSchema(["bool"])).toMatchInlineSnapshot(`
7
+ {
8
+ "dynamicFields": [],
9
+ "staticFields": [
10
+ "bool",
11
+ ],
12
+ }
13
+ `);
14
+ expect(abiTypesToSchema(["bool", "bool[]"])).toMatchInlineSnapshot(`
15
+ {
16
+ "dynamicFields": [
17
+ "bool[]",
18
+ ],
19
+ "staticFields": [
20
+ "bool",
21
+ ],
22
+ }
23
+ `);
24
+ expect(abiTypesToSchema(["bytes32", "int32", "uint256[]", "address[]", "bytes", "string"])).toMatchInlineSnapshot(`
25
+ {
26
+ "dynamicFields": [
27
+ "uint256[]",
28
+ "address[]",
29
+ "bytes",
30
+ "string",
31
+ ],
32
+ "staticFields": [
33
+ "bytes32",
34
+ "int32",
35
+ ],
36
+ }
37
+ `);
38
+ });
39
+ });
@@ -0,0 +1,12 @@
1
+ import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type";
2
+ import { Schema } from "./common";
3
+
4
+ export function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema {
5
+ const staticFields: StaticAbiType[] = [];
6
+ const dynamicFields: DynamicAbiType[] = [];
7
+ for (const abiType of abiTypes) {
8
+ if (isDynamicAbiType(abiType)) dynamicFields.push(abiType);
9
+ else staticFields.push(abiType);
10
+ }
11
+ return { staticFields, dynamicFields };
12
+ }
@@ -0,0 +1,13 @@
1
+ import { Hex } from "viem";
2
+ import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from "@latticexyz/schema-type";
3
+ import { decodeDynamicField } from "./decodeDynamicField";
4
+ import { decodeStaticField } from "./decodeStaticField";
5
+
6
+ export function decodeField<
7
+ TAbiType extends SchemaAbiType,
8
+ TPrimitiveType extends SchemaAbiTypeToPrimitiveType<TAbiType>
9
+ >(abiType: TAbiType, data: Hex): TPrimitiveType {
10
+ return (
11
+ isDynamicAbiType(abiType) ? decodeDynamicField(abiType, data) : decodeStaticField(abiType, data)
12
+ ) as TPrimitiveType;
13
+ }
@@ -1,6 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { decodeKeyTuple } from "./decodeKeyTuple";
3
- import { Schema } from "./common";
4
3
 
5
4
  describe("decodeKeyTuple", () => {
6
5
  it("can decode bool key tuple", () => {
@@ -0,0 +1,39 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { encodeKeyTuple } from "./encodeKeyTuple";
3
+
4
+ describe("encodeKeyTuple", () => {
5
+ it("can encode bool key tuple", () => {
6
+ expect(encodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [false])).toStrictEqual([
7
+ "0x0000000000000000000000000000000000000000000000000000000000000000",
8
+ ]);
9
+ expect(
10
+ encodeKeyTuple(
11
+ {
12
+ staticFields: ["bool"],
13
+ dynamicFields: [],
14
+ },
15
+ [true]
16
+ )
17
+ ).toStrictEqual(["0x0000000000000000000000000000000000000000000000000000000000000001"]);
18
+ });
19
+
20
+ it("can encode complex key tuple", () => {
21
+ expect(
22
+ encodeKeyTuple({ staticFields: ["uint256", "int32", "bytes16", "address", "bool", "int8"], dynamicFields: [] }, [
23
+ 42n,
24
+ -42,
25
+ "0x12340000000000000000000000000000",
26
+ "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
27
+ true,
28
+ 3,
29
+ ])
30
+ ).toStrictEqual([
31
+ "0x000000000000000000000000000000000000000000000000000000000000002a",
32
+ "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6",
33
+ "0x1234000000000000000000000000000000000000000000000000000000000000",
34
+ "0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
35
+ "0x0000000000000000000000000000000000000000000000000000000000000001",
36
+ "0x0000000000000000000000000000000000000000000000000000000000000003",
37
+ ]);
38
+ });
39
+ });
@@ -0,0 +1,7 @@
1
+ import { StaticPrimitiveType } from "@latticexyz/schema-type";
2
+ import { Hex, encodeAbiParameters } from "viem";
3
+ import { Schema } from "./common";
4
+
5
+ export function encodeKeyTuple(keySchema: Schema, keyTuple: StaticPrimitiveType[]): Hex[] {
6
+ return keyTuple.map((key, index) => encodeAbiParameters([{ type: keySchema.staticFields[index] }], [key]));
7
+ }
package/src/index.ts CHANGED
@@ -1,10 +1,17 @@
1
+ export * from "./abiTypesToSchema";
1
2
  export * from "./common";
2
3
  export * from "./decodeDynamicField";
4
+ export * from "./decodeField";
3
5
  export * from "./decodeKeyTuple";
4
6
  export * from "./decodeRecord";
5
7
  export * from "./decodeStaticField";
6
8
  export * from "./encodeField";
9
+ export * from "./encodeKeyTuple";
7
10
  export * from "./encodeRecord";
8
11
  export * from "./errors";
9
12
  export * from "./hexToPackedCounter";
13
+ export * from "./hexToSchema";
10
14
  export * from "./hexToTableSchema";
15
+ export * from "./schemaIndexToAbiType";
16
+ export * from "./schemaToHex";
17
+ export * from "./staticDataLength";
@@ -0,0 +1,9 @@
1
+ import { SchemaAbiType } from "@latticexyz/schema-type";
2
+ import { Schema } from "./common";
3
+
4
+ export function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType {
5
+ if (schemaIndex < schema.staticFields.length) {
6
+ return schema.staticFields[schemaIndex];
7
+ }
8
+ return schema.dynamicFields[schemaIndex - schema.staticFields.length];
9
+ }