64 lines
1.4 KiB
JavaScript
64 lines
1.4 KiB
JavaScript
const fs = require('fs').promises;
|
|
|
|
const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
|
|
const BigNumber = require('bignumber.js');
|
|
|
|
function h(num) {
|
|
return "0x"+BigNumber(num).toString(16);
|
|
}
|
|
|
|
async function getSiblings(tree, key) {
|
|
const res = await tree.find(key);
|
|
|
|
let siblings = res.siblings
|
|
.map((x) => tree.F.toObject(x))
|
|
.map(h);
|
|
while (siblings.length<10) siblings.push(0);
|
|
return siblings;
|
|
}
|
|
|
|
(async function() {
|
|
let from = 10;
|
|
let to = 11;
|
|
|
|
let fromBalance = 20;
|
|
let toBalance = 30;
|
|
let amount = 5
|
|
|
|
// tree init
|
|
let tree = await newMemEmptyTrie();
|
|
await tree.insert(from, fromBalance);
|
|
await tree.insert(to, toBalance);
|
|
|
|
const f = (x) => tree.F.toObject(x);
|
|
|
|
const oldRoot = f(tree.root);
|
|
const fromSiblings = await getSiblings(tree, from);
|
|
|
|
await tree.update(from, fromBalance - amount);
|
|
|
|
const midRoot = f(tree.root);
|
|
const toSiblings = await getSiblings(tree, to);
|
|
await tree.update(to, toBalance + amount);
|
|
|
|
const newRoot = f(tree.root);
|
|
|
|
const inputs = {
|
|
oldRoot: h(oldRoot),
|
|
from: from,
|
|
amount: amount,
|
|
to: to,
|
|
signature: 0,
|
|
fromBalance: fromBalance,
|
|
fromSiblings: fromSiblings,
|
|
toBalance: toBalance,
|
|
toSiblings: toSiblings,
|
|
}
|
|
|
|
const asStr = JSON.stringify(inputs, null, 2);
|
|
console.log(asStr)
|
|
console.log("new root: ", newRoot)
|
|
await fs.writeFile("./account_change.json", asStr)
|
|
})()
|
|
.catch(console.log);
|