@enbox/identity-agent
Version:
| A library for building Web5 identity management applications | | ------------------------------------------------------------ |
240 lines • 12.5 kB
JavaScript
"use strict";
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Web5IdentityAgent = void 0;
var agent_1 = require("@enbox/agent");
var common_1 = require("@enbox/common");
var dids_1 = require("@enbox/dids");
var agent_2 = require("@enbox/agent");
var Web5IdentityAgent = /** @class */ (function () {
function Web5IdentityAgent(params) {
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;
}
Object.defineProperty(Web5IdentityAgent.prototype, "agentDid", {
get: function () {
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: function (did) {
this._agentDid = did;
},
enumerable: false,
configurable: true
});
/**
* If any of the required agent components are not provided, instantiate default implementations.
*/
Web5IdentityAgent.create = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.dataPath, dataPath = _c === void 0 ? 'DATA/AGENT' : _c, agentDid = _b.agentDid, agentVault = _b.agentVault, cryptoApi = _b.cryptoApi, didApi = _b.didApi, dwnApi = _b.dwnApi, identityApi = _b.identityApi, keyManager = _b.keyManager, permissionsApi = _b.permissionsApi, rpcClient = _b.rpcClient, syncApi = _b.syncApi;
return __awaiter(this, void 0, void 0, function () {
var _d, _e;
var _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
agentVault !== null && agentVault !== void 0 ? agentVault : (agentVault = new agent_2.HdIdentityVault({
keyDerivationWorkFactor: 210000,
store: new common_1.LevelStore({ location: "".concat(dataPath, "/VAULT_STORE") })
}));
cryptoApi !== null && cryptoApi !== void 0 ? cryptoApi : (cryptoApi = new agent_2.AgentCryptoApi());
didApi !== null && didApi !== void 0 ? didApi : (didApi = new agent_2.AgentDidApi({
didMethods: [dids_1.DidDht, dids_1.DidJwk],
resolverCache: new dids_1.DidResolverCacheLevel({ location: "".concat(dataPath, "/DID_RESOLVERCACHE") }),
store: new agent_2.DwnDidStore()
}));
if (!(dwnApi !== null && dwnApi !== void 0)) return [3 /*break*/, 1];
_d = dwnApi;
return [3 /*break*/, 3];
case 1:
_e = agent_2.AgentDwnApi.bind;
_f = {};
return [4 /*yield*/, agent_2.AgentDwnApi.createDwn({ dataPath: dataPath, didResolver: didApi })];
case 2:
_d = (dwnApi = new (_e.apply(agent_2.AgentDwnApi, [void 0, (_f.dwn = _g.sent(),
_f)]))());
_g.label = 3;
case 3:
_d;
identityApi !== null && identityApi !== void 0 ? identityApi : (identityApi = new agent_2.AgentIdentityApi({ store: new agent_2.DwnIdentityStore() }));
keyManager !== null && keyManager !== void 0 ? keyManager : (keyManager = new agent_2.LocalKeyManager({ keyStore: new agent_2.DwnKeyStore() }));
permissionsApi !== null && permissionsApi !== void 0 ? permissionsApi : (permissionsApi = new agent_1.AgentPermissionsApi());
rpcClient !== null && rpcClient !== void 0 ? rpcClient : (rpcClient = new agent_2.Web5RpcClient());
syncApi !== null && syncApi !== void 0 ? syncApi : (syncApi = new agent_2.AgentSyncApi({ syncEngine: new agent_2.SyncEngineLevel({ dataPath: dataPath }) }));
// Instantiate the Agent using the provided or default components.
return [2 /*return*/, new Web5IdentityAgent({
agentDid: agentDid,
agentVault: agentVault,
cryptoApi: cryptoApi,
didApi: didApi,
dwnApi: dwnApi,
keyManager: keyManager,
permissionsApi: permissionsApi,
identityApi: identityApi,
rpcClient: rpcClient,
syncApi: syncApi
})];
}
});
});
};
Web5IdentityAgent.prototype.firstLaunch = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.vault.isInitialized()];
case 1:
// Check whether data vault is already initialize
return [2 /*return*/, (_a.sent()) === 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.
*/
Web5IdentityAgent.prototype.initialize = function (_a) {
var password = _a.password, recoveryPhrase = _a.recoveryPhrase;
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.vault.initialize({ password: password, recoveryPhrase: recoveryPhrase })];
case 1:
// Initialize the Agent vault.
recoveryPhrase = _b.sent();
return [2 /*return*/, recoveryPhrase];
}
});
});
};
Web5IdentityAgent.prototype.processDidRequest = function (request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.did.processRequest(request)];
});
});
};
Web5IdentityAgent.prototype.processDwnRequest = function (request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.dwn.processRequest(request)];
});
});
};
Web5IdentityAgent.prototype.processVcRequest = function (_request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error('Not implemented');
});
});
};
Web5IdentityAgent.prototype.sendDidRequest = function (_request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error('Not implemented');
});
});
};
Web5IdentityAgent.prototype.sendDwnRequest = function (request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.dwn.sendRequest(request)];
});
});
};
Web5IdentityAgent.prototype.sendVcRequest = function (_request) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error('Not implemented');
});
});
};
Web5IdentityAgent.prototype.start = function (_a) {
var password = _a.password;
return __awaiter(this, void 0, void 0, function () {
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!this.vault.isLocked()) return [3 /*break*/, 2];
return [4 /*yield*/, this.vault.unlock({ password: password })];
case 1:
_c.sent();
_c.label = 2;
case 2:
// Set the Agent's DID.
_b = this;
return [4 /*yield*/, this.vault.getDid()];
case 3:
// Set the Agent's DID.
_b.agentDid = _c.sent();
return [2 /*return*/];
}
});
});
};
return Web5IdentityAgent;
}());
exports.Web5IdentityAgent = Web5IdentityAgent;
//# sourceMappingURL=identity-agent.js.map