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

feat: optional encoder arg for standardLeafHash #47

Open
wants to merge 1 commit 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
11 changes: 9 additions & 2 deletions src/hashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { BytesLike, HexString, concat, compare } from './bytes';

export type LeafHash<T> = (leaf: T) => HexString;
export type NodeHash = (left: BytesLike, right: BytesLike) => HexString;
export type Encoder = {
encode: (types: string[], values: any[]) => string;
};

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

export function standardNodeHash(a: BytesLike, b: BytesLike): HexString {
Expand Down
26 changes: 26 additions & 0 deletions src/standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HashZero as zero } from '@ethersproject/constants';
import { keccak256 } from '@ethersproject/keccak256';
import { StandardMerkleTree } from './standard';
import { InvalidArgumentError, InvariantError } from './utils/errors';
import { defaultAbiCoder } from '@ethersproject/abi';

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

Expand Down Expand Up @@ -149,3 +150,28 @@ test('reject malformed tree dump', t => {
new InvariantError('Merkle tree is invalid'),
);
});

const customEncoderLeaf = fc.tuple(
fc.uint8Array({ minLength: 1, maxLength: 1 }),
fc.uint8Array({ minLength: 1, maxLength: 1 }),
);
const customEncoderLeaves = fc.array(customEncoderLeaf, { minLength: 1, maxLength: 1000 });

testProp('custom encoder', [customEncoderLeaves], (t, customEncoderLeaves) => {
const customEncoder = {
encode: (types: string[], values: any[]) => {
return defaultAbiCoder.encode(types.slice().reverse(), values);
},
};

const customEncoderEncoding = ['bytes', 'bytes1'];

const customEncoderTree = StandardMerkleTree.of(customEncoderLeaves, customEncoderEncoding, {}, customEncoder);
const reversedEncodingTree = StandardMerkleTree.of(customEncoderLeaves, customEncoderEncoding.slice().reverse());

t.deepEqual(customEncoderTree.root, reversedEncodingTree.root);

const defaultEncodingTree = StandardMerkleTree.of(customEncoderLeaves, customEncoderEncoding);

t.notDeepEqual(customEncoderTree.root, defaultEncodingTree.root);
});
33 changes: 24 additions & 9 deletions src/standard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defaultAbiCoder } from '@ethersproject/abi';
import { BytesLike, HexString, toHex } from './bytes';
import { MultiProof, processProof, processMultiProof } from './core';
import { MerkleTreeData, MerkleTreeImpl } from './merkletree';
import { MerkleTreeOptions } from './options';
import { standardLeafHash } from './hashes';
import { Encoder, standardLeafHash } from './hashes';
import { validateArgument } from './utils/errors';

export interface StandardMerkleTreeData<T extends any[]> extends MerkleTreeData<T> {
Expand All @@ -15,44 +16,58 @@ export class StandardMerkleTree<T extends any[]> extends MerkleTreeImpl<T> {
protected readonly tree: HexString[],
protected readonly values: StandardMerkleTreeData<T>['values'],
protected readonly leafEncoding: string[],
protected readonly encoder: Encoder = defaultAbiCoder,
) {
super(tree, values, leaf => standardLeafHash(leafEncoding, leaf));
super(tree, values, leaf => standardLeafHash(leafEncoding, leaf, encoder));
}

static of<T extends any[]>(
values: T[],
leafEncoding: string[],
options: MerkleTreeOptions = {},
encoder: Encoder = defaultAbiCoder,
): StandardMerkleTree<T> {
// use default nodeHash (standardNodeHash)
const [tree, indexedValues] = MerkleTreeImpl.prepare(values, options, leaf => standardLeafHash(leafEncoding, leaf));
return new StandardMerkleTree(tree, indexedValues, leafEncoding);
const [tree, indexedValues] = MerkleTreeImpl.prepare(values, options, leaf =>
standardLeafHash(leafEncoding, leaf, encoder),
);
return new StandardMerkleTree(tree, indexedValues, leafEncoding, encoder);
}

static load<T extends any[]>(data: StandardMerkleTreeData<T>): StandardMerkleTree<T> {
static load<T extends any[]>(
data: StandardMerkleTreeData<T>,
encoder: Encoder = defaultAbiCoder,
): StandardMerkleTree<T> {
validateArgument(data.format === 'standard-v1', `Unknown format '${data.format}'`);
validateArgument(data.leafEncoding !== undefined, 'Expected leaf encoding');

const tree = new StandardMerkleTree(data.tree, data.values, data.leafEncoding);
const tree = new StandardMerkleTree(data.tree, data.values, data.leafEncoding, encoder);
tree.validate();
return tree;
}

static verify<T extends any[]>(root: BytesLike, leafEncoding: string[], leaf: T, proof: BytesLike[]): boolean {
static verify<T extends any[]>(
root: BytesLike,
leafEncoding: string[],
leaf: T,
proof: BytesLike[],
encoder: Encoder = defaultAbiCoder,
): boolean {
// use default nodeHash (standardNodeHash) for processProof
return toHex(root) === processProof(standardLeafHash(leafEncoding, leaf), proof);
return toHex(root) === processProof(standardLeafHash(leafEncoding, leaf, encoder), proof);
}

static verifyMultiProof<T extends any[]>(
root: BytesLike,
leafEncoding: string[],
multiproof: MultiProof<BytesLike, T>,
encoder: Encoder = defaultAbiCoder,
): boolean {
// use default nodeHash (standardNodeHash) for processMultiProof
return (
toHex(root) ===
processMultiProof({
leaves: multiproof.leaves.map(leaf => standardLeafHash(leafEncoding, leaf)),
leaves: multiproof.leaves.map(leaf => standardLeafHash(leafEncoding, leaf, encoder)),
proof: multiproof.proof,
proofFlags: multiproof.proofFlags,
})
Expand Down