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

Replace ethers 5.7 dependency with @metamask/abi-utils and ethereum-cryptography #56

Merged
merged 9 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
714 changes: 304 additions & 410 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
"author": "Francisco Giordano <[email protected]>",
"license": "MIT",
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/constants": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0"
"@metamask/abi-utils": "^2.0.4",
"@metamask/utils": "^9.3.0",
"ethereum-cryptography": "^3.0.0"
},
"devDependencies": {
"@fast-check/ava": "^1.2.1",
Expand Down
6 changes: 3 additions & 3 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BytesLike } from '@ethersproject/bytes';
type HexString = string;
import type { BytesLike } from '@metamask/utils';
type HexString = `0x${string}`;
ernestognw marked this conversation as resolved.
Show resolved Hide resolved

import { arrayify as toBytes, hexlify as toHex, concat } from '@ethersproject/bytes';
import { createBytes as toBytes, createHex as toHex, concatBytes as concat } from '@metamask/utils';

function compare(a: BytesLike, b: BytesLike): number {
const diff = BigInt(toHex(a)) - BigInt(toHex(b));
Expand Down
5 changes: 3 additions & 2 deletions src/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { test, testProp, fc } from '@fast-check/ava';
import { HashZero as zero } from '@ethersproject/constants';
import {
makeMerkleTree,
getProof,
Expand All @@ -9,9 +8,11 @@ import {
isValidMerkleTree,
renderMerkleTree,
} from './core';
import { toHex } from './bytes';
import { HexString, toHex } from './bytes';
import { InvalidArgumentError, InvariantError } from './utils/errors';

const zero: HexString = '0x0000000000000000000000000000000000000000000000000000000000000000';

const leaf = fc.uint8Array({ minLength: 32, maxLength: 32 });
const leaves = fc.array(leaf, { minLength: 1 });
const leavesAndIndex = leaves.chain(xs => fc.tuple(fc.constant(xs), fc.nat({ max: xs.length - 1 })));
Expand Down
6 changes: 3 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function makeMerkleTree(leaves: BytesLike[], nodeHash: NodeHash = standar
export function getProof(tree: BytesLike[], index: number): HexString[] {
checkLeafNode(tree, index);

const proof = [];
const proof: HexString[] = [];
while (index > 0) {
proof.push(toHex(tree[siblingIndex(index)]!));
index = parentIndex(index);
Expand Down Expand Up @@ -67,7 +67,7 @@ export function getMultiProof(tree: BytesLike[], indices: number[]): MultiProof<
);

const stack = Array.from(indices); // copy
const proof = [];
const proof: HexString[] = [];
const proofFlags = [];

while (stack.length > 0 && stack[0]! > 0) {
Expand Down Expand Up @@ -145,7 +145,7 @@ export function isValidMerkleTree(tree: BytesLike[], nodeHash: NodeHash = standa
return tree.length > 0;
}

export function renderMerkleTree(tree: BytesLike[]): HexString {
export function renderMerkleTree(tree: BytesLike[]): string {
validateArgument(tree.length !== 0, 'Expected non-zero number of nodes');

const stack: [number, number[]][] = [[0, []]];
Expand Down
12 changes: 8 additions & 4 deletions src/hashes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { defaultAbiCoder } from '@ethersproject/abi';
import { keccak256 } from '@ethersproject/keccak256';
import { BytesLike, HexString, concat, compare } from './bytes';
import { encode } from '@metamask/abi-utils';
import { keccak256 as _keccak256 } from 'ethereum-cryptography/keccak.js';
import { BytesLike, HexString, toHex, toBytes, concat, compare } from './bytes';

export type LeafHash<T> = (leaf: T) => HexString;
export type NodeHash = (left: BytesLike, right: BytesLike) => HexString;

export function keccak256(input: BytesLike): HexString {
return toHex(_keccak256(toBytes(input)));
}

export function standardLeafHash<T extends any[]>(types: string[], value: T): HexString {
return keccak256(keccak256(defaultAbiCoder.encode(types, value)));
return keccak256(keccak256(encode(types, value)));
}

export function standardNodeHash(a: BytesLike, b: BytesLike): HexString {
Expand Down
7 changes: 4 additions & 3 deletions src/simple.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { test, testProp, fc } from '@fast-check/ava';
import { HashZero as zero } from '@ethersproject/constants';
import { keccak256 } from '@ethersproject/keccak256';
import { SimpleMerkleTree } from './simple';
import { BytesLike, HexString, concat, compare, toHex } from './bytes';
import { keccak256 } from './hashes';
import { InvalidArgumentError, InvariantError } from './utils/errors';

const zero: HexString = '0x0000000000000000000000000000000000000000000000000000000000000000';

fc.configureGlobal({ numRuns: process.env.CI ? 5000 : 100 });

const reverseNodeHash = (a: BytesLike, b: BytesLike): HexString => keccak256(concat([a, b].sort(compare).reverse()));
Expand Down Expand Up @@ -149,7 +150,7 @@ testProp('reject loading dump with wrong node hash', [fc.array(leaf, { minLength
test('reject invalid leaf size', t => {
const invalidLeaf = '0x000000000000000000000000000000000000000000000000000000000000000000';
t.throws(() => SimpleMerkleTree.of([invalidLeaf]), {
message: `incorrect data length (argument=null, value="${invalidLeaf}", code=INVALID_ARGUMENT, version=abi/5.7.0)`,
message: 'Unable to encode value: Expected a value of length 32, but received a value of length 33.',
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/simple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultAbiCoder } from '@ethersproject/abi';
import { encode } from '@metamask/abi-utils';
import { BytesLike, HexString, toHex } from './bytes';
import { MultiProof, processProof, processMultiProof } from './core';
import { MerkleTreeData, MerkleTreeImpl } from './merkletree';
Expand All @@ -16,7 +16,7 @@ export interface SimpleMerkleTreeOptions extends MerkleTreeOptions {
}

export function formatLeaf(value: BytesLike): HexString {
return defaultAbiCoder.encode(['bytes32'], [value]);
return toHex(encode(['bytes32'], [value]));
}

export class SimpleMerkleTree extends MerkleTreeImpl<BytesLike> {
Expand Down
6 changes: 4 additions & 2 deletions src/standard.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { test, testProp, fc } from '@fast-check/ava';
import { HashZero as zero } from '@ethersproject/constants';
import { keccak256 } from '@ethersproject/keccak256';
import { StandardMerkleTree } from './standard';
import { HexString } from './bytes';
import { keccak256 } from './hashes';
import { InvalidArgumentError, InvariantError } from './utils/errors';

const zero: HexString = '0x0000000000000000000000000000000000000000000000000000000000000000';

fc.configureGlobal({ numRuns: process.env.CI ? 5000 : 100 });

const leafEncoding = ['uint256', 'string[]'];
Expand Down
Loading