Skip to content

Commit

Permalink
DWN Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Jul 17, 2024
1 parent 8baa679 commit 87c7fb2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './bearer-identity.js';
export * from './crypto-api.js';
export * from './did-api.js';
export * from './dwn-api.js';
export * from './dwn-registrar.js';
export * from './hd-identity-vault.js';
export * from './identity-api.js';
export * from './local-key-manager.js';
Expand Down
44 changes: 42 additions & 2 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { DwnRecordsPermissionScope, DwnProtocolDefinition, DwnRegistrar } from '@web5/agent';
import { VcApi } from './vc-api.js';
import { Web5UserAgent } from '@web5/user-agent';

Expand Down Expand Up @@ -147,6 +147,17 @@ export type Web5ConnectOptions = {
* See {@link DidCreateOptions} for available options.
*/
didCreateOptions?: DidCreateOptions;

/**
* If the `registration` option is provided, the agent DID and the connected DID will be registered with the DWN endpoints provided by `techPreview` or `didCreateOptions`.
*
* If registration fails, the `onFailure` callback will be called with the error.
* If registration is successful, the `onSuccess` callback will be called.
*/
registration? : {
onSuccess: () => void;
onFailure: (error: any) => void;
}
}

/**
Expand Down Expand Up @@ -224,7 +235,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, didCreateOptions
agent, agentVault, connectedDid, password, recoveryPhrase, sync, techPreview, didCreateOptions, registration
}: Web5ConnectOptions = {}): Promise<Web5ConnectResult> {
if (agent === undefined) {
// A custom Web5Agent implementation was not specified, so use default managed user agent.
Expand Down Expand Up @@ -313,6 +324,35 @@ export class Web5 {
connectedDid = identity.did.uri;
}

if (registration !== undefined) {
// If a registration object is passed, we attempt to register the AgentDID and the ConnectedDID with the DWN endpoints provided

const serviceEndpointNodes = techPreview?.dwnEndpoints ?? didCreateOptions?.dwnEndpoints;
for (const dwnEndpoint of serviceEndpointNodes) {
try {

// check if endpoint needs registration
const serverInfo = await userAgent.rpc.getServerInfo(dwnEndpoint);
if (serverInfo.registrationRequirements.length === 0) {
// no registration required
continue;
}

// register the agent DID
await DwnRegistrar.registerTenant(dwnEndpoint, agent.agentDid.uri);

// register the connected Identity DID
await DwnRegistrar.registerTenant(dwnEndpoint, connectedDid);

// if no failure occurs, call the onSuccess callback
registration.onSuccess();
} catch(error) {
// for any failure, call the onFailure callback with the error
registration.onFailure(error);
}
}
}

// Enable sync, unless explicitly disabled.
if (sync !== 'off') {
// First, register the user identity for sync.
Expand Down

0 comments on commit 87c7fb2

Please sign in to comment.