Skip to content

Commit

Permalink
feat: upgrade to open attestation v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulis committed Dec 18, 2019
1 parent e2f888d commit cc42bce
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 90 deletions.
71 changes: 45 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@commitlint/prompt": "8.2.0",
"@ls-age/commitlint-circle": "1.0.0",
"@types/jest": "^24.0.18",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.6.0",
"@typescript-eslint/eslint-plugin": "1.13.0",
"@typescript-eslint/parser": "1.13.0",
"commitizen": "4.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
Expand All @@ -41,10 +41,10 @@
"eslint-plugin-prettier": "^3.0.1",
"git-cz": "3.2.1",
"jest": "^24.7.1",
"prettier": "^1.16.4",
"prettier": "^1.19.1",
"semantic-release": "^15.13.24",
"ts-jest": "^24.1.0",
"typescript": "^3.6.3"
"typescript": "^3.7.3"
},
"publishConfig": {
"access": "public"
Expand Down
37 changes: 30 additions & 7 deletions src/common/smartContract/documentToSmartContracts.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { Document, getData } from "@govtechsg/open-attestation";
import { getData, WrappedDocument, v3, v2 } from "@govtechsg/open-attestation";
import { issuerToSmartContract } from "./issuerToSmartContract";
import { Issuer } from "../../types";
import { contractInstance } from "./contractInstance";
import documentStoreAbi from "./abi/documentStore.json";
import { OpenAttestationContract } from "../../types";

// Given a list of issuers, convert to smart contract
const mapIssuersToSmartContracts = (issuers: Issuer[], network: string) =>
const mapIssuersToSmartContracts = (issuers: v2.Issuer[], network: string) =>
issuers.map(issuer => issuerToSmartContract(issuer, network));

const isV1Document = (
document: any
): document is v2.OpenAttestationDocument => {
return !document.issuer;
};

// Given a raw document, return list of all smart contracts
export const documentToSmartContracts = (
document: Document,
document:
| WrappedDocument<v3.OpenAttestationDocument>
| WrappedDocument<v2.OpenAttestationDocument>,
network: string
) => {
): OpenAttestationContract[] => {
const data = getData(document);
const issuers = data.issuers || [];

return mapIssuersToSmartContracts(issuers, network);
if (isV1Document(data)) {
const issuers = data.issuers || [];
return mapIssuersToSmartContracts(issuers, network);
}
return [
{
type: data.proof.method,
address: data.proof.value,
instance: contractInstance({
contractAddress: data.proof.value,
abi: documentStoreAbi,
network
})
}
];
};
5 changes: 3 additions & 2 deletions src/common/smartContract/issuerToSmartContract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { v2 } from "@govtechsg/open-attestation";
import { contractInstance } from "./contractInstance";
import { TYPES } from "./constants";
import tokenRegistryAbi from "./abi/tokenRegistry.json";
import documentStoreAbi from "./abi/documentStore.json";
import { Issuer, OpenAttestationContract } from "../../types";
import { OpenAttestationContract } from "../../types";

export const issuerToSmartContract = (
issuer: Issuer,
issuer: v2.Issuer,
network: string
): OpenAttestationContract => {
switch (true) {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SignedDocument } from "@govtechsg/open-attestation";
import { WrappedDocument } from "@govtechsg/open-attestation";
import { verifyHash } from "./hash/hash";
import { verifyIssued } from "./issued/verify";
import { verifyRevoked } from "./revoked/verify";
Expand Down Expand Up @@ -27,7 +27,7 @@ type VerificationChecksWithValidity = [
];

const getAllChecks = (
document: SignedDocument,
document: WrappedDocument,
smartContracts: OpenAttestationContract[]
): VerificationChecks => [
verifyHash(document),
Expand All @@ -47,7 +47,7 @@ const isDocumentValid = (
* @returns
*/
export const verify = async (
document: SignedDocument,
document: WrappedDocument,
network = "homestead"
) => {
const smartContracts = documentToSmartContracts(document, network);
Expand All @@ -69,7 +69,7 @@ export const verify = async (
* The last promise resolves to the overall validity based on all the checks.
*/
export const verifyWithIndividualChecks = (
document: SignedDocument,
document: WrappedDocument,
network = "homestead"
): VerificationChecksWithValidity => {
const smartContracts = documentToSmartContracts(document, network);
Expand Down
32 changes: 25 additions & 7 deletions src/issued/contractInterface.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { issuerToSmartContract } from "../common/smartContract/issuerToSmartCont
describe("isIssuedOnTokenRegistry", () => {
it("returns true if token is created on tokenRegistry", async () => {
const smartContract = issuerToSmartContract(
{ tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3" },
{
tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3",
name: "some"
},
"ropsten"
);
const issued = await isIssuedOnTokenRegistry(
Expand All @@ -21,7 +24,10 @@ describe("isIssuedOnTokenRegistry", () => {

it("allows error to bubble if token is nonexistent on tokenRegistry", async () => {
const smartContract = issuerToSmartContract(
{ tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3" },
{
tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3",
name: "some"
},
"ropsten"
);
await expect(
Expand All @@ -33,7 +39,10 @@ describe("isIssuedOnTokenRegistry", () => {
describe("isIssuedOnDocumentStore", () => {
it("returns true if document is issued on documentStore", async () => {
const smartContract = issuerToSmartContract(
{ documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6" },
{
documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6",
name: "some"
},
"ropsten"
);
const issued = await isIssuedOnDocumentStore(
Expand All @@ -46,7 +55,10 @@ describe("isIssuedOnDocumentStore", () => {
it("returns false if document is not issued on documentStore", async () => {
const hash = constants.HashZero;
const smartContract = issuerToSmartContract(
{ documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6" },
{
documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6",
name: "some"
},
"ropsten"
);
const issued = await isIssuedOnDocumentStore(smartContract, hash);
Expand All @@ -55,7 +67,7 @@ describe("isIssuedOnDocumentStore", () => {

it("allows error to bubble if documentStore is not deployed", async () => {
const smartContract = issuerToSmartContract(
{ documentStore: constants.AddressZero },
{ documentStore: constants.AddressZero, name: "some" },
"ropsten"
);
await expect(
Expand All @@ -67,7 +79,10 @@ describe("isIssuedOnDocumentStore", () => {
describe("isIssued", () => {
it("works for tokenRegistry", async () => {
const smartContract = issuerToSmartContract(
{ tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3" },
{
tokenRegistry: "0x48399Fb88bcD031C556F53e93F690EEC07963Af3",
name: "some"
},
"ropsten"
);
const hash =
Expand All @@ -80,7 +95,10 @@ describe("isIssued", () => {
const hash =
"0x85df2b4e905a82cf10c317df8f4b659b5cf38cc12bd5fbaffba5fc901ef0011b";
const smartContract = issuerToSmartContract(
{ documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6" },
{
documentStore: "0x008486e2b14Cb1B5231DbA10B2170271af3196d6",
name: "some"
},
"ropsten"
);
const issued = await isIssued(smartContract, hash);
Expand Down
6 changes: 4 additions & 2 deletions src/issued/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ describe("verifyIssued", () => {
];
const summary = await verifyIssued(
{
version: "1.0",
schema: "schema",
data: "data",
data: { any: "data" },
signature: {
merkleRoot: "MERKLE_ROOT",
type: "SHA3MerkleProof",
Expand Down Expand Up @@ -123,8 +124,9 @@ describe("verifyIssued", () => {
];
const summary = await verifyIssued(
{
version: "1.0",
schema: "schema",
data: "data",
data: { any: "data" },
signature: {
merkleRoot: "MERKLE_ROOT",
type: "SHA3MerkleProof",
Expand Down
4 changes: 2 additions & 2 deletions src/issued/verify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SignedDocument } from "@govtechsg/open-attestation";
import { WrappedDocument } from "@govtechsg/open-attestation";
import { isIssued } from "./contractInterface";
import { Hash, OpenAttestationContract } from "../types";

Expand Down Expand Up @@ -29,7 +29,7 @@ export const isIssuedOnAll = (
};

export const verifyIssued = async (
document: SignedDocument,
document: WrappedDocument,
smartContracts: OpenAttestationContract[] = []
) => {
const hash = `0x${document.signature.merkleRoot}`;
Expand Down
Loading

0 comments on commit cc42bce

Please sign in to comment.