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

Issues implementing custom nodeHash without etherjs dependency #52

Open
kevzzsk opened this issue Aug 16, 2024 · 2 comments
Open

Issues implementing custom nodeHash without etherjs dependency #52

kevzzsk opened this issue Aug 16, 2024 · 2 comments

Comments

@kevzzsk
Copy link

kevzzsk commented Aug 16, 2024

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?

@frangio
Copy link
Contributor

frangio commented Aug 17, 2024

[...] 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.

@kevzzsk
Copy link
Author

kevzzsk commented Aug 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants