Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify leaf hash generation for merkle tree creation and verification #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { MerkleTree } = require('merkletreejs');
const SHA256 = require('crypto-js/sha256');
const LEAVES_HASH_LEN = 16;
const DELIMITER = '\t';
const NEW_LINE = '\r\n';
const NEW_LINE = '\n';

let rawFile, merkleFile;
/**
Expand Down Expand Up @@ -84,35 +84,34 @@ function createMerkle(UserBalance, fileName) {

// process input file
const content = UserBalance;
var list = content.split(NEW_LINE); // read UID and balance from input file
var balances_hash = [];
var list = content.split(/\r?\n/); // read UID and balance from input file
var leaves = [];
var total_balance = 0;
var negative_balance = 0;
for (var i = 0; i < list.length; i++) {
var row = list[i];
if (row[0] == '#') continue;
var data = row.split(',');
if (data.length != 2) continue;
var uid = data[0];
var balance = data[1];
let uid = data[0];
let balance = Number(data[1]);
if (balance === 0) continue;
if (balance < 0) {
negative_balance -= balance;
}
total_balance += balance * 1; // calculate total balance

//concatenate hashed uid and balance to form transaction data
var uid_hash = SHA256(uid);
var balance_hash = SHA256(balance);
balances_hash.push(uid_hash + balance_hash); // underlying data to build Merkle tree
let leafStr = generateAccountHash(uid, balance)
leaves.push(leafStr); // underlying data to build Merkle tree

if (i % 10000 == 0) {
console.log("users:" + i + "; balances:" + total_balance);
}
}

console.log('number of balances hash: ' + balances_hash.length);
// construct leaves and shorten hashed value in leaves
const leaves = balances_hash.map(x =>
SHA256(x)
.toString()
.substring(0, LEAVES_HASH_LEN)
);
console.log('number of balances hash: ' + leaves.length);

// build Merlke tree
const tree = new MerkleTree(leaves, SHA256);

Expand Down Expand Up @@ -140,6 +139,7 @@ function createMerkle(UserBalance, fileName) {

$('.rootHash').html(bufferToString(tree.getRoot()));
$('.userNums').html(leavesFromTree.length);
$('.negativeBalance').html(negative_balance);
$('.totalBalance').html(total_balance);
}

Expand All @@ -163,13 +163,7 @@ function verifyMerkle(VerifyTXT, params) {
}

// compute the hashed value with given uid and balance
let uid = params.uid;
let balance = Number(params.balance).toFixed(8);
var uid_hash = SHA256(uid);
var balance_hash = SHA256(balance);
let leafStr = SHA256(uid_hash + balance_hash)
.toString()
.substring(0, LEAVES_HASH_LEN);
let leafStr = generateAccountHash(params.uid, params.balance);

// process input value
var content = VerifyTXT;
Expand Down Expand Up @@ -222,3 +216,19 @@ function verifyMerkle(VerifyTXT, params) {
$('.result').html('Could not find your information in the Merkle Tree.');
}
}

/**
* receive unformatted input data, and generate standard hash used for Merkle tree generation and verification
*
* @param uid the user hash id
* @param balance the user account balance
* @returns {string} the hash to be used as the Merkle tree leaf
*/
function generateAccountHash(uid, balance) {
let balance_satoshis = Number(balance).toFixed(8);
var uid_hash = SHA256(uid);
var balance_hash = SHA256(balance_satoshis);
return SHA256(uid_hash + balance_hash)
.toString()
.substring(0, LEAVES_HASH_LEN);
}
1 change: 1 addition & 0 deletions generator.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<div class="showBox">
<b>Merkle RootHash:</b> <span class="rootHash"></span> <br>
<b>User Count:</b> <span class="userNums"></span> <br>
<b>Negative Amount:</b> <span class="negativeBalance"></span> <br>
<b>Total Amount:</b> <span class="totalBalance"></span>
</div>
</div>
Expand Down