@dwn-protocol/id-sdk
Version:
SDK for accessing the features and capabilities
109 lines (108 loc) • 5.82 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as Sdk from '@dwn-protocol/id';
import ms from 'ms';
import { IDUserAgent } from './user-agent/index.js';
import { DwnApi } from './dwn-api.js';
import { DidApi } from './did-api.js';
import { getServiceDwnEndpoints } from './service-options.js';
import { DidKeyMethod, DidDhtMethod, DidIonMethod } from './dids/index.js';
import { Metadata } from './interfaces/metadata.js';
import { Queue } from './interfaces/queue.js';
import { Services } from './interfaces/services.js';
import { Transactions } from './interfaces/transactions.js';
import { Jose } from './crypto/index.js';
import { VcApi } from './vc-api.js';
import { Jws } from '@dwn-protocol/id';
export class IDDwn {
constructor(options) {
const { agent, connectedDid } = options;
this.agent = agent;
this.connectedDid = connectedDid;
this.did = new DidApi({ agent, connectedDid });
this.dwn = new DwnApi({ agent, connectedDid });
this.metadata = new Metadata({ agent, connectedDid });
this.protocol = Sdk;
this.queue = new Queue({ agent, connectedDid });
this.services = new Services({ agent, connectedDid });
this.transactions = new Transactions({ agent, connectedDid });
this.utils = { DidKeyMethod, DidDhtMethod, DidIonMethod, Jose, Jws };
this.vc = new VcApi({ agent, connectedDid });
}
/**
* Connects to a {@link IDAgent}. Defaults to creating a local {@link IDUserAgent}
* if one isn't provided.
*
* @param options - optional overrides
* @returns
*/
static connect(options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let { agent, appData, connectedDid, sync, serviceOptions, passphrase } = options;
if (agent === undefined) {
// Create the agent.
const userAgent = yield IDUserAgent.create({ appData });
agent = userAgent;
if (passphrase === undefined) {
passphrase = 'insecure-static-phrase';
}
// Start the agent.
yield userAgent.start({ passphrase });
// Connect attempt failed or was rejected so fallback to local user agent.
// if (IDUserAgent.isConnected() === false) {
// Query the Agent's DWN tenant for identity records.
const identities = yield userAgent.identityManager.list();
const storedIdentities = identities.length;
// If an existing identity is not found, create a new one.
if (storedIdentities === 0) {
// Use the specified DWN endpoints or get default relayer nodes.
const serviceEndpointNodes = (_a = serviceOptions === null || serviceOptions === void 0 ? void 0 : serviceOptions.dwnEndpoints) !== null && _a !== void 0 ? _a : yield getServiceDwnEndpoints();
// Generate ION DID service and key set.
const didOptions = yield DidIonMethod.generateDwnOptions({ serviceEndpointNodes });
// Generate a new Identity for the end-user.
const identity = yield userAgent.identityManager.create({
name: 'Default',
didMethod: 'ion',
didOptions,
kms: 'local'
});
/** Import the Identity metadata to the User Agent's tenant so that it can be restored
* on subsequent launches or page reloads. */
yield userAgent.identityManager.import({ identity, context: userAgent.agentDid });
// Set the newly created identity as the connected DID.
// connectedDid = restoreDid? restoreDid : identity.did;
connectedDid = identity.did;
}
else {
// An existing identity was found in the User Agent's tenant.
const [identity] = identities;
// Set the stored identity as the connected DID.
// connectedDid = restoreDid? restoreDid : identity.did;
connectedDid = identity.did;
}
// }
// Enable sync, unless disabled.
if (sync !== 'off') {
// First, register the user identity for sync.
yield userAgent.syncManager.registerIdentity({ did: connectedDid });
// Enable sync using the specified interval or default.
sync !== null && sync !== void 0 ? sync : (sync = '1m');
userAgent.syncManager.startSync({ interval: ms(sync) })
.catch((error) => __awaiter(this, void 0, void 0, function* () {
console.error(`Sync failed: ${error}`);
}));
}
}
const iddwn = new IDDwn({ agent, connectedDid });
return { iddwn, did: connectedDid };
});
}
}