-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
3,473 additions
and
2,704 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import * as ethers from "ethers"; | ||
import { INFURA_API_KEY } from "../../config"; | ||
import { Hash } from "../../types"; | ||
import { Hash } from "../../types/core"; | ||
|
||
interface ContractInstance { | ||
abi: any; // type is any of json file in abi folder | ||
network: string; | ||
contractAddress: Hash; | ||
} | ||
export const contractInstance = ({ | ||
abi, | ||
network, | ||
contractAddress | ||
}: ContractInstance) => { | ||
export const contractInstance = ({ abi, network, contractAddress }: ContractInstance) => { | ||
const provider = new ethers.providers.InfuraProvider(network, INFURA_API_KEY); | ||
return new ethers.Contract(contractAddress, abi, provider); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,24 @@ | ||
import { SignedDocument } from "@govtechsg/open-attestation"; | ||
import { verifyHash } from "./hash/hash"; | ||
import { verifyIssued } from "./issued/verify"; | ||
import { verifyRevoked } from "./revoked/verify"; | ||
import { documentToSmartContracts } from "./common/smartContract/documentToSmartContracts"; | ||
import { OpenAttestationContract } from "./types"; | ||
|
||
/** | ||
* Unwraps the resolve type of promises | ||
* e.g. | ||
* type foo = () => Promise<boolean>; | ||
* type bar = ResolveType<foo>; // bar is a boolean | ||
*/ | ||
type ResolveType<T> = T extends (...args: any[]) => Promise<infer U> ? U : T; | ||
|
||
type VerificationChecks = [ | ||
ReturnType<typeof verifyHash>, | ||
ReturnType<typeof verifyIssued>, | ||
ReturnType<typeof verifyRevoked> | ||
]; | ||
|
||
type VerificationChecksWithValidity = [ | ||
ReturnType<typeof verifyHash>, | ||
ReturnType<typeof verifyIssued>, | ||
ReturnType<typeof verifyRevoked>, | ||
Promise<boolean> | ||
import { verificationManager } from "./verifiers/verificationManager"; | ||
import { Verifier } from "./types/core"; | ||
import { openAttestationTamperCheck } from "./verifiers/openAttestationTamperCheck"; | ||
import { openAttestationDnsTxtIdentity } from "./verifiers/openAttestationDnsTxtIdentity"; | ||
import { openAttestationEthereumDocumentStoreIssued } from "./verifiers/openAttestationEthereumDocumentStoreIssued"; | ||
import { openAttestationEthereumDocumentStoreRevoked } from "./verifiers/openAttestationEthereumDocumentStoreRevoked"; | ||
|
||
const defaultVerifiers: Verifier[] = [ | ||
openAttestationTamperCheck, | ||
openAttestationEthereumDocumentStoreIssued, | ||
openAttestationEthereumDocumentStoreRevoked, | ||
openAttestationDnsTxtIdentity | ||
]; | ||
|
||
const getAllChecks = ( | ||
document: SignedDocument, | ||
smartContracts: OpenAttestationContract[] | ||
): VerificationChecks => [ | ||
verifyHash(document), | ||
verifyIssued(document, smartContracts), | ||
verifyRevoked(document, smartContracts) | ||
]; | ||
|
||
const isDocumentValid = ( | ||
hash: ResolveType<typeof verifyHash>, | ||
issued: ResolveType<typeof verifyIssued>, | ||
revoked: ResolveType<typeof verifyRevoked> | ||
) => hash.checksumMatch && issued.issuedOnAll && !revoked.revokedOnAny; | ||
|
||
/** | ||
* @param {object} document Entire document object to be validated | ||
* @param {string} network Network to check against, defaults to "homestead". Other valid choices: "ropsten", "kovan", etc | ||
* @returns | ||
*/ | ||
export const verify = async ( | ||
document: SignedDocument, | ||
network = "homestead" | ||
) => { | ||
const smartContracts = documentToSmartContracts(document, network); | ||
const checks = getAllChecks(document, smartContracts); | ||
const [hash, issued, revoked] = await Promise.all(checks); | ||
|
||
return { | ||
hash, | ||
issued, | ||
revoked, | ||
valid: isDocumentValid(hash, issued, revoked) | ||
}; | ||
const defaultVerificationManager = verificationManager(defaultVerifiers); | ||
|
||
export { | ||
verificationManager, | ||
openAttestationEthereumDocumentStoreIssued, | ||
openAttestationDnsTxtIdentity, | ||
openAttestationTamperCheck, | ||
openAttestationEthereumDocumentStoreRevoked, | ||
defaultVerifiers, | ||
defaultVerificationManager | ||
}; | ||
|
||
/** | ||
* @param {object} document Entire document object to be validated | ||
* @param {string} network Network to check against, defaults to "homestead". Other valid choices: "ropsten", "kovan", etc | ||
* @returns {array} Array of promises, each promise corresponds to a verification check. | ||
* The last promise resolves to the overall validity based on all the checks. | ||
*/ | ||
export const verifyWithIndividualChecks = ( | ||
document: SignedDocument, | ||
network = "homestead" | ||
): VerificationChecksWithValidity => { | ||
const smartContracts = documentToSmartContracts(document, network); | ||
const [hash, issued, revoked] = getAllChecks(document, smartContracts); | ||
|
||
// If any of the checks are invalid, resolve the overall validity early | ||
const valid = Promise.all([ | ||
new Promise(async (resolve, reject) => | ||
(await hash).checksumMatch ? resolve() : reject() | ||
), | ||
new Promise(async (resolve, reject) => | ||
(await issued).issuedOnAll ? resolve() : reject() | ||
), | ||
new Promise(async (resolve, reject) => | ||
(await revoked).revokedOnAny ? reject() : resolve() | ||
) | ||
]) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
return [hash, issued, revoked, valid]; | ||
}; | ||
|
||
export default verify; // backward compatible |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.