merkle-tree
merkle-tree copied to clipboard
Issues implementing custom nodeHash without etherjs dependency
I am trying to create a merkle tree outside of EVM context so I am using SimpleMerkleTree. While the consumer are free to implement the leafHash function, the nodeHash is proving to be difficult to deal with without depending on @ethersproject/bytes package because of the type BytesLike.
for instance,
const v1 = sha256(Buffer.from("8d6cd2a958c38796bbf791ef81835a92e0c85539771b37a12cd249886daf2136", "hex"));
const v2 = sha256(Buffer.from("cde8ec76677273a270af18285503ce3075b59f7921c0906bee59366bc9d4b8b3", "hex"));
const tree2 = SimpleMerkleTree.of([v1, v2], {
nodeHash: (left, right) => sha256(Buffer.concat([Buffer.from(left), Buffer.from(right)])).toString("hex"),
});
There is a typing issue because BytesLike cannot be converted easily to buffer
is there any ways to write custom nodeHash function with sha256 and does not have dependency on the etherjs lib?
[...] and does not have dependency on the etherjs lib?
Note that by using this library you already have a dependency on @ethersproject/bytes so I would do something like:
import { arrayify } from '@ethersproject/bytes';
...
nodeHash: (left, right) => sha256(Buffer.concat([arrayify(left), arrayify(right)])).toString("hex"),
Also note that this is not a dependency on all of ethers.js. The bytes package is small.
What sha256 dependency are you using?
I do think that nodeHash could have a better type, because BytesLike is a union type and it would be much simpler for the user to have to handle just one type of argument. My preference is consolidating on Uint8Array like the ethereum-cryptography library.
Note that by using this library you already have a dependency on @ethersproject/bytes
yeap, but I will still need to install @ethersproject/bytes as direct dependency. I prefer not to have that direct dependency. Will be good if this library also export some of the utils function necessary to work with BytesLike type?
What sha256 dependency are you using?
I am using sha256 from bitcoinjs, which expect a Buffer type input