This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsparse_merkle_tree.js
134 lines (124 loc) · 4.13 KB
/
sparse_merkle_tree.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
const {
in_memory_smt_new,
in_memory_smt_update,
in_memory_smt_prove,
in_memory_smt_verify,
in_memory_smt_calculate_root,
in_memory_smt_remove_keys_from_proof,
} = require("./bin-package/index.node");
const { isInclusionProofForQueryKey } = require('./utils');
const DEFAULT_KEY_LENGTH = 38;
const copyBuffer = h => {
const copied = Buffer.alloc(h.length);
h.copy(copied);
return copied;
}
class SparseMerkleTree {
constructor(keyLength = DEFAULT_KEY_LENGTH) {
this._keyLength = keyLength;
this._inner = in_memory_smt_new(keyLength);
}
async update(root, kvpairs) {
return new Promise((resolve, reject) => {
in_memory_smt_update.call(this._inner, root, kvpairs, (err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
});
});
}
async prove(root, queries) {
return new Promise((resolve, reject) => {
in_memory_smt_prove.call(this._inner, root, queries, (err, result) => {
if (err) {
reject(err);
return;
}
// If result is empty, force to use different memory space from what's given from binding
// Issue: https://github.com/nodejs/node/issues/32463
// Try console.log(proof) at the end of Jumbo fixture test without copy
resolve({
siblingHashes: result.siblingHashes.map(copyBuffer),
queries: result.queries.map(q => ({
key: copyBuffer(q.key),
value: copyBuffer(q.value),
bitmap: copyBuffer(q.bitmap),
})),
});
});
});
}
async verify(root, queries, proof) {
return new Promise((resolve, reject) => {
in_memory_smt_verify.call(null, root, queries, proof, this._keyLength, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
async verifyInclusionProof(root, queries, proof) {
if (queries.length !== proof.queries.length) {
return false;
}
for (let i = 0; i < queries.length; i++) {
if (!isInclusionProofForQueryKey(queries[i], proof.queries[i])) {
return false;
}
}
return this.verify(root, queries, proof);
}
async verifyNonInclusionProof(root, queries, proof) {
if (queries.length !== proof.queries.length) {
return false;
}
for (let i = 0; i < queries.length; i++) {
if (isInclusionProofForQueryKey(queries[i], proof.queries[i])) {
return false;
}
}
return this.verify(root, queries, proof);
}
async calculateRoot(proof) {
return new Promise((resolve, reject) => {
in_memory_smt_calculate_root.call(null, proof, (err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
});
});
}
async removeKeysFromProof(proof, removedKeys) {
return new Promise((resolve, reject) => {
in_memory_smt_remove_keys_from_proof.call(null, proof, removedKeys, (err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
});
});
}
}
module.exports = {
SparseMerkleTree,
};