@latticexyz/protocol-parser 0.0.0-main-20230705225731
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/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/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 +11 -0
- package/src/schemaToHex.test.ts +16 -0
- package/src/schemaToHex.ts +18 -0
- package/src/staticDataLength.ts +5 -0
|
@@ -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,11 @@
|
|
|
1
|
+
export * from "./common";
|
|
2
|
+
export * from "./decodeDynamicField";
|
|
3
|
+
export * from "./decodeKeyTuple";
|
|
4
|
+
export * from "./decodeRecord";
|
|
5
|
+
export * from "./decodeStaticField";
|
|
6
|
+
export * from "./encodeField";
|
|
7
|
+
export * from "./encodeKeyTuple";
|
|
8
|
+
export * from "./encodeRecord";
|
|
9
|
+
export * from "./errors";
|
|
10
|
+
export * from "./hexToPackedCounter";
|
|
11
|
+
export * from "./hexToTableSchema";
|
|
@@ -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
|
+
}
|