get layers as object

This commit is contained in:
Miguel Mota 2018-12-09 19:57:31 -08:00
parent ede0c13c3a
commit a781f3b314
4 changed files with 55 additions and 17 deletions

View File

@ -39,6 +39,7 @@ class MerkleTree {
this.createHashes(this.leaves)
}
// TODO: documentation
createHashes(nodes) {
while (nodes.length > 1) {
@ -255,20 +256,16 @@ class MerkleTree {
return Buffer.compare(hash, root) === 0
}
static bufferify(x) {
return bufferify(x)
}
static print(tree, opts) {
opts = opts || {}
const log = opts instanceof Object && opts.log !== false
const layers = tree.getLayers().map(x => x.map(x => x.toString('hex')))
// TODO: documentation
getLayersAsObject() {
const layers = this.getLayers().map(x => x.map(x => x.toString('hex')))
const objs = []
for (let i = 0; i < layers.length; i++) {
const arr = []
for (let j = 0; j < layers[i].length; j++) {
const obj = { [layers[i][j]]: {} }
const obj = { [layers[i][j]]: null }
if (objs.length) {
obj[layers[i][j]] = {}
const a = objs.shift()
const akey = Object.keys(a)[0]
obj[layers[i][j]][akey] = a[akey]
@ -285,10 +282,33 @@ class MerkleTree {
objs.push(...arr)
}
const str = treeify.asTree(objs[0], true)
if (log) console.log(str)
return objs[0]
}
return str
// TODO: documentation
print() {
MerkleTree.print(this)
}
// TODO: documentation
toTreeString() {
const obj = this.getLayersAsObject()
return treeify.asTree(obj, true)
}
// TODO: documentation
toString() {
return this.toTreeString()
}
// TODO: documentation
static bufferify(x) {
return bufferify(x)
}
// TODO: documentation
static print(tree) {
console.log(tree.toString())
}
}
@ -319,7 +339,7 @@ function bufferifyFn (f) {
}
}
function isHexStr(v, size) {
function isHexStr(v) {
return (typeof v === 'string' && /^(0x)?[0-9A-Fa-f]*$/.test(v))
}

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "merkletreejs",
"version": "0.0.19",
"version": "0.0.20",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "merkletreejs",
"version": "0.0.19",
"version": "0.0.20",
"description": "Construct Merkle Trees and verify proofs",
"main": "index.js",
"types": "index.d.ts",

View File

@ -310,14 +310,32 @@ test('crypto-js bufferify', t => {
t.deepEqual(leaves.map(MerkleTree.bufferify), leaves.map(bufferifyCryptoJS))
})
test('getLayersAsObject', t => {
t.plan(1)
const leaves = ['a', 'b', 'c'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha256)
const obj = tree.getLayersAsObject()
t.deepEqual(obj, {
'311d2e46f49b15fff8b746b74ad57f2cc9e0d9939fda94387141a2d3fdf187ae': {
'0b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b2': {
'0b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b2': null
},
'176f0f307632fdd5831875eb709e2f68d770b102262998b214ddeb3f04164ae1': {
'3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb': null,
'b5553de315e0edf504d9150af82dafa5c4667fa618ed0a6f19c69b41166c5510': null
}
}
})
})
test('print', t => {
t.plan(1)
const leaves = ['a', 'b', 'c'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha256)
const str = MerkleTree.print(tree, {log: false})
t.equal(str,
t.equal(tree.toString(),
`└─ 311d2e46f49b15fff8b746b74ad57f2cc9e0d9939fda94387141a2d3fdf187ae
176f0f307632fdd5831875eb709e2f68d770b102262998b214ddeb3f04164ae1
3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb