Skip to content

Commit 0069d3e

Browse files
author
Wes Biggs
committed
Various tweaks.
* Remove "-plugin" from package name * Generate types in dist * Allow constuction with pre-existing ApiPromise * Don't put DID document @context in an array, ed25519-multikey doesn't like it
1 parent b6cf954 commit 0069d3e

File tree

6 files changed

+503
-460
lines changed

6 files changed

+503
-460
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
# Overview
22

3-
This package contains a plugin for the [@dsnp/did-resolver](https://github.com/LibertyDSNP/dsnp-did-resolver) library which enables resolution of `did:dsnp:*` DIDs on the [Frequency](https://github.com/LibertyDSNP/frequency) blockchain.
3+
This package contains a resolver for the [@dsnp/did-resolver](https://github.com/LibertyDSNP/dsnp-did-resolver) library which enables resolution of `did:dsnp:*` DIDs on the [Frequency](https://github.com/LibertyDSNP/frequency) blockchain.
4+
5+
# Installation
6+
7+
`npm install @dsnp/did-resolver-frequency`
48

59
# Usage
610

7-
The plugin must be initialized with Frequency connection information.
11+
The resolver object can be constructed with Frequency connection information in one of two ways.
12+
13+
1. Construct with provider URI:
814

915
```
10-
import { FrequencyResolver } from "@dsnp/did-resolver-plugin-frequency";
16+
import { FrequencyResolver } from "@dsnp/did-resolver-frequency";
1117
1218
const frequencyResolver = new FrequencyResolver({
1319
providerUri: "ws://127.0.0.1:9944",
1420
frequencyNetwork: "local",
1521
});
1622
```
1723

18-
The plugin will automatically register itself with the DSNP DID resolver when initialized.
24+
If constructed this way, you must call `disconnect()` to explicitly release the connection; the process will not exit if this is not done.
25+
26+
or,
27+
28+
2. Construct with preconfigured `ApiPromise` object from `@polkadot/api`:
29+
30+
```
31+
import { FrequencyResolver } from "@dsnp/did-resolver-frequency";
32+
33+
const frequencyResolver = new FrequencyResolver({
34+
apiPromise: myApiPromise, // from ApiPromise.create(...)
35+
frequencyNetwork: "local",
36+
});
37+
```
38+
39+
The `frequencyNetwork` key is required in both cases (this is expected to be unnecessary with Frequency schema naming in the future).
1940

20-
The following options must be provided:
41+
Summary of options:
2142

2243
| Configuration option | Description |
2344
| --- | --- |
24-
| `providerUri` | Provider URI for Frequency RPC node |
45+
| `providerUri` | Provider URI for Frequency RPC node (optional; alternative to `apiPromise` |
46+
| `apiPromise` | An `ApiPromise` object (optional; alternative to `providerUri` |
2547
| `frequencyNetwork` | One of `local`, `testnet`, `mainnet` |
2648

2749
See `.env.example` for example configuration.
@@ -56,15 +78,14 @@ frequencyResolver.disconnect();
5678
"id": "did:dsnp:13972",
5779
"assertionMethod": [
5880
{
59-
"@context": [
60-
"https://w3id.org/security/multikey/v1"
61-
],
81+
"@context": "https://w3id.org/security/multikey/v1",
6282
"id": "did:dsnp:13972#z6MkuzE4hBVHTmwFff37ZuPQs9sbkdJo8jifN9sZ1jXbgyMp",
6383
"type": "Multikey",
6484
"controller": "did:dsnp:13972",
6585
"publicKeyMultibase": "z6MkuzE4hBVHTmwFff37ZuPQs9sbkdJo8jifN9sZ1jXbgyMp"
6686
}
67-
]
87+
],
88+
"keyAgreement": []
6889
},
6990
"didDocumentMetadata": {}
7091
}
@@ -82,7 +103,7 @@ npm run resolve -- 13972
82103

83104
# Features
84105

85-
Currently this plugin implements the minimal functionality required to support lookup of public keys by DSNP applications.
106+
Currently this resolver implements the minimal functionality required to support lookup of public keys by DSNP applications.
86107

87108
- DSNP public keys with `keyType` 1 are listed in the `keyAgreement` array.
88109
- DSNP public keys with `keyType` 2 are listed in the `assertionMethod` array.

index.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
import { expect, jest, test } from "@jest/globals";
22
import { FrequencyResolver } from "./index.js";
3+
import { ApiPromise, WsProvider } from "@polkadot/api";
4+
import { options } from "@frequency-chain/api-augment";
35

4-
describe("dsnp-did-resolver-plugin-frequency", () => {
5-
it("can be constructed", async () => {
6-
const _resolver = new FrequencyResolver({
6+
describe("dsnp-did-resolver-frequency", () => {
7+
it("can be constructed with providerUri", async () => {
8+
const resolver = new FrequencyResolver({
79
providerUri: "ws://127.0.0.1:9944",
810
frequencyNetwork: "local",
911
});
12+
13+
await resolver.disconnect();
14+
});
15+
16+
it("can be constructed with apiPromise", async () => {
17+
const apiPromise = ApiPromise.create({
18+
provider: new WsProvider("ws://127.0.0.1:9944"),
19+
throwOnConnect: true,
20+
...options,
21+
});
22+
23+
const resolver = new FrequencyResolver({
24+
apiPromise,
25+
frequencyNetwork: "local",
26+
});
27+
28+
await resolver.disconnect();
1029
});
1130

1231
/* Need to mock for this...

index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function makeVerificationMethod(
1313
publicKeyMultibase: string,
1414
) {
1515
return {
16-
"@context": ["https://w3id.org/security/multikey/v1"],
16+
"@context": "https://w3id.org/security/multikey/v1",
1717
id: `${controller}#${publicKeyMultibase}`,
1818
type: "Multikey",
1919
controller,
@@ -22,15 +22,17 @@ function makeVerificationMethod(
2222
}
2323

2424
export class FrequencyResolver implements DSNPResolver {
25-
private providerUri: string;
25+
private providerUri: string | null = null;
2626
private frequencyNetwork: string;
2727
private _singletonApi: Promise<ApiPromise> | null = null;
2828

29-
constructor(options: { providerUri: string; frequencyNetwork: string }) {
30-
this.providerUri = options.providerUri;
31-
32-
if (!this.providerUri) {
33-
throw new Error("providerUri is required");
29+
constructor(options: { providerUri?: string; apiPromise?: Promise<ApiPromise>, frequencyNetwork: string }) {
30+
if (options.providerUri) {
31+
this.providerUri = options.providerUri;
32+
} else if (options.apiPromise != null) {
33+
this._singletonApi = options.apiPromise;
34+
} else {
35+
throw new Error("providerUri or apiPromise is required");
3436
}
3537

3638
this.frequencyNetwork = options.frequencyNetwork;
@@ -47,7 +49,7 @@ export class FrequencyResolver implements DSNPResolver {
4749
async getApi(): Promise<ApiPromise> {
4850
if (this._singletonApi == null) {
4951
this._singletonApi = ApiPromise.create({
50-
provider: new WsProvider(this.providerUri),
52+
provider: new WsProvider(this.providerUri as string),
5153
throwOnConnect: true,
5254
...options,
5355
});

0 commit comments

Comments
 (0)