@accumulators/merkle-mountain-range
Advanced tools
@@ -7,2 +7,3 @@ import { AppendResult, PeaksFormattingOptions, Proof, ProofOptions, PeaksOptions } from "./types"; | ||
constructor(store: IStore, hasher: IHasher, mmrId?: string); | ||
static createWithGenesis(store: IStore, hasher: IHasher, mmrId?: string): Promise<CoreMMR>; | ||
append(value: string): Promise<AppendResult>; | ||
@@ -9,0 +10,0 @@ getProof(leafIndex: number, options?: ProofOptions): Promise<Proof>; |
@@ -12,2 +12,10 @@ "use strict"; | ||
} | ||
static async createWithGenesis(store, hasher, mmrId) { | ||
const mmr = new CoreMMR(store, hasher, mmrId); | ||
if ((await mmr.elementsCount.get()) != 0) { | ||
throw new Error("Cannot call createWithGenesis on a non-empty MMR. Please provide an empty store or change the MMR id."); | ||
} | ||
await mmr.append(hasher.getGenesis()); | ||
return mmr; | ||
} | ||
async append(value) { | ||
@@ -20,5 +28,4 @@ if (!this.hasher.isElementSizeValid(value)) | ||
const leafIndex = lastElementIdx; | ||
const hash = this.hasher.hash([lastElementIdx.toString(), value]); | ||
await this.hashes.set(hash, lastElementIdx); | ||
peaks.push(hash); | ||
await this.hashes.set(value, lastElementIdx); | ||
peaks.push(value); | ||
let height = 0; | ||
@@ -29,3 +36,3 @@ while ((0, helpers_1.getHeight)(lastElementIdx + 1) > height) { | ||
const leftHash = peaks.pop(); | ||
const parentHash = this.hasher.hash([lastElementIdx.toString(), this.hasher.hash([leftHash, rightHash])]); | ||
const parentHash = this.hasher.hash([leftHash, rightHash]); | ||
await this.hashes.set(parentHash, lastElementIdx); | ||
@@ -137,10 +144,7 @@ peaks.push(parentHash); | ||
throw new Error("Index must be in the tree"); | ||
let hash = this.hasher.hash([leafIndex.toString(), leafValue]); | ||
let hash = leafValue; | ||
for (const proofHash of siblingsHashes) { | ||
const isRight = (0, helpers_1.getHeight)(leafIndex + 1) == (0, helpers_1.getHeight)(leafIndex) + 1; | ||
leafIndex = isRight ? leafIndex + 1 : leafIndex + (0, helpers_1.parentOffset)((0, helpers_1.getHeight)(leafIndex)); | ||
hash = this.hasher.hash([ | ||
leafIndex.toString(), | ||
isRight ? this.hasher.hash([proofHash, hash]) : this.hasher.hash([hash, proofHash]), | ||
]); | ||
hash = isRight ? this.hasher.hash([proofHash, hash]) : this.hasher.hash([hash, proofHash]); | ||
} | ||
@@ -166,3 +170,3 @@ return (await this.retrievePeaksHashes((0, helpers_1.findPeaks)(treeSize))).includes(hash); | ||
else if (peaksIdxs.length === 1) { | ||
return this.hasher.hash([treeSize.toString(), peaksHashes[0]]); | ||
return peaksHashes[0]; | ||
} | ||
@@ -174,3 +178,3 @@ const root0 = this.hasher.hash([peaksHashes[peaksHashes.length - 2], peaksHashes[peaksHashes.length - 1]]); | ||
.reduce((prev, cur) => this.hasher.hash([cur, prev]), root0); | ||
return this.hasher.hash([treeSize.toString(), root]); | ||
return root; | ||
} | ||
@@ -177,0 +181,0 @@ async retrievePeaksHashes(peaksIdxs, formattingOpts) { |
{ | ||
"name": "@accumulators/merkle-mountain-range", | ||
"version": "2.0.4", | ||
"version": "3.0.1", | ||
"description": "A TypeScript implementation of Merkle Mountain Ranges", | ||
@@ -36,10 +36,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@accumulators/core": "^2.0.4", | ||
"@accumulators/core": "^3.0.1", | ||
"ulid": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"@accumulators/hashers": "^2.0.4", | ||
"@accumulators/memory": "^2.0.4" | ||
"@accumulators/hashers": "^3.0.1", | ||
"@accumulators/memory": "^3.0.1" | ||
}, | ||
"gitHead": "72c3f1d9380cf8aaed4d33eda81593271c402915" | ||
"gitHead": "46d7d26d5d12dbbe9dfba9e15e7ba375efd0ff49" | ||
} |
Sorry, the diff of this file is not supported yet
41802
0.08%535
0.94%