Skip to content

@lelantos-org/sdk / advanced / TreePersistence

Interface: TreePersistence ​

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:52

Storage backend that persists the Merkle tree across page loads.

TreeStoreState.leaves is bigint[], which JSON.stringify cannot serialise, so a JSON backend must encode it. Use 0x-prefixed hex: BigInt parses unprefixed strings as decimal, so bare hex with only decimal digits would decode to a different number. A structured-clone backend (IndexedDB) stores bigint directly.

Example ​

ts
class MyPersistence implements TreePersistence {
    async load() {
        const raw = localStorage.getItem("tree");
        if (raw === null) return null;
        const { leaves, syncedCount } = JSON.parse(raw) as {
            leaves: string[];
            syncedCount: number;
        };
        return { leaves: leaves.map(BigInt), syncedCount };
    }
    async save(state: TreeStoreState) {
        localStorage.setItem(
            "tree",
            JSON.stringify({
                leaves: state.leaves.map((v) => `0x${v.toString(16)}`),
                syncedCount: state.syncedCount,
            }),
        );
    }
    async clear() { localStorage.removeItem("tree"); }
}
const wallet = await connect({ ..., storage: { tree: new MyPersistence() } });

The example omits nodes on save, which is valid; see TreeStoreState.nodes for the cost.

Methods ​

clear() ​

ts
clear(): Promise<void>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:59

Discard every record written for this tree. TreeStore.reset depends on it: otherwise load() would restore the discarded tree on the next start and the rebuild would repeat.

Returns ​

Promise<void>


load() ​

ts
load(): Promise<TreeStoreState | null>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:53

Returns ​

Promise<TreeStoreState | null>


save() ​

ts
save(state): Promise<void>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:54

Parameters ​

ParameterType
stateTreeStoreState

Returns ​

Promise<void>