@latticexyz/protocol-parser 2.0.0-next.17 → 2.0.0-next.18
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 +1 -128
- 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 +18 -8
- package/src/abiTypesToSchema.ts +1 -1
- package/src/common.ts +6 -1
- 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 +2 -2
- 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.ts +3 -3
- package/src/errors.ts +11 -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 +2 -2
- package/src/hexToSchema.ts +7 -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 -35
- package/src/index.ts +0 -27
package/src/decodeValueArgs.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { concatHex } from "viem";
|
|
2
|
-
import { isStaticAbiType } from "@latticexyz/schema-type";
|
|
2
|
+
import { isStaticAbiType } from "@latticexyz/schema-type/internal";
|
|
3
3
|
import { SchemaToPrimitives, ValueArgs, ValueSchema } from "./common";
|
|
4
4
|
import { decodeValue } from "./decodeValue";
|
|
5
5
|
import { staticDataLength } from "./staticDataLength";
|
|
@@ -7,7 +7,7 @@ import { readHex } from "@latticexyz/common";
|
|
|
7
7
|
|
|
8
8
|
export function decodeValueArgs<TSchema extends ValueSchema>(
|
|
9
9
|
valueSchema: TSchema,
|
|
10
|
-
{ staticData, encodedLengths, dynamicData }: ValueArgs
|
|
10
|
+
{ staticData, encodedLengths, dynamicData }: ValueArgs,
|
|
11
11
|
): SchemaToPrimitives<TSchema> {
|
|
12
12
|
return decodeValue(
|
|
13
13
|
valueSchema,
|
|
@@ -15,6 +15,6 @@ export function decodeValueArgs<TSchema extends ValueSchema>(
|
|
|
15
15
|
readHex(staticData, 0, staticDataLength(Object.values(valueSchema).filter(isStaticAbiType))),
|
|
16
16
|
encodedLengths,
|
|
17
17
|
dynamicData,
|
|
18
|
-
])
|
|
18
|
+
]),
|
|
19
19
|
);
|
|
20
20
|
}
|
package/src/encodeField.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { SchemaAbiType,
|
|
1
|
+
import { SchemaAbiType, arrayToStaticAbiType, isArrayAbiType } from "@latticexyz/schema-type/internal";
|
|
2
2
|
import { AbiParameterToPrimitiveType } from "abitype";
|
|
3
3
|
import { Hex, encodePacked } from "viem";
|
|
4
4
|
|
|
5
5
|
export function encodeField<TSchemaAbiType extends SchemaAbiType>(
|
|
6
6
|
fieldType: TSchemaAbiType,
|
|
7
|
-
value: AbiParameterToPrimitiveType<{ type: TSchemaAbiType }
|
|
7
|
+
value: AbiParameterToPrimitiveType<{ type: TSchemaAbiType }>,
|
|
8
8
|
): Hex {
|
|
9
9
|
if (isArrayAbiType(fieldType) && Array.isArray(value)) {
|
|
10
|
-
const staticFieldType =
|
|
10
|
+
const staticFieldType = arrayToStaticAbiType(fieldType);
|
|
11
11
|
// TODO: we can remove conditional once this is fixed: https://github.com/wagmi-dev/viem/pull/1147
|
|
12
12
|
return value.length === 0
|
|
13
13
|
? "0x"
|
|
14
14
|
: encodePacked(
|
|
15
15
|
value.map(() => staticFieldType),
|
|
16
|
-
value
|
|
16
|
+
value,
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
19
|
return encodePacked([fieldType], [value]);
|
package/src/encodeKey.ts
CHANGED
|
@@ -12,8 +12,8 @@ describe("encodeKeyTuple", () => {
|
|
|
12
12
|
staticFields: ["bool"],
|
|
13
13
|
dynamicFields: [],
|
|
14
14
|
},
|
|
15
|
-
[true]
|
|
16
|
-
)
|
|
15
|
+
[true],
|
|
16
|
+
),
|
|
17
17
|
).toStrictEqual(["0x0000000000000000000000000000000000000000000000000000000000000001"]);
|
|
18
18
|
});
|
|
19
19
|
|
|
@@ -26,7 +26,7 @@ describe("encodeKeyTuple", () => {
|
|
|
26
26
|
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
|
|
27
27
|
true,
|
|
28
28
|
3,
|
|
29
|
-
])
|
|
29
|
+
]),
|
|
30
30
|
).toStrictEqual([
|
|
31
31
|
"0x000000000000000000000000000000000000000000000000000000000000002a",
|
|
32
32
|
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6",
|
package/src/encodeKeyTuple.ts
CHANGED
|
@@ -4,13 +4,13 @@ import { encodeLengths } from "./encodeLengths";
|
|
|
4
4
|
describe("encodeLengths", () => {
|
|
5
5
|
it("can encode empty tuple", () => {
|
|
6
6
|
expect(encodeLengths([])).toMatchInlineSnapshot(
|
|
7
|
-
'"0x0000000000000000000000000000000000000000000000000000000000000000"'
|
|
7
|
+
'"0x0000000000000000000000000000000000000000000000000000000000000000"',
|
|
8
8
|
);
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
it("can encode bool key tuple", () => {
|
|
12
12
|
expect(encodeLengths(["0x1234", "0x12345678"])).toMatchInlineSnapshot(
|
|
13
|
-
'"0x0000000000000000000000000000000000000004000000000200000000000006"'
|
|
13
|
+
'"0x0000000000000000000000000000000000000004000000000200000000000006"',
|
|
14
14
|
);
|
|
15
15
|
});
|
|
16
16
|
});
|
package/src/encodeLengths.ts
CHANGED
package/src/encodeRecord.test.ts
CHANGED
|
@@ -6,7 +6,7 @@ describe("encodeRecord", () => {
|
|
|
6
6
|
const valueSchema = { staticFields: ["uint32", "uint128"], dynamicFields: ["uint32[]", "string"] } as const;
|
|
7
7
|
const hex = encodeRecord(valueSchema, [1, 2n, [3, 4], "some string"]);
|
|
8
8
|
expect(hex).toBe(
|
|
9
|
-
"0x0000000100000000000000000000000000000002000000000000000000000000000000000000000b0000000008000000000000130000000300000004736f6d6520737472696e67"
|
|
9
|
+
"0x0000000100000000000000000000000000000002000000000000000000000000000000000000000b0000000008000000000000130000000300000004736f6d6520737472696e67",
|
|
10
10
|
);
|
|
11
11
|
});
|
|
12
12
|
|
package/src/encodeRecord.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type";
|
|
1
|
+
import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type/internal";
|
|
2
2
|
import { Hex } from "viem";
|
|
3
3
|
import { encodeField } from "./encodeField";
|
|
4
4
|
import { Schema } from "./common";
|
|
@@ -6,7 +6,7 @@ import { Schema } from "./common";
|
|
|
6
6
|
/** @deprecated use `encodeValue` instead */
|
|
7
7
|
export function encodeRecord(
|
|
8
8
|
valueSchema: Schema,
|
|
9
|
-
values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[]
|
|
9
|
+
values: readonly (StaticPrimitiveType | DynamicPrimitiveType)[],
|
|
10
10
|
): Hex {
|
|
11
11
|
const staticValues = values.slice(0, valueSchema.staticFields.length) as readonly StaticPrimitiveType[];
|
|
12
12
|
const dynamicValues = values.slice(valueSchema.staticFields.length) as readonly DynamicPrimitiveType[];
|
|
@@ -18,7 +18,7 @@ export function encodeRecord(
|
|
|
18
18
|
if (valueSchema.dynamicFields.length === 0) return `0x${staticData}`;
|
|
19
19
|
|
|
20
20
|
const dynamicDataItems = dynamicValues.map((value, i) =>
|
|
21
|
-
encodeField(valueSchema.dynamicFields[i], value).replace(/^0x/, "")
|
|
21
|
+
encodeField(valueSchema.dynamicFields[i], value).replace(/^0x/, ""),
|
|
22
22
|
);
|
|
23
23
|
|
|
24
24
|
const dynamicFieldByteLengths = dynamicDataItems.map((value) => value.length / 2).reverse();
|
|
@@ -26,9 +26,9 @@ export function encodeRecord(
|
|
|
26
26
|
|
|
27
27
|
const dynamicData = dynamicDataItems.join("");
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const encodedLengths = `${dynamicFieldByteLengths
|
|
30
30
|
.map((length) => encodeField("uint40", length).replace(/^0x/, ""))
|
|
31
31
|
.join("")}${encodeField("uint56", dynamicTotalByteLength).replace(/^0x/, "")}`.padStart(64, "0");
|
|
32
32
|
|
|
33
|
-
return `0x${staticData}${
|
|
33
|
+
return `0x${staticData}${encodedLengths}${dynamicData}`;
|
|
34
34
|
}
|
package/src/encodeValue.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { encodeValueArgs } from "./encodeValueArgs";
|
|
|
4
4
|
|
|
5
5
|
export function encodeValue<TSchema extends ValueSchema>(
|
|
6
6
|
valueSchema: TSchema,
|
|
7
|
-
value: SchemaToPrimitives<TSchema
|
|
7
|
+
value: SchemaToPrimitives<TSchema>,
|
|
8
8
|
): Hex {
|
|
9
9
|
const { staticData, encodedLengths, dynamicData } = encodeValueArgs(valueSchema, value);
|
|
10
10
|
return concatHex([staticData, encodedLengths, dynamicData]);
|
package/src/encodeValueArgs.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
isDynamicAbiType,
|
|
6
6
|
StaticAbiType,
|
|
7
7
|
DynamicAbiType,
|
|
8
|
-
} from "@latticexyz/schema-type";
|
|
8
|
+
} from "@latticexyz/schema-type/internal";
|
|
9
9
|
import { concatHex } from "viem";
|
|
10
10
|
import { encodeField } from "./encodeField";
|
|
11
11
|
import { SchemaToPrimitives, ValueArgs, ValueSchema } from "./common";
|
|
@@ -13,7 +13,7 @@ import { encodeLengths } from "./encodeLengths";
|
|
|
13
13
|
|
|
14
14
|
export function encodeValueArgs<TSchema extends ValueSchema>(
|
|
15
15
|
valueSchema: TSchema,
|
|
16
|
-
value: SchemaToPrimitives<TSchema
|
|
16
|
+
value: SchemaToPrimitives<TSchema>,
|
|
17
17
|
): ValueArgs {
|
|
18
18
|
const valueSchemaEntries = Object.entries(valueSchema);
|
|
19
19
|
const staticFields = valueSchemaEntries.filter(([, type]) => isStaticAbiType(type)) as [string, StaticAbiType][];
|
|
@@ -23,7 +23,7 @@ export function encodeValueArgs<TSchema extends ValueSchema>(
|
|
|
23
23
|
|
|
24
24
|
const encodedStaticValues = staticFields.map(([name, type]) => encodeField(type, value[name] as StaticPrimitiveType));
|
|
25
25
|
const encodedDynamicValues = dynamicFields.map(([name, type]) =>
|
|
26
|
-
encodeField(type, value[name] as DynamicPrimitiveType)
|
|
26
|
+
encodeField(type, value[name] as DynamicPrimitiveType),
|
|
27
27
|
);
|
|
28
28
|
|
|
29
29
|
const encodedLengths = encodeLengths(encodedDynamicValues);
|
package/src/errors.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hex } from "viem";
|
|
2
2
|
import { MUDError } from "@latticexyz/common/errors";
|
|
3
|
-
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
|
|
3
|
+
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
|
|
4
4
|
|
|
5
5
|
export class InvalidHexLengthError extends MUDError {
|
|
6
6
|
override name = "InvalidHexValueError";
|
|
@@ -16,10 +16,10 @@ export class InvalidHexLengthForSchemaError extends MUDError {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class
|
|
20
|
-
override name = "
|
|
19
|
+
export class InvalidHexLengthForEncodedLengthsError extends MUDError {
|
|
20
|
+
override name = "InvalidHexLengthForEncodedLengthsError";
|
|
21
21
|
constructor(value: Hex) {
|
|
22
|
-
super(`Hex value "${value}" has length of ${value.length - 2}, but expected length of 64 for
|
|
22
|
+
super(`Hex value "${value}" has length of ${value.length - 2}, but expected length of 64 for encoded lengths.`);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -29,7 +29,7 @@ export class InvalidHexLengthForStaticFieldError extends MUDError {
|
|
|
29
29
|
super(
|
|
30
30
|
`Hex value "${value}" has length of ${value.length - 2}, but expected length of ${
|
|
31
31
|
staticAbiTypeToByteLength[abiType] * 2
|
|
32
|
-
} for ${abiType} type
|
|
32
|
+
} for ${abiType} type.`,
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -40,7 +40,7 @@ export class InvalidHexLengthForArrayFieldError extends MUDError {
|
|
|
40
40
|
super(
|
|
41
41
|
`Hex value "${value}" has length of ${value.length - 2}, but expected a multiple of ${
|
|
42
42
|
staticAbiTypeToByteLength[abiType] * 2
|
|
43
|
-
} for ${abiType}[] type
|
|
43
|
+
} for ${abiType}[] type.`,
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -50,16 +50,16 @@ export class SchemaStaticLengthMismatchError extends MUDError {
|
|
|
50
50
|
constructor(schemaData: Hex, definedLength: number, summedLength: number) {
|
|
51
51
|
super(
|
|
52
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
|
|
53
|
+
`Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`,
|
|
54
54
|
);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export class
|
|
59
|
-
override name = "
|
|
60
|
-
constructor(
|
|
58
|
+
export class EncodedLengthsLengthMismatchError extends MUDError {
|
|
59
|
+
override name = "EncodedLengthsLengthMismatchError";
|
|
60
|
+
constructor(encodedLengthsData: Hex, definedLength: bigint, summedLength: bigint) {
|
|
61
61
|
super(
|
|
62
|
-
`
|
|
62
|
+
`EncodedLengths "${encodedLengthsData}" total bytes length (${definedLength}) did not match the summed length of all field byte lengths (${summedLength}).`,
|
|
63
63
|
);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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";
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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,22 +1,22 @@
|
|
|
1
1
|
import { Hex } from "viem";
|
|
2
2
|
import { decodeStaticField } from "./decodeStaticField";
|
|
3
3
|
import { decodeDynamicField } from "./decodeDynamicField";
|
|
4
|
-
import {
|
|
4
|
+
import { InvalidHexLengthForEncodedLengthsError, EncodedLengthsLengthMismatchError } from "./errors";
|
|
5
5
|
import { readHex } from "@latticexyz/common";
|
|
6
6
|
|
|
7
|
-
// Keep this logic in sync with
|
|
7
|
+
// Keep this logic in sync with EncodedLengths.sol
|
|
8
8
|
|
|
9
9
|
// - Last 7 bytes (uint56) are used for the total byte length of the dynamic data
|
|
10
10
|
// - The next 5 byte (uint40) sections are used for the byte length of each field, indexed from right to left
|
|
11
11
|
|
|
12
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
13
|
|
|
14
|
-
export function
|
|
14
|
+
export function hexToEncodedLengths(data: Hex): {
|
|
15
15
|
totalByteLength: bigint;
|
|
16
16
|
fieldByteLengths: readonly number[];
|
|
17
17
|
} {
|
|
18
18
|
if (data.length !== 66) {
|
|
19
|
-
throw new
|
|
19
|
+
throw new InvalidHexLengthForEncodedLengthsError(data);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const totalByteLength = decodeStaticField("uint56", readHex(data, 32 - 7, 32));
|
|
@@ -27,7 +27,7 @@ export function hexToPackedCounter(data: Hex): {
|
|
|
27
27
|
|
|
28
28
|
const summedLength = BigInt(fieldByteLengths.reduce((total, length) => total + length, 0));
|
|
29
29
|
if (summedLength !== totalByteLength) {
|
|
30
|
-
throw new
|
|
30
|
+
throw new EncodedLengthsLengthMismatchError(data, totalByteLength, summedLength);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
return { totalByteLength, fieldByteLengths };
|
package/src/hexToSchema.test.ts
CHANGED
|
@@ -55,14 +55,14 @@ describe("hexToSchema", () => {
|
|
|
55
55
|
|
|
56
56
|
it("throws if schema hex data is not bytes32", () => {
|
|
57
57
|
expect(() => hexToSchema("0x002502045f2381c3c4c5")).toThrow(
|
|
58
|
-
'Hex value "0x002502045f2381c3c4c5" has length of 20, but expected length of 64 for a schema.'
|
|
58
|
+
'Hex value "0x002502045f2381c3c4c5" has length of 20, but expected length of 64 for a schema.',
|
|
59
59
|
);
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
it("throws if schema static field lengths do not match", () => {
|
|
63
63
|
expect(() => hexToSchema("0x002502045f2381c3c4c500000000000000000000000000000000000000000000")).toThrow(
|
|
64
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?'
|
|
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
66
|
);
|
|
67
67
|
});
|
|
68
68
|
});
|
package/src/hexToSchema.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
StaticAbiType,
|
|
3
|
+
DynamicAbiType,
|
|
4
|
+
schemaAbiTypes,
|
|
5
|
+
staticAbiTypeToByteLength,
|
|
6
|
+
} from "@latticexyz/schema-type/internal";
|
|
2
7
|
import { Hex, hexToNumber, sliceHex } from "viem";
|
|
3
8
|
import { Schema } from "./common";
|
|
4
9
|
import { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from "./errors";
|
|
@@ -28,7 +33,7 @@ export function hexToSchema(data: Hex): Schema {
|
|
|
28
33
|
if (actualStaticDataLength !== staticDataLength) {
|
|
29
34
|
console.warn(
|
|
30
35
|
`Schema "${data}" static data length (${staticDataLength}) did not match the summed length of all static fields (${actualStaticDataLength}). ` +
|
|
31
|
-
`Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types
|
|
36
|
+
`Is \`staticAbiTypeToByteLength\` up to date with Solidity schema types?`,
|
|
32
37
|
);
|
|
33
38
|
throw new SchemaStaticLengthMismatchError(data, staticDataLength, actualStaticDataLength);
|
|
34
39
|
}
|
package/src/keySchemaToHex.ts
CHANGED
package/src/schemaToHex.test.ts
CHANGED
|
@@ -4,13 +4,13 @@ import { schemaToHex } from "./schemaToHex";
|
|
|
4
4
|
describe("schemaToHex", () => {
|
|
5
5
|
it("converts schema to hex", () => {
|
|
6
6
|
expect(schemaToHex({ staticFields: ["bool"], dynamicFields: [] })).toBe(
|
|
7
|
-
"0x0001010060000000000000000000000000000000000000000000000000000000"
|
|
7
|
+
"0x0001010060000000000000000000000000000000000000000000000000000000",
|
|
8
8
|
);
|
|
9
9
|
expect(schemaToHex({ staticFields: ["bool"], dynamicFields: ["bool[]"] })).toBe(
|
|
10
|
-
"0x0001010160c20000000000000000000000000000000000000000000000000000"
|
|
10
|
+
"0x0001010160c20000000000000000000000000000000000000000000000000000",
|
|
11
11
|
);
|
|
12
12
|
expect(
|
|
13
|
-
schemaToHex({ staticFields: ["bytes32", "int32"], dynamicFields: ["uint256[]", "address[]", "bytes", "string"] })
|
|
13
|
+
schemaToHex({ staticFields: ["bytes32", "int32"], dynamicFields: ["uint256[]", "address[]", "bytes", "string"] }),
|
|
14
14
|
).toBe("0x002402045f2381c3c4c500000000000000000000000000000000000000000000");
|
|
15
15
|
});
|
|
16
16
|
});
|
package/src/schemaToHex.ts
CHANGED
package/src/staticDataLength.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
|
|
1
|
+
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
|
|
2
2
|
|
|
3
3
|
export function staticDataLength(staticFields: readonly StaticAbiType[]): number {
|
|
4
4
|
return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hex } from "viem";
|
|
2
2
|
import { ValueSchema } from "./common";
|
|
3
|
-
import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
|
|
3
|
+
import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";
|
|
4
4
|
|
|
5
5
|
// TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)
|
|
6
6
|
export function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {
|
package/src/valueSchemaToHex.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type";
|
|
1
|
+
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type/internal";
|
|
2
2
|
import { Hex } from "viem";
|
|
3
3
|
import { ValueSchema } from "./common";
|
|
4
4
|
import { schemaToHex } from "./schemaToHex";
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { hexToPackedCounter } from "./hexToPackedCounter";
|
|
3
|
-
|
|
4
|
-
describe("hexToPackedCounter", () => {
|
|
5
|
-
it("decodes hex data to packed counter", () => {
|
|
6
|
-
expect(hexToPackedCounter("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(() => hexToPackedCounter("0x01234")).toThrowErrorMatchingInlineSnapshot(
|
|
23
|
-
'"Hex value \\"0x01234\\" has length of 5, but expected length of 64 for a packed counter."'
|
|
24
|
-
);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("throws if packed counter total byte length doesn't match summed byte length of fields", () => {
|
|
28
|
-
expect(() =>
|
|
29
|
-
hexToPackedCounter("0x0000000000000000000000000000400000000020000000002000000000000040")
|
|
30
|
-
).toThrowErrorMatchingInlineSnapshot(
|
|
31
|
-
// eslint-disable-next-line max-len
|
|
32
|
-
'"PackedCounter \\"0x0000000000000000000000000000400000000020000000002000000000000040\\" total bytes length (64) did not match the summed length of all field byte lengths (128)."'
|
|
33
|
-
);
|
|
34
|
-
});
|
|
35
|
-
});
|
package/src/index.ts
DELETED
|
@@ -1,27 +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 "./hexToPackedCounter";
|
|
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";
|