Skip to content

Commit

Permalink
Support BYO DID
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Aug 21, 2023
1 parent 53d193c commit d1da466
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export type Web5ConnectOptions = {
* {@link Web5UserAgent} if one isn't provided */
agent?: Web5Agent;

/** Specify an existing DID to connect to. */
connectedDid?: string;

/** Provide an instance of a {@link AppDataStore} implementation. Defaults to
* a LevelDB-backed store with an insecure, static unlock passphrase if one
* isn't provided. To allow the app user to enter a secure passphrase of
Expand All @@ -44,13 +47,15 @@ type Web5Options = {
};

export class Web5 {
agent: Web5Agent;
did: DidApi;
dwn: DwnApi;
vc: VcApi;
private connectedDid: string;

constructor(options: Web5Options) {
const { agent, connectedDid } = options;
this.agent = agent;
this.connectedDid = connectedDid;
this.did = new DidApi({ agent, connectedDid });
this.dwn = new DwnApi({ agent, connectedDid });
Expand All @@ -65,7 +70,7 @@ export class Web5 {
* @returns
*/
static async connect(options: Web5ConnectOptions = {}) {
let { agent, appData, techPreview } = options;
let { agent, appData, connectedDid, techPreview } = options;

if (agent === undefined) {
// A custom Web5Agent implementation was not specified, so use default managed user agent.
Expand All @@ -90,18 +95,19 @@ export class Web5 {
if (identities.length === 0) {
const serviceEndpointNodes = techPreview?.dwnEndpoints ?? await getTechPreviewDwnEndpoints();
const didOptions = await DidIonMethod.generateDwnOptions({ serviceEndpointNodes });
await userAgent.identityManager.create({
const identity = await userAgent.identityManager.create({
name : 'Default',
didMethod : 'ion',
didOptions,
kms : 'local'
});
connectedDid = identity.did;
}
}
}

const web5 = new Web5({ agent, connectedDid: agent.agentDid });
const web5 = new Web5({ agent, connectedDid });

return { web5, did: agent.agentDid };
return { web5, did: connectedDid };
}
}
30 changes: 29 additions & 1 deletion packages/api/tests/web5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { AppDataVault, TestManagedAgent } from '@web5/agent';
import { Web5 } from '../src/web5.js';
import { TestUserAgent } from './utils/test-user-agent.js';
import { MemoryStore } from '@web5/common';
import { DidIonMethod } from '@web5/dids';
import { Web5UserAgent } from '@web5/user-agent';

Check failure on line 8 in packages/api/tests/web5.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node

'Web5UserAgent' is defined but never used. Allowed unused vars must match /^_/u

describe('Web5', () => {
describe.only('Web5', () => {

Check warning on line 10 in packages/api/tests/web5.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node

Unexpected exclusive mocha test
describe('using TestManagedAgent', () => {
let testAgent: TestManagedAgent;

Expand All @@ -26,6 +28,32 @@ describe('Web5', () => {
await testAgent.closeStorage();
});

describe('connect()', () => {
it('accepts an externally created ION DID', async () => {
// Create an ION DID.
const didOptions = await DidIonMethod.generateDwnOptions({
serviceEndpointNodes: ['https://dwn.example.com']
});
const portableDid = await DidIonMethod.create({ ...didOptions });

// Import the previously created DID.
await testAgent.agent.identityManager.import({
identity : { name: 'Test', did: portableDid.did },
did : portableDid,
kms : 'local'
});

// Call connect() with the custom agent.
const { web5, did } = await Web5.connect({
agent : testAgent.agent,
connectedDid : portableDid.did
});

expect(did).to.exist;
expect(web5).to.exist;
}).timeout(5000);
});

describe('constructor', () => {
it('instantiates Web5 API with provided Web5Agent and connectedDid', async () => {
// Create a new Identity.
Expand Down

0 comments on commit d1da466

Please sign in to comment.