@iden3/js-merkletree
Version:
javascript sparse merkle tree library
22 lines (21 loc) • 812 B
JavaScript
// LeafKey computes the key of a leaf node given the hIndex and hValue of the
// entry of the leaf.
import { hashElemsKey } from '../hash/hash';
import { NODE_VALUE_BYTE_ARR_LENGTH } from '../../constants';
import { bigIntToUINT8Array } from './bigint';
export const leafKey = async (k, v) => {
return hashElemsKey(BigInt(1), [k.bigInt(), v.bigInt()]);
};
export const nodeValue = (type, a, b) => {
const bytes = new Uint8Array(NODE_VALUE_BYTE_ARR_LENGTH);
const kBytes = bigIntToUINT8Array(a.bigInt());
const vBytes = bigIntToUINT8Array(b.bigInt());
bytes[0] = type;
for (let idx = 1; idx < 33; idx += 1) {
bytes[idx] = kBytes[idx - 1];
}
for (let idx = 33; idx <= NODE_VALUE_BYTE_ARR_LENGTH; idx += 1) {
bytes[idx] = vBytes[idx - 33];
}
return bytes;
};