@self.id/web
Version:
Read and write records in browsers environments
47 lines (46 loc) • 1.59 kB
JavaScript
import { ThreeIdConnect } from '@3id/connect';
import { Core } from '@self.id/core';
import { DID } from 'dids';
/**
* WebClient extends the {@linkcode core.Core Core class} with authentication support in browser
* environments.
*
* It is exported by the {@linkcode web} module.
*
* ```sh
* import { WebClient } from '@self.id/web'
* ```
*/ export class WebClient extends Core {
/** 3ID Connect instance used internally. */ get threeId() {
return this._threeId;
}
/**
* Create and authenticate a DID instance using the given `authProvider`.
*
* By default, this also attaches the created DID instance to the internal Ceramic client
* instance. This behavior can be disabled by setting `attachToCeramic` to `false`.
*/ async authenticate(authProvider, attachToCeramic = true) {
const did = await this.connect(authProvider);
await did.authenticate();
if (attachToCeramic) {
this.ceramic.did = did;
}
return did;
}
/**
* Create a DID instance using the given `authProvider`.
*
* ⚠️ This method does **not** attempt to authenticate immediately, use {@linkcode authenticate}
* instead if this is the wanted behavior.
*/ async connect(authProvider) {
await this._threeId.connect(authProvider);
return new DID({
provider: this._threeId.getDidProvider(),
resolver: this.resolver
});
}
constructor(params){
super(params);
this._threeId = new ThreeIdConnect(params.connectNetwork ?? params.ceramic);
}
}