@latticexyz/protocol-parser 2.0.0-snapshot-test-32d38619 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +145 -0
- package/dist/internal.js +2 -0
- package/dist/internal.js.map +1 -0
- package/package.json +19 -9
- package/src/abiTypesToSchema.ts +1 -1
- package/src/common.ts +17 -3
- package/src/decodeDynamicField.test.ts +26 -26
- package/src/decodeDynamicField.ts +4 -4
- package/src/decodeField.ts +2 -2
- package/src/decodeKey.ts +2 -2
- package/src/decodeKeyTuple.test.ts +3 -3
- package/src/decodeKeyTuple.ts +3 -3
- package/src/decodeRecord.test.ts +1 -1
- package/src/decodeRecord.ts +5 -5
- package/src/decodeStaticField.test.ts +16 -16
- package/src/decodeStaticField.ts +2 -2
- package/src/decodeValue.ts +2 -2
- package/src/decodeValueArgs.ts +3 -3
- package/src/encodeField.ts +4 -4
- package/src/encodeKey.ts +1 -1
- package/src/encodeKeyTuple.test.ts +3 -3
- package/src/encodeKeyTuple.ts +1 -1
- package/src/encodeLengths.test.ts +7 -1
- package/src/encodeLengths.ts +1 -1
- package/src/encodeRecord.test.ts +1 -1
- package/src/encodeRecord.ts +5 -5
- package/src/encodeValue.ts +1 -1
- package/src/encodeValueArgs.test.ts +53 -0
- package/src/encodeValueArgs.ts +18 -9
- package/src/errors.ts +12 -11
- package/src/exports/index.ts +5 -0
- package/src/exports/internal.ts +31 -0
- package/src/getKeySchema.ts +9 -0
- package/src/getSchemaTypes.ts +10 -0
- package/src/getValueSchema.ts +11 -0
- package/src/hexToEncodedLengths.test.ts +35 -0
- package/src/{hexToPackedCounter.ts → hexToEncodedLengths.ts} +5 -5
- package/src/hexToSchema.test.ts +3 -2
- package/src/hexToSchema.ts +8 -2
- package/src/keySchemaToHex.ts +1 -1
- package/src/schemaIndexToAbiType.ts +1 -1
- package/src/schemaToHex.test.ts +3 -3
- package/src/schemaToHex.ts +1 -1
- package/src/staticDataLength.ts +1 -1
- package/src/valueSchemaToFieldLayoutHex.ts +1 -1
- package/src/valueSchemaToHex.ts +1 -1
- package/src/hexToPackedCounter.test.ts +0 -34
- package/src/index.ts +0 -27
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import{isDynamicAbiType as R}from"@latticexyz/schema-type";function we(e){let t=[],c=[];for(let i of e)R(i)?c.push(i):t.push(i);return{staticFields:t,dynamicFields:c}}import{hexToString as Q,sliceHex as X}from"viem";import{assertExhaustive as Y}from"@latticexyz/common/utils";import{arrayAbiTypeToStaticAbiType as Z,staticAbiTypeToByteLength as ee}from"@latticexyz/schema-type";import{getAddress as N,hexToBigInt as W,hexToBool as _}from"viem";import{assertExhaustive as q}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as G,staticAbiTypeToDefaultValue as J}from"@latticexyz/schema-type";import{MUDError as l}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as $}from"@latticexyz/schema-type";var u=class extends l{name="InvalidHexValueError";constructor(t){super(`Hex value "${t}" is an odd length (${t.length-2}). It must be an even length.`)}},S=class extends l{name="InvalidHexLengthForSchemaError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for a schema.`)}},g=class extends l{name="InvalidHexLengthForPackedCounterError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for a packed counter.`)}},A=class extends l{name="InvalidHexLengthForStaticFieldError";constructor(t,c){super(`Hex value "${c}" has length of ${c.length-2}, but expected length of ${$[t]*2} for ${t} type.`)}},v=class extends l{name="InvalidHexLengthForArrayFieldError";constructor(t,c){super(`Hex value "${c}" has length of ${c.length-2}, but expected a multiple of ${$[t]*2} for ${t}[] type.`)}},H=class extends l{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?`)}},F=class extends l{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 d(e,t){if(t.length>3&&t.length%2!==0)throw new u(t);if((t.length-2)/2!==G[e])throw new A(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=W(t,{signed:e.startsWith("int")}),a=typeof J[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 _(t);case"address":return N(t)}return q(e,`Unsupported static ABI type: ${e}`)}function h(e,t){if(e==="bytes")return t;if(e==="string")return Q(t);if(t.length>3&&t.length%2!==0)throw new u(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=Z(e),a=ee[i];if(c%a!==0)throw new v(i,t);return new Array(c/a).fill(void 0).map((n,s)=>{let o=X(t,s*a,(s+1)*a);return d(i,o)})}}return Y(e,`Unsupported dynamic ABI type: ${e}`)}import{isDynamicAbiType as te}from"@latticexyz/schema-type";function rt(e,t){return te(e)?h(e,t):d(e,t)}import{decodeAbiParameters as ie}from"viem";function V(e,t){if(e.staticFields.length!==t.length)throw new Error(`key tuple length ${t.length} does not match key schema length ${e.staticFields.length}`);return t.map((c,i)=>ie([{type:e.staticFields[i]}],c)[0])}function dt(e,t){let c=V({staticFields:Object.values(e),dynamicFields:[]},t);return Object.fromEntries(Object.keys(e).map((i,a)=>[i,c[a]]))}import{staticAbiTypeToByteLength as ae,dynamicAbiTypeToDefaultValue as se}from"@latticexyz/schema-type";import{readHex as B}from"@latticexyz/common";function w(e){if(e.length!==66)throw new g(e);let t=d("uint56",B(e,32-7,32)),c=h("uint40[]",B(e,0,32-7)),i=Object.freeze([...c].reverse()),a=BigInt(i.reduce((r,n)=>r+n,0));if(a!==t)throw new F(e,t,a);return{totalByteLength:t,fieldByteLengths:i}}import{staticAbiTypeToByteLength as ce}from"@latticexyz/schema-type";function f(e){return e.reduce((t,c)=>t+ce[c],0)}import{readHex as D}from"@latticexyz/common";function j(e,t){let c=[],i=0;e.staticFields.forEach(n=>{let s=ae[n],o=d(n,D(t,i,i+s));i+=s,c.push(o)});let a=f(e.staticFields),r=i;if(r!==a&&console.warn("Decoded static data length does not match value schema's expected static data length. Data may get corrupted. Is `getStaticByteLength` outdated?",{expectedLength:a,actualLength:r,bytesOffset:i}),e.dynamicFields.length>0){let n=w(D(t,i,i+32));i+=32,e.dynamicFields.forEach((o,T)=>{let m=n.fieldByteLengths[T];if(m>0){let p=h(o,D(t,i,i+m));i+=m,c.push(p)}else c.push(se[o])});let s=i-32-r;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{isStaticAbiType as re,isDynamicAbiType as ne}from"@latticexyz/schema-type";function O(e,t){let c=Object.values(e).filter(re),i=Object.values(e).filter(ne),a=j({staticFields:c,dynamicFields:i},t);return Object.fromEntries(Object.keys(e).map((r,n)=>[r,a[n]]))}import{concatHex as oe}from"viem";import{isStaticAbiType as me}from"@latticexyz/schema-type";import{readHex as pe}from"@latticexyz/common";function zt(e,{staticData:t,encodedLengths:c,dynamicData:i}){return O(e,oe([pe(t,0,f(Object.values(e).filter(me))),c,i]))}import{arrayAbiTypeToStaticAbiType as ye,isArrayAbiType as de}from"@latticexyz/schema-type";import{encodePacked as E}from"viem";function y(e,t){if(de(e)&&Array.isArray(t)){let c=ye(e);return t.length===0?"0x":E(t.map(()=>c),t)}return E([e],[t])}import{isStaticAbiType as ue}from"@latticexyz/schema-type";import{encodeAbiParameters as le}from"viem";function I(e,t){return t.map((c,i)=>le([{type:e.staticFields[i]}],[c]))}function Xt(e,t){let c=Object.values(e).filter(ue);return I({staticFields:c,dynamicFields:[]},Object.values(t))}function ei(e,t){let c=t.slice(0,e.staticFields.length),i=t.slice(e.staticFields.length),a=c.map((m,p)=>y(e.staticFields[p],m).replace(/^0x/,"")).join("");if(e.dynamicFields.length===0)return`0x${a}`;let r=i.map((m,p)=>y(e.dynamicFields[p],m).replace(/^0x/,"")),n=r.map(m=>m.length/2).reverse(),s=n.reduce((m,p)=>m+BigInt(p),0n),o=r.join(""),T=`${n.map(m=>y("uint40",m).replace(/^0x/,"")).join("")}${y("uint56",s).replace(/^0x/,"")}`.padStart(64,"0");return`0x${a}${T}${o}`}import{concatHex as Se}from"viem";import{isStaticAbiType as xe,isDynamicAbiType as be}from"@latticexyz/schema-type";import{concatHex as k}from"viem";import{concatHex as he,padHex as fe,size as Te}from"viem";function K(e){let t=e.map(Te).reverse(),c=t.reduce((i,a)=>i+BigInt(a),0n);return fe(he([...t.map(i=>y("uint40",i)),y("uint56",c)]),{size:32,dir:"left"})}function z(e,t){let c=Object.values(e).filter(xe),i=Object.values(e).filter(be),a=Object.values(t),r=a.slice(0,c.length),n=a.slice(c.length),s=r.map((m,p)=>y(c[p],m)),o=n.map((m,p)=>y(i[p],m)),T=K(o);return{staticData:k(s),encodedLengths:T,dynamicData:k(o)}}function fi(e,t){let{staticData:c,encodedLengths:i,dynamicData:a}=z(e,t);return Se([c,i,a])}function xi(e){return`0x${[e.staticFieldLengths.reduce((c,i)=>c+i,0).toString(16).padStart(4,"0"),e.staticFieldLengths.length.toString(16).padStart(2,"0"),e.numDynamicFields.toString(16).padStart(2,"0"),...e.staticFieldLengths.map(c=>c.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}import{schemaAbiTypes as C,staticAbiTypeToByteLength as ge}from"@latticexyz/schema-type";import{hexToNumber as x,sliceHex as b}from"viem";function L(e){if(e.length!==66)throw new S(e);let t=x(b(e,0,2)),c=x(b(e,2,3)),i=x(b(e,3,4)),a=[],r=[];for(let s=4;s<4+c;s++){let o=x(b(e,s,s+1));a.push(C[o])}for(let s=4+c;s<4+c+i;s++){let o=x(b(e,s,s+1));r.push(C[o])}let n=a.reduce((s,o)=>s+ge[o],0);if(n!==t)throw console.warn(`Schema "${e}" static data length (${t}) did not match the summed length of all static fields (${n}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`),new H(e,t,n);return{staticFields:a,dynamicFields:r}}import{sliceHex as U}from"viem";function Vi(e){let t=L(U(e,0,32));return{keySchema:L(U(e,32,64)),valueSchema:t}}import{isStaticAbiType as Ae}from"@latticexyz/schema-type";import{schemaAbiTypes as M}from"@latticexyz/schema-type";function P(e){let t=e.staticFields.map(i=>M.indexOf(i)),c=e.dynamicFields.map(i=>M.indexOf(i));return`0x${[f(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")}`}function Ki(e){return P({staticFields:Object.values(e).filter(Ae),dynamicFields:[]})}function zi(e,t){return t<e.staticFields.length?e.staticFields[t]:e.dynamicFields[t-e.staticFields.length]}import{isDynamicAbiType as ve,isStaticAbiType as He,staticAbiTypeToByteLength as Fe}from"@latticexyz/schema-type";function Mi(e){let t=Object.values(e).filter(He),c=Object.values(e).filter(ve),i=t.map(r=>Fe[r]);return`0x${[i.reduce((r,n)=>r+n,0).toString(16).padStart(4,"0"),t.length.toString(16).padStart(2,"0"),c.length.toString(16).padStart(2,"0"),...i.map(r=>r.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}import{isDynamicAbiType as Pe,isStaticAbiType as De}from"@latticexyz/schema-type";function _i(e){return P({staticFields:Object.values(e).filter(De),dynamicFields:Object.values(e).filter(Pe)})}export{u as InvalidHexLengthError,v as InvalidHexLengthForArrayFieldError,g as InvalidHexLengthForPackedCounterError,S as InvalidHexLengthForSchemaError,A as InvalidHexLengthForStaticFieldError,F as PackedCounterLengthMismatchError,H as SchemaStaticLengthMismatchError,we as abiTypesToSchema,h as decodeDynamicField,rt as decodeField,dt as decodeKey,V as decodeKeyTuple,j as decodeRecord,d as decodeStaticField,O as decodeValue,zt as decodeValueArgs,y as encodeField,Xt as encodeKey,I as encodeKeyTuple,ei as encodeRecord,fi as encodeValue,z as encodeValueArgs,xi as fieldLayoutToHex,w as hexToPackedCounter,L as hexToSchema,Vi as hexToTableSchema,Ki as keySchemaToHex,zi as schemaIndexToAbiType,P as schemaToHex,f as staticDataLength,Mi as valueSchemaToFieldLayoutHex,_i as valueSchemaToHex};
|
|
2
1
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/abiTypesToSchema.ts","../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeField.ts","../src/decodeKeyTuple.ts","../src/decodeKey.ts","../src/decodeRecord.ts","../src/hexToPackedCounter.ts","../src/staticDataLength.ts","../src/decodeValue.ts","../src/decodeValueArgs.ts","../src/encodeField.ts","../src/encodeKey.ts","../src/encodeKeyTuple.ts","../src/encodeRecord.ts","../src/encodeValue.ts","../src/encodeValueArgs.ts","../src/encodeLengths.ts","../src/fieldLayoutToHex.ts","../src/hexToSchema.ts","../src/hexToTableSchema.ts","../src/keySchemaToHex.ts","../src/schemaToHex.ts","../src/schemaIndexToAbiType.ts","../src/valueSchemaToFieldLayoutHex.ts","../src/valueSchemaToHex.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\n/** @deprecated use `decodeKey` instead */\nexport function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {\n if (keySchema.staticFields.length !== keyTuple.length) {\n throw new Error(\n `key tuple length ${keyTuple.length} does not match key schema length ${keySchema.staticFields.length}`\n );\n }\n return keyTuple.map(\n (key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType\n );\n}\n","import { Hex } from \"viem\";\nimport { SchemaToPrimitives, KeySchema } from \"./common\";\nimport { decodeKeyTuple } from \"./decodeKeyTuple\";\n\nexport function decodeKey<TSchema extends KeySchema>(\n keySchema: TSchema,\n data: readonly Hex[]\n): SchemaToPrimitives<TSchema> {\n // TODO: refactor and move all decodeKeyTuple logic into this method so we can delete decodeKeyTuple\n const keyValues = decodeKeyTuple({ staticFields: Object.values(keySchema), dynamicFields: [] }, data);\n\n return Object.fromEntries(\n Object.keys(keySchema).map((name, i) => [name, keyValues[i]])\n ) as SchemaToPrimitives<TSchema>;\n}\n","import {\n StaticPrimitiveType,\n DynamicPrimitiveType,\n staticAbiTypeToByteLength,\n dynamicAbiTypeToDefaultValue,\n} from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { hexToPackedCounter } from \"./hexToPackedCounter\";\nimport { staticDataLength } from \"./staticDataLength\";\nimport { readHex } from \"@latticexyz/common\";\n\n/** @deprecated use `decodeValue` instead */\nexport function decodeRecord(valueSchema: Schema, data: Hex): readonly (StaticPrimitiveType | DynamicPrimitiveType)[] {\n const values: (StaticPrimitiveType | DynamicPrimitiveType)[] = [];\n\n let bytesOffset = 0;\n valueSchema.staticFields.forEach((fieldType) => {\n const fieldByteLength = staticAbiTypeToByteLength[fieldType];\n const value = decodeStaticField(fieldType, readHex(data, bytesOffset, bytesOffset + fieldByteLength));\n bytesOffset += fieldByteLength;\n values.push(value);\n });\n\n // Warn user if static data length doesn't match the value schema, because data corruption might be possible.\n const schemaStaticDataLength = staticDataLength(valueSchema.staticFields);\n const actualStaticDataLength = bytesOffset;\n if (actualStaticDataLength !== schemaStaticDataLength) {\n console.warn(\n \"Decoded static data length does not match value 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 (valueSchema.dynamicFields.length > 0) {\n const dataLayout = hexToPackedCounter(readHex(data, bytesOffset, bytesOffset + 32));\n bytesOffset += 32;\n\n valueSchema.dynamicFields.forEach((fieldType, i) => {\n const dataLength = dataLayout.fieldByteLengths[i];\n if (dataLength > 0) {\n const value = decodeDynamicField(fieldType, readHex(data, bytesOffset, bytesOffset + dataLength));\n bytesOffset += dataLength;\n values.push(value);\n } else {\n values.push(dynamicAbiTypeToDefaultValue[fieldType]);\n }\n });\n\n // Warn user if dynamic data length doesn't match the dynamic data length, 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 } from \"viem\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { InvalidHexLengthForPackedCounterError, PackedCounterLengthMismatchError } from \"./errors\";\nimport { readHex } from \"@latticexyz/common\";\n\n// Keep this logic in sync with PackedCounter.sol\n\n// - Last 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, indexed from right to left\n\n// We use byte lengths rather than item counts so that, on chain, we can slice without having to get the value 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\n const totalByteLength = decodeStaticField(\"uint56\", readHex(data, 32 - 7, 32));\n // TODO: use value schema to make sure we only parse as many as we need (rather than zeroes at the end)?\n const reversedFieldByteLengths = decodeDynamicField(\"uint40[]\", readHex(data, 0, 32 - 7));\n // Reverse the lengths\n const fieldByteLengths = Object.freeze([...reversedFieldByteLengths].reverse());\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 { isStaticAbiType, isDynamicAbiType } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { SchemaToPrimitives, ValueSchema } from \"./common\";\nimport { decodeRecord } from \"./decodeRecord\";\n\nexport function decodeValue<TSchema extends ValueSchema>(valueSchema: TSchema, data: Hex): SchemaToPrimitives<TSchema> {\n const staticFields = Object.values(valueSchema).filter(isStaticAbiType);\n const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);\n\n // TODO: refactor and move all decodeRecord logic into this method so we can delete decodeRecord\n const valueTuple = decodeRecord({ staticFields, dynamicFields }, data);\n\n return Object.fromEntries(\n Object.keys(valueSchema).map((name, i) => [name, valueTuple[i]])\n ) as SchemaToPrimitives<TSchema>;\n}\n","import { concatHex } from \"viem\";\nimport { isStaticAbiType } from \"@latticexyz/schema-type\";\nimport { SchemaToPrimitives, ValueArgs, ValueSchema } from \"./common\";\nimport { decodeValue } from \"./decodeValue\";\nimport { staticDataLength } from \"./staticDataLength\";\nimport { readHex } from \"@latticexyz/common\";\n\nexport function decodeValueArgs<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n { staticData, encodedLengths, dynamicData }: ValueArgs\n): SchemaToPrimitives<TSchema> {\n return decodeValue(\n valueSchema,\n concatHex([\n readHex(staticData, 0, staticDataLength(Object.values(valueSchema).filter(isStaticAbiType))),\n encodedLengths,\n dynamicData,\n ])\n );\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 // TODO: we can remove conditional once this is fixed: https://github.com/wagmi-dev/viem/pull/1147\n return value.length === 0\n ? \"0x\"\n : encodePacked(\n value.map(() => staticFieldType),\n value\n );\n }\n return encodePacked([fieldType], [value]);\n}\n","import { isStaticAbiType } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { SchemaToPrimitives, KeySchema } from \"./common\";\nimport { encodeKeyTuple } from \"./encodeKeyTuple\";\n\nexport function encodeKey<TSchema extends KeySchema>(keySchema: TSchema, key: SchemaToPrimitives<TSchema>): Hex[] {\n const staticFields = Object.values(keySchema).filter(isStaticAbiType);\n // TODO: refactor and move all encodeKeyTuple logic into this method so we can delete encodeKeyTuple\n return encodeKeyTuple({ staticFields, dynamicFields: [] }, Object.values(key));\n}\n","import { StaticPrimitiveType } from \"@latticexyz/schema-type\";\nimport { Hex, encodeAbiParameters } from \"viem\";\nimport { Schema } from \"./common\";\n\n/** @deprecated use `encodeKey` instead */\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\n/** @deprecated use `encodeValue` instead */\nexport function encodeRecord(\n valueSchema: Schema,\n values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[]\n): Hex {\n const staticValues = values.slice(0, valueSchema.staticFields.length) as readonly StaticPrimitiveType[];\n const dynamicValues = values.slice(valueSchema.staticFields.length) as readonly DynamicPrimitiveType[];\n\n const staticData = staticValues\n .map((value, i) => encodeField(valueSchema.staticFields[i], value).replace(/^0x/, \"\"))\n .join(\"\");\n\n if (valueSchema.dynamicFields.length === 0) return `0x${staticData}`;\n\n const dynamicDataItems = dynamicValues.map((value, i) =>\n encodeField(valueSchema.dynamicFields[i], value).replace(/^0x/, \"\")\n );\n\n const dynamicFieldByteLengths = dynamicDataItems.map((value) => value.length / 2).reverse();\n const dynamicTotalByteLength = dynamicFieldByteLengths.reduce((total, length) => total + BigInt(length), 0n);\n\n const dynamicData = dynamicDataItems.join(\"\");\n\n const packedCounter = `${dynamicFieldByteLengths\n .map((length) => encodeField(\"uint40\", length).replace(/^0x/, \"\"))\n .join(\"\")}${encodeField(\"uint56\", dynamicTotalByteLength).replace(/^0x/, \"\")}`.padStart(64, \"0\");\n\n return `0x${staticData}${packedCounter}${dynamicData}`;\n}\n","import { Hex, concatHex } from \"viem\";\nimport { SchemaToPrimitives, ValueSchema } from \"./common\";\nimport { encodeValueArgs } from \"./encodeValueArgs\";\n\nexport function encodeValue<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n value: SchemaToPrimitives<TSchema>\n): Hex {\n const { staticData, encodedLengths, dynamicData } = encodeValueArgs(valueSchema, value);\n return concatHex([staticData, encodedLengths, dynamicData]);\n}\n","import { StaticPrimitiveType, DynamicPrimitiveType, isStaticAbiType, isDynamicAbiType } from \"@latticexyz/schema-type\";\nimport { concatHex } from \"viem\";\nimport { encodeField } from \"./encodeField\";\nimport { SchemaToPrimitives, ValueArgs, ValueSchema } from \"./common\";\nimport { encodeLengths } from \"./encodeLengths\";\n\nexport function encodeValueArgs<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n value: SchemaToPrimitives<TSchema>\n): ValueArgs {\n const staticFields = Object.values(valueSchema).filter(isStaticAbiType);\n const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);\n\n const values = Object.values(value);\n const staticValues = values.slice(0, staticFields.length) as readonly StaticPrimitiveType[];\n const dynamicValues = values.slice(staticFields.length) as readonly DynamicPrimitiveType[];\n\n const encodedStaticValues = staticValues.map((value, i) => encodeField(staticFields[i], value));\n const encodedDynamicValues = dynamicValues.map((value, i) => encodeField(dynamicFields[i], value));\n const encodedLengths = encodeLengths(encodedDynamicValues);\n\n return {\n staticData: concatHex(encodedStaticValues),\n encodedLengths,\n dynamicData: concatHex(encodedDynamicValues),\n };\n}\n","import { Hex, concatHex, padHex, size } from \"viem\";\nimport { encodeField } from \"./encodeField\";\n\nexport function encodeLengths(values: Hex[]): Hex {\n const byteLengths = values.map(size).reverse();\n const totalByteLength = byteLengths.reduce((total, length) => total + BigInt(length), 0n);\n\n return padHex(\n concatHex([...byteLengths.map((length) => encodeField(\"uint40\", length)), encodeField(\"uint56\", totalByteLength)]),\n { size: 32, dir: \"left\" }\n );\n}\n","import { Hex } from \"viem\";\nimport { FieldLayout } from \"./common\";\n\n/** @deprecated use `valueSchemaToFieldLayoutHex` instead */\nexport function fieldLayoutToHex(fieldLayout: FieldLayout): Hex {\n const staticDataLength = fieldLayout.staticFieldLengths.reduce((totalLength, length) => totalLength + length, 0);\n return `0x${[\n staticDataLength.toString(16).padStart(4, \"0\"),\n fieldLayout.staticFieldLengths.length.toString(16).padStart(2, \"0\"),\n fieldLayout.numDynamicFields.toString(16).padStart(2, \"0\"),\n ...fieldLayout.staticFieldLengths.map((schemaType) => schemaType.toString(16).padStart(2, \"0\")),\n ]\n .join(\"\")\n .padEnd(64, \"0\")}`;\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 console.warn(\n `Schema \"${data}\" static data length (${staticDataLength}) did not match the summed length of all static fields (${actualStaticDataLength}). Is \\`staticAbiTypeToByteLength\\` up to date with Solidity schema types?`\n );\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 { isStaticAbiType } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { KeySchema } from \"./common\";\nimport { schemaToHex } from \"./schemaToHex\";\n\nexport function keySchemaToHex(keySchema: KeySchema): Hex {\n return schemaToHex({ staticFields: Object.values(keySchema).filter(isStaticAbiType), dynamicFields: [] });\n}\n","import { schemaAbiTypes } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { staticDataLength } from \"./staticDataLength\";\n\n/** @deprecated use `keySchemaToHex` or `valueSchemaToHex` instead */\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","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 { Hex } from \"viem\";\nimport { ValueSchema } from \"./common\";\nimport { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type\";\n\n// TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)\nexport function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {\n const staticFields = Object.values(valueSchema).filter(isStaticAbiType);\n const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);\n\n const staticFieldLengths = staticFields.map((fieldType) => staticAbiTypeToByteLength[fieldType]);\n const staticDataLength = staticFieldLengths.reduce((dataLength, fieldLength) => dataLength + fieldLength, 0);\n\n return `0x${[\n staticDataLength.toString(16).padStart(4, \"0\"),\n staticFields.length.toString(16).padStart(2, \"0\"),\n dynamicFields.length.toString(16).padStart(2, \"0\"),\n ...staticFieldLengths.map((fieldLength) => fieldLength.toString(16).padStart(2, \"0\")),\n ]\n .join(\"\")\n .padEnd(64, \"0\")}`;\n}\n","import { isDynamicAbiType, isStaticAbiType } from \"@latticexyz/schema-type\";\nimport { Hex } from \"viem\";\nimport { ValueSchema } from \"./common\";\nimport { schemaToHex } from \"./schemaToHex\";\n\nexport function valueSchemaToHex(valueSchema: ValueSchema): Hex {\n return schemaToHex({\n staticFields: Object.values(valueSchema).filter(isStaticAbiType),\n dynamicFields: Object.values(valueSchema).filter(isDynamicAbiType),\n });\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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KAAK,CAFaD,EAAmB,OAAO,CAACE,EAAYC,IAAgBD,EAAaC,EAAa,CAAC,EAGxF,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAC7CL,EAAa,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAChDC,EAAc,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EACjD,GAAGC,EAAmB,IAAKG,GAAgBA,EAAY,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,CACtF,EACG,KAAK,EAAE,EACP,OAAO,GAAI,GAAG,GACnB,CCpBA,OAAS,oBAAAC,GAAkB,mBAAAC,OAAuB,0BAK3C,SAASC,GAAiBC,EAA+B,CAC9D,OAAOC,EAAY,CACjB,aAAc,OAAO,OAAOD,CAAW,EAAE,OAAOE,EAAe,EAC/D,cAAe,OAAO,OAAOF,CAAW,EAAE,OAAOG,EAAgB,CACnE,CAAC,CACH","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","decodeKey","keySchema","data","keyValues","decodeKeyTuple","name","i","staticAbiTypeToByteLength","dynamicAbiTypeToDefaultValue","readHex","hexToPackedCounter","data","InvalidHexLengthForPackedCounterError","totalByteLength","decodeStaticField","reversedFieldByteLengths","decodeDynamicField","fieldByteLengths","summedLength","total","length","PackedCounterLengthMismatchError","staticAbiTypeToByteLength","staticDataLength","staticFields","length","fieldType","readHex","decodeRecord","valueSchema","data","values","bytesOffset","fieldType","fieldByteLength","staticAbiTypeToByteLength","value","decodeStaticField","schemaStaticDataLength","staticDataLength","actualStaticDataLength","dataLayout","hexToPackedCounter","i","dataLength","decodeDynamicField","dynamicAbiTypeToDefaultValue","actualDynamicDataLength","isStaticAbiType","isDynamicAbiType","decodeValue","valueSchema","data","staticFields","isStaticAbiType","dynamicFields","isDynamicAbiType","valueTuple","decodeRecord","name","i","concatHex","isStaticAbiType","readHex","decodeValueArgs","valueSchema","staticData","encodedLengths","dynamicData","decodeValue","concatHex","staticDataLength","isStaticAbiType","arrayAbiTypeToStaticAbiType","isArrayAbiType","encodePacked","encodeField","fieldType","value","staticFieldType","isStaticAbiType","encodeAbiParameters","encodeKeyTuple","keySchema","keyTuple","key","index","encodeKey","keySchema","key","staticFields","isStaticAbiType","encodeKeyTuple","encodeRecord","valueSchema","values","staticValues","dynamicValues","staticData","value","i","encodeField","dynamicDataItems","dynamicFieldByteLengths","dynamicTotalByteLength","total","length","dynamicData","packedCounter","concatHex","isStaticAbiType","isDynamicAbiType","concatHex","concatHex","padHex","size","encodeLengths","values","byteLengths","size","totalByteLength","total","length","padHex","concatHex","encodeField","encodeValueArgs","valueSchema","value","staticFields","isStaticAbiType","dynamicFields","isDynamicAbiType","values","staticValues","dynamicValues","encodedStaticValues","i","encodeField","encodedDynamicValues","encodedLengths","encodeLengths","concatHex","encodeValue","valueSchema","value","staticData","encodedLengths","dynamicData","encodeValueArgs","concatHex","fieldLayoutToHex","fieldLayout","totalLength","length","schemaType","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","isStaticAbiType","schemaAbiTypes","schemaToHex","schema","staticSchemaTypes","abiType","schemaAbiTypes","dynamicSchemaTypes","staticDataLength","schemaType","keySchemaToHex","keySchema","schemaToHex","isStaticAbiType","schemaIndexToAbiType","schema","schemaIndex","isDynamicAbiType","isStaticAbiType","staticAbiTypeToByteLength","valueSchemaToFieldLayoutHex","valueSchema","staticFields","dynamicFields","staticFieldLengths","fieldType","dataLength","fieldLength","isDynamicAbiType","isStaticAbiType","valueSchemaToHex","valueSchema","schemaToHex","isStaticAbiType","isDynamicAbiType"]}
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { StaticAbiType, DynamicAbiType, SchemaAbiType, SchemaAbiTypeToPrimitiveType, DynamicAbiTypeToPrimitiveType, StaticPrimitiveType, DynamicPrimitiveType, StaticAbiTypeToPrimitiveType } from '@latticexyz/schema-type/internal';
|
|
2
|
+
import { Hex } from 'viem';
|
|
3
|
+
import { AbiParameterToPrimitiveType } from 'abitype';
|
|
4
|
+
import { MUDError } from '@latticexyz/common/errors';
|
|
5
|
+
import { Table, Schema as Schema$1 } from '@latticexyz/config';
|
|
6
|
+
|
|
7
|
+
/** @deprecated use `KeySchema` or `ValueSchema` instead */
|
|
8
|
+
type Schema = {
|
|
9
|
+
readonly staticFields: readonly StaticAbiType[];
|
|
10
|
+
readonly dynamicFields: readonly DynamicAbiType[];
|
|
11
|
+
};
|
|
12
|
+
/** @deprecated use `KeySchema` and `ValueSchema` instead */
|
|
13
|
+
type TableSchema = {
|
|
14
|
+
readonly keySchema: Schema;
|
|
15
|
+
readonly valueSchema: Schema;
|
|
16
|
+
};
|
|
17
|
+
type FieldLayout = {
|
|
18
|
+
readonly staticFieldLengths: readonly number[];
|
|
19
|
+
readonly numDynamicFields: number;
|
|
20
|
+
};
|
|
21
|
+
type UserTypes = Record<string, {
|
|
22
|
+
internalType: SchemaAbiType;
|
|
23
|
+
}>;
|
|
24
|
+
type KeySchema<userTypes extends UserTypes | undefined = undefined> = Record<string, userTypes extends UserTypes ? StaticAbiType | keyof userTypes : StaticAbiType>;
|
|
25
|
+
type ValueSchema<userTypes extends UserTypes | undefined = undefined> = Record<string, userTypes extends UserTypes ? SchemaAbiType | keyof userTypes : SchemaAbiType>;
|
|
26
|
+
/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */
|
|
27
|
+
type SchemaToPrimitives<TSchema extends ValueSchema> = {
|
|
28
|
+
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>;
|
|
29
|
+
};
|
|
30
|
+
type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = {
|
|
31
|
+
key: SchemaToPrimitives<TKeySchema>;
|
|
32
|
+
value: SchemaToPrimitives<TValueSchema>;
|
|
33
|
+
};
|
|
34
|
+
type ValueArgs = {
|
|
35
|
+
staticData: Hex;
|
|
36
|
+
encodedLengths: Hex;
|
|
37
|
+
dynamicData: Hex;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
declare function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema;
|
|
41
|
+
|
|
42
|
+
declare function decodeDynamicField<TAbiType extends DynamicAbiType, TPrimitiveType extends DynamicAbiTypeToPrimitiveType<TAbiType>>(abiType: TAbiType, data: Hex): TPrimitiveType;
|
|
43
|
+
|
|
44
|
+
declare function decodeField<TAbiType extends SchemaAbiType, TPrimitiveType extends SchemaAbiTypeToPrimitiveType<TAbiType>>(abiType: TAbiType, data: Hex): TPrimitiveType;
|
|
45
|
+
|
|
46
|
+
declare function decodeKey<TSchema extends KeySchema>(keySchema: TSchema, data: readonly Hex[]): SchemaToPrimitives<TSchema>;
|
|
47
|
+
|
|
48
|
+
/** @deprecated use `decodeKey` instead */
|
|
49
|
+
declare function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[];
|
|
50
|
+
|
|
51
|
+
/** @deprecated use `decodeValue` instead */
|
|
52
|
+
declare function decodeRecord(valueSchema: Schema, data: Hex): readonly (StaticPrimitiveType | DynamicPrimitiveType)[];
|
|
53
|
+
|
|
54
|
+
declare function decodeStaticField<TAbiType extends StaticAbiType, TPrimitiveType extends StaticAbiTypeToPrimitiveType<TAbiType>>(abiType: TAbiType, data: Hex): TPrimitiveType;
|
|
55
|
+
|
|
56
|
+
declare function decodeValue<TSchema extends ValueSchema>(valueSchema: TSchema, data: Hex): SchemaToPrimitives<TSchema>;
|
|
57
|
+
|
|
58
|
+
declare function decodeValueArgs<TSchema extends ValueSchema>(valueSchema: TSchema, { staticData, encodedLengths, dynamicData }: ValueArgs): SchemaToPrimitives<TSchema>;
|
|
59
|
+
|
|
60
|
+
declare function encodeField<TSchemaAbiType extends SchemaAbiType>(fieldType: TSchemaAbiType, value: AbiParameterToPrimitiveType<{
|
|
61
|
+
type: TSchemaAbiType;
|
|
62
|
+
}>): Hex;
|
|
63
|
+
|
|
64
|
+
declare function encodeKey<TSchema extends KeySchema>(keySchema: TSchema, key: SchemaToPrimitives<TSchema>): Hex[];
|
|
65
|
+
|
|
66
|
+
/** @deprecated use `encodeKey` instead */
|
|
67
|
+
declare function encodeKeyTuple(keySchema: Schema, keyTuple: StaticPrimitiveType[]): Hex[];
|
|
68
|
+
|
|
69
|
+
/** @deprecated use `encodeValue` instead */
|
|
70
|
+
declare function encodeRecord(valueSchema: Schema, values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[]): Hex;
|
|
71
|
+
|
|
72
|
+
declare function encodeValue<TSchema extends ValueSchema>(valueSchema: TSchema, value: SchemaToPrimitives<TSchema>): Hex;
|
|
73
|
+
|
|
74
|
+
declare function encodeValueArgs<TSchema extends ValueSchema>(valueSchema: TSchema, value: SchemaToPrimitives<TSchema>): ValueArgs;
|
|
75
|
+
|
|
76
|
+
declare class InvalidHexLengthError extends MUDError {
|
|
77
|
+
name: string;
|
|
78
|
+
constructor(value: Hex);
|
|
79
|
+
}
|
|
80
|
+
declare class InvalidHexLengthForSchemaError extends MUDError {
|
|
81
|
+
name: string;
|
|
82
|
+
constructor(value: Hex);
|
|
83
|
+
}
|
|
84
|
+
declare class InvalidHexLengthForEncodedLengthsError extends MUDError {
|
|
85
|
+
name: string;
|
|
86
|
+
constructor(value: Hex);
|
|
87
|
+
}
|
|
88
|
+
declare class InvalidHexLengthForStaticFieldError extends MUDError {
|
|
89
|
+
name: string;
|
|
90
|
+
constructor(abiType: StaticAbiType, value: Hex);
|
|
91
|
+
}
|
|
92
|
+
declare class InvalidHexLengthForArrayFieldError extends MUDError {
|
|
93
|
+
name: string;
|
|
94
|
+
constructor(abiType: StaticAbiType, value: Hex);
|
|
95
|
+
}
|
|
96
|
+
declare class SchemaStaticLengthMismatchError extends MUDError {
|
|
97
|
+
name: string;
|
|
98
|
+
constructor(schemaData: Hex, definedLength: number, summedLength: number);
|
|
99
|
+
}
|
|
100
|
+
declare class EncodedLengthsLengthMismatchError extends MUDError {
|
|
101
|
+
name: string;
|
|
102
|
+
constructor(encodedLengthsData: Hex, definedLength: bigint, summedLength: bigint);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @deprecated use `valueSchemaToFieldLayoutHex` instead */
|
|
106
|
+
declare function fieldLayoutToHex(fieldLayout: FieldLayout): Hex;
|
|
107
|
+
|
|
108
|
+
declare function hexToEncodedLengths(data: Hex): {
|
|
109
|
+
totalByteLength: bigint;
|
|
110
|
+
fieldByteLengths: readonly number[];
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
declare function hexToSchema(data: Hex): Schema;
|
|
114
|
+
|
|
115
|
+
declare function hexToTableSchema(data: Hex): TableSchema;
|
|
116
|
+
|
|
117
|
+
declare function keySchemaToHex(keySchema: KeySchema): Hex;
|
|
118
|
+
|
|
119
|
+
declare function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType;
|
|
120
|
+
|
|
121
|
+
/** @deprecated use `keySchemaToHex` or `valueSchemaToHex` instead */
|
|
122
|
+
declare function schemaToHex(schema: Schema): Hex;
|
|
123
|
+
|
|
124
|
+
declare function staticDataLength(staticFields: readonly StaticAbiType[]): number;
|
|
125
|
+
|
|
126
|
+
declare function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex;
|
|
127
|
+
|
|
128
|
+
declare function valueSchemaToHex(valueSchema: ValueSchema): Hex;
|
|
129
|
+
|
|
130
|
+
type getKeySchema<table extends Table> = {
|
|
131
|
+
[fieldName in table["key"][number]]: table["schema"][fieldName];
|
|
132
|
+
};
|
|
133
|
+
declare function getKeySchema<table extends Table>(table: table): getKeySchema<table>;
|
|
134
|
+
|
|
135
|
+
type getValueSchema<table extends Table> = {
|
|
136
|
+
[fieldName in Exclude<keyof table["schema"], table["key"][number]>]: table["schema"][fieldName];
|
|
137
|
+
};
|
|
138
|
+
declare function getValueSchema<table extends Table>(table: table): getValueSchema<table>;
|
|
139
|
+
|
|
140
|
+
type getSchemaTypes<schema extends Schema$1> = {
|
|
141
|
+
readonly [k in keyof schema]: schema[k]["type"];
|
|
142
|
+
};
|
|
143
|
+
declare function getSchemaTypes<schema extends Schema$1>(schema: schema): getSchemaTypes<schema>;
|
|
144
|
+
|
|
145
|
+
export { EncodedLengthsLengthMismatchError, FieldLayout, InvalidHexLengthError, InvalidHexLengthForArrayFieldError, InvalidHexLengthForEncodedLengthsError, InvalidHexLengthForSchemaError, InvalidHexLengthForStaticFieldError, KeySchema, Schema, SchemaStaticLengthMismatchError, SchemaToPrimitives, TableRecord, TableSchema, UserTypes, ValueArgs, ValueSchema, abiTypesToSchema, decodeDynamicField, decodeField, decodeKey, decodeKeyTuple, decodeRecord, decodeStaticField, decodeValue, decodeValueArgs, encodeField, encodeKey, encodeKeyTuple, encodeRecord, encodeValue, encodeValueArgs, fieldLayoutToHex, getKeySchema, getSchemaTypes, getValueSchema, hexToEncodedLengths, hexToSchema, hexToTableSchema, keySchemaToHex, schemaIndexToAbiType, schemaToHex, staticDataLength, valueSchemaToFieldLayoutHex, valueSchemaToHex };
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{isDynamicAbiType as R}from"@latticexyz/schema-type/internal";function we(e){let t=[],a=[];for(let i of e)R(i)?a.push(i):t.push(i);return{staticFields:t,dynamicFields:a}}import{hexToString as Q,sliceHex as X}from"viem";import{assertExhaustive as Y}from"@latticexyz/common/utils";import{arrayToStaticAbiType as Z,staticAbiTypeToByteLength as ee}from"@latticexyz/schema-type/internal";import{getAddress as W,hexToBigInt as _,hexToBool as q}from"viem";import{assertExhaustive as C}from"@latticexyz/common/utils";import{staticAbiTypeToByteLength as G,staticAbiTypeToDefaultValue as J}from"@latticexyz/schema-type/internal";import{MUDError as l}from"@latticexyz/common/errors";import{staticAbiTypeToByteLength as $}from"@latticexyz/schema-type/internal";var u=class extends l{name="InvalidHexValueError";constructor(t){super(`Hex value "${t}" is an odd length (${t.length-2}). It must be an even length.`)}},S=class extends l{name="InvalidHexLengthForSchemaError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for a schema.`)}},g=class extends l{name="InvalidHexLengthForEncodedLengthsError";constructor(t){super(`Hex value "${t}" has length of ${t.length-2}, but expected length of 64 for encoded lengths.`)}},A=class extends l{name="InvalidHexLengthForStaticFieldError";constructor(t,a){super(`Hex value "${a}" has length of ${a.length-2}, but expected length of ${$[t]*2} for ${t} type.`)}},v=class extends l{name="InvalidHexLengthForArrayFieldError";constructor(t,a){super(`Hex value "${a}" has length of ${a.length-2}, but expected a multiple of ${$[t]*2} for ${t}[] type.`)}},H=class extends l{name="SchemaStaticLengthMismatchError";constructor(t,a,i){super(`Schema "${t}" static data length (${a}) did not match the summed length of all static fields (${i}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`)}},F=class extends l{name="EncodedLengthsLengthMismatchError";constructor(t,a,i){super(`EncodedLengths "${t}" total bytes length (${a}) did not match the summed length of all field byte lengths (${i}).`)}};function y(e,t){if(t.length>3&&t.length%2!==0)throw new u(t);if((t.length-2)/2!==G[e])throw new A(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=_(t,{signed:e.startsWith("int")}),c=typeof J[e];if(c==="number")return Number(i);if(c==="bigint")return i;throw new Error(`Unexpected default value type (${c}) 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 q(t);case"address":return W(t)}return C(e,`Unsupported static ABI type: ${e}`)}function f(e,t){if(e==="bytes")return t;if(e==="string")return Q(t);if(t.length>3&&t.length%2!==0)throw new u(t);let a=(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=Z(e),c=ee[i];if(a%c!==0)throw new v(i,t);return new Array(a/c).fill(void 0).map((r,s)=>{let o=X(t,s*c,(s+1)*c);return y(i,o)})}}return Y(e,`Unsupported dynamic ABI type: ${e}`)}import{isDynamicAbiType as te}from"@latticexyz/schema-type/internal";function nt(e,t){return te(e)?f(e,t):y(e,t)}import{decodeAbiParameters as ie}from"viem";function V(e,t){if(e.staticFields.length!==t.length)throw new Error(`key tuple length ${t.length} does not match key schema length ${e.staticFields.length}`);return t.map((a,i)=>ie([{type:e.staticFields[i]}],a)[0])}function ht(e,t){let a=V({staticFields:Object.values(e),dynamicFields:[]},t);return Object.fromEntries(Object.keys(e).map((i,c)=>[i,a[c]]))}import{staticAbiTypeToByteLength as ce,dynamicAbiTypeToDefaultValue as se}from"@latticexyz/schema-type/internal";import{readHex as B}from"@latticexyz/common";function E(e){if(e.length!==66)throw new g(e);let t=y("uint56",B(e,32-7,32)),a=f("uint40[]",B(e,0,32-7)),i=Object.freeze([...a].reverse()),c=BigInt(i.reduce((n,r)=>n+r,0));if(c!==t)throw new F(e,t,c);return{totalByteLength:t,fieldByteLengths:i}}import{staticAbiTypeToByteLength as ae}from"@latticexyz/schema-type/internal";function T(e){return e.reduce((t,a)=>t+ae[a],0)}import{readHex as P}from"@latticexyz/common";function w(e,t){let a=[],i=0;e.staticFields.forEach(r=>{let s=ce[r],o=y(r,P(t,i,i+s));i+=s,a.push(o)});let c=T(e.staticFields),n=i;if(n!==c&&console.warn("Decoded static data length does not match value schema's expected static data length. Data may get corrupted. Is `getStaticByteLength` outdated?",{expectedLength:c,actualLength:n,bytesOffset:i}),e.dynamicFields.length>0){let r=E(P(t,i,i+32));i+=32,e.dynamicFields.forEach((o,d)=>{let m=r.fieldByteLengths[d];if(m>0){let h=f(o,P(t,i,i+m));i+=m,a.push(h)}else a.push(se[o])});let s=i-32-n;BigInt(s)!==r.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:r.totalByteLength,actualLength:s,bytesOffset:i})}return a}import{isStaticAbiType as re,isDynamicAbiType as ne}from"@latticexyz/schema-type/internal";function j(e,t){let a=Object.values(e).filter(re),i=Object.values(e).filter(ne),c=w({staticFields:a,dynamicFields:i},t);return Object.fromEntries(Object.keys(e).map((n,r)=>[n,c[r]]))}import{concatHex as oe}from"viem";import{isStaticAbiType as me}from"@latticexyz/schema-type/internal";import{readHex as pe}from"@latticexyz/common";function Nt(e,{staticData:t,encodedLengths:a,dynamicData:i}){return j(e,oe([pe(t,0,T(Object.values(e).filter(me))),a,i]))}import{arrayToStaticAbiType as ye,isArrayAbiType as de}from"@latticexyz/schema-type/internal";import{encodePacked as O}from"viem";function p(e,t){if(de(e)&&Array.isArray(t)){let a=ye(e);return t.length===0?"0x":O(t.map(()=>a),t)}return O([e],[t])}import{isStaticAbiType as le}from"@latticexyz/schema-type/internal";import{encodeAbiParameters as he}from"viem";function I(e,t){return t.map((a,i)=>he([{type:e.staticFields[i]}],[a]))}function Yt(e,t){let a=Object.values(e).filter(le);return I({staticFields:a,dynamicFields:[]},Object.values(t))}function ti(e,t){let a=t.slice(0,e.staticFields.length),i=t.slice(e.staticFields.length),c=a.map((m,h)=>p(e.staticFields[h],m).replace(/^0x/,"")).join("");if(e.dynamicFields.length===0)return`0x${c}`;let n=i.map((m,h)=>p(e.dynamicFields[h],m).replace(/^0x/,"")),r=n.map(m=>m.length/2).reverse(),s=r.reduce((m,h)=>m+BigInt(h),0n),o=n.join(""),d=`${r.map(m=>p("uint40",m).replace(/^0x/,"")).join("")}${p("uint56",s).replace(/^0x/,"")}`.padStart(64,"0");return`0x${c}${d}${o}`}import{concatHex as Se}from"viem";import{isStaticAbiType as xe,isDynamicAbiType as be}from"@latticexyz/schema-type/internal";import{concatHex as k}from"viem";import{concatHex as ue,padHex as fe,size as Te}from"viem";function K(e){let t=e.map(Te).reverse(),a=t.reduce((i,c)=>i+BigInt(c),0n);return fe(ue([...t.map(i=>p("uint40",i)),p("uint56",a)]),{size:32,dir:"left"})}function z(e,t){let a=Object.entries(e),i=a.filter(([,o])=>xe(o)),c=a.filter(([,o])=>be(o)),n=i.map(([o,d])=>p(d,t[o])),r=c.map(([o,d])=>p(d,t[o])),s=K(r);return{staticData:k(n),encodedLengths:s,dynamicData:k(r)}}function bi(e,t){let{staticData:a,encodedLengths:i,dynamicData:c}=z(e,t);return Se([a,i,c])}function gi(e){return`0x${[e.staticFieldLengths.reduce((a,i)=>a+i,0).toString(16).padStart(4,"0"),e.staticFieldLengths.length.toString(16).padStart(2,"0"),e.numDynamicFields.toString(16).padStart(2,"0"),...e.staticFieldLengths.map(a=>a.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}import{schemaAbiTypes as N,staticAbiTypeToByteLength as ge}from"@latticexyz/schema-type/internal";import{hexToNumber as x,sliceHex as b}from"viem";function D(e){if(e.length!==66)throw new S(e);let t=x(b(e,0,2)),a=x(b(e,2,3)),i=x(b(e,3,4)),c=[],n=[];for(let s=4;s<4+a;s++){let o=x(b(e,s,s+1));c.push(N[o])}for(let s=4+a;s<4+a+i;s++){let o=x(b(e,s,s+1));n.push(N[o])}let r=c.reduce((s,o)=>s+ge[o],0);if(r!==t)throw console.warn(`Schema "${e}" static data length (${t}) did not match the summed length of all static fields (${r}). Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`),new H(e,t,r);return{staticFields:c,dynamicFields:n}}import{sliceHex as U}from"viem";function wi(e){let t=D(U(e,0,32));return{keySchema:D(U(e,32,64)),valueSchema:t}}import{isStaticAbiType as Ae}from"@latticexyz/schema-type/internal";import{schemaAbiTypes as M}from"@latticexyz/schema-type/internal";function L(e){let t=e.staticFields.map(i=>M.indexOf(i)),a=e.dynamicFields.map(i=>M.indexOf(i));return`0x${[T(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")),...a.map(i=>i.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}function Ni(e){return L({staticFields:Object.values(e).filter(Ae),dynamicFields:[]})}function Mi(e,t){return t<e.staticFields.length?e.staticFields[t]:e.dynamicFields[t-e.staticFields.length]}import{isDynamicAbiType as ve,isStaticAbiType as He,staticAbiTypeToByteLength as Fe}from"@latticexyz/schema-type/internal";function _i(e){let t=Object.values(e).filter(He),a=Object.values(e).filter(ve),i=t.map(n=>Fe[n]);return`0x${[i.reduce((n,r)=>n+r,0).toString(16).padStart(4,"0"),t.length.toString(16).padStart(2,"0"),a.length.toString(16).padStart(2,"0"),...i.map(n=>n.toString(16).padStart(2,"0"))].join("").padEnd(64,"0")}`}import{isDynamicAbiType as Le,isStaticAbiType as Pe}from"@latticexyz/schema-type/internal";function Ji(e){return L({staticFields:Object.values(e).filter(Pe),dynamicFields:Object.values(e).filter(Le)})}function Xi(e){return Object.fromEntries(e.key.map(t=>[t,e.schema[t]]))}function Zi(e){return Object.fromEntries(Object.entries(e.schema).filter(([t])=>!e.key.includes(t)))}import{mapObject as De}from"@latticexyz/common/utils";function ia(e){return De(e,t=>t.type)}export{F as EncodedLengthsLengthMismatchError,u as InvalidHexLengthError,v as InvalidHexLengthForArrayFieldError,g as InvalidHexLengthForEncodedLengthsError,S as InvalidHexLengthForSchemaError,A as InvalidHexLengthForStaticFieldError,H as SchemaStaticLengthMismatchError,we as abiTypesToSchema,f as decodeDynamicField,nt as decodeField,ht as decodeKey,V as decodeKeyTuple,w as decodeRecord,y as decodeStaticField,j as decodeValue,Nt as decodeValueArgs,p as encodeField,Yt as encodeKey,I as encodeKeyTuple,ti as encodeRecord,bi as encodeValue,z as encodeValueArgs,gi as fieldLayoutToHex,Xi as getKeySchema,ia as getSchemaTypes,Zi as getValueSchema,E as hexToEncodedLengths,D as hexToSchema,wi as hexToTableSchema,Ni as keySchemaToHex,Mi as schemaIndexToAbiType,L as schemaToHex,T as staticDataLength,_i as valueSchemaToFieldLayoutHex,Ji as valueSchemaToHex};
|
|
2
|
+
//# sourceMappingURL=internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/abiTypesToSchema.ts","../src/decodeDynamicField.ts","../src/decodeStaticField.ts","../src/errors.ts","../src/decodeField.ts","../src/decodeKeyTuple.ts","../src/decodeKey.ts","../src/decodeRecord.ts","../src/hexToEncodedLengths.ts","../src/staticDataLength.ts","../src/decodeValue.ts","../src/decodeValueArgs.ts","../src/encodeField.ts","../src/encodeKey.ts","../src/encodeKeyTuple.ts","../src/encodeRecord.ts","../src/encodeValue.ts","../src/encodeValueArgs.ts","../src/encodeLengths.ts","../src/fieldLayoutToHex.ts","../src/hexToSchema.ts","../src/hexToTableSchema.ts","../src/keySchemaToHex.ts","../src/schemaToHex.ts","../src/schemaIndexToAbiType.ts","../src/valueSchemaToFieldLayoutHex.ts","../src/valueSchemaToHex.ts","../src/getKeySchema.ts","../src/getValueSchema.ts","../src/getSchemaTypes.ts"],"sourcesContent":["import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from \"@latticexyz/schema-type/internal\";\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 arrayToStaticAbiType,\n staticAbiTypeToByteLength,\n} from \"@latticexyz/schema-type/internal\";\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 = arrayToStaticAbiType(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/internal\";\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/internal\";\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 InvalidHexLengthForEncodedLengthsError extends MUDError {\n override name = \"InvalidHexLengthForEncodedLengthsError\";\n constructor(value: Hex) {\n super(`Hex value \"${value}\" has length of ${value.length - 2}, but expected length of 64 for encoded lengths.`);\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}). ` +\n `Is \\`staticAbiTypeToByteLength\\` up to date with Solidity schema types?`,\n );\n }\n}\n\nexport class EncodedLengthsLengthMismatchError extends MUDError {\n override name = \"EncodedLengthsLengthMismatchError\";\n constructor(encodedLengthsData: Hex, definedLength: bigint, summedLength: bigint) {\n super(\n `EncodedLengths \"${encodedLengthsData}\" 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/internal\";\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/internal\";\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\n/** @deprecated use `decodeKey` instead */\nexport function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {\n if (keySchema.staticFields.length !== keyTuple.length) {\n throw new Error(\n `key tuple length ${keyTuple.length} does not match key schema length ${keySchema.staticFields.length}`,\n );\n }\n return keyTuple.map(\n (key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType,\n );\n}\n","import { Hex } from \"viem\";\nimport { SchemaToPrimitives, KeySchema } from \"./common\";\nimport { decodeKeyTuple } from \"./decodeKeyTuple\";\n\nexport function decodeKey<TSchema extends KeySchema>(\n keySchema: TSchema,\n data: readonly Hex[],\n): SchemaToPrimitives<TSchema> {\n // TODO: refactor and move all decodeKeyTuple logic into this method so we can delete decodeKeyTuple\n const keyValues = decodeKeyTuple({ staticFields: Object.values(keySchema), dynamicFields: [] }, data);\n\n return Object.fromEntries(\n Object.keys(keySchema).map((name, i) => [name, keyValues[i]]),\n ) as SchemaToPrimitives<TSchema>;\n}\n","import {\n StaticPrimitiveType,\n DynamicPrimitiveType,\n staticAbiTypeToByteLength,\n dynamicAbiTypeToDefaultValue,\n} from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { hexToEncodedLengths } from \"./hexToEncodedLengths\";\nimport { staticDataLength } from \"./staticDataLength\";\nimport { readHex } from \"@latticexyz/common\";\n\n/** @deprecated use `decodeValue` instead */\nexport function decodeRecord(valueSchema: Schema, data: Hex): readonly (StaticPrimitiveType | DynamicPrimitiveType)[] {\n const values: (StaticPrimitiveType | DynamicPrimitiveType)[] = [];\n\n let bytesOffset = 0;\n valueSchema.staticFields.forEach((fieldType) => {\n const fieldByteLength = staticAbiTypeToByteLength[fieldType];\n const value = decodeStaticField(fieldType, readHex(data, bytesOffset, bytesOffset + fieldByteLength));\n bytesOffset += fieldByteLength;\n values.push(value);\n });\n\n // Warn user if static data length doesn't match the value schema, because data corruption might be possible.\n const schemaStaticDataLength = staticDataLength(valueSchema.staticFields);\n const actualStaticDataLength = bytesOffset;\n if (actualStaticDataLength !== schemaStaticDataLength) {\n console.warn(\n \"Decoded static data length does not match value 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 (valueSchema.dynamicFields.length > 0) {\n const dataLayout = hexToEncodedLengths(readHex(data, bytesOffset, bytesOffset + 32));\n bytesOffset += 32;\n\n valueSchema.dynamicFields.forEach((fieldType, i) => {\n const dataLength = dataLayout.fieldByteLengths[i];\n if (dataLength > 0) {\n const value = decodeDynamicField(fieldType, readHex(data, bytesOffset, bytesOffset + dataLength));\n bytesOffset += dataLength;\n values.push(value);\n } else {\n values.push(dynamicAbiTypeToDefaultValue[fieldType]);\n }\n });\n\n // Warn user if dynamic data length doesn't match the dynamic data length, 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 } from \"viem\";\nimport { decodeStaticField } from \"./decodeStaticField\";\nimport { decodeDynamicField } from \"./decodeDynamicField\";\nimport { InvalidHexLengthForEncodedLengthsError, EncodedLengthsLengthMismatchError } from \"./errors\";\nimport { readHex } from \"@latticexyz/common\";\n\n// Keep this logic in sync with EncodedLengths.sol\n\n// - Last 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, indexed from right to left\n\n// We use byte lengths rather than item counts so that, on chain, we can slice without having to get the value schema first (and thus the field lengths of each dynamic type)\n\nexport function hexToEncodedLengths(data: Hex): {\n totalByteLength: bigint;\n fieldByteLengths: readonly number[];\n} {\n if (data.length !== 66) {\n throw new InvalidHexLengthForEncodedLengthsError(data);\n }\n\n const totalByteLength = decodeStaticField(\"uint56\", readHex(data, 32 - 7, 32));\n // TODO: use value schema to make sure we only parse as many as we need (rather than zeroes at the end)?\n const reversedFieldByteLengths = decodeDynamicField(\"uint40[]\", readHex(data, 0, 32 - 7));\n // Reverse the lengths\n const fieldByteLengths = Object.freeze([...reversedFieldByteLengths].reverse());\n\n const summedLength = BigInt(fieldByteLengths.reduce((total, length) => total + length, 0));\n if (summedLength !== totalByteLength) {\n throw new EncodedLengthsLengthMismatchError(data, totalByteLength, summedLength);\n }\n\n return { totalByteLength, fieldByteLengths };\n}\n","import { StaticAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type/internal\";\n\nexport function staticDataLength(staticFields: readonly StaticAbiType[]): number {\n return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);\n}\n","import { isStaticAbiType, isDynamicAbiType } from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { SchemaToPrimitives, ValueSchema } from \"./common\";\nimport { decodeRecord } from \"./decodeRecord\";\n\nexport function decodeValue<TSchema extends ValueSchema>(valueSchema: TSchema, data: Hex): SchemaToPrimitives<TSchema> {\n const staticFields = Object.values(valueSchema).filter(isStaticAbiType);\n const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);\n\n // TODO: refactor and move all decodeRecord logic into this method so we can delete decodeRecord\n const valueTuple = decodeRecord({ staticFields, dynamicFields }, data);\n\n return Object.fromEntries(\n Object.keys(valueSchema).map((name, i) => [name, valueTuple[i]]),\n ) as SchemaToPrimitives<TSchema>;\n}\n","import { concatHex } from \"viem\";\nimport { isStaticAbiType } from \"@latticexyz/schema-type/internal\";\nimport { SchemaToPrimitives, ValueArgs, ValueSchema } from \"./common\";\nimport { decodeValue } from \"./decodeValue\";\nimport { staticDataLength } from \"./staticDataLength\";\nimport { readHex } from \"@latticexyz/common\";\n\nexport function decodeValueArgs<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n { staticData, encodedLengths, dynamicData }: ValueArgs,\n): SchemaToPrimitives<TSchema> {\n return decodeValue(\n valueSchema,\n concatHex([\n readHex(staticData, 0, staticDataLength(Object.values(valueSchema).filter(isStaticAbiType))),\n encodedLengths,\n dynamicData,\n ]),\n );\n}\n","import { SchemaAbiType, arrayToStaticAbiType, isArrayAbiType } from \"@latticexyz/schema-type/internal\";\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 = arrayToStaticAbiType(fieldType);\n // TODO: we can remove conditional once this is fixed: https://github.com/wagmi-dev/viem/pull/1147\n return value.length === 0\n ? \"0x\"\n : encodePacked(\n value.map(() => staticFieldType),\n value,\n );\n }\n return encodePacked([fieldType], [value]);\n}\n","import { isStaticAbiType } from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { SchemaToPrimitives, KeySchema } from \"./common\";\nimport { encodeKeyTuple } from \"./encodeKeyTuple\";\n\nexport function encodeKey<TSchema extends KeySchema>(keySchema: TSchema, key: SchemaToPrimitives<TSchema>): Hex[] {\n const staticFields = Object.values(keySchema).filter(isStaticAbiType);\n // TODO: refactor and move all encodeKeyTuple logic into this method so we can delete encodeKeyTuple\n return encodeKeyTuple({ staticFields, dynamicFields: [] }, Object.values(key));\n}\n","import { StaticPrimitiveType } from \"@latticexyz/schema-type/internal\";\nimport { Hex, encodeAbiParameters } from \"viem\";\nimport { Schema } from \"./common\";\n\n/** @deprecated use `encodeKey` instead */\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/internal\";\nimport { Hex } from \"viem\";\nimport { encodeField } from \"./encodeField\";\nimport { Schema } from \"./common\";\n\n/** @deprecated use `encodeValue` instead */\nexport function encodeRecord(\n valueSchema: Schema,\n values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[],\n): Hex {\n const staticValues = values.slice(0, valueSchema.staticFields.length) as readonly StaticPrimitiveType[];\n const dynamicValues = values.slice(valueSchema.staticFields.length) as readonly DynamicPrimitiveType[];\n\n const staticData = staticValues\n .map((value, i) => encodeField(valueSchema.staticFields[i], value).replace(/^0x/, \"\"))\n .join(\"\");\n\n if (valueSchema.dynamicFields.length === 0) return `0x${staticData}`;\n\n const dynamicDataItems = dynamicValues.map((value, i) =>\n encodeField(valueSchema.dynamicFields[i], value).replace(/^0x/, \"\"),\n );\n\n const dynamicFieldByteLengths = dynamicDataItems.map((value) => value.length / 2).reverse();\n const dynamicTotalByteLength = dynamicFieldByteLengths.reduce((total, length) => total + BigInt(length), 0n);\n\n const dynamicData = dynamicDataItems.join(\"\");\n\n const encodedLengths = `${dynamicFieldByteLengths\n .map((length) => encodeField(\"uint40\", length).replace(/^0x/, \"\"))\n .join(\"\")}${encodeField(\"uint56\", dynamicTotalByteLength).replace(/^0x/, \"\")}`.padStart(64, \"0\");\n\n return `0x${staticData}${encodedLengths}${dynamicData}`;\n}\n","import { Hex, concatHex } from \"viem\";\nimport { SchemaToPrimitives, ValueSchema } from \"./common\";\nimport { encodeValueArgs } from \"./encodeValueArgs\";\n\nexport function encodeValue<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n value: SchemaToPrimitives<TSchema>,\n): Hex {\n const { staticData, encodedLengths, dynamicData } = encodeValueArgs(valueSchema, value);\n return concatHex([staticData, encodedLengths, dynamicData]);\n}\n","import {\n StaticPrimitiveType,\n DynamicPrimitiveType,\n isStaticAbiType,\n isDynamicAbiType,\n StaticAbiType,\n DynamicAbiType,\n} from \"@latticexyz/schema-type/internal\";\nimport { concatHex } from \"viem\";\nimport { encodeField } from \"./encodeField\";\nimport { SchemaToPrimitives, ValueArgs, ValueSchema } from \"./common\";\nimport { encodeLengths } from \"./encodeLengths\";\n\nexport function encodeValueArgs<TSchema extends ValueSchema>(\n valueSchema: TSchema,\n value: SchemaToPrimitives<TSchema>,\n): ValueArgs {\n const valueSchemaEntries = Object.entries(valueSchema);\n const staticFields = valueSchemaEntries.filter(([, type]) => isStaticAbiType(type)) as [string, StaticAbiType][];\n const dynamicFields = valueSchemaEntries.filter(([, type]) => isDynamicAbiType(type)) as [string, DynamicAbiType][];\n // TODO: validate <=5 dynamic fields\n // TODO: validate <=28 total fields\n\n const encodedStaticValues = staticFields.map(([name, type]) => encodeField(type, value[name] as StaticPrimitiveType));\n const encodedDynamicValues = dynamicFields.map(([name, type]) =>\n encodeField(type, value[name] as DynamicPrimitiveType),\n );\n\n const encodedLengths = encodeLengths(encodedDynamicValues);\n\n return {\n staticData: concatHex(encodedStaticValues),\n encodedLengths,\n dynamicData: concatHex(encodedDynamicValues),\n };\n}\n","import { Hex, concatHex, padHex, size } from \"viem\";\nimport { encodeField } from \"./encodeField\";\n\nexport function encodeLengths(values: Hex[]): Hex {\n const byteLengths = values.map(size).reverse();\n const totalByteLength = byteLengths.reduce((total, length) => total + BigInt(length), 0n);\n\n return padHex(\n concatHex([...byteLengths.map((length) => encodeField(\"uint40\", length)), encodeField(\"uint56\", totalByteLength)]),\n { size: 32, dir: \"left\" },\n );\n}\n","import { Hex } from \"viem\";\nimport { FieldLayout } from \"./common\";\n\n/** @deprecated use `valueSchemaToFieldLayoutHex` instead */\nexport function fieldLayoutToHex(fieldLayout: FieldLayout): Hex {\n const staticDataLength = fieldLayout.staticFieldLengths.reduce((totalLength, length) => totalLength + length, 0);\n return `0x${[\n staticDataLength.toString(16).padStart(4, \"0\"),\n fieldLayout.staticFieldLengths.length.toString(16).padStart(2, \"0\"),\n fieldLayout.numDynamicFields.toString(16).padStart(2, \"0\"),\n ...fieldLayout.staticFieldLengths.map((schemaType) => schemaType.toString(16).padStart(2, \"0\")),\n ]\n .join(\"\")\n .padEnd(64, \"0\")}`;\n}\n","import {\n StaticAbiType,\n DynamicAbiType,\n schemaAbiTypes,\n staticAbiTypeToByteLength,\n} from \"@latticexyz/schema-type/internal\";\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 console.warn(\n `Schema \"${data}\" static data length (${staticDataLength}) did not match the summed length of all static fields (${actualStaticDataLength}). ` +\n `Is \\`staticAbiTypeToByteLength\\` up to date with Solidity schema types?`,\n );\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 { isStaticAbiType } from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { KeySchema } from \"./common\";\nimport { schemaToHex } from \"./schemaToHex\";\n\nexport function keySchemaToHex(keySchema: KeySchema): Hex {\n return schemaToHex({ staticFields: Object.values(keySchema).filter(isStaticAbiType), dynamicFields: [] });\n}\n","import { schemaAbiTypes } from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { Schema } from \"./common\";\nimport { staticDataLength } from \"./staticDataLength\";\n\n/** @deprecated use `keySchemaToHex` or `valueSchemaToHex` instead */\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","import { SchemaAbiType } from \"@latticexyz/schema-type/internal\";\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 { Hex } from \"viem\";\nimport { ValueSchema } from \"./common\";\nimport { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from \"@latticexyz/schema-type/internal\";\n\n// TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)\nexport function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {\n const staticFields = Object.values(valueSchema).filter(isStaticAbiType);\n const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);\n\n const staticFieldLengths = staticFields.map((fieldType) => staticAbiTypeToByteLength[fieldType]);\n const staticDataLength = staticFieldLengths.reduce((dataLength, fieldLength) => dataLength + fieldLength, 0);\n\n return `0x${[\n staticDataLength.toString(16).padStart(4, \"0\"),\n staticFields.length.toString(16).padStart(2, \"0\"),\n dynamicFields.length.toString(16).padStart(2, \"0\"),\n ...staticFieldLengths.map((fieldLength) => fieldLength.toString(16).padStart(2, \"0\")),\n ]\n .join(\"\")\n .padEnd(64, \"0\")}`;\n}\n","import { isDynamicAbiType, isStaticAbiType } from \"@latticexyz/schema-type/internal\";\nimport { Hex } from \"viem\";\nimport { ValueSchema } from \"./common\";\nimport { schemaToHex } from \"./schemaToHex\";\n\nexport function valueSchemaToHex(valueSchema: ValueSchema): Hex {\n return schemaToHex({\n staticFields: Object.values(valueSchema).filter(isStaticAbiType),\n dynamicFields: Object.values(valueSchema).filter(isDynamicAbiType),\n });\n}\n","import { Table } from \"@latticexyz/config\";\n\nexport type getKeySchema<table extends Table> = {\n [fieldName in table[\"key\"][number]]: table[\"schema\"][fieldName];\n};\n\nexport function getKeySchema<table extends Table>(table: table): getKeySchema<table> {\n return Object.fromEntries(table.key.map((fieldName) => [fieldName, table.schema[fieldName]])) as getKeySchema<table>;\n}\n","import { Table } from \"@latticexyz/config\";\n\nexport type getValueSchema<table extends Table> = {\n [fieldName in Exclude<keyof table[\"schema\"], table[\"key\"][number]>]: table[\"schema\"][fieldName];\n};\n\nexport function getValueSchema<table extends Table>(table: table): getValueSchema<table> {\n return Object.fromEntries(\n Object.entries(table.schema).filter(([fieldName]) => !table.key.includes(fieldName)),\n ) as getValueSchema<table>;\n}\n","import { Schema } from \"@latticexyz/config\";\nimport { mapObject } from \"@latticexyz/common/utils\";\n\nexport type getSchemaTypes<schema extends Schema> = {\n readonly [k in keyof schema]: schema[k][\"type\"];\n};\n\nexport function getSchemaTypes<schema extends Schema>(schema: schema): getSchemaTypes<schema> {\n return mapObject(schema, (value) => value.type);\n}\n"],"mappings":"AAAA,OAAuD,oBAAAA,MAAwB,mCAGxE,SAASC,GAAiBC,EAAmC,CAClE,IAAMC,EAAgC,CAAC,EACjCC,EAAkC,CAAC,EACzC,QAAWC,KAAWH,EAChBF,E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KG,GAAcN,GAA0BM,CAAS,CAAC,EAG/F,MAAO,KAAK,CAFaD,EAAmB,OAAO,CAACE,EAAYC,IAAgBD,EAAaC,EAAa,CAAC,EAGxF,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAC7CL,EAAa,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EAChDC,EAAc,OAAO,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,EACjD,GAAGC,EAAmB,IAAKG,GAAgBA,EAAY,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,CACtF,EACG,KAAK,EAAE,EACP,OAAO,GAAI,GAAG,GACnB,CCpBA,OAAS,oBAAAC,GAAkB,mBAAAC,OAAuB,mCAK3C,SAASC,GAAiBC,EAA+B,CAC9D,OAAOC,EAAY,CACjB,aAAc,OAAO,OAAOD,CAAW,EAAE,OAAOE,EAAe,EAC/D,cAAe,OAAO,OAAOF,CAAW,EAAE,OAAOG,EAAgB,CACnE,CAAC,CACH,CCJO,SAASC,GAAkCC,EAAmC,CACnF,OAAO,OAAO,YAAYA,EAAM,IAAI,IAAKC,GAAc,CAACA,EAAWD,EAAM,OAAOC,CAAS,CAAC,CAAC,CAAC,CAC9F,CCFO,SAASC,GAAoCC,EAAqC,CACvF,OAAO,OAAO,YACZ,OAAO,QAAQA,EAAM,MAAM,EAAE,OAAO,CAAC,CAACC,CAAS,IAAM,CAACD,EAAM,IAAI,SAASC,CAAS,CAAC,CACrF,CACF,CCTA,OAAS,aAAAC,OAAiB,2BAMnB,SAASC,GAAsCC,EAAwC,CAC5F,OAAOF,GAAUE,EAASC,GAAUA,EAAM,IAAI,CAChD","names":["isDynamicAbiType","abiTypesToSchema","abiTypes","staticFields","dynamicFields","abiType","hexToString","sliceHex","assertExhaustive","arrayToStaticAbiType","staticAbiTypeToByteLength","getAddress","hexToBigInt","hexToBool","assertExhaustive","staticAbiTypeToByteLength","staticAbiTypeToDefaultValue","MUDError","staticAbiTypeToByteLength","InvalidHexLengthError","value","InvalidHexLengthForSchemaError","InvalidHexLengthForEncodedLengthsError","InvalidHexLengthForStaticFieldError","abiType","InvalidHexLengthForArrayFieldError","SchemaStaticLengthMismatchError","schemaData","definedLength","summedLength","EncodedLengthsLengthMismatchError","encodedLengthsData","decodeStaticField","abiType","data","InvalidHexLengthError","staticAbiTypeToByteLength","InvalidHexLengthForStaticFieldError","value","hexToBigInt","defaultValueType","staticAbiTypeToDefaultValue","hexToBool","getAddress","assertExhaustive","decodeDynamicField","abiType","data","hexToString","InvalidHexLengthError","dataSize","staticAbiType","arrayToStaticAbiType","itemByteLength","staticAbiTypeToByteLength","InvalidHexLengthForArrayFieldError","_","i","itemData","sliceHex","decodeStaticField","assertExhaustive","isDynamicAbiType","decodeField","abiType","data","isDynamicAbiType","decodeDynamicField","decodeStaticField","decodeAbiParameters","decodeKeyTuple","keySchema","keyTuple","key","index","decodeKey","keySchema","data","keyValues","decodeKeyTuple","name","i","staticAbiTypeToByteLength","dynamicAbiTypeToDefaultValue","readHex","hexToEncodedLengths","data","InvalidHexLengthForEncodedLengthsError","totalByteLength","decodeStaticField","reversedFieldByteLengths","decodeDynamicField","fieldByteLengths","summedLength","total","length","EncodedLengthsLengthMismatchError","staticAbiTypeToByteLength","staticDataLength","staticFields","length","fieldType","readHex","decodeRecord","valueSchema","data","values","bytesOffset","fieldType","fieldByteLength","staticAbiTypeToByteLength","value","decodeStaticField","schemaStaticDataLength","staticDataLength","actualStaticDataLength","dataLayout","hexToEncodedLengths","i","dataLength","decodeDynamicField","dynamicAbiTypeToDefaultValue","actualDynamicDataLength","isStaticAbiType","isDynamicAbiType","decodeValue","valueSchema","data","staticFields","isStaticAbiType","dynamicFields","isDynamicAbiType","valueTuple","decodeRecord","name","i","concatHex","isStaticAbiType","readHex","decodeValueArgs","valueSchema","staticData","encodedLengths","dynamicData","decodeValue","concatHex","staticDataLength","isStaticAbiType","arrayToStaticAbiType","isArrayAbiType","encodePacked","encodeField","fieldType","value","staticFieldType","isStaticAbiType","encodeAbiParameters","encodeKeyTuple","keySchema","keyTuple","key","index","encodeKey","keySchema","key","staticFields","isStaticAbiType","encodeKeyTuple","encodeRecord","valueSchema","values","staticValues","dynamicValues","staticData","value","i","encodeField","dynamicDataItems","dynamicFieldByteLengths","dynamicTotalByteLength","total","length","dynamicData","encodedLengths","concatHex","isStaticAbiType","isDynamicAbiType","concatHex","concatHex","padHex","size","encodeLengths","values","byteLengths","size","totalByteLength","total","length","padHex","concatHex","encodeField","encodeValueArgs","valueSchema","value","valueSchemaEntries","staticFields","type","isStaticAbiType","dynamicFields","isDynamicAbiType","encodedStaticValues","name","encodeField","encodedDynamicValues","encodedLengths","encodeLengths","concatHex","encodeValue","valueSchema","value","staticData","encodedLengths","dynamicData","encodeValueArgs","concatHex","fieldLayoutToHex","fieldLayout","totalLength","length","schemaType","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","isStaticAbiType","schemaAbiTypes","schemaToHex","schema","staticSchemaTypes","abiType","schemaAbiTypes","dynamicSchemaTypes","staticDataLength","schemaType","keySchemaToHex","keySchema","schemaToHex","isStaticAbiType","schemaIndexToAbiType","schema","schemaIndex","isDynamicAbiType","isStaticAbiType","staticAbiTypeToByteLength","valueSchemaToFieldLayoutHex","valueSchema","staticFields","dynamicFields","staticFieldLengths","fieldType","dataLength","fieldLength","isDynamicAbiType","isStaticAbiType","valueSchemaToHex","valueSchema","schemaToHex","isStaticAbiType","isDynamicAbiType","getKeySchema","table","fieldName","getValueSchema","table","fieldName","mapObject","getSchemaTypes","schema","value"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latticexyz/protocol-parser",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Parser utilities for the MUD protocol",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,23 +10,33 @@
|
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"exports": {
|
|
13
|
-
".": "./dist/index.js"
|
|
13
|
+
".": "./dist/index.js",
|
|
14
|
+
"./internal": "./dist/internal.js"
|
|
15
|
+
},
|
|
16
|
+
"typesVersions": {
|
|
17
|
+
"*": {
|
|
18
|
+
"index": [
|
|
19
|
+
"./src/exports/index.ts"
|
|
20
|
+
],
|
|
21
|
+
"internal": [
|
|
22
|
+
"./src/exports/internal.ts"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
14
25
|
},
|
|
15
|
-
"types": "src/index.ts",
|
|
16
26
|
"dependencies": {
|
|
17
|
-
"abitype": "0.
|
|
18
|
-
"viem": "
|
|
19
|
-
"@latticexyz/common": "2.0.0
|
|
20
|
-
"@latticexyz/
|
|
27
|
+
"abitype": "1.0.0",
|
|
28
|
+
"viem": "2.7.12",
|
|
29
|
+
"@latticexyz/common": "2.0.0",
|
|
30
|
+
"@latticexyz/config": "2.0.0",
|
|
31
|
+
"@latticexyz/schema-type": "2.0.0"
|
|
21
32
|
},
|
|
22
33
|
"devDependencies": {
|
|
23
34
|
"tsup": "^6.7.0",
|
|
24
|
-
"vitest": "0.
|
|
35
|
+
"vitest": "0.34.6"
|
|
25
36
|
},
|
|
26
37
|
"publishConfig": {
|
|
27
38
|
"access": "public"
|
|
28
39
|
},
|
|
29
|
-
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f",
|
|
30
40
|
"scripts": {
|
|
31
41
|
"build": "pnpm run build:js",
|
|
32
42
|
"build:js": "tsup",
|
package/src/abiTypesToSchema.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type";
|
|
1
|
+
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
|
|
2
2
|
import { Schema } from "./common";
|
|
3
3
|
|
|
4
4
|
export function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema {
|
package/src/common.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DynamicAbiType,
|
|
3
|
+
SchemaAbiType,
|
|
4
|
+
SchemaAbiTypeToPrimitiveType,
|
|
5
|
+
StaticAbiType,
|
|
6
|
+
} from "@latticexyz/schema-type/internal";
|
|
2
7
|
import { Hex } from "viem";
|
|
3
8
|
|
|
4
9
|
/** @deprecated use `KeySchema` or `ValueSchema` instead */
|
|
@@ -18,8 +23,17 @@ export type FieldLayout = {
|
|
|
18
23
|
readonly numDynamicFields: number;
|
|
19
24
|
};
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
export type
|
|
26
|
+
// TODO: helper to filter user types to StaticAbiType
|
|
27
|
+
export type UserTypes = Record<string, { internalType: SchemaAbiType }>;
|
|
28
|
+
|
|
29
|
+
export type KeySchema<userTypes extends UserTypes | undefined = undefined> = Record<
|
|
30
|
+
string,
|
|
31
|
+
userTypes extends UserTypes ? StaticAbiType | keyof userTypes : StaticAbiType
|
|
32
|
+
>;
|
|
33
|
+
export type ValueSchema<userTypes extends UserTypes | undefined = undefined> = Record<
|
|
34
|
+
string,
|
|
35
|
+
userTypes extends UserTypes ? SchemaAbiType | keyof userTypes : SchemaAbiType
|
|
36
|
+
>;
|
|
23
37
|
|
|
24
38
|
/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */
|
|
25
39
|
export type SchemaToPrimitives<TSchema extends ValueSchema> = {
|