Skip to content

Commit 283af71

Browse files
add hash function parameter
1 parent 43ea2cc commit 283af71

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/merkle-tree.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { assertEquals } from "https://deno.land/std@0.86.0/testing/asserts.ts"
22
import { MerkleTree, Helper } from "../mod.ts"
33

4-
const exampleArray = ["dog", "horse", "cow", "chicken", "rabbit", "bird", "bee", "me"]
5-
64
Deno.test("should return valid as the investigated entry is in array", async () => {
75

6+
const exampleArray = ["dog", "horse", "cow", "chicken", "rabbit", "bird", "bee", "me"]
7+
88
for (const investigatedEntry of exampleArray) {
99
const merkleTree = new MerkleTree(exampleArray)
1010

@@ -20,6 +20,8 @@ Deno.test("should return valid as the investigated entry is in array", async ()
2020

2121
Deno.test("should return invalid as the investigated entry is not in array", async () => {
2222

23+
const exampleArray = ["dog", "horse", "cow", "chicken", "rabbit", "bird", "bee", "me"]
24+
2325
const merkleTree = new MerkleTree(exampleArray)
2426
const investigatedEntry = "an entry which is not in the array"
2527
const proof = merkleTree.getProofElements(exampleArray.indexOf(investigatedEntry))
@@ -30,3 +32,25 @@ Deno.test("should return invalid as the investigated entry is not in array", asy
3032
assertEquals(isValid, false)
3133

3234
})
35+
36+
37+
Deno.test("should be able to utilize a custom hash function", async () => {
38+
39+
const exampleArray = [2, 4, 8, 1]
40+
41+
const merkleTree = new MerkleTree(exampleArray, (x: number) => x * 2 % 10)
42+
const rootHash = merkleTree.getRootHash()
43+
44+
assertEquals(rootHash, 8)
45+
46+
const proof = merkleTree.getProofElements(exampleArray.indexOf(1))
47+
console.log(proof)
48+
49+
const investigatedEntry = 1
50+
const investigatedEntryHashed = investigatedEntry * 2 % 10
51+
const isValid = merkleTree.verify(proof, investigatedEntryHashed, rootHash, exampleArray.indexOf(investigatedEntry))
52+
53+
assertEquals(isValid, true)
54+
55+
56+
})

src/merkle-tree.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,37 @@ export class MerkleTree {
1010

1111
private hashes: IMerkleHashes[] = []
1212

13+
private hashFunction: any | undefined
1314

14-
public constructor(array: any[]) {
15+
public constructor(array: any[], hashFunction?: any) {
1516

1617
this.checkInput(array)
1718

19+
this.hashFunction = hashFunction
20+
1821
this.generateTree(array)
1922

2023
}
2124

2225

23-
public verify(proof: string[], leaf: string, rootHash: string, index: number) {
26+
public verify(proof: string[] | number[], leaf: string | number, rootHash: string | number, index: number) {
2427

2528
let hash = leaf
2629

2730
for (let i = 0; i < proof.length; i++) {
2831

2932
if (index % 2 === 0) {
30-
hash = Helper.sha256(`${hash}${proof[i]}`)
33+
if (this.hashFunction === undefined) {
34+
hash = Helper.sha256(`${hash}${proof[i]}`)
35+
} else {
36+
hash = this.hashFunction(`${hash}${proof[i]}`)
37+
}
3138
} else {
32-
hash = Helper.sha256(`${proof[i]}${hash}`)
39+
if (this.hashFunction === undefined) {
40+
hash = Helper.sha256(`${proof[i]}${hash}`)
41+
} else {
42+
hash = this.hashFunction(`${proof[i]}${hash}`)
43+
}
3344
}
3445

3546
index = Math.floor(index / 2)
@@ -95,7 +106,6 @@ export class MerkleTree {
95106

96107
while (itemsOnThisLevel.length > 1) {
97108
itemsOnThisLevel = this.getHashesForLevel(level, itemsOnThisLevel)
98-
99109
this.hashes.push({ level, hashes: itemsOnThisLevel })
100110

101111
level++
@@ -110,9 +120,20 @@ export class MerkleTree {
110120

111121
for (let i = 0; i <= array.length; i++) {
112122
if (level === 0) {
113-
hashesOnThisLevel.push(Helper.sha256(array[i]))
123+
let hash
124+
if (this.hashFunction === undefined) {
125+
hash = Helper.sha256(array[i])
126+
} else {
127+
hash = this.hashFunction(array[i])
128+
}
129+
hashesOnThisLevel.push(hash)
114130
} else if (i % 2 === 0 && i > 0) {
115-
const hash: string = Helper.sha256(`${array[i - 2]}${array[i - 1]}`)
131+
let hash: string = Helper.sha256(`${array[i - 2]}${array[i - 1]}`)
132+
if (this.hashFunction === undefined) {
133+
hash = Helper.sha256(`${array[i - 2]}${array[i - 1]}`)
134+
} else {
135+
hash = this.hashFunction(`${array[i - 2]}${array[i - 1]}`)
136+
}
116137
hashesOnThisLevel.push(hash)
117138
}
118139
}

0 commit comments

Comments
 (0)