Skip to content

Commit

Permalink
Merge pull request #34 from confio/export-specs-in-js
Browse files Browse the repository at this point in the history
Export specs in js
  • Loading branch information
ethanfrey authored Aug 17, 2020
2 parents 65ad804 + 3cb1bc2 commit f173e62
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@confio/ics23",
"version": "0.6.0",
"version": "0.6.3",
"description": "Merkle proof verification library - implements Cosmos ICS23 Spec",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export { verifyMembership, verifyNonMembership } from "./ics23";
export {
calculateExistenceRoot,
CommitmentRoot,
iavlSpec,
tendermintSpec,
verifyExistence,
verifyNonExistence
} from "./proofs";
22 changes: 11 additions & 11 deletions js/src/proofs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ics23 } from "./generated/codecimpl";

import { fromHex, toAscii } from "./helpers";
import { calculateExistenceRoot, ensureSpec, IavlSpec } from "./proofs";
import { calculateExistenceRoot, ensureSpec, iavlSpec } from "./proofs";

describe("calculateExistenceRoot", () => {
it("must have at least one step", () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe("calculateExistenceRoot", () => {
});

describe("ensureSpec", () => {
const validLeaf = IavlSpec.leafSpec;
const validLeaf = iavlSpec.leafSpec;
const invalidLeaf = {
prefix: Uint8Array.from([0]),
hash: ics23.HashOp.SHA512,
Expand All @@ -87,8 +87,8 @@ describe("ensureSpec", () => {
};

const depthLimitedSpec = {
leafSpec: IavlSpec.leafSpec,
innerSpec: IavlSpec.innerSpec,
leafSpec: iavlSpec.leafSpec,
innerSpec: iavlSpec.innerSpec,
minDepth: 2,
maxDepth: 4
};
Expand All @@ -98,7 +98,7 @@ describe("ensureSpec", () => {
key: toAscii("foo"),
value: toAscii("bar")
};
expect(() => ensureSpec(proof, IavlSpec)).toThrow();
expect(() => ensureSpec(proof, iavlSpec)).toThrow();
});

it("accepts one valid leaf", () => {
Expand All @@ -108,7 +108,7 @@ describe("ensureSpec", () => {
leaf: validLeaf
};
// fail if this throws (invalid spec)
ensureSpec(proof, IavlSpec);
ensureSpec(proof, iavlSpec);
});

it("rejects invalid leaf", () => {
Expand All @@ -117,7 +117,7 @@ describe("ensureSpec", () => {
value: toAscii("bar"),
leaf: invalidLeaf
};
expect(() => ensureSpec(proof, IavlSpec)).toThrow();
expect(() => ensureSpec(proof, iavlSpec)).toThrow();
});

it("rejects inner without leaf", () => {
Expand All @@ -126,7 +126,7 @@ describe("ensureSpec", () => {
value: toAscii("bar"),
path: [validInner]
};
expect(() => ensureSpec(proof, IavlSpec)).toThrow();
expect(() => ensureSpec(proof, iavlSpec)).toThrow();
});

it("accepts leaf with one inner", () => {
Expand All @@ -137,7 +137,7 @@ describe("ensureSpec", () => {
path: [validInner]
};
// fail if this throws (invalid spec)
ensureSpec(proof, IavlSpec);
ensureSpec(proof, iavlSpec);
});

it("rejects with invalid inner (prefix)", () => {
Expand All @@ -147,7 +147,7 @@ describe("ensureSpec", () => {
leaf: validLeaf,
path: [invalidInner, validInner]
};
expect(() => ensureSpec(proof, IavlSpec)).toThrow();
expect(() => ensureSpec(proof, iavlSpec)).toThrow();
});

it("rejects with invalid inner (hash)", () => {
Expand All @@ -157,7 +157,7 @@ describe("ensureSpec", () => {
leaf: validLeaf,
path: [validInner, invalidInnerHash]
};
expect(() => ensureSpec(proof, IavlSpec)).toThrow();
expect(() => ensureSpec(proof, iavlSpec)).toThrow();
});

it("accepts depth limited with proper number of nodes", () => {
Expand Down
4 changes: 2 additions & 2 deletions js/src/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ensureLeaf
} from "./specs";

export const IavlSpec: ics23.IProofSpec = {
export const iavlSpec: ics23.IProofSpec = {
leafSpec: {
prefix: Uint8Array.from([0]),
hash: ics23.HashOp.SHA256,
Expand All @@ -25,7 +25,7 @@ export const IavlSpec: ics23.IProofSpec = {
}
};

export const TendermintSpec: ics23.IProofSpec = {
export const tendermintSpec: ics23.IProofSpec = {
leafSpec: {
prefix: Uint8Array.from([0]),
hash: ics23.HashOp.SHA256,
Expand Down
38 changes: 19 additions & 19 deletions js/src/testvectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
verifyMembership,
verifyNonMembership
} from "./ics23";
import { IavlSpec, TendermintSpec } from "./proofs";
import { iavlSpec, tendermintSpec } from "./proofs";

describe("calculateExistenceRoot", () => {
interface RefData {
Expand Down Expand Up @@ -61,58 +61,58 @@ describe("calculateExistenceRoot", () => {
}

it("should parse iavl left", () => {
validateTestVector("../testdata/iavl/exist_left.json", IavlSpec);
validateTestVector("../testdata/iavl/exist_left.json", iavlSpec);
});
it("should parse iavl right", () => {
validateTestVector("../testdata/iavl/exist_right.json", IavlSpec);
validateTestVector("../testdata/iavl/exist_right.json", iavlSpec);
});
it("should parse iavl middle", () => {
validateTestVector("../testdata/iavl/exist_middle.json", IavlSpec);
validateTestVector("../testdata/iavl/exist_middle.json", iavlSpec);
});
it("should parse iavl left - nonexist", () => {
validateTestVector("../testdata/iavl/nonexist_left.json", IavlSpec);
validateTestVector("../testdata/iavl/nonexist_left.json", iavlSpec);
});
it("should parse iavl right - nonexist", () => {
validateTestVector("../testdata/iavl/nonexist_right.json", IavlSpec);
validateTestVector("../testdata/iavl/nonexist_right.json", iavlSpec);
});
it("should parse iavl middle - nonexist", () => {
validateTestVector("../testdata/iavl/nonexist_middle.json", IavlSpec);
validateTestVector("../testdata/iavl/nonexist_middle.json", iavlSpec);
});

it("should parse tendermint left", () => {
validateTestVector(
"../testdata/tendermint/exist_left.json",
TendermintSpec
tendermintSpec
);
});
it("should parse tendermint right", () => {
validateTestVector(
"../testdata/tendermint/exist_right.json",
TendermintSpec
tendermintSpec
);
});
it("should parse tendermint middle", () => {
validateTestVector(
"../testdata/tendermint/exist_middle.json",
TendermintSpec
tendermintSpec
);
});
it("should parse tendermint left - nonexist", () => {
validateTestVector(
"../testdata/tendermint/nonexist_left.json",
TendermintSpec
tendermintSpec
);
});
it("should parse tendermint right - nonexist", () => {
validateTestVector(
"../testdata/tendermint/nonexist_right.json",
TendermintSpec
tendermintSpec
);
});
it("should parse tendermint middle - nonexist", () => {
validateTestVector(
"../testdata/tendermint/nonexist_middle.json",
TendermintSpec
tendermintSpec
);
});

Expand Down Expand Up @@ -172,7 +172,7 @@ describe("calculateExistenceRoot", () => {
"../testdata/iavl/nonexist_right.json",
"../testdata/iavl/nonexist_middle.json"
]);
validateBatch(proof, IavlSpec, data[0]);
validateBatch(proof, iavlSpec, data[0]);
});

it("should validate iavl batch nonexist", () => {
Expand All @@ -184,7 +184,7 @@ describe("calculateExistenceRoot", () => {
"../testdata/iavl/nonexist_right.json",
"../testdata/iavl/nonexist_middle.json"
]);
validateBatch(proof, IavlSpec, data[5]);
validateBatch(proof, iavlSpec, data[5]);
});

it("should validate compressed iavl batch exist", () => {
Expand All @@ -205,7 +205,7 @@ describe("calculateExistenceRoot", () => {
expect(origBin).toEqual(origBin2);
expect(origBin).not.toEqual(smallBin);

validateBatch(small, IavlSpec, data[0]);
validateBatch(small, iavlSpec, data[0]);
});

it("should validate compressed iavl batch nonexist", () => {
Expand All @@ -226,7 +226,7 @@ describe("calculateExistenceRoot", () => {
expect(origBin).toEqual(origBin2);
expect(origBin).not.toEqual(smallBin);

validateBatch(small, IavlSpec, data[5]);
validateBatch(small, iavlSpec, data[5]);
});

it("should validate tendermint batch exist", () => {
Expand All @@ -238,7 +238,7 @@ describe("calculateExistenceRoot", () => {
"../testdata/tendermint/nonexist_right.json",
"../testdata/tendermint/nonexist_middle.json"
]);
validateBatch(proof, TendermintSpec, data[2]);
validateBatch(proof, tendermintSpec, data[2]);
});

it("should validate tendermint batch nonexist", () => {
Expand All @@ -250,6 +250,6 @@ describe("calculateExistenceRoot", () => {
"../testdata/tendermint/nonexist_right.json",
"../testdata/tendermint/nonexist_middle.json"
]);
validateBatch(proof, TendermintSpec, data[3]);
validateBatch(proof, tendermintSpec, data[3]);
});
});

0 comments on commit f173e62

Please sign in to comment.