Skip to content

Commit

Permalink
Merge pull request #28 from vaxxnz/feature/browser_and_node_builds
Browse files Browse the repository at this point in the history
Feature/browser and node builds
  • Loading branch information
noway authored Nov 8, 2021
2 parents 834100d + b568070 commit ff70607
Show file tree
Hide file tree
Showing 8 changed files with 990 additions and 76 deletions.
38 changes: 9 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# NZCP.js

A verification library for COVID-19 Vaccine Passes in New Zealand built on top of the [NZ Covid Pass Spec](https://github.com/minhealthnz/nzcovidpass-spec) provided by the Ministry of Health. All contributions welcome 🥳
A JavaScript implementation of [NZ COVID Pass](https://github.com/minhealthnz/nzcovidpass-spec) verification, New Zealand's proof of COVID-19 vaccination solution, written in TypeScript. All contributions welcome 🥳

> This library can be used for both Client and Server-side implementations.
## Install

```bash
# NPM
npm i __package-name__
npm i @vaxxnz/nzcp

# Yarn
yarn add __package-name__
yarn add @vaxxnz/nzcp
```

Peer dependencies on: cbor, did-resolver, elliptic, rfc4648, web-did-resolver

## Usage

```javascript
import { verifyPass } from "__package-name__";
import { verifyPass } from "@vaxxnz/nzcp";

// Verify a New Zealand COVID-19 Pass
const result = await verifyPass("NZCP:/1/2KCEVIQEIVVWK6...");
Expand Down Expand Up @@ -57,23 +55,6 @@ On **unsuccessful** verification of the given pass, the `verifyPass` method retu
}
```

### Advanced Parameters

To allow for flexibility in this library, the `verifyPass` method allows for additional parameters as documented below.

#### Customize Trusted Issuers

```javascript
// An array of trusted issuers which work with the NZ COVID Pass - Technical Specification
// https://nzcp.covid19.health.nz/
const nzcpTrustedIssuers = ["did:web:nzcp.covid19.health.nz"];

const result = await verifyPass(
"NZCP:/1/2KCEVIQEIVVWK6...", // COVID-19 Pass to be verified
nzcpTrustedIssuers // Array of trusted issuers
);
```

## Support

See something that can be improved? [Report an Issue](https://github.com/vaxxnz/nzcp-js/issues) or contact us to [report a security concern](mailto:[email protected]).
Expand All @@ -82,17 +63,16 @@ Want to help us build a better library? We welcome contributions via [pull reque

---

### Yarn Commands
## Contribute

```bash
# Install dependencies
yarn install
# Now you can run various yarn commands:
yarn cli
```
```bash
# Use developer scripts
yarn lint
yarn test
yarn test-watch
yarn build-all
yarn ts-node <filename>
yarn esbuild-browser
...
```
29 changes: 29 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { build } from "esbuild"
import { nodeBuiltIns } from "esbuild-node-builtins"
async function main() {
const resultBrowser = await build({
plugins: [nodeBuiltIns()],
entryPoints: ['src/browser.ts'],
bundle: true,
outfile: 'dist/esbuild/browser.js',
sourcemap: "both",
minify: true,
platform: "browser",
format: "cjs",
target: "es6"
})
console.log(resultBrowser)

const resultNode = await build({
entryPoints: ['src/node.ts'],
bundle: true,
outfile: 'dist/esbuild/node.js',
sourcemap: "both",
minify: true,
platform: "node",
format: "cjs",
target: "es6"
})
console.log(resultNode)
}
main()
43 changes: 17 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
{
"name": "nzcp-js",
"version": "0.0.0",
"description": "Javascript implemtation of the NZ covid pass verifier spec",
"author": "Will Seagar <[email protected]>",
"name": "@vaxxnz/nzcp",
"version": "0.0.1",
"description": "A JavaScript implementation of the NZ COVID Pass verification",
"author": "vaxxnz <[email protected]>",
"repository": "https://github.com/vaxxnz/nzcp-js",
"license": "MIT",
"keywords": [
"typescript",
"boilerplate",
"esbuild"
],
"main": "./dist/tsc/main.js",
"main": "./dist/esbuild/node.js",
"types": "./dist/tsc/main.d.ts",
"browser": "./dist/esbuild/browser.js",
"bin": {
"my-cli-tool": "./dist/esbuild/cli.js"
},
"scripts": {
"cli": "ts-node src/cli.ts",
"lint": "eslint src/ --ext .js,.jsx,.ts,.tsx",
"test": "jest",
"test-watch": "jest --watch",
"clean": "rm -rf dist build package",
"ts-node": "ts-node",
"docs": "typedoc --entryPoints src/main.ts",
"build": "tsc -p tsconfig.json",
"build-all": "yarn clean && yarn build && yarn esbuild-node && yarn esbuild-browser",
"esbuild-browser": "esbuild src/browser.ts --bundle --minify --sourcemap=external --outfile=dist/esbuild/browser.js",
"esbuild-browser:dev": "esbuild src/browser.ts --bundle --outfile=dist/esbuild/browser.js",
"esbuild-browser:watch": "esbuild src/browser.ts --bundle --watch --outfile=dist/esbuild/browser.js",
"esbuild-node": "esbuild src/cli.ts --bundle --platform=node --minify --sourcemap=external --outfile=dist/esbuild/cli.js",
"esbuild-node:dev": "esbuild src/cli.ts --bundle --sourcemap=external --outfile=dist/esbuild/cli.js",
"esbuild-node:watch": "esbuild src/cli.ts --bundle --watch --sourcemap=external --outfile=dist/esbuild/cli.js"
"build-all": "yarn clean && patch-package && tsc -p tsconfig.json && yarn ts-node build.ts"
},
"devDependencies": {
"@types/elliptic": "^6.4.14",
Expand All @@ -40,19 +28,22 @@
"@types/node-fetch": "^2.5.6",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"cbor": "^6.0.1",
"did-resolver": "^3.1.3",
"elliptic": "^6.5.4",
"esbuild": "^0.11.11",
"esbuild-node-builtins": "^0.1.0",
"eslint": "^7.22.0",
"jest": "^26.6.3",
"js-sha256": "^0.9.0",
"patch-package": "^6.4.7",
"postinstall-postinstall": "^2.1.0",
"rfc4648": "^1.5.0",
"ts-jest": "^26.5.4",
"ts-node": "^9.1.1",
"typedoc": "^0.20.35",
"typescript": "^4.2.3"
},
"dependencies": {
"cbor": "^6.0.1",
"did-resolver": "^3.1.3",
"elliptic": "^6.5.4",
"rfc4648": "^1.5.0",
"typescript": "^4.2.3",
"web-did-resolver": "^2.0.7"
}
},
"dependencies": {}
}
13 changes: 13 additions & 0 deletions patches/cbor+6.0.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/cbor/lib/constants.js b/node_modules/cbor/lib/constants.js
index 932e1c5..9b4502c 100644
--- a/node_modules/cbor/lib/constants.js
+++ b/node_modules/cbor/lib/constants.js
@@ -2,7 +2,7 @@

// Let's get consistent first, then we can think about feature testing
// for BigNumber support
-const {BigNumber} = require('bignumber.js')
+const {BigNumber} = require('bignumber.js/bignumber.mjs')

exports.BigNumber = BigNumber

8 changes: 2 additions & 6 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
* This file is the entrypoint of browser builds.
* The code executes when loaded in a browser.
*/
import { foo } from './main'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).foo = foo // instead of casting window to any, you can extend the Window interface: https://stackoverflow.com/a/43513740/5433572

console.log('Method "foo" was added to the window object. You can try it yourself by just entering "await foo()"')
import { verifyPass } from './main'
export { verifyPass }
4 changes: 2 additions & 2 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cbor from "cbor";
import crypto from "crypto";
import { sha256 } from "js-sha256";
import elliptic from "elliptic";
import { DecodedCOSEStructure } from "./coseTypes";

Expand Down Expand Up @@ -33,7 +33,7 @@ export function validateCOSESignature(
// ]
const SigStructure = ["Signature1", protected_, Buffer.alloc(0), payload_];
const ToBeSigned = cbor.encode(SigStructure);
const messageHash = crypto.createHash("SHA256").update(ToBeSigned).digest();
const messageHash = sha256.digest(ToBeSigned)
const signature = {
r: signature_.slice(0, signature_.length / 2),
s: signature_.slice(signature_.length / 2),
Expand Down
6 changes: 6 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This file is the entrypoint of node builds.
* The code executes when loaded in a node.
*/
import { verifyPass } from './main'
export { verifyPass }
Loading

0 comments on commit ff70607

Please sign in to comment.