Skip to content

Commit 10314f1

Browse files
committed
fix(sdk): ensure entityIds are properly padded
1 parent 8293147 commit 10314f1

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

.changeset/tiny-suns-read.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@dojoengine/predeployed-connector": minor
3+
"@dojoengine/create-burner": minor
4+
"@dojoengine/torii-client": minor
5+
"@dojoengine/create-dojo": minor
6+
"@dojoengine/torii-wasm": minor
7+
"@dojoengine/utils-wasm": minor
8+
"@dojoengine/react": minor
9+
"@dojoengine/state": minor
10+
"@dojoengine/utils": minor
11+
"@dojoengine/core": minor
12+
"@dojoengine/grpc": minor
13+
"@dojoengine/sdk": minor
14+
---
15+
16+
fix(sdk): ensure entityIds are properly padded

packages/sdk/src/__tests__/parseEntities.test.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe("parseEntities", () => {
148148
expect(result).toEqual([
149149
{
150150
entityId:
151-
"0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85",
151+
"0x014c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85",
152152
models: {
153153
dojo_starter: {
154154
Position: {
@@ -169,7 +169,7 @@ describe("parseEntities", () => {
169169
},
170170
{
171171
entityId:
172-
"0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d",
172+
"0x0144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d",
173173
models: {
174174
dojo_starter: {
175175
Moves: {
@@ -409,6 +409,72 @@ describe("parseEntities", () => {
409409
).toBe("number");
410410
});
411411

412+
it("should properly pad entity IDs to consistent length", () => {
413+
const toriiResult: torii.Entity[] = [
414+
{
415+
// Unpadded entity ID (shorter)
416+
hashed_keys: "0x1",
417+
models: {
418+
"test-Model": {
419+
id: {
420+
type: "primitive",
421+
type_name: "u32",
422+
value: 1,
423+
key: true,
424+
},
425+
},
426+
},
427+
},
428+
{
429+
// Already padded entity ID
430+
hashed_keys:
431+
"0x0000000000000000000000000000000000000000000000000000000000000002",
432+
models: {
433+
"test-Model": {
434+
id: {
435+
type: "primitive",
436+
type_name: "u32",
437+
value: 2,
438+
key: true,
439+
},
440+
},
441+
},
442+
},
443+
{
444+
// Partially padded entity ID
445+
hashed_keys: "0x00000003",
446+
models: {
447+
"test-Model": {
448+
id: {
449+
type: "primitive",
450+
type_name: "u32",
451+
value: 3,
452+
key: true,
453+
},
454+
},
455+
},
456+
},
457+
];
458+
459+
const res = parseEntities(toriiResult);
460+
461+
// All entity IDs should be padded to 66 characters (0x + 64 hex chars)
462+
expect(res[0]?.entityId).toBe(
463+
"0x0000000000000000000000000000000000000000000000000000000000000001"
464+
);
465+
expect(res[1]?.entityId).toBe(
466+
"0x0000000000000000000000000000000000000000000000000000000000000002"
467+
);
468+
expect(res[2]?.entityId).toBe(
469+
"0x0000000000000000000000000000000000000000000000000000000000000003"
470+
);
471+
472+
// Verify all have the same length
473+
expect(res[0]?.entityId?.length).toBe(66);
474+
expect(res[1]?.entityId?.length).toBe(66);
475+
expect(res[2]?.entityId?.length).toBe(66);
476+
});
477+
412478
it("should parse all primitive types correctly according to Rust serialization", () => {
413479
const toriiResult: torii.Entity[] = [
414480
{

packages/sdk/src/internal/parseEntities.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type * as torii from "@dojoengine/torii-wasm/types";
2-
import { CairoCustomEnum, CairoOption, CairoOptionVariant } from "starknet";
2+
import {
3+
CairoCustomEnum,
4+
CairoOption,
5+
CairoOptionVariant,
6+
addAddressPadding,
7+
} from "starknet";
38
import type {
49
ParsedEntity,
510
SchemaType,
@@ -14,7 +19,7 @@ export function parseEntities<T extends SchemaType>(
1419
const result: ParsedEntity<T>[] = [];
1520

1621
for (const entity of entities) {
17-
const entityId = entity.hashed_keys;
22+
const entityId = addAddressPadding(entity.hashed_keys);
1823
const entityData = entity.models;
1924
const parsedEntity: ParsedEntity<T> = {
2025
entityId,

0 commit comments

Comments
 (0)