import { Web5Rpc, DidRequest, VcResponse, DidResponse, DwnResponse, DidInterface, DwnInterface, SendVcRequest, SendDwnRequest, ProcessVcRequest, ProcessDwnRequest, Web5PlatformAgent, AgentPermissionsApi, } from '@web5/agent'; import { LevelStore } from '@web5/common'; import { BearerDid, DidDht, DidJwk, DidResolverCacheLevel } from '@web5/dids'; import { AgentDidApi, AgentDwnApi, DwnDidStore, DwnKeyStore, AgentSyncApi, Web5RpcClient, AgentCryptoApi, AgentKeyManager, HdIdentityVault, LocalKeyManager, SyncEngineLevel, AgentIdentityApi, DwnIdentityStore, } from '@web5/agent'; /** * Initialization parameters for {@link Web5IdentityAgent}, including an optional recovery phrase that * can be used to derive keys to encrypt the vault and generate a DID. */ export type AgentInitializeParams = { /** * The password used to secure the Agent vault. * * The password selected should be strong and securely managed to prevent unauthorized access. */ password: string; /** * An optional recovery phrase used to deterministically generate the cryptographic keys for the * Agent vault. * * Supplying this phrase enables the vault's contents to be restored or replicated across devices. * If omitted, a new phrase is generated, which should be securely recorded for future recovery needs. */ recoveryPhrase?: string; }; export type AgentStartParams = { /** * The password used to unlock the previously initialized Agent vault. */ password: string; } export type AgentParams = { /** Optional. The Decentralized Identifier (DID) representing this Web5 User Agent. */ agentDid?: BearerDid; /** Encrypted vault used for managing the Agent's DID and associated keys. */ agentVault: HdIdentityVault; /** Provides cryptographic capabilties like signing, encryption, hashing and key derivation. */ cryptoApi: AgentCryptoApi; /** Specifies the local path to be used by the Agent's persistent data stores. */ dataPath?: string; /** Facilitates DID operations including create, update, and resolve. */ didApi: AgentDidApi; /** Facilitates DWN operations including processing and sending requests. */ dwnApi: AgentDwnApi; /** Facilitates decentralized Identity operations including create, import, and export. */ identityApi: AgentIdentityApi; /** Responsible for securely managing the cryptographic keys of the agent. */ keyManager: TKeyManager; /** Facilitates fetching, requesting, creating, revoking and validating revocation status of permissions */ permissionsApi: AgentPermissionsApi; /** Remote procedure call (RPC) client used to communicate with other Web5 services. */ rpcClient: Web5Rpc; /** Facilitates data synchronization of DWN records between nodes. */ syncApi: AgentSyncApi; } export class Web5IdentityAgent implements Web5PlatformAgent { public crypto: AgentCryptoApi; public did: AgentDidApi; public dwn: AgentDwnApi; public identity: AgentIdentityApi; public keyManager: TKeyManager; public permissions: AgentPermissionsApi; public rpc: Web5Rpc; public sync: AgentSyncApi; public vault: HdIdentityVault; private _agentDid?: BearerDid; constructor(params: AgentParams) { this._agentDid = params.agentDid; this.crypto = params.cryptoApi; this.did = params.didApi; this.dwn = params.dwnApi; this.identity = params.identityApi; this.keyManager = params.keyManager; this.permissions = params.permissionsApi; this.rpc = params.rpcClient; this.sync = params.syncApi; this.vault = params.agentVault; // Set this agent to be the default agent. this.did.agent = this; this.dwn.agent = this; this.identity.agent = this; this.keyManager.agent = this; this.permissions.agent = this; this.sync.agent = this; } get agentDid(): BearerDid { if (this._agentDid === undefined) { throw new Error( 'Web5UserAgent: The "agentDid" property is not set. Ensure the agent is properly ' + 'initialized and a DID is assigned.' ); } return this._agentDid; } set agentDid(did: BearerDid) { this._agentDid = did; } /** * If any of the required agent components are not provided, instantiate default implementations. */ public static async create({ dataPath = 'DATA/AGENT', agentDid, agentVault, cryptoApi, didApi, dwnApi, identityApi, keyManager, permissionsApi, rpcClient, syncApi }: Partial = {} ): Promise { agentVault ??= new HdIdentityVault({ keyDerivationWorkFactor : 210_000, store : new LevelStore({ location: `${dataPath}/VAULT_STORE` }) }); cryptoApi ??= new AgentCryptoApi(); didApi ??= new AgentDidApi({ didMethods : [DidDht, DidJwk], resolverCache : new DidResolverCacheLevel({ location: `${dataPath}/DID_RESOLVERCACHE` }), store : new DwnDidStore() }); dwnApi ??= new AgentDwnApi({ dwn: await AgentDwnApi.createDwn({ dataPath, didResolver: didApi }) }); identityApi ??= new AgentIdentityApi({ store: new DwnIdentityStore() }); keyManager ??= new LocalKeyManager({ keyStore: new DwnKeyStore() }); permissionsApi ??= new AgentPermissionsApi(); rpcClient ??= new Web5RpcClient(); syncApi ??= new AgentSyncApi({ syncEngine: new SyncEngineLevel({ dataPath }) }); // Instantiate the Agent using the provided or default components. return new Web5IdentityAgent({ agentDid, agentVault, cryptoApi, didApi, dwnApi, keyManager, permissionsApi, identityApi, rpcClient, syncApi }); } public async firstLaunch(): Promise { // Check whether data vault is already initialize return await this.vault.isInitialized() === false; } /** * Initializes the User Agent with a password, and optionally a recovery phrase. * * This method is typically called once, the first time the Agent is launched, and is responsible * for setting up the agent's operational environment, cryptographic key material, and readiness * for processing Web5 requests. * * The password is used to secure the Agent vault, and the recovery phrase is used to derive the * cryptographic keys for the vault. If a recovery phrase is not provided, a new recovery phrase * will be generated and returned. The password should be chosen and entered by the end-user. */ public async initialize({ password, recoveryPhrase }: AgentInitializeParams): Promise { // Initialize the Agent vault. recoveryPhrase = await this.vault.initialize({ password, recoveryPhrase }); return recoveryPhrase; } async processDidRequest( request: DidRequest ): Promise> { return this.did.processRequest(request); } public async processDwnRequest( request: ProcessDwnRequest ): Promise> { return this.dwn.processRequest(request); } public async processVcRequest(_request: ProcessVcRequest): Promise { throw new Error('Not implemented'); } public async sendDidRequest( _request: DidRequest ): Promise> { throw new Error('Not implemented'); } public async sendDwnRequest( request: SendDwnRequest ): Promise> { return this.dwn.sendRequest(request); } public async sendVcRequest(_request: SendVcRequest): Promise { throw new Error('Not implemented'); } public async start({ password }: AgentInitializeParams): Promise { // If the Agent vault is locked, unlock it. if (this.vault.isLocked()) { await this.vault.unlock({ password }); } // Set the Agent's DID. this.agentDid = await this.vault.getDid(); } }