Skip to content

Commit

Permalink
Merge pull request #1 from Sphereon-Opensource/ONTO-3
Browse files Browse the repository at this point in the history
ONTO-3
  • Loading branch information
nklomp authored Aug 11, 2021
2 parents b66056f + b7b4477 commit bb4c3e1
Show file tree
Hide file tree
Showing 18 changed files with 4,518 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": { "project": "./tsconfig.json" },
"env": { "es6": true },
"ignorePatterns": ["node_modules", "dist", "coverage", "jest.config.js"],
"plugins": ["import", "eslint-comments"],
"extends": [
"eslint:recommended",
"plugin:eslint-comments/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"prettier"
],
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/explicit-module-boundary-types": "off",
"eslint-comments/disable-enable-pair": [
"error",
{ "allowWholeFile": true }
],
"eslint-comments/no-unused-disable": "error",
"import/order": [
"error",
{ "newlines-between": "always", "alphabetize": { "order": "asc" } }
],
"sort-imports": [
"error",
{ "ignoreDeclarationSort": true, "ignoreCase": true }
]
}
}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vscode/*
.idea/*
*.iml
.nyc_output
dist
node_modules
coverage
*.log
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# package.json is formatted by package managers, so we ignore it here
package.json
116 changes: 115 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,115 @@
# did-uni-client
<h1 align="center">
<br>
<a href="https://www.sphereon.com"><img src="https://sphereon.com/content/themes/sphereon/assets/img/logo.svg" alt="Sphereon" width="400"></a>
<br>
</h1>

### Did-uni-client
The did-uni-client is a library to call a universal registrar (e.g. https://uniregistrar.io) to create, update and deactivate decentralized identifiers.
And to call a universal resolver (e.g. https://dev.uniresolver.io) to resolve decentralized identifiers to did documents. It is written in Typescript and can be compiled to any target JavaScript version.

### Supported actions
* Creating a decentralized identifier (DID).
* Updating a decentralized identifier (DID).
* Deactivating a decentralized identifier (DID).
* Resolving a decentralized identifier (DID).

#### Examples

##### DID creation
```typescript
const {Registrar, CrudRequestBuilder} = require('did-uni-client');

const method = 'btcr';
const request = new CrudRequestBuilder()
.withOptions({chain: 'TESTNET'})
.build();
const registrar = new Registrar();

registrar.create(method, request)
.then(result => 'success')
.catch(error => 'failed');
```

##### DID updating
```typescript
const {Registrar, CrudRequestBuilder} = require('did-uni-client');
const did = 'did:btcr:xz35-jznz-q6mr-7q6';
const request = new CrudRequestBuilder()
.withOptions({chain: 'TESTNET'})
.withSecret({token:"ey..."})
.build();
const registrar = new Registrar();
registrar.update(did, request)
.then(result => 'success')
.catch(error => 'failed');
```

##### DID deactivating
```typescript
const {Registrar, CrudRequestBuilder} = require('did-uni-client');
const did = 'did:btcr:xz35-jznz-q6mr-7q6';
const request = new CrudRequestBuilder()
.withOptions({chain: 'TESTNET'})
.withSecret({token:"ey..."})
.build();
const registrar = new Registrar();
registrar.deactivate(did, request)
.then(result => 'success')
.catch(error => 'failed');
```

##### DID resolution
```typescript
const {Resolver} = require('did-uni-client');
const did = 'did:btcr:xz35-jznz-q6mr-7q6';
const resolver = new Resolver();
resolver.resolve(did)
.then(result => 'success')
.catch(error => 'failed');
```

### Configuration
To use the library, URL's needs to be available for universal registrar endpoints and universal resolver endpoints. There are three options to configure the URL's.
The library will first check if there is an environment variable, if this is not present it will look in the config file. It is also possible to overwrite the default URL's by using one of the URL setters.

##### Environment variable
###### Registrar
REGISTRAR_URL_CREATE - Defines the URL for a create endpoint (e.g. https://uniregistrar.io/1.0/create).
REGISTRAR_URL_UPDATE - Defines the URL for a update endpoint (e.g. https://uniregistrar.io/1.0/update).
REGISTRAR_URL_DEACTIVATE - Defines the URL for a deactivate endpoint (e.g. https://uniregistrar.io/1.0/deactivate).

###### Resolver
RESOLVER_URL_RESOLVE - Defines the URL for a resolve endpoint (e.g. https://dev.uniresolver.io/1.0/identifiers).

##### Config file
A config file is available here 'src/config.ts'.

### Build
```shell
yarn build
```

### Test
The test command runs:
* `eslint`
* `prettier`
* `unit`
* `coverage`

You can also run only a single section of these tests, using for example `yarn test:unit`.
```shell
yarn test
```

### Utility scripts
There are several other utility scripts that help with development.

* `yarn fix` - runs `eslint --fix` as well as `prettier` to fix code style.
* `yarn cov` - generates code coverage report.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
};
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@sphereon/did-uni-client",
"version": "0.1.0",
"description": "A Typescript library to create, update, deactivate and resolve DID documents using a Universal Registrar and Universal Resolver",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"license": "Apache-2.0",
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "run-s build test:* cov",
"test:lint": "eslint . --ext .ts",
"test:prettier": "prettier \"{src, test}/**/*.ts\" --list-different",
"test:unit": "jest",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"{src, test}/**/*.ts\" --write",
"fix:lint": "eslint . --ext .ts --fix",
"cov": "jest --coverage"
},
"files": [
"/dist",
"LICENSE",
"README.md"
],
"dependencies": {
"cross-fetch": "^3.1.4",
"did-resolver": "^3.1.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.23.4",
"jest": "^27.0.6",
"@jest/globals": "^27.0.6",
"nock": "^13.1.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
},
"prettier": {
"singleQuote": true,
"printWidth": 120
}
}
3 changes: 3 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export abstract class Constants {
static readonly INVALID_DID: string = 'invalidDid';
}
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Registrar URL's*/
export const registrarUrlCreate = 'https://uniregistrar.io/1.0/create';
export const registrarUrlUpdate = 'https://uniregistrar.io/1.0/update';
export const registrarUrlDeactivate = 'https://uniregistrar.io/1.0/deactivate';

/** Resolver URL's*/
export const resolverUrlResolve = 'https://dev.uniresolver.io/1.0/identifiers';
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Resolver } from './resolver/Resolver';
export { Registrar } from './registrar/Registrar';
export { CrudRequestBuilder } from './registrar/rest/CrudRequestBuilder';
Loading

0 comments on commit bb4c3e1

Please sign in to comment.