Skip to content

Commit

Permalink
more formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Feb 22, 2024
1 parent a97a469 commit ed1e96a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { HexString } from "./bytes";

// Dump/Load format
export type MerkleTreeData<T> = {
format: 'standard-v1' | 'custom-v1';
format: 'standard-v1' | 'simple-v1' | 'custom-v1';

Check warning on line 5 in src/format.ts

View check run for this annotation

Codecov / codecov/patch

src/format.ts#L5

Added line #L5 was not covered by tests
tree: HexString[];
values: {
value: T;
Expand Down
12 changes: 8 additions & 4 deletions src/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@ describe('simple merkle tree', () => {
() => SimpleMerkleTree.load({ format: 'nonstandard' } as any),
/^Error: Unknown format 'nonstandard'$/,
);
assert.throws(
() => SimpleMerkleTree.load({ format: 'standard-v1'} as any, reverseHashPair),
/^Error: Unknown format 'standard-v1'$/,
);
});

it('reject standard tree dump with a custom hash', () => {
assert.throws(
() => SimpleMerkleTree.load({ format: 'standard-v1'} as any, reverseHashPair),
/^Error: Format 'standard-v1' does not support custom hashing functions$/,
() => SimpleMerkleTree.load({ format: 'simple-v1'} as any, reverseHashPair),
/^Error: Format 'simple-v1' does not support custom hashing functions$/,
);
});

Expand All @@ -179,7 +183,7 @@ describe('simple merkle tree', () => {

it('reject malformed tree dump', () => {
const loadedTree1 = SimpleMerkleTree.load({
format: 'standard-v1',
format: 'simple-v1',
tree: [zero],
values: [{ value: '0x0000000000000000000000000000000000000000000000000000000000000001', treeIndex: 0 }],
});
Expand All @@ -189,7 +193,7 @@ describe('simple merkle tree', () => {
);

const loadedTree2 = SimpleMerkleTree.load({
format: 'standard-v1',
format: 'simple-v1',
tree: [zero, zero, zero],
values: [{ value: zero, treeIndex: 2 }],
});
Expand Down
4 changes: 2 additions & 2 deletions src/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class SimpleMerkleTree {

static load(data: MerkleTreeData<BytesLike>, hashPair ?: HashPairFn): SimpleMerkleTree {
switch (data.format) {
case 'standard-v1':
case 'simple-v1':
if (hashPair !== undefined) throwError(`Format '${data.format}' does not support custom hashing functions`);
break;
case 'custom-v1':
Expand Down Expand Up @@ -106,7 +106,7 @@ export class SimpleMerkleTree {

dump(): MerkleTreeData<BytesLike> {
return {
format: this.hashPair === undefined ? 'standard-v1' : 'custom-v1',
format: this.hashPair === undefined ? 'simple-v1' : 'custom-v1',
tree: this.tree,
values: this.values,
};
Expand Down
8 changes: 8 additions & 0 deletions src/standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ describe('standard merkle tree', () => {
() => StandardMerkleTree.load({ format: 'nonstandard' } as any),
/^Error: Unknown format 'nonstandard'$/,
);
assert.throws(
() => StandardMerkleTree.load({ format: 'simple-v1' } as any),
/^Error: Unknown format 'simple-v1'$/,
);
assert.throws(
() => StandardMerkleTree.load({ format: 'custom-v1' } as any),
/^Error: Unknown format 'custom-v1'$/,
);
});

it('reject malformed tree dump', () => {
Expand Down

0 comments on commit ed1e96a

Please sign in to comment.