@latticexyz/protocol-parser 0.0.0-indexing-20230707130424
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/LICENSE +21 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
- package/src/abiTypesToSchema.test.ts +39 -0
- package/src/abiTypesToSchema.ts +12 -0
- package/src/common.ts +11 -0
- package/src/decodeDynamicField.test-d.ts +48 -0
- package/src/decodeDynamicField.test.ts +175 -0
- package/src/decodeDynamicField.ts +145 -0
- package/src/decodeField.ts +13 -0
- package/src/decodeKeyTuple.test.ts +37 -0
- package/src/decodeKeyTuple.ts +11 -0
- package/src/decodeRecord.test.ts +13 -0
- package/src/decodeRecord.ts +61 -0
- package/src/decodeStaticField.test-d.ts +40 -0
- package/src/decodeStaticField.test.ts +94 -0
- package/src/decodeStaticField.ts +145 -0
- package/src/encodeField.ts +17 -0
- package/src/encodeKeyTuple.test.ts +39 -0
- package/src/encodeKeyTuple.ts +7 -0
- package/src/encodeRecord.test.ts +18 -0
- package/src/encodeRecord.ts +30 -0
- package/src/errors.ts +64 -0
- package/src/hexToPackedCounter.test.ts +34 -0
- package/src/hexToPackedCounter.ts +31 -0
- package/src/hexToSchema.test.ts +51 -0
- package/src/hexToSchema.ts +33 -0
- package/src/hexToTableSchema.ts +12 -0
- package/src/index.ts +17 -0
- package/src/schemaIndexToAbiType.ts +9 -0
- package/src/schemaToHex.test.ts +16 -0
- package/src/schemaToHex.ts +18 -0
- package/src/staticDataLength.ts +5 -0
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
|
|
40
|
+
it("throws if schema hex data is not bytes32", () => {
|
|
41
|
+
expect(() => hexToSchema("0x002502045f2381c3c4c5")).toThrow(
|
|
42
|
+
'Hex value "0x002502045f2381c3c4c5" has length of 20, but expected length of 64 for a schema.'
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("throws if schema static field lengths do not match", () => {
|
|
47
|
+
expect(() => hexToSchema("0x002502045f2381c3c4c500000000000000000000000000000000000000000000")).toThrow(
|
|
48
|
+
'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?'
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { StaticAbiType, DynamicAbiType, schemaAbiTypes, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
|
|
2
|
+
import { Hex, hexToNumber, sliceHex } from "viem";
|
|
3
|
+
import { Schema } from "./common";
|
|
4
|
+
import { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from "./errors";
|
|
5
|
+
|
|
6
|
+
export function hexToSchema(data: Hex): Schema {
|
|
7
|
+
if (data.length !== 66) {
|
|
8
|
+
throw new InvalidHexLengthForSchemaError(data);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const staticDataLength = hexToNumber(sliceHex(data, 0, 2));
|
|
12
|
+
const numStaticFields = hexToNumber(sliceHex(data, 2, 3));
|
|
13
|
+
const numDynamicFields = hexToNumber(sliceHex(data, 3, 4));
|
|
14
|
+
const staticFields: StaticAbiType[] = [];
|
|
15
|
+
const dynamicFields: DynamicAbiType[] = [];
|
|
16
|
+
|
|
17
|
+
for (let i = 4; i < 4 + numStaticFields; i++) {
|
|
18
|
+
const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));
|
|
19
|
+
staticFields.push(schemaAbiTypes[schemaTypeIndex] as StaticAbiType);
|
|
20
|
+
}
|
|
21
|
+
for (let i = 4 + numStaticFields; i < 4 + numStaticFields + numDynamicFields; i++) {
|
|
22
|
+
const schemaTypeIndex = hexToNumber(sliceHex(data, i, i + 1));
|
|
23
|
+
dynamicFields.push(schemaAbiTypes[schemaTypeIndex] as DynamicAbiType);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// validate static data length
|
|
27
|
+
const actualStaticDataLength = staticFields.reduce((acc, fieldType) => acc + staticAbiTypeToByteLength[fieldType], 0);
|
|
28
|
+
if (actualStaticDataLength !== staticDataLength) {
|
|
29
|
+
throw new SchemaStaticLengthMismatchError(data, staticDataLength, actualStaticDataLength);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return { staticFields, dynamicFields };
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./abiTypesToSchema";
|
|
2
|
+
export * from "./common";
|
|
3
|
+
export * from "./decodeDynamicField";
|
|
4
|
+
export * from "./decodeField";
|
|
5
|
+
export * from "./decodeKeyTuple";
|
|
6
|
+
export * from "./decodeRecord";
|
|
7
|
+
export * from "./decodeStaticField";
|
|
8
|
+
export * from "./encodeField";
|
|
9
|
+
export * from "./encodeKeyTuple";
|
|
10
|
+
export * from "./encodeRecord";
|
|
11
|
+
export * from "./errors";
|
|
12
|
+
export * from "./hexToPackedCounter";
|
|
13
|
+
export * from "./hexToSchema";
|
|
14
|
+
export * from "./hexToTableSchema";
|
|
15
|
+
export * from "./schemaIndexToAbiType";
|
|
16
|
+
export * from "./schemaToHex";
|
|
17
|
+
export * from "./staticDataLength";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SchemaAbiType } from "@latticexyz/schema-type";
|
|
2
|
+
import { Schema } from "./common";
|
|
3
|
+
|
|
4
|
+
export function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType {
|
|
5
|
+
if (schemaIndex < schema.staticFields.length) {
|
|
6
|
+
return schema.staticFields[schemaIndex];
|
|
7
|
+
}
|
|
8
|
+
return schema.dynamicFields[schemaIndex - schema.staticFields.length];
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { schemaToHex } from "./schemaToHex";
|
|
3
|
+
|
|
4
|
+
describe("schemaToHex", () => {
|
|
5
|
+
it("converts schema to hex", () => {
|
|
6
|
+
expect(schemaToHex({ staticFields: ["bool"], dynamicFields: [] })).toBe(
|
|
7
|
+
"0x0001010060000000000000000000000000000000000000000000000000000000"
|
|
8
|
+
);
|
|
9
|
+
expect(schemaToHex({ staticFields: ["bool"], dynamicFields: ["bool[]"] })).toBe(
|
|
10
|
+
"0x0001010160c20000000000000000000000000000000000000000000000000000"
|
|
11
|
+
);
|
|
12
|
+
expect(
|
|
13
|
+
schemaToHex({ staticFields: ["bytes32", "int32"], dynamicFields: ["uint256[]", "address[]", "bytes", "string"] })
|
|
14
|
+
).toBe("0x002402045f2381c3c4c500000000000000000000000000000000000000000000");
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { schemaAbiTypes } from "@latticexyz/schema-type";
|
|
2
|
+
import { Hex } from "viem";
|
|
3
|
+
import { Schema } from "./common";
|
|
4
|
+
import { staticDataLength } from "./staticDataLength";
|
|
5
|
+
|
|
6
|
+
export function schemaToHex(schema: Schema): Hex {
|
|
7
|
+
const staticSchemaTypes = schema.staticFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
|
|
8
|
+
const dynamicSchemaTypes = schema.dynamicFields.map((abiType) => schemaAbiTypes.indexOf(abiType));
|
|
9
|
+
return `0x${[
|
|
10
|
+
staticDataLength(schema.staticFields).toString(16).padStart(4, "0"),
|
|
11
|
+
schema.staticFields.length.toString(16).padStart(2, "0"),
|
|
12
|
+
schema.dynamicFields.length.toString(16).padStart(2, "0"),
|
|
13
|
+
...staticSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
|
|
14
|
+
...dynamicSchemaTypes.map((schemaType) => schemaType.toString(16).padStart(2, "0")),
|
|
15
|
+
]
|
|
16
|
+
.join("")
|
|
17
|
+
.padEnd(64, "0")}`;
|
|
18
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
|
|
2
|
+
|
|
3
|
+
export function staticDataLength(staticFields: readonly StaticAbiType[]): number {
|
|
4
|
+
return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);
|
|
5
|
+
}
|