Created
January 28, 2019 15:44
-
-
Save OR13/bf9bf03ce73cdf071f5130aa52c1f696 to your computer and use it in GitHub Desktop.
Sidetree style merkle proof receipts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MerkleTools = require("merkle-tools"); | |
const crypto = require("crypto"); | |
const sha256 = data => { | |
const h = crypto.createHash("sha256"); | |
h.update(data); | |
return h.digest("hex"); | |
}; | |
let treeOptions = { | |
hashType: "sha256" // optional, defaults to 'sha256' | |
}; | |
let operations = ["create:did:123", "update:did:456", "delete:did:789"]; | |
let merkleTools = new MerkleTools(treeOptions); | |
merkleTools.addLeaves(operations, true); | |
merkleTools.makeTree(); | |
const merkleRoot = merkleTools.getMerkleRoot(); | |
const receipts = operations.map(operation => { | |
return { | |
operationHash: sha256(operation), | |
operationProof: merkleTools.getProof(operations.indexOf(operation)) | |
}; | |
}); | |
console.log(JSON.stringify(receipts, null, 2)); | |
const validatedReceipts = receipts.map(r => { | |
return merkleTools.validateProof( | |
r.operationProof, | |
r.operationHash, | |
merkleRoot | |
); | |
}); | |
console.log(validatedReceipts); | |
merkleTools.resetTree(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment