@latticexyz/schema-type 1.40.0 → 2.0.0-alpha.0
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/typescript/AbiTypeToSchemaType.d.ts +4 -0
- package/dist/typescript/AbiTypeToSchemaType.js +3 -0
- package/dist/typescript/AbiTypeToSchemaType.js.map +1 -0
- package/dist/typescript/AbiTypes.d.ts +4 -0
- package/dist/typescript/AbiTypes.js +3 -0
- package/dist/typescript/AbiTypes.js.map +1 -0
- package/dist/typescript/ArraySchemaType.d.ts +2 -0
- package/dist/typescript/ArraySchemaType.js +2 -0
- package/dist/typescript/ArraySchemaType.js.map +1 -0
- package/dist/typescript/DynamicSchemaType.d.ts +3 -0
- package/dist/typescript/DynamicSchemaType.js +2 -0
- package/dist/typescript/DynamicSchemaType.js.map +1 -0
- package/dist/typescript/SchemaType.d.ts +0 -204
- package/dist/typescript/SchemaType.js +0 -324
- package/dist/typescript/SchemaType.js.map +1 -1
- package/dist/typescript/SchemaTypeArrayToElement.d.ts +201 -0
- package/dist/typescript/SchemaTypeArrayToElement.js +102 -0
- package/dist/typescript/SchemaTypeArrayToElement.js.map +1 -0
- package/dist/typescript/SchemaTypeId.d.ts +2 -0
- package/dist/typescript/SchemaTypeId.js +202 -0
- package/dist/typescript/SchemaTypeId.js.map +1 -0
- package/dist/typescript/SchemaTypeToAbiType.d.ts +200 -0
- package/dist/typescript/SchemaTypeToAbiType.js +202 -0
- package/dist/typescript/SchemaTypeToAbiType.js.map +1 -0
- package/dist/typescript/SchemaTypeToPrimitive.d.ts +201 -0
- package/dist/typescript/SchemaTypeToPrimitive.js +2 -0
- package/dist/typescript/SchemaTypeToPrimitive.js.map +1 -0
- package/dist/typescript/StaticAbiTypes.d.ts +4 -0
- package/dist/typescript/StaticAbiTypes.js +5 -0
- package/dist/typescript/StaticAbiTypes.js.map +1 -0
- package/dist/typescript/StaticSchemaType.d.ts +3 -0
- package/dist/typescript/StaticSchemaType.js +2 -0
- package/dist/typescript/StaticSchemaType.js.map +1 -0
- package/dist/typescript/encodeSchema.d.ts +8 -0
- package/dist/typescript/encodeSchema.js +43 -0
- package/dist/typescript/encodeSchema.js.map +1 -0
- package/dist/typescript/getStaticByteLength.d.ts +2 -0
- package/dist/typescript/getStaticByteLength.js +26 -0
- package/dist/typescript/getStaticByteLength.js.map +1 -0
- package/dist/typescript/index.d.ts +14 -0
- package/dist/typescript/index.js +9 -0
- package/dist/typescript/index.js.map +1 -0
- package/package.json +4 -4
- package/src/typescript/AbiTypeToSchemaType.ts +6 -0
- package/src/typescript/AbiTypes.ts +5 -0
- package/src/typescript/ArraySchemaType.ts +101 -0
- package/src/typescript/DynamicSchemaType.ts +4 -0
- package/src/typescript/SchemaType.ts +0 -441
- package/src/typescript/SchemaTypeArrayToElement.ts +109 -0
- package/src/typescript/SchemaTypeToAbiType.ts +210 -0
- package/src/typescript/SchemaTypeToPrimitive.ts +210 -0
- package/src/typescript/StaticAbiTypes.ts +10 -0
- package/src/typescript/StaticSchemaType.ts +4 -0
- package/src/typescript/encodeSchema.ts +47 -0
- package/src/typescript/getStaticByteLength.ts +25 -0
- package/src/typescript/index.ts +15 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getStaticByteLength } from "./getStaticByteLength.js";
|
|
2
|
+
/**
|
|
3
|
+
* Encode a table schema into a bytes32 hex string
|
|
4
|
+
* Port of `Schema.sol` from `@latticexyz/store`
|
|
5
|
+
* @param schema The schema to encode SchemaType[]
|
|
6
|
+
* @returns The encoded schema as a 32 byte hex string
|
|
7
|
+
*/
|
|
8
|
+
export function encodeSchema(schema) {
|
|
9
|
+
if (schema.length > 28)
|
|
10
|
+
throw new Error("Schema can only have up to 28 fields");
|
|
11
|
+
const encodedSchema = new Uint8Array(32);
|
|
12
|
+
let length = 0;
|
|
13
|
+
let staticFields = 0;
|
|
14
|
+
// Compute the length of the schema and the number of static fields
|
|
15
|
+
// and store the schema types in the encoded schema
|
|
16
|
+
let hasDynamicFields = false;
|
|
17
|
+
for (let i = 0; i < schema.length; i++) {
|
|
18
|
+
const staticByteLength = getStaticByteLength(schema[i]);
|
|
19
|
+
// Increase the static field count if the field is static
|
|
20
|
+
if (staticByteLength > 0) {
|
|
21
|
+
// Revert if we have seen a dynamic field before, but now we see a static field
|
|
22
|
+
if (hasDynamicFields)
|
|
23
|
+
throw new Error("Static fields must come before dynamic fields in the schema");
|
|
24
|
+
staticFields++;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// Flag that we have seen a dynamic field
|
|
28
|
+
hasDynamicFields = true;
|
|
29
|
+
}
|
|
30
|
+
length += staticByteLength;
|
|
31
|
+
encodedSchema[i + 4] = schema[i];
|
|
32
|
+
}
|
|
33
|
+
// Require max 14 dynamic fields
|
|
34
|
+
const dynamicFields = schema.length - staticFields;
|
|
35
|
+
if (dynamicFields > 14)
|
|
36
|
+
throw new Error("Schema can only have up to 14 dynamic fields");
|
|
37
|
+
// Store total static length, and number of static and dynamic fields
|
|
38
|
+
new DataView(encodedSchema.buffer).setUint16(0, length); // 2 length bytes
|
|
39
|
+
encodedSchema[2] = staticFields; // number of static fields
|
|
40
|
+
encodedSchema[3] = dynamicFields; // number of dynamic fields
|
|
41
|
+
return encodedSchema;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=encodeSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeSchema.js","sourceRoot":"","sources":["../../src/typescript/encodeSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAG/D;;;;;GAKG;AAEH,MAAM,UAAU,YAAY,CAAC,MAAoB;IAC/C,IAAI,MAAM,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAChF,MAAM,aAAa,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACzC,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|
+
if (val < 32) {
|
|
5
|
+
// uint8-256
|
|
6
|
+
return val + 1;
|
|
7
|
+
}
|
|
8
|
+
else if (val < 64) {
|
|
9
|
+
// int8-256, offset by 32
|
|
10
|
+
return val + 1 - 32;
|
|
11
|
+
}
|
|
12
|
+
else if (val < 96) {
|
|
13
|
+
// bytes1-32, offset by 64
|
|
14
|
+
return val + 1 - 64;
|
|
15
|
+
}
|
|
16
|
+
// Other static types
|
|
17
|
+
if (schemaType == SchemaType.BOOL) {
|
|
18
|
+
return 1;
|
|
19
|
+
}
|
|
20
|
+
else if (schemaType == SchemaType.ADDRESS) {
|
|
21
|
+
return 20;
|
|
22
|
+
}
|
|
23
|
+
// Return 0 for all dynamic types
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=getStaticByteLength.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getStaticByteLength.js","sourceRoot":"","sources":["../../src/typescript/getStaticByteLength.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,UAAU,mBAAmB,CAAC,UAAsB;IACxD,MAAM,GAAG,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC;IACjC,IAAI,GAAG,GAAG,EAAE,EAAE;QACZ,YAAY;QACZ,OAAO,GAAG,GAAG,CAAC,CAAC;KAChB;SAAM,IAAI,GAAG,GAAG,EAAE,EAAE;QACnB,yBAAyB;QACzB,OAAO,GAAG,GAAG,CAAC,GAAG,EAAE,CAAC;KACrB;SAAM,IAAI,GAAG,GAAG,EAAE,EAAE;QACnB,0BAA0B;QAC1B,OAAO,GAAG,GAAG,CAAC,GAAG,EAAE,CAAC;KACrB;IAED,qBAAqB;IACrB,IAAI,UAAU,IAAI,UAAU,CAAC,IAAI,EAAE;QACjC,OAAO,CAAC,CAAC;KACV;SAAM,IAAI,UAAU,IAAI,UAAU,CAAC,OAAO,EAAE;QAC3C,OAAO,EAAE,CAAC;KACX;IAED,iCAAiC;IACjC,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { StaticSchemaType } from "./StaticSchemaType.js";
|
|
2
|
+
export type { DynamicSchemaType } from "./DynamicSchemaType.js";
|
|
3
|
+
export type { ArraySchemaType } from "./ArraySchemaType.js";
|
|
4
|
+
export type { SchemaTypeToPrimitive } from "./SchemaTypeToPrimitive.js";
|
|
5
|
+
export type { AbiType } from "./AbiTypes.js";
|
|
6
|
+
export type { StaticAbiType } from "./StaticAbiTypes.js";
|
|
7
|
+
export { SchemaType } from "./SchemaType.js";
|
|
8
|
+
export { encodeSchema } from "./encodeSchema.js";
|
|
9
|
+
export { getStaticByteLength } from "./getStaticByteLength.js";
|
|
10
|
+
export { SchemaTypeArrayToElement } from "./SchemaTypeArrayToElement.js";
|
|
11
|
+
export { SchemaTypeToAbiType } from "./SchemaTypeToAbiType.js";
|
|
12
|
+
export { AbiTypeToSchemaType } from "./AbiTypeToSchemaType.js";
|
|
13
|
+
export { AbiTypes } from "./AbiTypes.js";
|
|
14
|
+
export { StaticAbiTypes } from "./StaticAbiTypes.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { SchemaType } from "./SchemaType.js";
|
|
2
|
+
export { encodeSchema } from "./encodeSchema.js";
|
|
3
|
+
export { getStaticByteLength } from "./getStaticByteLength.js";
|
|
4
|
+
export { SchemaTypeArrayToElement } from "./SchemaTypeArrayToElement.js";
|
|
5
|
+
export { SchemaTypeToAbiType } from "./SchemaTypeToAbiType.js";
|
|
6
|
+
export { AbiTypeToSchemaType } from "./AbiTypeToSchemaType.js";
|
|
7
|
+
export { AbiTypes } from "./AbiTypes.js";
|
|
8
|
+
export { StaticAbiTypes } from "./StaticAbiTypes.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/typescript/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latticexyz/schema-type",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0-alpha.0",
|
|
5
5
|
"description": "SchemaType enum for various languages",
|
|
6
|
-
"main": "src/typescript/
|
|
7
|
-
"source": "src/typescript/
|
|
6
|
+
"main": "src/typescript/index.ts",
|
|
7
|
+
"source": "src/typescript/index.ts",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"forge-std": "https://github.com/foundry-rs/forge-std.git#b4f121555729b3afb3c5ffccb62ff4b6e2818fd3",
|
|
29
29
|
"rimraf": "^3.0.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "fcb2166c25edd27ead54f0afa1b71d2583939603"
|
|
32
32
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SchemaTypeToAbiType } from "./SchemaTypeToAbiType.js";
|
|
2
|
+
import { SchemaType } from "./SchemaType.js";
|
|
3
|
+
|
|
4
|
+
export const AbiTypeToSchemaType = Object.fromEntries(
|
|
5
|
+
Object.entries(SchemaTypeToAbiType).map(([schemaType, abiType]) => [abiType, parseInt(schemaType) as SchemaType])
|
|
6
|
+
) satisfies Record<string, SchemaType>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { SchemaType } from "./SchemaType.js";
|
|
2
|
+
|
|
3
|
+
export type ArraySchemaType =
|
|
4
|
+
| SchemaType.UINT8_ARRAY
|
|
5
|
+
| SchemaType.UINT16_ARRAY
|
|
6
|
+
| SchemaType.UINT24_ARRAY
|
|
7
|
+
| SchemaType.UINT32_ARRAY
|
|
8
|
+
| SchemaType.UINT40_ARRAY
|
|
9
|
+
| SchemaType.UINT48_ARRAY
|
|
10
|
+
| SchemaType.UINT56_ARRAY
|
|
11
|
+
| SchemaType.UINT64_ARRAY
|
|
12
|
+
| SchemaType.UINT72_ARRAY
|
|
13
|
+
| SchemaType.UINT80_ARRAY
|
|
14
|
+
| SchemaType.UINT88_ARRAY
|
|
15
|
+
| SchemaType.UINT96_ARRAY
|
|
16
|
+
| SchemaType.UINT104_ARRAY
|
|
17
|
+
| SchemaType.UINT112_ARRAY
|
|
18
|
+
| SchemaType.UINT120_ARRAY
|
|
19
|
+
| SchemaType.UINT128_ARRAY
|
|
20
|
+
| SchemaType.UINT136_ARRAY
|
|
21
|
+
| SchemaType.UINT144_ARRAY
|
|
22
|
+
| SchemaType.UINT152_ARRAY
|
|
23
|
+
| SchemaType.UINT160_ARRAY
|
|
24
|
+
| SchemaType.UINT168_ARRAY
|
|
25
|
+
| SchemaType.UINT176_ARRAY
|
|
26
|
+
| SchemaType.UINT184_ARRAY
|
|
27
|
+
| SchemaType.UINT192_ARRAY
|
|
28
|
+
| SchemaType.UINT200_ARRAY
|
|
29
|
+
| SchemaType.UINT208_ARRAY
|
|
30
|
+
| SchemaType.UINT216_ARRAY
|
|
31
|
+
| SchemaType.UINT224_ARRAY
|
|
32
|
+
| SchemaType.UINT232_ARRAY
|
|
33
|
+
| SchemaType.UINT240_ARRAY
|
|
34
|
+
| SchemaType.UINT248_ARRAY
|
|
35
|
+
| SchemaType.UINT256_ARRAY
|
|
36
|
+
| SchemaType.INT8_ARRAY
|
|
37
|
+
| SchemaType.INT16_ARRAY
|
|
38
|
+
| SchemaType.INT24_ARRAY
|
|
39
|
+
| SchemaType.INT32_ARRAY
|
|
40
|
+
| SchemaType.INT40_ARRAY
|
|
41
|
+
| SchemaType.INT48_ARRAY
|
|
42
|
+
| SchemaType.INT56_ARRAY
|
|
43
|
+
| SchemaType.INT64_ARRAY
|
|
44
|
+
| SchemaType.INT72_ARRAY
|
|
45
|
+
| SchemaType.INT80_ARRAY
|
|
46
|
+
| SchemaType.INT88_ARRAY
|
|
47
|
+
| SchemaType.INT96_ARRAY
|
|
48
|
+
| SchemaType.INT104_ARRAY
|
|
49
|
+
| SchemaType.INT112_ARRAY
|
|
50
|
+
| SchemaType.INT120_ARRAY
|
|
51
|
+
| SchemaType.INT128_ARRAY
|
|
52
|
+
| SchemaType.INT136_ARRAY
|
|
53
|
+
| SchemaType.INT144_ARRAY
|
|
54
|
+
| SchemaType.INT152_ARRAY
|
|
55
|
+
| SchemaType.INT160_ARRAY
|
|
56
|
+
| SchemaType.INT168_ARRAY
|
|
57
|
+
| SchemaType.INT176_ARRAY
|
|
58
|
+
| SchemaType.INT184_ARRAY
|
|
59
|
+
| SchemaType.INT192_ARRAY
|
|
60
|
+
| SchemaType.INT200_ARRAY
|
|
61
|
+
| SchemaType.INT208_ARRAY
|
|
62
|
+
| SchemaType.INT216_ARRAY
|
|
63
|
+
| SchemaType.INT224_ARRAY
|
|
64
|
+
| SchemaType.INT232_ARRAY
|
|
65
|
+
| SchemaType.INT240_ARRAY
|
|
66
|
+
| SchemaType.INT248_ARRAY
|
|
67
|
+
| SchemaType.INT256_ARRAY
|
|
68
|
+
| SchemaType.BYTES1_ARRAY
|
|
69
|
+
| SchemaType.BYTES2_ARRAY
|
|
70
|
+
| SchemaType.BYTES3_ARRAY
|
|
71
|
+
| SchemaType.BYTES4_ARRAY
|
|
72
|
+
| SchemaType.BYTES5_ARRAY
|
|
73
|
+
| SchemaType.BYTES6_ARRAY
|
|
74
|
+
| SchemaType.BYTES7_ARRAY
|
|
75
|
+
| SchemaType.BYTES8_ARRAY
|
|
76
|
+
| SchemaType.BYTES9_ARRAY
|
|
77
|
+
| SchemaType.BYTES10_ARRAY
|
|
78
|
+
| SchemaType.BYTES11_ARRAY
|
|
79
|
+
| SchemaType.BYTES12_ARRAY
|
|
80
|
+
| SchemaType.BYTES13_ARRAY
|
|
81
|
+
| SchemaType.BYTES14_ARRAY
|
|
82
|
+
| SchemaType.BYTES15_ARRAY
|
|
83
|
+
| SchemaType.BYTES16_ARRAY
|
|
84
|
+
| SchemaType.BYTES17_ARRAY
|
|
85
|
+
| SchemaType.BYTES18_ARRAY
|
|
86
|
+
| SchemaType.BYTES19_ARRAY
|
|
87
|
+
| SchemaType.BYTES20_ARRAY
|
|
88
|
+
| SchemaType.BYTES21_ARRAY
|
|
89
|
+
| SchemaType.BYTES22_ARRAY
|
|
90
|
+
| SchemaType.BYTES23_ARRAY
|
|
91
|
+
| SchemaType.BYTES24_ARRAY
|
|
92
|
+
| SchemaType.BYTES25_ARRAY
|
|
93
|
+
| SchemaType.BYTES26_ARRAY
|
|
94
|
+
| SchemaType.BYTES27_ARRAY
|
|
95
|
+
| SchemaType.BYTES28_ARRAY
|
|
96
|
+
| SchemaType.BYTES29_ARRAY
|
|
97
|
+
| SchemaType.BYTES30_ARRAY
|
|
98
|
+
| SchemaType.BYTES31_ARRAY
|
|
99
|
+
| SchemaType.BYTES32_ARRAY
|
|
100
|
+
| SchemaType.BOOL_ARRAY
|
|
101
|
+
| SchemaType.ADDRESS_ARRAY;
|