Public
Edited
Nov 21, 2021
Fork of Merkle Trees
1 fork
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
transactions = [{to: 'me', from: 'you', value: 10}, {to: 'you', from: 'me', value: 5}, {to: 'alice', from: 'bob', value: 5}, {to: 'bob', from: 'me', value: 15}, {to: 'jon', from: 'me', value: 15}, {to: 'bob', from: 'sally', value: 1}, {to: 'jan', from: 'sally', value: 105}, {to: 'len', from: 'johanna', value: 1}]
Insert cell
Insert cell
Insert cell
leaves = transactions.map(x => sha256(JSON.stringify(x)))
Insert cell
Insert cell
// For demonstration purposes only!!

function createMerkleTree(nodes){
// if the nodes.length is greater than 1, we have not yet found the root, so loop through the the leaves and compute the hashes
if (nodes.length > 1) {
let newNodes = []
let leafNodes = []

// if leaf nodes, format them
if(typeof nodes[0] =='string'){
nodes.forEach((value, index, array) => {
leafNodes.push({hash: value})
})
nodes = leafNodes
}
// if odd number of nodes, duplicate the last one
// reference: https://ethereum.stackexchange.com/questions/29286/can-a-mined-block-have-an-odd-number-of-transactions-if-so-how-does-the-merkle
if(nodes.length % 2 == 1){
nodes.push(nodes[nodes.length - 1])
}
// iterate over the nodes, hashing pairs into a single node 1 level up
nodes.forEach((value, index, array) => {
if(index % 2 == 0){
newNodes.push({hash: sha256(value.hash + array[index + 1].hash), children: [value, array[index+1]]})
}
})
return createMerkleTree(newNodes)
} else {
// if the nodes.length === 1, it's the root, so let's return it
return nodes[0]
}
}
Insert cell
Insert cell
simpleMerkleTree = createMerkleTree(leaves, 0)
Insert cell
Insert cell
Insert cell
function verify(transaction, orderedHashes){
// get the txHash
let txHash = sha256(JSON.stringify(transaction))
let hash = txHash
for(var i = 0; i < orderedHashes.length; i++){
hash = sha256(hash + orderedHashes[i])
}
return (hash === simpleMerkleTree.hash)
}
Insert cell
Insert cell
verify(transactions[0], [sha256(JSON.stringify(transactions[1])), sha256(sha256(JSON.stringify(transactions[2])) + sha256(JSON.stringify(transactions[3]))), sha256(sha256(sha256(JSON.stringify(transactions[4])) + sha256(JSON.stringify(transactions[5]))) + sha256(sha256(JSON.stringify(transactions[6])) + sha256(JSON.stringify(transactions[7]))))])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 80, right: 10, bottom: 10, left: 10})
Insert cell
tree = d3.tree().nodeSize([dx, dy]).size([800, 200])
Insert cell
dy = 10
Insert cell
dx = 70
Insert cell
data = simpleMerkleTree
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more