@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.
- package/package.json +7 -14
- package/src/abiTypesToSchema.test.ts +0 -39
- package/src/abiTypesToSchema.ts +0 -12
- package/src/common.ts +0 -52
- package/src/decodeDynamicField.test-d.ts +0 -48
- package/src/decodeDynamicField.test.ts +0 -175
- package/src/decodeDynamicField.ts +0 -145
- package/src/decodeField.ts +0 -13
- package/src/decodeKey.ts +0 -15
- package/src/decodeKeyTuple.test.ts +0 -37
- package/src/decodeKeyTuple.ts +0 -17
- package/src/decodeRecord.test.ts +0 -36
- package/src/decodeRecord.ts +0 -72
- package/src/decodeStaticField.test-d.ts +0 -40
- package/src/decodeStaticField.test.ts +0 -94
- package/src/decodeStaticField.ts +0 -145
- package/src/decodeValue.ts +0 -16
- package/src/decodeValueArgs.ts +0 -20
- package/src/encodeField.ts +0 -20
- package/src/encodeKey.ts +0 -13
- package/src/encodeKeyTuple.test.ts +0 -39
- package/src/encodeKeyTuple.ts +0 -8
- package/src/encodeLengths.test.ts +0 -16
- package/src/encodeLengths.ts +0 -12
- package/src/encodeRecord.test.ts +0 -24
- package/src/encodeRecord.ts +0 -34
- package/src/encodeValue.ts +0 -11
- package/src/encodeValueArgs.test.ts +0 -53
- package/src/encodeValueArgs.ts +0 -36
- package/src/errors.ts +0 -65
- package/src/exports/index.ts +0 -5
- package/src/exports/internal.ts +0 -31
- package/src/fieldLayoutToHex.ts +0 -15
- package/src/getKeySchema.ts +0 -9
- package/src/getSchemaTypes.ts +0 -10
- package/src/getValueSchema.ts +0 -11
- package/src/hexToEncodedLengths.test.ts +0 -35
- package/src/hexToEncodedLengths.ts +0 -34
- package/src/hexToSchema.test.ts +0 -68
- package/src/hexToSchema.ts +0 -42
- package/src/hexToTableSchema.ts +0 -12
- package/src/keySchemaToHex.ts +0 -8
- package/src/schemaIndexToAbiType.ts +0 -9
- package/src/schemaToHex.test.ts +0 -16
- package/src/schemaToHex.ts +0 -19
- package/src/staticDataLength.ts +0 -5
- package/src/valueSchemaToFieldLayoutHex.ts +0 -21
- package/src/valueSchemaToHex.ts +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latticexyz/protocol-parser",
|
|
3
|
-
"version": "2.0.12-main-
|
|
3
|
+
"version": "2.0.12-main-96e7bf43",
|
|
4
4
|
"description": "Parser utilities for the MUD protocol",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,22 +13,15 @@
|
|
|
13
13
|
".": "./dist/index.js",
|
|
14
14
|
"./internal": "./dist/internal.js"
|
|
15
15
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"./src/exports/index.ts"
|
|
20
|
-
],
|
|
21
|
-
"internal": [
|
|
22
|
-
"./src/exports/internal.ts"
|
|
23
|
-
]
|
|
24
|
-
}
|
|
25
|
-
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
26
19
|
"dependencies": {
|
|
27
20
|
"abitype": "1.0.0",
|
|
28
21
|
"viem": "2.9.20",
|
|
29
|
-
"@latticexyz/common": "2.0.12-main-
|
|
30
|
-
"@latticexyz/config": "2.0.12-main-
|
|
31
|
-
"@latticexyz/schema-type": "2.0.12-main-
|
|
22
|
+
"@latticexyz/common": "2.0.12-main-96e7bf43",
|
|
23
|
+
"@latticexyz/config": "2.0.12-main-96e7bf43",
|
|
24
|
+
"@latticexyz/schema-type": "2.0.12-main-96e7bf43"
|
|
32
25
|
},
|
|
33
26
|
"devDependencies": {
|
|
34
27
|
"tsup": "^6.7.0",
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { abiTypesToSchema } from "./abiTypesToSchema";
|
|
3
|
-
|
|
4
|
-
describe("hexToSchema", () => {
|
|
5
|
-
it("converts hex to schema", () => {
|
|
6
|
-
expect(abiTypesToSchema(["bool"])).toMatchInlineSnapshot(`
|
|
7
|
-
{
|
|
8
|
-
"dynamicFields": [],
|
|
9
|
-
"staticFields": [
|
|
10
|
-
"bool",
|
|
11
|
-
],
|
|
12
|
-
}
|
|
13
|
-
`);
|
|
14
|
-
expect(abiTypesToSchema(["bool", "bool[]"])).toMatchInlineSnapshot(`
|
|
15
|
-
{
|
|
16
|
-
"dynamicFields": [
|
|
17
|
-
"bool[]",
|
|
18
|
-
],
|
|
19
|
-
"staticFields": [
|
|
20
|
-
"bool",
|
|
21
|
-
],
|
|
22
|
-
}
|
|
23
|
-
`);
|
|
24
|
-
expect(abiTypesToSchema(["bytes32", "int32", "uint256[]", "address[]", "bytes", "string"])).toMatchInlineSnapshot(`
|
|
25
|
-
{
|
|
26
|
-
"dynamicFields": [
|
|
27
|
-
"uint256[]",
|
|
28
|
-
"address[]",
|
|
29
|
-
"bytes",
|
|
30
|
-
"string",
|
|
31
|
-
],
|
|
32
|
-
"staticFields": [
|
|
33
|
-
"bytes32",
|
|
34
|
-
"int32",
|
|
35
|
-
],
|
|
36
|
-
}
|
|
37
|
-
`);
|
|
38
|
-
});
|
|
39
|
-
});
|
package/src/abiTypesToSchema.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
|
|
2
|
-
import { Schema } from "./common";
|
|
3
|
-
|
|
4
|
-
export function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema {
|
|
5
|
-
const staticFields: StaticAbiType[] = [];
|
|
6
|
-
const dynamicFields: DynamicAbiType[] = [];
|
|
7
|
-
for (const abiType of abiTypes) {
|
|
8
|
-
if (isDynamicAbiType(abiType)) dynamicFields.push(abiType);
|
|
9
|
-
else staticFields.push(abiType);
|
|
10
|
-
}
|
|
11
|
-
return { staticFields, dynamicFields };
|
|
12
|
-
}
|
package/src/common.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DynamicAbiType,
|
|
3
|
-
SchemaAbiType,
|
|
4
|
-
SchemaAbiTypeToPrimitiveType,
|
|
5
|
-
StaticAbiType,
|
|
6
|
-
} from "@latticexyz/schema-type/internal";
|
|
7
|
-
import { Hex } from "viem";
|
|
8
|
-
|
|
9
|
-
/** @deprecated use `KeySchema` or `ValueSchema` instead */
|
|
10
|
-
export type Schema = {
|
|
11
|
-
readonly staticFields: readonly StaticAbiType[];
|
|
12
|
-
readonly dynamicFields: readonly DynamicAbiType[];
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/** @deprecated use `KeySchema` and `ValueSchema` instead */
|
|
16
|
-
export type TableSchema = {
|
|
17
|
-
readonly keySchema: Schema; // TODO: refine to set dynamicFields to []
|
|
18
|
-
readonly valueSchema: Schema;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type FieldLayout = {
|
|
22
|
-
readonly staticFieldLengths: readonly number[];
|
|
23
|
-
readonly numDynamicFields: number;
|
|
24
|
-
};
|
|
25
|
-
|
|
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
|
-
>;
|
|
37
|
-
|
|
38
|
-
/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */
|
|
39
|
-
export type SchemaToPrimitives<TSchema extends ValueSchema> = {
|
|
40
|
-
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = {
|
|
44
|
-
key: SchemaToPrimitives<TKeySchema>;
|
|
45
|
-
value: SchemaToPrimitives<TValueSchema>;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export type ValueArgs = {
|
|
49
|
-
staticData: Hex;
|
|
50
|
-
encodedLengths: Hex;
|
|
51
|
-
dynamicData: Hex;
|
|
52
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { assertType, describe, it } from "vitest";
|
|
2
|
-
import { Hex } from "viem";
|
|
3
|
-
import { decodeDynamicField } from "./decodeDynamicField";
|
|
4
|
-
|
|
5
|
-
describe("decodeDynamicField", () => {
|
|
6
|
-
it("returns a boolean array for bool[] ABI type", () => {
|
|
7
|
-
assertType<boolean[]>(decodeDynamicField("bool[]", "0x"));
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("returns a number array or bigint array for uint[] ABI types", () => {
|
|
11
|
-
assertType<number[]>(decodeDynamicField("uint8[]", "0x"));
|
|
12
|
-
assertType<number[]>(decodeDynamicField("uint16[]", "0x"));
|
|
13
|
-
assertType<number[]>(decodeDynamicField("uint32[]", "0x"));
|
|
14
|
-
assertType<number[]>(decodeDynamicField("uint48[]", "0x"));
|
|
15
|
-
assertType<bigint[]>(decodeDynamicField("uint56[]", "0x"));
|
|
16
|
-
assertType<bigint[]>(decodeDynamicField("uint128[]", "0x"));
|
|
17
|
-
assertType<bigint[]>(decodeDynamicField("uint256[]", "0x"));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("returns a number array or bigint array for int[] ABI types", () => {
|
|
21
|
-
assertType<number[]>(decodeDynamicField("int8[]", "0x"));
|
|
22
|
-
assertType<number[]>(decodeDynamicField("int16[]", "0x"));
|
|
23
|
-
assertType<number[]>(decodeDynamicField("int32[]", "0x"));
|
|
24
|
-
assertType<number[]>(decodeDynamicField("int48[]", "0x"));
|
|
25
|
-
assertType<bigint[]>(decodeDynamicField("int56[]", "0x"));
|
|
26
|
-
assertType<bigint[]>(decodeDynamicField("int128[]", "0x"));
|
|
27
|
-
assertType<bigint[]>(decodeDynamicField("int256[]", "0x"));
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("returns a hex array for bytes[] ABI types", () => {
|
|
31
|
-
assertType<Hex[]>(decodeDynamicField("bytes1[]", "0x"));
|
|
32
|
-
assertType<Hex[]>(decodeDynamicField("bytes2[]", "0x"));
|
|
33
|
-
assertType<Hex[]>(decodeDynamicField("bytes8[]", "0x"));
|
|
34
|
-
assertType<Hex[]>(decodeDynamicField("bytes32[]", "0x"));
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("returns a hex array for address ABI type", () => {
|
|
38
|
-
assertType<Hex[]>(decodeDynamicField("address[]", "0x"));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("returns a hex for bytes ABI type", () => {
|
|
42
|
-
assertType<Hex>(decodeDynamicField("bytes", "0x"));
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("returns a string for string ABI type", () => {
|
|
46
|
-
assertType<string>(decodeDynamicField("string", "0x"));
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { decodeDynamicField } from "./decodeDynamicField";
|
|
3
|
-
|
|
4
|
-
describe("decodeDynamicField", () => {
|
|
5
|
-
it("can decode bool[]", () => {
|
|
6
|
-
expect(decodeDynamicField("bool[]", "0x00")).toStrictEqual([false]);
|
|
7
|
-
expect(decodeDynamicField("bool[]", "0x01")).toStrictEqual([true]);
|
|
8
|
-
expect(decodeDynamicField("bool[]", "0x0000")).toStrictEqual([false, false]);
|
|
9
|
-
expect(decodeDynamicField("bool[]", "0x0001")).toStrictEqual([false, true]);
|
|
10
|
-
expect(decodeDynamicField("bool[]", "0x0100")).toStrictEqual([true, false]);
|
|
11
|
-
expect(decodeDynamicField("bool[]", "0x0101")).toStrictEqual([true, true]);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("can decode uint8[]", () => {
|
|
15
|
-
expect(decodeDynamicField("uint8[]", "0x00")).toStrictEqual([0]);
|
|
16
|
-
expect(decodeDynamicField("uint8[]", "0x01")).toStrictEqual([1]);
|
|
17
|
-
expect(decodeDynamicField("uint8[]", "0xff")).toStrictEqual([255]);
|
|
18
|
-
expect(decodeDynamicField("uint8[]", "0x0000")).toStrictEqual([0, 0]);
|
|
19
|
-
expect(decodeDynamicField("uint8[]", "0x0101")).toStrictEqual([1, 1]);
|
|
20
|
-
expect(decodeDynamicField("uint8[]", "0xffff")).toStrictEqual([255, 255]);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("can decode uint256[]", () => {
|
|
24
|
-
expect(
|
|
25
|
-
decodeDynamicField("uint256[]", "0x0000000000000000000000000000000000000000000000000000000000000000"),
|
|
26
|
-
).toStrictEqual([0n]);
|
|
27
|
-
expect(
|
|
28
|
-
decodeDynamicField("uint256[]", "0x0000000000000000000000000000000000000000000000000000000000000001"),
|
|
29
|
-
).toStrictEqual([1n]);
|
|
30
|
-
expect(
|
|
31
|
-
decodeDynamicField("uint256[]", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
|
32
|
-
).toStrictEqual([115792089237316195423570985008687907853269984665640564039457584007913129639935n]);
|
|
33
|
-
expect(
|
|
34
|
-
decodeDynamicField("uint256[]", "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"),
|
|
35
|
-
).toStrictEqual([115792089237316195423570985008687907853269984665640564039457584007913129639934n]);
|
|
36
|
-
expect(
|
|
37
|
-
decodeDynamicField(
|
|
38
|
-
"uint256[]",
|
|
39
|
-
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
|
|
40
|
-
),
|
|
41
|
-
).toStrictEqual([1n, 1n]);
|
|
42
|
-
expect(
|
|
43
|
-
decodeDynamicField(
|
|
44
|
-
"uint256[]",
|
|
45
|
-
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
46
|
-
),
|
|
47
|
-
).toStrictEqual([
|
|
48
|
-
115792089237316195423570985008687907853269984665640564039457584007913129639935n,
|
|
49
|
-
115792089237316195423570985008687907853269984665640564039457584007913129639935n,
|
|
50
|
-
]);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("can decode int8[]", () => {
|
|
54
|
-
expect(decodeDynamicField("int8[]", "0x00")).toStrictEqual([0]);
|
|
55
|
-
expect(decodeDynamicField("int8[]", "0x01")).toStrictEqual([1]);
|
|
56
|
-
expect(decodeDynamicField("int8[]", "0x7f")).toStrictEqual([127]);
|
|
57
|
-
expect(decodeDynamicField("int8[]", "0x80")).toStrictEqual([-128]);
|
|
58
|
-
expect(decodeDynamicField("int8[]", "0x81")).toStrictEqual([-127]);
|
|
59
|
-
expect(decodeDynamicField("int8[]", "0xff")).toStrictEqual([-1]);
|
|
60
|
-
|
|
61
|
-
expect(decodeDynamicField("int8[]", "0x0000")).toStrictEqual([0, 0]);
|
|
62
|
-
expect(decodeDynamicField("int8[]", "0x0100")).toStrictEqual([1, 0]);
|
|
63
|
-
expect(decodeDynamicField("int8[]", "0x007f")).toStrictEqual([0, 127]);
|
|
64
|
-
expect(decodeDynamicField("int8[]", "0x8080")).toStrictEqual([-128, -128]);
|
|
65
|
-
expect(decodeDynamicField("int8[]", "0x8181")).toStrictEqual([-127, -127]);
|
|
66
|
-
expect(decodeDynamicField("int8[]", "0x00ff")).toStrictEqual([0, -1]);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("can decode int256[]", () => {
|
|
70
|
-
expect(
|
|
71
|
-
decodeDynamicField("int256[]", "0x0000000000000000000000000000000000000000000000000000000000000000"),
|
|
72
|
-
).toStrictEqual([0n]);
|
|
73
|
-
expect(
|
|
74
|
-
decodeDynamicField("int256[]", "0x0000000000000000000000000000000000000000000000000000000000000001"),
|
|
75
|
-
).toStrictEqual([1n]);
|
|
76
|
-
expect(
|
|
77
|
-
decodeDynamicField("int256[]", "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
|
78
|
-
).toStrictEqual([57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
79
|
-
expect(
|
|
80
|
-
decodeDynamicField("int256[]", "0x8000000000000000000000000000000000000000000000000000000000000000"),
|
|
81
|
-
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819968n]);
|
|
82
|
-
expect(
|
|
83
|
-
decodeDynamicField("int256[]", "0x8000000000000000000000000000000000000000000000000000000000000001"),
|
|
84
|
-
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
85
|
-
expect(
|
|
86
|
-
decodeDynamicField("int256[]", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
|
87
|
-
).toStrictEqual([-1n]);
|
|
88
|
-
|
|
89
|
-
expect(
|
|
90
|
-
decodeDynamicField(
|
|
91
|
-
"int256[]",
|
|
92
|
-
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
93
|
-
),
|
|
94
|
-
).toStrictEqual([57896044618658097711785492504343953926634992332820282019728792003956564819967n, -1n]);
|
|
95
|
-
expect(
|
|
96
|
-
decodeDynamicField(
|
|
97
|
-
"int256[]",
|
|
98
|
-
"0x80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
|
99
|
-
),
|
|
100
|
-
).toStrictEqual([-57896044618658097711785492504343953926634992332820282019728792003956564819968n, 0n]);
|
|
101
|
-
expect(
|
|
102
|
-
decodeDynamicField(
|
|
103
|
-
"int256[]",
|
|
104
|
-
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000001",
|
|
105
|
-
),
|
|
106
|
-
).toStrictEqual([-1n, -57896044618658097711785492504343953926634992332820282019728792003956564819967n]);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("can decode bytes arrays", () => {
|
|
110
|
-
expect(decodeDynamicField("bytes1[]", "0x01")).toStrictEqual(["0x01"]);
|
|
111
|
-
expect(decodeDynamicField("bytes1[]", "0x0001")).toStrictEqual(["0x00", "0x01"]);
|
|
112
|
-
expect(decodeDynamicField("bytes2[]", "0x0001")).toStrictEqual(["0x0001"]);
|
|
113
|
-
expect(decodeDynamicField("bytes8[]", "0xff00ff00ff00ff00")).toStrictEqual(["0xff00ff00ff00ff00"]);
|
|
114
|
-
expect(decodeDynamicField("bytes1[]", "0xff00ff00ff00ff00")).toStrictEqual([
|
|
115
|
-
"0xff",
|
|
116
|
-
"0x00",
|
|
117
|
-
"0xff",
|
|
118
|
-
"0x00",
|
|
119
|
-
"0xff",
|
|
120
|
-
"0x00",
|
|
121
|
-
"0xff",
|
|
122
|
-
"0x00",
|
|
123
|
-
]);
|
|
124
|
-
expect(decodeDynamicField("bytes2[]", "0xff00ff00ff00ff00")).toStrictEqual([
|
|
125
|
-
"0xff00",
|
|
126
|
-
"0xff00",
|
|
127
|
-
"0xff00",
|
|
128
|
-
"0xff00",
|
|
129
|
-
]);
|
|
130
|
-
expect(decodeDynamicField("bytes4[]", "0xff00ff00ff00ff00")).toStrictEqual(["0xff00ff00", "0xff00ff00"]);
|
|
131
|
-
expect(
|
|
132
|
-
decodeDynamicField("bytes32[]", "0x0000000000000000000000000000000000000000000000000000000000000001"),
|
|
133
|
-
).toStrictEqual(["0x0000000000000000000000000000000000000000000000000000000000000001"]);
|
|
134
|
-
expect(
|
|
135
|
-
decodeDynamicField(
|
|
136
|
-
"bytes32[]",
|
|
137
|
-
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
|
138
|
-
),
|
|
139
|
-
).toStrictEqual([
|
|
140
|
-
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
141
|
-
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
142
|
-
]);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it("can decode address[]", () => {
|
|
146
|
-
expect(decodeDynamicField("address[]", "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")).toStrictEqual([
|
|
147
|
-
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
|
|
148
|
-
]);
|
|
149
|
-
expect(decodeDynamicField("address[]", "0xffffffffffffffffffffffffffffffffffffffff")).toStrictEqual([
|
|
150
|
-
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
|
|
151
|
-
]);
|
|
152
|
-
|
|
153
|
-
expect(() => decodeDynamicField("address[]", "0x00")).toThrow(
|
|
154
|
-
'Hex value "0x00" has length of 2, but expected a multiple of 40 for address[] type.',
|
|
155
|
-
);
|
|
156
|
-
expect(() => decodeDynamicField("address[]", "0xffffffffffffffffffffffffffffffffffffffffff")).toThrow(
|
|
157
|
-
'Hex value "0xffffffffffffffffffffffffffffffffffffffffff" has length of 42, but expected a multiple of 40 for address[] type.',
|
|
158
|
-
);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it("can decode bytes", () => {
|
|
162
|
-
expect(decodeDynamicField("bytes", "0x")).toBe("0x");
|
|
163
|
-
expect(decodeDynamicField("bytes", "0x01")).toBe("0x01");
|
|
164
|
-
expect(decodeDynamicField("bytes", "0x0001")).toBe("0x0001");
|
|
165
|
-
expect(decodeDynamicField("bytes", "0xff00ff00ff00ff00")).toBe("0xff00ff00ff00ff00");
|
|
166
|
-
expect(decodeDynamicField("bytes", "0x0000000000000000000000000000000000000000000000000000000000000001")).toBe(
|
|
167
|
-
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
168
|
-
);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it("can decode string", () => {
|
|
172
|
-
expect(decodeDynamicField("string", "0x")).toBe("");
|
|
173
|
-
expect(decodeDynamicField("string", "0x68656c6c6f")).toBe("hello");
|
|
174
|
-
});
|
|
175
|
-
});
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { Hex, hexToString, sliceHex } from "viem";
|
|
2
|
-
import { assertExhaustive } from "@latticexyz/common/utils";
|
|
3
|
-
import {
|
|
4
|
-
DynamicAbiType,
|
|
5
|
-
DynamicAbiTypeToPrimitiveType,
|
|
6
|
-
arrayToStaticAbiType,
|
|
7
|
-
staticAbiTypeToByteLength,
|
|
8
|
-
} from "@latticexyz/schema-type/internal";
|
|
9
|
-
import { decodeStaticField } from "./decodeStaticField";
|
|
10
|
-
import { InvalidHexLengthError, InvalidHexLengthForArrayFieldError } from "./errors";
|
|
11
|
-
|
|
12
|
-
// 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)
|
|
13
|
-
// this means the error should probably change from "invalid length" to "too short", which viem has that we could reuse?
|
|
14
|
-
|
|
15
|
-
export function decodeDynamicField<
|
|
16
|
-
TAbiType extends DynamicAbiType,
|
|
17
|
-
TPrimitiveType extends DynamicAbiTypeToPrimitiveType<TAbiType>,
|
|
18
|
-
>(abiType: TAbiType, data: Hex): TPrimitiveType {
|
|
19
|
-
if (abiType === "bytes") {
|
|
20
|
-
return data as TPrimitiveType;
|
|
21
|
-
}
|
|
22
|
-
if (abiType === "string") {
|
|
23
|
-
return hexToString(data) as TPrimitiveType;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (data.length > 3 && data.length % 2 !== 0) {
|
|
27
|
-
throw new InvalidHexLengthError(data);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const dataSize = (data.length - 2) / 2;
|
|
31
|
-
|
|
32
|
-
switch (abiType) {
|
|
33
|
-
case "uint8[]":
|
|
34
|
-
case "uint16[]":
|
|
35
|
-
case "uint24[]":
|
|
36
|
-
case "uint32[]":
|
|
37
|
-
case "uint40[]":
|
|
38
|
-
case "uint48[]":
|
|
39
|
-
case "uint56[]":
|
|
40
|
-
case "uint64[]":
|
|
41
|
-
case "uint72[]":
|
|
42
|
-
case "uint80[]":
|
|
43
|
-
case "uint88[]":
|
|
44
|
-
case "uint96[]":
|
|
45
|
-
case "uint104[]":
|
|
46
|
-
case "uint112[]":
|
|
47
|
-
case "uint120[]":
|
|
48
|
-
case "uint128[]":
|
|
49
|
-
case "uint136[]":
|
|
50
|
-
case "uint144[]":
|
|
51
|
-
case "uint152[]":
|
|
52
|
-
case "uint160[]":
|
|
53
|
-
case "uint168[]":
|
|
54
|
-
case "uint176[]":
|
|
55
|
-
case "uint184[]":
|
|
56
|
-
case "uint192[]":
|
|
57
|
-
case "uint200[]":
|
|
58
|
-
case "uint208[]":
|
|
59
|
-
case "uint216[]":
|
|
60
|
-
case "uint224[]":
|
|
61
|
-
case "uint232[]":
|
|
62
|
-
case "uint240[]":
|
|
63
|
-
case "uint248[]":
|
|
64
|
-
case "uint256[]":
|
|
65
|
-
case "int8[]":
|
|
66
|
-
case "int16[]":
|
|
67
|
-
case "int24[]":
|
|
68
|
-
case "int32[]":
|
|
69
|
-
case "int40[]":
|
|
70
|
-
case "int48[]":
|
|
71
|
-
case "int56[]":
|
|
72
|
-
case "int64[]":
|
|
73
|
-
case "int72[]":
|
|
74
|
-
case "int80[]":
|
|
75
|
-
case "int88[]":
|
|
76
|
-
case "int96[]":
|
|
77
|
-
case "int104[]":
|
|
78
|
-
case "int112[]":
|
|
79
|
-
case "int120[]":
|
|
80
|
-
case "int128[]":
|
|
81
|
-
case "int136[]":
|
|
82
|
-
case "int144[]":
|
|
83
|
-
case "int152[]":
|
|
84
|
-
case "int160[]":
|
|
85
|
-
case "int168[]":
|
|
86
|
-
case "int176[]":
|
|
87
|
-
case "int184[]":
|
|
88
|
-
case "int192[]":
|
|
89
|
-
case "int200[]":
|
|
90
|
-
case "int208[]":
|
|
91
|
-
case "int216[]":
|
|
92
|
-
case "int224[]":
|
|
93
|
-
case "int232[]":
|
|
94
|
-
case "int240[]":
|
|
95
|
-
case "int248[]":
|
|
96
|
-
case "int256[]":
|
|
97
|
-
case "bytes1[]":
|
|
98
|
-
case "bytes2[]":
|
|
99
|
-
case "bytes3[]":
|
|
100
|
-
case "bytes4[]":
|
|
101
|
-
case "bytes5[]":
|
|
102
|
-
case "bytes6[]":
|
|
103
|
-
case "bytes7[]":
|
|
104
|
-
case "bytes8[]":
|
|
105
|
-
case "bytes9[]":
|
|
106
|
-
case "bytes10[]":
|
|
107
|
-
case "bytes11[]":
|
|
108
|
-
case "bytes12[]":
|
|
109
|
-
case "bytes13[]":
|
|
110
|
-
case "bytes14[]":
|
|
111
|
-
case "bytes15[]":
|
|
112
|
-
case "bytes16[]":
|
|
113
|
-
case "bytes17[]":
|
|
114
|
-
case "bytes18[]":
|
|
115
|
-
case "bytes19[]":
|
|
116
|
-
case "bytes20[]":
|
|
117
|
-
case "bytes21[]":
|
|
118
|
-
case "bytes22[]":
|
|
119
|
-
case "bytes23[]":
|
|
120
|
-
case "bytes24[]":
|
|
121
|
-
case "bytes25[]":
|
|
122
|
-
case "bytes26[]":
|
|
123
|
-
case "bytes27[]":
|
|
124
|
-
case "bytes28[]":
|
|
125
|
-
case "bytes29[]":
|
|
126
|
-
case "bytes30[]":
|
|
127
|
-
case "bytes31[]":
|
|
128
|
-
case "bytes32[]":
|
|
129
|
-
case "bool[]":
|
|
130
|
-
case "address[]": {
|
|
131
|
-
const staticAbiType = arrayToStaticAbiType(abiType);
|
|
132
|
-
const itemByteLength = staticAbiTypeToByteLength[staticAbiType];
|
|
133
|
-
if (dataSize % itemByteLength !== 0) {
|
|
134
|
-
throw new InvalidHexLengthForArrayFieldError(staticAbiType, data);
|
|
135
|
-
}
|
|
136
|
-
const items = new Array(dataSize / itemByteLength).fill(undefined).map((_, i) => {
|
|
137
|
-
const itemData = sliceHex(data, i * itemByteLength, (i + 1) * itemByteLength);
|
|
138
|
-
return decodeStaticField(staticAbiType, itemData);
|
|
139
|
-
});
|
|
140
|
-
return items as TPrimitiveType;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return assertExhaustive(abiType, `Unsupported dynamic ABI type: ${abiType}`);
|
|
145
|
-
}
|
package/src/decodeField.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Hex } from "viem";
|
|
2
|
-
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
|
|
3
|
-
import { decodeDynamicField } from "./decodeDynamicField";
|
|
4
|
-
import { decodeStaticField } from "./decodeStaticField";
|
|
5
|
-
|
|
6
|
-
export function decodeField<
|
|
7
|
-
TAbiType extends SchemaAbiType,
|
|
8
|
-
TPrimitiveType extends SchemaAbiTypeToPrimitiveType<TAbiType>,
|
|
9
|
-
>(abiType: TAbiType, data: Hex): TPrimitiveType {
|
|
10
|
-
return (
|
|
11
|
-
isDynamicAbiType(abiType) ? decodeDynamicField(abiType, data) : decodeStaticField(abiType, data)
|
|
12
|
-
) as TPrimitiveType;
|
|
13
|
-
}
|
package/src/decodeKey.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Hex } from "viem";
|
|
2
|
-
import { SchemaToPrimitives, KeySchema } from "./common";
|
|
3
|
-
import { decodeKeyTuple } from "./decodeKeyTuple";
|
|
4
|
-
|
|
5
|
-
export function decodeKey<TSchema extends KeySchema>(
|
|
6
|
-
keySchema: TSchema,
|
|
7
|
-
data: readonly Hex[],
|
|
8
|
-
): SchemaToPrimitives<TSchema> {
|
|
9
|
-
// TODO: refactor and move all decodeKeyTuple logic into this method so we can delete decodeKeyTuple
|
|
10
|
-
const keyValues = decodeKeyTuple({ staticFields: Object.values(keySchema), dynamicFields: [] }, data);
|
|
11
|
-
|
|
12
|
-
return Object.fromEntries(
|
|
13
|
-
Object.keys(keySchema).map((name, i) => [name, keyValues[i]]),
|
|
14
|
-
) as SchemaToPrimitives<TSchema>;
|
|
15
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { decodeKeyTuple } from "./decodeKeyTuple";
|
|
3
|
-
|
|
4
|
-
describe("decodeKeyTuple", () => {
|
|
5
|
-
it("can decode bool key tuple", () => {
|
|
6
|
-
expect(
|
|
7
|
-
decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
|
|
8
|
-
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
9
|
-
]),
|
|
10
|
-
).toStrictEqual([false]);
|
|
11
|
-
expect(
|
|
12
|
-
decodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [
|
|
13
|
-
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
14
|
-
]),
|
|
15
|
-
).toStrictEqual([true]);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("can decode complex key tuple", () => {
|
|
19
|
-
expect(
|
|
20
|
-
decodeKeyTuple({ staticFields: ["uint256", "int32", "bytes16", "address", "bool", "int8"], dynamicFields: [] }, [
|
|
21
|
-
"0x000000000000000000000000000000000000000000000000000000000000002a",
|
|
22
|
-
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6",
|
|
23
|
-
"0x1234000000000000000000000000000000000000000000000000000000000000",
|
|
24
|
-
"0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
|
|
25
|
-
"0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
26
|
-
"0x0000000000000000000000000000000000000000000000000000000000000003",
|
|
27
|
-
]),
|
|
28
|
-
).toStrictEqual([
|
|
29
|
-
42n,
|
|
30
|
-
-42,
|
|
31
|
-
"0x12340000000000000000000000000000",
|
|
32
|
-
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF",
|
|
33
|
-
true,
|
|
34
|
-
3,
|
|
35
|
-
]);
|
|
36
|
-
});
|
|
37
|
-
});
|
package/src/decodeKeyTuple.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Hex, decodeAbiParameters } from "viem";
|
|
2
|
-
import { StaticPrimitiveType } from "@latticexyz/schema-type/internal";
|
|
3
|
-
import { Schema } from "./common";
|
|
4
|
-
|
|
5
|
-
// key tuples are encoded in the same way as abi.encode, so we can decode them with viem
|
|
6
|
-
|
|
7
|
-
/** @deprecated use `decodeKey` instead */
|
|
8
|
-
export function decodeKeyTuple(keySchema: Schema, keyTuple: readonly Hex[]): StaticPrimitiveType[] {
|
|
9
|
-
if (keySchema.staticFields.length !== keyTuple.length) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
`key tuple length ${keyTuple.length} does not match key schema length ${keySchema.staticFields.length}`,
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
return keyTuple.map(
|
|
15
|
-
(key, index) => decodeAbiParameters([{ type: keySchema.staticFields[index] }], key)[0] as StaticPrimitiveType,
|
|
16
|
-
);
|
|
17
|
-
}
|
package/src/decodeRecord.test.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { decodeRecord } from "./decodeRecord";
|
|
3
|
-
|
|
4
|
-
describe("decodeRecord", () => {
|
|
5
|
-
it("can decode hex to record values", () => {
|
|
6
|
-
const valueSchema = { staticFields: ["uint32", "uint128"], dynamicFields: ["uint32[]", "string"] } as const;
|
|
7
|
-
const values = decodeRecord(
|
|
8
|
-
valueSchema,
|
|
9
|
-
"0x0000000100000000000000000000000000000002000000000000000000000000000000000000000b0000000008000000000000130000000300000004736f6d6520737472696e67",
|
|
10
|
-
);
|
|
11
|
-
expect(values).toStrictEqual([1, 2n, [3, 4], "some string"]);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("can decode an empty record", () => {
|
|
15
|
-
const valueSchema = { staticFields: [], dynamicFields: ["string", "string"] } as const;
|
|
16
|
-
const values = decodeRecord(valueSchema, "0x0000000000000000000000000000000000000000000000000000000000000000");
|
|
17
|
-
expect(values).toMatchInlineSnapshot(`
|
|
18
|
-
[
|
|
19
|
-
"",
|
|
20
|
-
"",
|
|
21
|
-
]
|
|
22
|
-
`);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("can decode an out of bounds array", () => {
|
|
26
|
-
const valueSchema = { staticFields: [], dynamicFields: ["uint32[]"] } as const;
|
|
27
|
-
const values = decodeRecord(valueSchema, "0x0000000000000000000000000000000000000000000000000400000000000004");
|
|
28
|
-
expect(values).toMatchInlineSnapshot(`
|
|
29
|
-
[
|
|
30
|
-
[
|
|
31
|
-
0,
|
|
32
|
-
],
|
|
33
|
-
]
|
|
34
|
-
`);
|
|
35
|
-
});
|
|
36
|
-
});
|