-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMerkleWitnessResolver.ts
More file actions
67 lines (56 loc) · 2.05 KB
/
MerkleWitnessResolver.ts
File metadata and controls
67 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Arg, Field, ObjectType, Query } from "type-graphql";
import { Length } from "class-validator";
import { inject } from "tsyringe";
import { RollupMerkleTree, RollupMerkleTreeWitness } from "@proto-kit/common";
import {
BlockStorage,
CachedMerkleTreeStore,
MaskName,
TreeStoreCreator,
} from "@proto-kit/sequencer";
import { GraphqlModule, graphqlModule } from "../GraphqlModule";
@ObjectType()
export class MerkleWitnessDTO {
public static fromServiceLayerObject(witness: RollupMerkleTreeWitness) {
const siblings = witness.path.map((item) => item.toString());
const isLefts = witness.isLeft.map((item) => item.toBoolean());
return new MerkleWitnessDTO(siblings, isLefts);
}
public constructor(siblings: string[], isLefts: boolean[]) {
this.siblings = siblings;
this.isLefts = isLefts;
}
@Field(() => [String])
@Length(255)
public siblings: string[];
@Field(() => [Boolean])
@Length(255)
public isLefts: boolean[];
}
@graphqlModule()
export class MerkleWitnessResolver extends GraphqlModule<object> {
public constructor(
@inject("TreeStoreCreator")
private readonly treeStoreCreator: TreeStoreCreator,
@inject("BlockStorage") private readonly blockStorage: BlockStorage
) {
super();
}
@Query(() => MerkleWitnessDTO, {
description:
"Allows retrieval of merkle witnesses corresponding to a specific path in the appchain's state tree. These proves are generally retrieved from the current 'proven' state",
})
public async witness(@Arg("path") path: string) {
const latestBlock = await this.blockStorage.getLatestBlock();
const maskName =
latestBlock !== undefined
? MaskName.block(latestBlock.block.height)
: MaskName.base();
const treeStore = this.treeStoreCreator.getMask(maskName);
const syncStore = new CachedMerkleTreeStore(treeStore);
await syncStore.preloadKey(BigInt(path));
const tree = new RollupMerkleTree(syncStore);
const witness = tree.getWitness(BigInt(path));
return MerkleWitnessDTO.fromServiceLayerObject(witness);
}
}