Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Dec 18, 2023
1 parent d6ba1e0 commit b8c0d74
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// MerkleTree building options
export type MerkleTreeOptions = Partial<{
// Enable or disable sorted leaves. Sorting is strongly recommended for multiproofs.
sortLeaves: boolean;
}>;

// For backward compatibility reasons, leaves are sorted by default.
// This can be disabled for usecases where leaves ordering needs to be preserved
// Recommended (default) options.
// - leaves are sorted by default to facilitate onchain verification of multiproofs.
export const defaultOptions: Required<MerkleTreeOptions> = {
sortLeaves: true,
};

export function withDefault(options: MerkleTreeOptions) : Required<MerkleTreeOptions> {
return { ...defaultOptions, ...options };
}
4 changes: 2 additions & 2 deletions src/standard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { equalsBytes, hexToBytes } from 'ethereum-cryptography/utils';
import { Bytes, compareBytes, hex } from './bytes';
import { getProof, isValidMerkleTree, makeMerkleTree, processProof, renderMerkleTree, MultiProof, getMultiProof, processMultiProof } from './core';
import { MerkleTreeOptions, withDefault } from './options';
import { MerkleTreeOptions, defaultOptions } from './options';
import { checkBounds } from './utils/check-bounds';
import { throwError } from './utils/throw-error';
import { standardLeafHash } from './utils/standard-leaf-hash';
Expand Down Expand Up @@ -32,7 +32,7 @@ export class StandardMerkleTree<T extends any[]> {
}

static of<T extends any[]>(values: T[], leafEncoding: string[], options: MerkleTreeOptions = {}) {
const { sortLeaves } = withDefault(options);
const sortLeaves = options.sortLeaves ?? defaultOptions.sortLeaves;

const hashedValues = values.map((value, valueIndex) => ({ value, valueIndex, hash: standardLeafHash(value, leafEncoding) }));

Expand Down

0 comments on commit b8c0d74

Please sign in to comment.