From 87c7fb27f4fc48f1e2517ef3e648c72676476ed1 Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Wed, 17 Jul 2024 18:15:43 -0400 Subject: [PATCH] DWN Registration --- packages/agent/src/index.ts | 1 + packages/api/src/web5.ts | 44 +++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index 8f63aee6c..4c5220cef 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -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'; diff --git a/packages/api/src/web5.ts b/packages/api/src/web5.ts index f9f566cbc..baf23f5d8 100644 --- a/packages/api/src/web5.ts +++ b/packages/api/src/web5.ts @@ -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'; @@ -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; + } } /** @@ -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 { if (agent === undefined) { // A custom Web5Agent implementation was not specified, so use default managed user agent. @@ -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.