Skip to content

Instantly share code, notes, and snippets.

@tynes
Last active August 10, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tynes/2032bf211815e1ae7eadda6b888a7e3b to your computer and use it in GitHub Desktop.
Save tynes/2032bf211815e1ae7eadda6b888a7e3b to your computer and use it in GitHub Desktop.
quick urkel tree test
const bcrypto = require('bcrypto');
const random = require('bcrypto/lib/random');
const BN = require('bcrypto/lib/bn.js');
const urkel = require('urkel/radix');
//const urkel = require('urkel/trie');
const {BLAKE2b} = bcrypto;
const {randomBytes} = random;
const {Tree, Proof} = urkel;
const dir = process.cwd();
(async () => {
const tree = new Tree(BLAKE2b, 256, dir);
await tree.open();
const txn = tree.transaction();
const key = Buffer.alloc(32);
const value = randomBytes(300);
let k = new BN(1);
for (let i = 0; i < 256; i++) {
const v = randomBytes(300);
const buf = k.toBuffer('be', 32);
await txn.insert(buf, v);
k.ishl(new BN(1))
}
const root = await txn.commit();
const snapshot = tree.snapshot(root);
const proof = await snapshot.prove(key)
console.log(proof.nodes.length)
})().catch(err => {
console.log(err
});
// npm install urkel bcrypto
const bcrypto = require('bcrypto');
const random = require('bcrypto/lib/random');
const urkel = require('urkel');
const {BLAKE2b} = bcrypto;
const {randomBytes} = random;
const {Tree, Proof} = urkel;
const dir = process.cwd();
(async () => {
const tree = new Tree(BLAKE2b, 256, dir);
await tree.open();
const txn = tree.transaction();
const k1 = Buffer.alloc(32);
const v1 = randomBytes(300);
const k2 = Buffer.alloc(32);
k2[31] = 1;
const v2 = randomBytes(300);
await txn.insert(k1, v1);
await txn.insert(k2, v2);
const root = await txn.commit();
const snapshot = tree.snapshot(root);
const proof = await snapshot.prove(k1);
console.log(proof.nodes.length)
})().catch(err => {
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment