-
Notifications
You must be signed in to change notification settings - Fork 5
/
merkle.d.ts
120 lines (93 loc) · 4.53 KB
/
merkle.d.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
declare module '@guildofweavers/merkle' {
// HASHING
// --------------------------------------------------------------------------------------------
export type HashAlgorithm = 'sha256' | 'blake2s256';
/**
* Creates a Hash object for the specified algorithm. If useWasm is set to true, will try to
* instantiate a WebAssembly-optimized version of the algorithm. If WASM optimization is not
* available for the specified algorithm, Node's native implementation will be used.
*/
export function createHash(algorithm: HashAlgorithm, useWasm?: boolean): Hash;
/**
* Tries to create a WebAssembly-optimized Hash object for the specified algorithm and pass
* the provided options to it. If WASM optimization is not available for the specified algorithm,
* Node's native implementation will be used.
*/
export function createHash(algorithm: HashAlgorithm, options: Partial<WasmOptions>): Hash;
export interface WasmOptions {
readonly memory: WebAssembly.Memory;
}
export interface Hash {
readonly algorithm : HashAlgorithm;
readonly digestSize : number;
readonly isOptimized: boolean;
/** Hashes the provided value */
digest(value: Buffer): Buffer;
/** Hashes a concatenation of a and b */
merge(a: Buffer, b: Buffer): Buffer;
buildMerkleNodes(depth: number, leaves: Vector): ArrayBuffer;
mergeVectorRows(vectors: Vector[]): Vector;
digestValues(values: Buffer, valueSize: number): Vector;
}
/** Returns true if WebAssembly optimization is available for the provided algorithm */
export function isWasmOptimized(hashAlgorithm: HashAlgorithm): boolean;
// MERKLE TREE
// --------------------------------------------------------------------------------------------
export class MerkleTree {
/**
* Returns a Merkle tree created from the specified values
* @param values Values that form the leaves of the tree
* @param hash Hash object to use for hashing of internal nodes
*/
static create(values: Buffer[] | Vector, hash: Hash): MerkleTree;
/**
* Returns a Promise for a Merkle tree created from the specified values
* @param values Values that form the leaves of the tree
* @param hash Hash object to use for hashing of internal nodes
*/
static createAsync(values: Buffer[] | Vector, hash: Hash): Promise<MerkleTree>;
/** Root of the tree */
readonly root: Buffer;
/** Returns a leaf node located at the specified index */
getLeaf(index: number): Buffer;
/** Returns all leaf nodes of the tree */
getLeaves(): Buffer[];
/** Returns a Merkle proof for a single leaf at the specified index */
prove(index: number): Buffer[];
/** Returns a compressed Merkle proof for leaves at the specified indexes */
proveBatch(indexes: number[]): BatchMerkleProof;
/**
* Verifies Merkle proof for a single index
* @param root Root of the Merkle tree
* @param index Index of a leaf to verify
* @param proof Merkle proof for the leaf at the specified index
* @param hash Hash object to use for hashing of internal nodes
*/
static verify(root: Buffer, index: number, proof: Buffer[], hash: Hash): boolean;
/**
* Verifies Merkle proof for a list of indexes
* @param root Root of the Merkle tree
* @param index Indexes of leaves to verify
* @param proof Compressed Merkle proof for the leaves at the specified indexes
* @param hash Hash object to use for hashing of internal nodes
*/
static verifyBatch(root: Buffer, indexes: number[], proof: BatchMerkleProof, hash: Hash): boolean;
}
export interface BatchMerkleProof {
/** leaf nodes located at the indexes covered by the proof */
values: Buffer[];
/** Internal nodes that form the actual proof */
nodes: Buffer[][];
/** Depth of the source Merkle tree */
depth: number;
}
// INTERNAL DATA STRUCTURES
// --------------------------------------------------------------------------------------------
export interface Vector {
readonly length : number;
readonly byteLength : number;
readonly elementSize : number;
copyValue(index: number, destination: Buffer, offset: number): number;
toBuffer(startIdx?: number, elementCount?: number): Buffer;
}
}