Skip to content

Commit

Permalink
DWN Endpoint during Web5.connect() DID Creation (#750)
Browse files Browse the repository at this point in the history
* remove selection for DWN endpoints during Connect DID creation, instead provide an option or a single default to `https://dwn.tbddev.org/beta`
  • Loading branch information
LiranCohen committed Jul 16, 2024
1 parent 8dd0832 commit 750aa1c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 249 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-crabs-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": patch
---

default DWN Server selection updated to select only 1 defaulting to the `beta` TBD hosted version
1 change: 0 additions & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export * from './protocol.js';
export * from './record.js';
export * from './vc-api.js';
export * from './web5.js';
export * from './tech-preview.js';
export * from './web-features.js';

import * as utils from './utils.js';
Expand Down
57 changes: 0 additions & 57 deletions packages/api/src/tech-preview.ts

This file was deleted.

19 changes: 15 additions & 4 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { BearerIdentity, HdIdentityVault, Web5Agent } from '@web5/agent';
import { DidApi } from './did-api.js';
import { DwnApi } from './dwn-api.js';
import { DwnRecordsPermissionScope, DwnProtocolDefinition } from '@web5/agent';
import { getTechPreviewDwnEndpoints } from './tech-preview.js';
import { VcApi } from './vc-api.js';
import { Web5UserAgent } from '@web5/user-agent';

Expand All @@ -13,6 +12,12 @@ export type TechPreviewOptions = {
dwnEndpoints?: string[];
}

/** Override defaults for DID creation. */
export type DidCreateOptions = {
/** Override default dwnEndpoints provided during DID creation. */
dwnEndpoints?: string[];
}

/**
* Options to provide when the initiating app wants to import a delegated identity/DID from an external wallet.
*/
Expand Down Expand Up @@ -136,6 +141,12 @@ export type Web5ConnectOptions = {
* See {@link TechPreviewOptions} for available options.
*/
techPreview?: TechPreviewOptions;

/**
* Override defaults configured options for creating a DID during connect.
* See {@link DidCreateOptions} for available options.
*/
didCreateOptions?: DidCreateOptions;
}

/**
Expand Down Expand Up @@ -213,7 +224,7 @@ export class Web5 {
* @returns A promise that resolves to a {@link Web5} instance and the connected DID.
*/
static async connect({
agent, agentVault, connectedDid, password, recoveryPhrase, sync, techPreview
agent, agentVault, connectedDid, password, recoveryPhrase, sync, techPreview, didCreateOptions
}: Web5ConnectOptions = {}): Promise<Web5ConnectResult> {
if (agent === undefined) {
// A custom Web5Agent implementation was not specified, so use default managed user agent.
Expand Down Expand Up @@ -254,8 +265,8 @@ export class Web5 {
// If an existing identity is not found found, create a new one.
const existingIdentityCount = identities.length;
if (existingIdentityCount === 0) {
// Use the specified DWN endpoints or get default tech preview hosted nodes.
const serviceEndpointNodes = techPreview?.dwnEndpoints ?? await getTechPreviewDwnEndpoints();
// Use the specified DWN endpoints or the latest TBD hosted DWN
const serviceEndpointNodes = techPreview?.dwnEndpoints ?? didCreateOptions?.dwnEndpoints ?? ['https://dwn.tbddev.org/beta'];

// Generate a new Identity for the end-user.
identity = await userAgent.identity.create({
Expand Down
186 changes: 0 additions & 186 deletions packages/api/tests/tech-preview.spec.ts

This file was deleted.

61 changes: 60 additions & 1 deletion packages/api/tests/web5.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { MemoryStore } from '@web5/common';
import { Web5UserAgent } from '@web5/user-agent';
import { HdIdentityVault, PlatformAgentTestHarness } from '@web5/agent';
import { AgentIdentityApi, HdIdentityVault, PlatformAgentTestHarness } from '@web5/agent';

import { Web5 } from '../src/web5.js';

Expand Down Expand Up @@ -131,6 +133,27 @@ describe('Web5', () => {
});

describe('connect()', () => {
let testHarness: PlatformAgentTestHarness;

before(async () => {
testHarness = await PlatformAgentTestHarness.setup({
agentClass : Web5UserAgent,
agentStores : 'memory'
});
});

beforeEach(async () => {
sinon.restore();
await testHarness.clearStorage();
await testHarness.createAgentDid();
});

after(async () => {
sinon.restore();
await testHarness.clearStorage();
await testHarness.closeStorage();
});

it('uses Web5UserAgent, by default', async () => {
// Create an in-memory identity vault store to speed up tests.
const agentVault = new HdIdentityVault({
Expand All @@ -145,5 +168,41 @@ describe('Web5', () => {
expect(recoveryPhrase).to.be.a('string');
expect(recoveryPhrase.split(' ')).to.have.lengthOf(12);
});

it('creates an identity using the provided techPreview dwnEndpoints', async () => {
sinon.stub(Web5UserAgent, 'create').resolves(testHarness.agent as Web5UserAgent);
const identityApiSpy = sinon.spy(AgentIdentityApi.prototype, 'create');
const { web5, did } = await Web5.connect({ techPreview: { dwnEndpoints: ['https://dwn.example.com/preview'] }});
expect(web5).to.exist;
expect(did).to.exist;

expect(identityApiSpy.calledOnce, 'identityApiSpy called').to.be.true;
const serviceEndpoints = (identityApiSpy.firstCall.args[0].didOptions as any).services[0].serviceEndpoint;
expect(serviceEndpoints).to.deep.equal(['https://dwn.example.com/preview']);
});

it('creates an identity using the provided didCreateOptions dwnEndpoints', async () => {
sinon.stub(Web5UserAgent, 'create').resolves(testHarness.agent as Web5UserAgent);
const identityApiSpy = sinon.spy(AgentIdentityApi.prototype, 'create');
const { web5, did } = await Web5.connect({ didCreateOptions: { dwnEndpoints: ['https://dwn.example.com'] }});
expect(web5).to.exist;
expect(did).to.exist;

expect(identityApiSpy.calledOnce, 'identityApiSpy called').to.be.true;
const serviceEndpoints = (identityApiSpy.firstCall.args[0].didOptions as any).services[0].serviceEndpoint;
expect(serviceEndpoints).to.deep.equal(['https://dwn.example.com']);
});

it('defaults to `https://dwn.tbddev.org/beta` as the single DWN Service endpoint if non is provided', async () => {
sinon.stub(Web5UserAgent, 'create').resolves(testHarness.agent as Web5UserAgent);
const identityApiSpy = sinon.spy(AgentIdentityApi.prototype, 'create');
const { web5, did } = await Web5.connect();
expect(web5).to.exist;
expect(did).to.exist;

expect(identityApiSpy.calledOnce, 'identityApiSpy called').to.be.true;
const serviceEndpoints = (identityApiSpy.firstCall.args[0].didOptions as any).services[0].serviceEndpoint;
expect(serviceEndpoints).to.deep.equal(['https://dwn.tbddev.org/beta']);
});
});
});

0 comments on commit 750aa1c

Please sign in to comment.