@latticexyz/protocol-parser 2.0.12-main-d7526607 → 2.0.12-main-96e7bf43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/package.json +7 -14
  2. package/src/abiTypesToSchema.test.ts +0 -39
  3. package/src/abiTypesToSchema.ts +0 -12
  4. package/src/common.ts +0 -52
  5. package/src/decodeDynamicField.test-d.ts +0 -48
  6. package/src/decodeDynamicField.test.ts +0 -175
  7. package/src/decodeDynamicField.ts +0 -145
  8. package/src/decodeField.ts +0 -13
  9. package/src/decodeKey.ts +0 -15
  10. package/src/decodeKeyTuple.test.ts +0 -37
  11. package/src/decodeKeyTuple.ts +0 -17
  12. package/src/decodeRecord.test.ts +0 -36
  13. package/src/decodeRecord.ts +0 -72
  14. package/src/decodeStaticField.test-d.ts +0 -40
  15. package/src/decodeStaticField.test.ts +0 -94
  16. package/src/decodeStaticField.ts +0 -145
  17. package/src/decodeValue.ts +0 -16
  18. package/src/decodeValueArgs.ts +0 -20
  19. package/src/encodeField.ts +0 -20
  20. package/src/encodeKey.ts +0 -13
  21. package/src/encodeKeyTuple.test.ts +0 -39
  22. package/src/encodeKeyTuple.ts +0 -8
  23. package/src/encodeLengths.test.ts +0 -16
  24. package/src/encodeLengths.ts +0 -12
  25. package/src/encodeRecord.test.ts +0 -24
  26. package/src/encodeRecord.ts +0 -34
  27. package/src/encodeValue.ts +0 -11
  28. package/src/encodeValueArgs.test.ts +0 -53
  29. package/src/encodeValueArgs.ts +0 -36
  30. package/src/errors.ts +0 -65
  31. package/src/exports/index.ts +0 -5
  32. package/src/exports/internal.ts +0 -31
  33. package/src/fieldLayoutToHex.ts +0 -15
  34. package/src/getKeySchema.ts +0 -9
  35. package/src/getSchemaTypes.ts +0 -10
  36. package/src/getValueSchema.ts +0 -11
  37. package/src/hexToEncodedLengths.test.ts +0 -35
  38. package/src/hexToEncodedLengths.ts +0 -34
  39. package/src/hexToSchema.test.ts +0 -68
  40. package/src/hexToSchema.ts +0 -42
  41. package/src/hexToTableSchema.ts +0 -12
  42. package/src/keySchemaToHex.ts +0 -8
  43. package/src/schemaIndexToAbiType.ts +0 -9
  44. package/src/schemaToHex.test.ts +0 -16
  45. package/src/schemaToHex.ts +0 -19
  46. package/src/staticDataLength.ts +0 -5
  47. package/src/valueSchemaToFieldLayoutHex.ts +0 -21
  48. package/src/valueSchemaToHex.ts +0 -11
package/src/errors.ts DELETED
@@ -1,65 +0,0 @@
1
- import { Hex } from "viem";
2
- import { MUDError } from "@latticexyz/common/errors";
3
- import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
4
-
5
- export class InvalidHexLengthError extends MUDError {
6
- override name = "InvalidHexValueError";
7
- constructor(value: Hex) {
8
- super(`Hex value "${value}" is an odd length (${value.length - 2}). It must be an even length.`);
9
- }
10
- }
11
-
12
- export class InvalidHexLengthForSchemaError extends MUDError {
13
- override name = "InvalidHexLengthForSchemaError";
14
- constructor(value: Hex) {
15
- super(`Hex value "${value}" has length of ${value.length - 2}, but expected length of 64 for a schema.`);
16
- }
17
- }
18
-
19
- export class InvalidHexLengthForEncodedLengthsError extends MUDError {
20
- override name = "InvalidHexLengthForEncodedLengthsError";
21
- constructor(value: Hex) {
22
- super(`Hex value "${value}" has length of ${value.length - 2}, but expected length of 64 for encoded lengths.`);
23
- }
24
- }
25
-
26
- export class InvalidHexLengthForStaticFieldError extends MUDError {
27
- override name = "InvalidHexLengthForStaticFieldError";
28
- constructor(abiType: StaticAbiType, value: Hex) {
29
- super(
30
- `Hex value "${value}" has length of ${value.length - 2}, but expected length of ${
31
- staticAbiTypeToByteLength[abiType] * 2
32
- } for ${abiType} type.`,
33
- );
34
- }
35
- }
36
-
37
- export class InvalidHexLengthForArrayFieldError extends MUDError {
38
- override name = "InvalidHexLengthForArrayFieldError";
39
- constructor(abiType: StaticAbiType, value: Hex) {
40
- super(
41
- `Hex value "${value}" has length of ${value.length - 2}, but expected a multiple of ${
42
- staticAbiTypeToByteLength[abiType] * 2
43
- } for ${abiType}[] type.`,
44
- );
45
- }
46
- }
47
-
48
- export class SchemaStaticLengthMismatchError extends MUDError {
49
- override name = "SchemaStaticLengthMismatchError";
50
- constructor(schemaData: Hex, definedLength: number, summedLength: number) {
51
- super(
52
- `Schema "${schemaData}" static data length (${definedLength}) did not match the summed length of all static fields (${summedLength}). ` +
53
- `Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`,
54
- );
55
- }
56
- }
57
-
58
- export class EncodedLengthsLengthMismatchError extends MUDError {
59
- override name = "EncodedLengthsLengthMismatchError";
60
- constructor(encodedLengthsData: Hex, definedLength: bigint, summedLength: bigint) {
61
- super(
62
- `EncodedLengths "${encodedLengthsData}" total bytes length (${definedLength}) did not match the summed length of all field byte lengths (${summedLength}).`,
63
- );
64
- }
65
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * External exports.
3
- *
4
- * Be sure we're ready to commit to these being supported and changes made backward compatible!
5
- */
@@ -1,31 +0,0 @@
1
- export * from "../abiTypesToSchema";
2
- export * from "../common";
3
- export * from "../decodeDynamicField";
4
- export * from "../decodeField";
5
- export * from "../decodeKey";
6
- export * from "../decodeKeyTuple";
7
- export * from "../decodeRecord";
8
- export * from "../decodeStaticField";
9
- export * from "../decodeValue";
10
- export * from "../decodeValueArgs";
11
- export * from "../encodeField";
12
- export * from "../encodeKey";
13
- export * from "../encodeKeyTuple";
14
- export * from "../encodeRecord";
15
- export * from "../encodeValue";
16
- export * from "../encodeValueArgs";
17
- export * from "../errors";
18
- export * from "../fieldLayoutToHex";
19
- export * from "../hexToEncodedLengths";
20
- export * from "../hexToSchema";
21
- export * from "../hexToTableSchema";
22
- export * from "../keySchemaToHex";
23
- export * from "../schemaIndexToAbiType";
24
- export * from "../schemaToHex";
25
- export * from "../staticDataLength";
26
- export * from "../valueSchemaToFieldLayoutHex";
27
- export * from "../valueSchemaToHex";
28
-
29
- export * from "../getKeySchema";
30
- export * from "../getValueSchema";
31
- export * from "../getSchemaTypes";
@@ -1,15 +0,0 @@
1
- import { Hex } from "viem";
2
- import { FieldLayout } from "./common";
3
-
4
- /** @deprecated use `valueSchemaToFieldLayoutHex` instead */
5
- export function fieldLayoutToHex(fieldLayout: FieldLayout): Hex {
6
- const staticDataLength = fieldLayout.staticFieldLengths.reduce((totalLength, length) => totalLength + length, 0);
7
- return `0x${[
8
- staticDataLength.toString(16).padStart(4, "0"),
9
- fieldLayout.staticFieldLengths.length.toString(16).padStart(2, "0"),
10
- fieldLayout.numDynamicFields.toString(16).padStart(2, "0"),
11
- ...fieldLayout.staticFieldLengths.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
12
- ]
13
- .join("")
14
- .padEnd(64, "0")}`;
15
- }
@@ -1,9 +0,0 @@
1
- import { Table } from "@latticexyz/config";
2
-
3
- export type getKeySchema<table extends Table> = {
4
- [fieldName in table["key"][number]]: table["schema"][fieldName];
5
- };
6
-
7
- export function getKeySchema<table extends Table>(table: table): getKeySchema<table> {
8
- return Object.fromEntries(table.key.map((fieldName) => [fieldName, table.schema[fieldName]])) as getKeySchema<table>;
9
- }
@@ -1,10 +0,0 @@
1
- import { Schema } from "@latticexyz/config";
2
- import { mapObject } from "@latticexyz/common/utils";
3
-
4
- export type getSchemaTypes<schema extends Schema> = {
5
- readonly [k in keyof schema]: schema[k]["type"];
6
- };
7
-
8
- export function getSchemaTypes<schema extends Schema>(schema: schema): getSchemaTypes<schema> {
9
- return mapObject(schema, (value) => value.type);
10
- }
@@ -1,11 +0,0 @@
1
- import { Table } from "@latticexyz/config";
2
-
3
- export type getValueSchema<table extends Table> = {
4
- [fieldName in Exclude<keyof table["schema"], table["key"][number]>]: table["schema"][fieldName];
5
- };
6
-
7
- export function getValueSchema<table extends Table>(table: table): getValueSchema<table> {
8
- return Object.fromEntries(
9
- Object.entries(table.schema).filter(([fieldName]) => !table.key.includes(fieldName)),
10
- ) as getValueSchema<table>;
11
- }
@@ -1,35 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { hexToEncodedLengths } from "./hexToEncodedLengths";
3
-
4
- describe("hexToEncodedLengths", () => {
5
- it("decodes hex data to encoded lengths", () => {
6
- expect(hexToEncodedLengths("0x0000000000000000000000000000400000000020000000002000000000000080"))
7
- .toMatchInlineSnapshot(`
8
- {
9
- "fieldByteLengths": [
10
- 32,
11
- 32,
12
- 64,
13
- 0,
14
- 0,
15
- ],
16
- "totalByteLength": 128n,
17
- }
18
- `);
19
- });
20
-
21
- it("throws if schema hex data is not bytes32", () => {
22
- expect(() => hexToEncodedLengths("0x01234")).toThrowErrorMatchingInlineSnapshot(
23
- '"Hex value \\"0x01234\\" has length of 5, but expected length of 64 for encoded lengths."',
24
- );
25
- });
26
-
27
- it("throws if encoded lengths total byte length doesn't match summed byte length of fields", () => {
28
- expect(() =>
29
- hexToEncodedLengths("0x0000000000000000000000000000400000000020000000002000000000000040"),
30
- ).toThrowErrorMatchingInlineSnapshot(
31
- // eslint-disable-next-line max-len
32
- '"EncodedLengths \\"0x0000000000000000000000000000400000000020000000002000000000000040\\" total bytes length (64) did not match the summed length of all field byte lengths (128)."',
33
- );
34
- });
35
- });
@@ -1,34 +0,0 @@
1
- import { Hex } from "viem";
2
- import { decodeStaticField } from "./decodeStaticField";
3
- import { decodeDynamicField } from "./decodeDynamicField";
4
- import { InvalidHexLengthForEncodedLengthsError, EncodedLengthsLengthMismatchError } from "./errors";
5
- import { readHex } from "@latticexyz/common";
6
-
7
- // Keep this logic in sync with EncodedLengths.sol
8
-
9
- // - Last 7 bytes (uint56) are used for the total byte length of the dynamic data
10
- // - The next 5 byte (uint40) sections are used for the byte length of each field, indexed from right to left
11
-
12
- // 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)
13
-
14
- export function hexToEncodedLengths(data: Hex): {
15
- totalByteLength: bigint;
16
- fieldByteLengths: readonly number[];
17
- } {
18
- if (data.length !== 66) {
19
- throw new InvalidHexLengthForEncodedLengthsError(data);
20
- }
21
-
22
- const totalByteLength = decodeStaticField("uint56", readHex(data, 32 - 7, 32));
23
- // TODO: use value schema to make sure we only parse as many as we need (rather than zeroes at the end)?
24
- const reversedFieldByteLengths = decodeDynamicField("uint40[]", readHex(data, 0, 32 - 7));
25
- // Reverse the lengths
26
- const fieldByteLengths = Object.freeze([...reversedFieldByteLengths].reverse());
27
-
28
- const summedLength = BigInt(fieldByteLengths.reduce((total, length) => total + length, 0));
29
- if (summedLength !== totalByteLength) {
30
- throw new EncodedLengthsLengthMismatchError(data, totalByteLength, summedLength);
31
- }
32
-
33
- return { totalByteLength, fieldByteLengths };
34
- }
@@ -1,68 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { hexToSchema } from "./hexToSchema";
3
-
4
- describe("hexToSchema", () => {
5
- it("converts hex to schema", () => {
6
- expect(hexToSchema("0x0001010060000000000000000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
7
- {
8
- "dynamicFields": [],
9
- "staticFields": [
10
- "bool",
11
- ],
12
- }
13
- `);
14
- expect(hexToSchema("0x0001010160c20000000000000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
15
- {
16
- "dynamicFields": [
17
- "bool[]",
18
- ],
19
- "staticFields": [
20
- "bool",
21
- ],
22
- }
23
- `);
24
- expect(hexToSchema("0x002402045f2381c3c4c500000000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
25
- {
26
- "dynamicFields": [
27
- "uint256[]",
28
- "address[]",
29
- "bytes",
30
- "string",
31
- ],
32
- "staticFields": [
33
- "bytes32",
34
- "int32",
35
- ],
36
- }
37
- `);
38
-
39
- expect(hexToSchema("0x00570800616100030700001f0000000000000000000000000000000000000000")).toMatchInlineSnapshot(`
40
- {
41
- "dynamicFields": [],
42
- "staticFields": [
43
- "address",
44
- "address",
45
- "uint8",
46
- "uint32",
47
- "uint64",
48
- "uint8",
49
- "uint8",
50
- "uint256",
51
- ],
52
- }
53
- `);
54
- });
55
-
56
- it("throws if schema hex data is not bytes32", () => {
57
- expect(() => hexToSchema("0x002502045f2381c3c4c5")).toThrow(
58
- 'Hex value "0x002502045f2381c3c4c5" has length of 20, but expected length of 64 for a schema.',
59
- );
60
- });
61
-
62
- it("throws if schema static field lengths do not match", () => {
63
- expect(() => hexToSchema("0x002502045f2381c3c4c500000000000000000000000000000000000000000000")).toThrow(
64
- // eslint-disable-next-line max-len
65
- 'Schema "0x002502045f2381c3c4c500000000000000000000000000000000000000000000" static data length (37) did not match the summed length of all static fields (36). Is `staticAbiTypeToByteLength` up to date with Solidity schema types?',
66
- );
67
- });
68
- });
@@ -1,42 +0,0 @@
1
- import {
2
- StaticAbiType,
3
- DynamicAbiType,
4
- schemaAbiTypes,
5
- staticAbiTypeToByteLength,
6
- } from "@latticexyz/schema-type/internal";
7
- import { Hex, hexToNumber, sliceHex } from "viem";
8
- import { Schema } from "./common";
9
- import { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from "./errors";
10
-
11
- export function hexToSchema(data: Hex): Schema {
12
- if (data.length !== 66) {
13
- throw new InvalidHexLengthForSchemaError(data);
14
- }
15
-
16
- const staticDataLength = hexToNumber(sliceHex(data, 0, 2));
17
- const numStaticFields = hexToNumber(sliceHex(data, 2, 3));
18
- const numDynamicFields = hexToNumber(sliceHex(data, 3, 4));
19
- const staticFields: StaticAbiType[] = [];
20
- const dynamicFields: DynamicAbiType[] = [];
21
-
22
- for (let i = 4; i < 4 + numStaticFields; i++) {
23
- const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));
24
- staticFields.push(schemaAbiTypes[schemaTypeIndex] as StaticAbiType);
25
- }
26
- for (let i = 4 + numStaticFields; i < 4 + numStaticFields + numDynamicFields; i++) {
27
- const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));
28
- dynamicFields.push(schemaAbiTypes[schemaTypeIndex] as DynamicAbiType);
29
- }
30
-
31
- // validate static data length
32
- const actualStaticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);
33
- if (actualStaticDataLength !== staticDataLength) {
34
- console.warn(
35
- `Schema "${data}" static data length (${staticDataLength}) did not match the summed length of all static fields (${actualStaticDataLength}). ` +
36
- `Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`,
37
- );
38
- throw new SchemaStaticLengthMismatchError(data, staticDataLength, actualStaticDataLength);
39
- }
40
-
41
- return { staticFields, dynamicFields };
42
- }
@@ -1,12 +0,0 @@
1
- import { Hex, sliceHex } from "viem";
2
- import { TableSchema } from "./common";
3
- import { hexToSchema } from "./hexToSchema";
4
-
5
- export function hexToTableSchema(data: Hex): TableSchema {
6
- const valueSchema = hexToSchema(sliceHex(data, 0, 32));
7
- const keySchema = hexToSchema(sliceHex(data, 32, 64));
8
- return {
9
- keySchema,
10
- valueSchema,
11
- };
12
- }
@@ -1,8 +0,0 @@
1
- import { isStaticAbiType } from "@latticexyz/schema-type/internal";
2
- import { Hex } from "viem";
3
- import { KeySchema } from "./common";
4
- import { schemaToHex } from "./schemaToHex";
5
-
6
- export function keySchemaToHex(keySchema: KeySchema): Hex {
7
- return schemaToHex({ staticFields: Object.values(keySchema).filter(isStaticAbiType), dynamicFields: [] });
8
- }
@@ -1,9 +0,0 @@
1
- import { SchemaAbiType } from "@latticexyz/schema-type/internal";
2
- import { Schema } from "./common";
3
-
4
- export function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType {
5
- if (schemaIndex < schema.staticFields.length) {
6
- return schema.staticFields[schemaIndex];
7
- }
8
- return schema.dynamicFields[schemaIndex - schema.staticFields.length];
9
- }
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { schemaToHex } from "./schemaToHex";
3
-
4
- describe("schemaToHex", () => {
5
- it("converts schema to hex", () => {
6
- expect(schemaToHex({ staticFields: ["bool"], dynamicFields: [] })).toBe(
7
- "0x0001010060000000000000000000000000000000000000000000000000000000",
8
- );
9
- expect(schemaToHex({ staticFields: ["bool"], dynamicFields: ["bool[]"] })).toBe(
10
- "0x0001010160c20000000000000000000000000000000000000000000000000000",
11
- );
12
- expect(
13
- schemaToHex({ staticFields: ["bytes32", "int32"], dynamicFields: ["uint256[]", "address[]", "bytes", "string"] }),
14
- ).toBe("0x002402045f2381c3c4c500000000000000000000000000000000000000000000");
15
- });
16
- });
@@ -1,19 +0,0 @@
1
- import { schemaAbiTypes } from "@latticexyz/schema-type/internal";
2
- import { Hex } from "viem";
3
- import { Schema } from "./common";
4
- import { staticDataLength } from "./staticDataLength";
5
-
6
- /** @deprecated use `keySchemaToHex` or `valueSchemaToHex` instead */
7
- export function schemaToHex(schema: Schema): Hex {
8
- const staticSchemaTypes = schema.staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
9
- const dynamicSchemaTypes = schema.dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
10
- return `0x${[
11
- staticDataLength(schema.staticFields).toString(16).padStart(4, "0"),
12
- schema.staticFields.length.toString(16).padStart(2, "0"),
13
- schema.dynamicFields.length.toString(16).padStart(2, "0"),
14
- ...staticSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
15
- ...dynamicSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
16
- ]
17
- .join("")
18
- .padEnd(64, "0")}`;
19
- }
@@ -1,5 +0,0 @@
1
- import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
2
-
3
- export function staticDataLength(staticFields: readonly StaticAbiType[]): number {
4
- return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);
5
- }
@@ -1,21 +0,0 @@
1
- import { Hex } from "viem";
2
- import { ValueSchema } from "./common";
3
- import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
4
-
5
- // TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)
6
- export function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {
7
- const staticFields = Object.values(valueSchema).filter(isStaticAbiType);
8
- const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);
9
-
10
- const staticFieldLengths = staticFields.map((fieldType) => staticAbiTypeToByteLength[fieldType]);
11
- const staticDataLength = staticFieldLengths.reduce((dataLength, fieldLength) => dataLength + fieldLength, 0);
12
-
13
- return `0x${[
14
- staticDataLength.toString(16).padStart(4, "0"),
15
- staticFields.length.toString(16).padStart(2, "0"),
16
- dynamicFields.length.toString(16).padStart(2, "0"),
17
- ...staticFieldLengths.map((fieldLength) => fieldLength.toString(16).padStart(2, "0")),
18
- ]
19
- .join("")
20
- .padEnd(64, "0")}`;
21
- }
@@ -1,11 +0,0 @@
1
- import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type/internal";
2
- import { Hex } from "viem";
3
- import { ValueSchema } from "./common";
4
- import { schemaToHex } from "./schemaToHex";
5
-
6
- export function valueSchemaToHex(valueSchema: ValueSchema): Hex {
7
- return schemaToHex({
8
- staticFields: Object.values(valueSchema).filter(isStaticAbiType),
9
- dynamicFields: Object.values(valueSchema).filter(isDynamicAbiType),
10
- });
11
- }