Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement verifier manager #74

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"prettier/prettier": "error",
"import/no-unresolved": "off",
"import/prefer-default-export": "off",
"@typescript-eslint/explicit-function-return-type":"off",
"@typescript-eslint/no-explicit-any":"off"
"import/extensions": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-unused-expressions": "off"
}
}
130 changes: 106 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CircleCI](https://circleci.com/gh/Open-Attestation/oa-verify.svg?style=svg)](https://circleci.com/gh/Open-Attestation/oa-verify)

Library to verify any [OpenAttestation](https://github.com/OpenCerts/open-attestation) document.
Library to verify any [OpenAttestation](https://github.com/OpenCerts/open-attestation) document. This library implements [the verifier ADR](https://github.com/Open-Attestation/adr/blob/master/verifier.md).

## Installation

Expand All @@ -12,38 +12,120 @@ npm install @govtechsg/oa-verify

## Usage

```js
const document = require("./document.json");
const verify = require("@govtechsg/oa-verify");
```typescript
import { documentRopstenValidWithToken } from "./test/fixtures/v2/documentRopstenValidWithToken";
import { verify, isValid } from "@govtechsg/oa-verify";

verify(document).then(console.log);
verify(documentRopstenValidWithToken).then(console.log); // see below
console.log(isValid(results)); // display true
```

```json
{
"hash": {
"checksumMatch": true
[
{
"data": true,
"status": "VALID",
"name": "OpenAttestationHash",
"type": "DOCUMENT_INTEGRITY"
},
"issued": {
"issuedOnAll": true,
"details": [
{
"address": "0x48399Fb88bcD031C556F53e93F690EEC07963Af3",
"issued": true
}
]
{
"message": "Document issuers doesn't have \"documentStore\" or \"certificateStore\" property or DOCUMENT_STORE method",
"name": "OpenAttestationEthereumDocumentStoreIssued",
"status": "SKIPPED",
"type": "DOCUMENT_STATUS"
},
{
"data": {
"details": [
{
"address": "0xe59877ac86c0310e9ddaeb627f42fdee5f793fbe",
"minted": true
}
],
"mintedOnAll": true
},
"status": "VALID",
"name": "OpenAttestationEthereumTokenRegistryMinted",
"type": "DOCUMENT_STATUS"
},
"revoked": {
"revokedOnAny": false,
"details": [
{
"message": "Document issuers doesn't have \"documentStore\" or \"certificateStore\" property or DOCUMENT_STORE method",
"name": "OpenAttestationEthereumDocumentStoreRevoked",
"status": "SKIPPED",
"type": "DOCUMENT_STATUS"
},
{
"data": [
{
"address": "0x48399Fb88bcD031C556F53e93F690EEC07963Af3",
"revoked": false
"dns": "example.tradetrust.io",
"identified": true,
"smartContract": "0xe59877ac86c0310e9ddaeb627f42fdee5f793fbe"
}
]
],
"status": "VALID",
"name": "OpenAttestationDnsTxt",
"type": "ISSUER_IDENTITY"
}
]
```

## Advanced usage

### Verify

By default the provided `verify` method performs multiple checks on a document

- for the type `DOCUMENT_STATUS`: it runs `OpenAttestationEthereumDocumentStoreIssued`, `OpenAttestationEthereumDocumentStoreRevoked` and `OpenAttestationEthereumTokenRegistryIssued` verifiers
- for the type `DOCUMENT_INTEGRITY`: it runs `OpenAttestationHash` verifier
- for the type `ISSUER_IDENTITY`: it runs `OpenAttestationDnsTxt` verifier

All those verifiers are exported as `openAttestationVerifiers`

You can build your own verify method or you own verifiers:

```typescript
import { verificationBuilder, openAttestationVerifiers } from "@govtechsg/oa-verify";

// creating your own verify using default exported verifiers
const verify = verificationBuilder(openAttestationVerifiers); // this verify is equivalent to the one exported by the library
const verify = verificationBuilder([openAttestationVerifiers[0], openAttestationVerifiers[1]]); // this verify only run 2 verifiers

// creating your own verify using custom verifier
import { verificationBuilder, openAttestationVerifiers, Verifier } from "@govtechsg/oa-verify";
const customVerifier: Verifier = {
skip: () => {
// return a SkippedVerificationFragment if the verifier should be skipped or throw an error if it should always run
},
test: () => {
// return true or false
},
"valid": true
}
verify: async document => {
// perform checks and returns a fragment
}
};

// create your own verify function with all verifiers and your custom one
const verify = verificationBuilder([...openAttestationVerifiers, customVerifier]);
```

### isValid

By default, `isValid` perform checks on every types that exists for a fragment:

- `DOCUMENT_STATUS`
- `DOCUMENT_INTEGRITY`
- `ISSUER_IDENTITY`
it ensures that for every types, there is at least one `VALID` fragment and no `INVALID` or `ERROR` fragment.

The function allow to specify as a second parameters the list of types on which to perform the checks

```typescript
import { documentRopstenValidWithCertificateStore } from "./test/fixtures/v2/documentRopstenValidWithCertificateStore";
import { verify, isValid } from "@govtechsg/oa-verify";

const fragments = verify(documentRopstenValidWithCertificateStore);
isValid(fragments); // display false because ISSUER_IDENTITY is INVALID
isValid(fragments, ["DOCUMENT_INTEGRITY", "DOCUMENT_STATUS"]); // display true because those types are VALID
```

## License
Expand Down
Loading