merkle-t
Version:
Merkle Tree with Poseidon Hash in TypeScript
31 lines • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FullMerkleTree = exports.MerkleTree = void 0;
const tree_1 = require("./tree");
Object.defineProperty(exports, "MerkleTree", { enumerable: true, get: function () { return tree_1.MerkleTree; } });
class FullMerkleTree extends tree_1.MerkleTree {
constructor(leavesInputs, options) {
super(leavesInputs.map((leafInput) => leafInput.hash()), leavesInputs.length, options);
this.leavesInputs = leavesInputs;
}
insert(leafInput) {
const max = Math.pow(2, this.depth) - 1;
if (this.orderedLeaves.length >= max) {
throw new Error("Tree is full");
}
this.leavesInputs.push(leafInput);
this.orderedLeaves.push({
index: this.orderedLeaves.length,
leaf: leafInput.hash(),
});
}
update(key, leafInput) {
const index = this.leavesInputs.findIndex((leafInput) => leafInput === key);
if (index === -1) {
throw new Error("Key not found");
}
this.leavesInputs[index] = leafInput;
}
}
exports.FullMerkleTree = FullMerkleTree;
//# sourceMappingURL=index.js.map