Skip to content

Commit

Permalink
Merge pull request #1 from sethfork/sha3-keccak
Browse files Browse the repository at this point in the history
Replace sha3 with keccak; Update ethereumjs-util
  • Loading branch information
sethfork authored Jun 30, 2019
2 parents e8964cd + ffad24b commit 6ac1097
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 98 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function sha256(data) {
return crypto.createHash('sha256').update(data).digest()
}

const leaves = ['a', 'b', 'c'].map(x => sha3(x))
const leaves = ['a', 'b', 'c'].map(x => keccak(x))

const tree = new MerkleTree(leaves, sha256)
```
Expand Down Expand Up @@ -323,8 +323,8 @@ const proof = tree.getProof(leaves[2])

*__example__*:
```js
const leaves = ['a', 'b', 'a'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha3)
const leaves = ['a', 'b', 'a'].map(x => keccak(x))
const tree = new MerkleTree(leaves, keccak)
const proof = tree.getProof(leaves[2], 2)
```

Expand Down
6 changes: 3 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export declare class MerkleTree {
* return crypto.createHash('sha256').update(data).digest()
*}
*
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
*const leaves = ['a', 'b', 'c'].map(x => keccak(x))
*
*const tree = new MerkleTree(leaves, sha256)
*```
Expand Down Expand Up @@ -96,8 +96,8 @@ export declare class MerkleTree {
*
* @example
*```js
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
*const tree = new MerkleTree(leaves, sha3)
*const leaves = ['a', 'b', 'a'].map(x => keccak(x))
*const tree = new MerkleTree(leaves, keccak)
*const proof = tree.getProof(leaves[2], 2)
*```
*/
Expand Down
6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var MerkleTree = /** @class */ (function () {
* return crypto.createHash('sha256').update(data).digest()
*}
*
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
*const leaves = ['a', 'b', 'c'].map(x => keccak(x))
*
*const tree = new MerkleTree(leaves, sha256)
*```
Expand Down Expand Up @@ -159,8 +159,8 @@ var MerkleTree = /** @class */ (function () {
*
* @example
*```js
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
*const tree = new MerkleTree(leaves, sha3)
*const leaves = ['a', 'b', 'a'].map(x => keccak(x))
*const tree = new MerkleTree(leaves, keccak)
*const proof = tree.getProof(leaves[2], 2)
*```
*/
Expand Down
32 changes: 16 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Options {
*/
export class MerkleTree {
duplicateOdd: boolean
hashAlgo: (value:any) => any
hashAlgo: (value: any) => any
hashLeaves: boolean
isBitcoinTree: boolean
leaves: any[]
Expand All @@ -50,12 +50,12 @@ export class MerkleTree {
* return crypto.createHash('sha256').update(data).digest()
*}
*
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
*const leaves = ['a', 'b', 'c'].map(x => keccak(x))
*
*const tree = new MerkleTree(leaves, sha256)
*```
*/
constructor(leaves, hashAlgorithm, options:Options={} as any) {
constructor(leaves, hashAlgorithm, options: Options = {} as any) {
this.isBitcoinTree = !!options.isBitcoinTree
this.hashLeaves = !!options.hashLeaves
this.sortLeaves = !!options.sortLeaves
Expand Down Expand Up @@ -92,9 +92,9 @@ export class MerkleTree {

for (let i = 0; i < nodes.length; i += 2) {

if (i+1 === nodes.length) {
if (i + 1 === nodes.length) {
if (nodes.length % 2 === 1) {
let data = nodes[nodes.length-1]
let data = nodes[nodes.length - 1]
let hash = data

// is bitcoin tree
Expand Down Expand Up @@ -183,7 +183,7 @@ export class MerkleTree {
*```
*/
getRoot() {
return this.layers[this.layers.length-1][0] || Buffer.from([])
return this.layers[this.layers.length - 1][0] || Buffer.from([])
}

// TODO: documentation
Expand All @@ -206,8 +206,8 @@ export class MerkleTree {
*
* @example
*```js
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
*const tree = new MerkleTree(leaves, sha3)
*const leaves = ['a', 'b', 'a'].map(x => keccak(x))
*const tree = new MerkleTree(leaves, keccak)
*const proof = tree.getProof(leaves[2], 2)
*```
*/
Expand Down Expand Up @@ -235,8 +235,8 @@ export class MerkleTree {
for (let i = 0; i < this.layers.length - 1; i++) {
const layer = this.layers[i]
const isRightNode = index % 2
const pairIndex = (isRightNode ? index - 1: index)
const position = isRightNode ? 'left': 'right'
const pairIndex = (isRightNode ? index - 1 : index)
const position = isRightNode ? 'left' : 'right'

if (pairIndex < layer.length) {
proof.push({
Expand All @@ -245,7 +245,7 @@ export class MerkleTree {
}

// set index to parent index
index = (index / 2)|0
index = (index / 2) | 0
}

return proof
Expand All @@ -260,13 +260,13 @@ export class MerkleTree {

if (pairIndex < layer.length) {
proof.push({
position: isRightNode ? 'left': 'right',
position: isRightNode ? 'left' : 'right',
data: layer[pairIndex]
})
}

// set index to parent index
index = (index / 2)|0
index = (index / 2) | 0

}

Expand Down Expand Up @@ -406,8 +406,8 @@ export class MerkleTree {
}
}

function bufferToHex(value:Buffer) {
return '0x'+value.toString('hex')
function bufferToHex(value: Buffer) {
return '0x' + value.toString('hex')
}

function bufferify(x) {
Expand All @@ -425,7 +425,7 @@ function bufferify(x) {
return x
}

function bufferifyFn (f) {
function bufferifyFn(f) {
return function (x) {
const v = f(x)
if (Buffer.isBuffer(v)) {
Expand Down
83 changes: 47 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@types/node": "^11.12.1",
"crypto": "0.0.3",
"ethereumjs-util": "^5.1.2",
"ethereumjs-util": "6.1.0",
"sha1": "^1.1.1",
"tape": "^4.9.2",
"typedoc": "^0.14.2",
Expand Down
Loading

0 comments on commit 6ac1097

Please sign in to comment.