"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.Web5 = void 0; var user_agent_1 = require("@web5/user-agent"); var vc_api_js_1 = require("./vc-api.js"); var dwn_api_js_1 = require("./dwn-api.js"); var did_api_js_1 = require("./did-api.js"); var tech_preview_js_1 = require("./tech-preview.js"); /** * The main Web5 API interface. It manages the creation of a DID if needed, the connection to the * local DWN and all the web5 main foundational APIs such as VC, syncing, etc. */ var Web5 = /** @class */ (function () { function Web5(_a) { var agent = _a.agent, connectedDid = _a.connectedDid; this.agent = agent; this.connectedDid = connectedDid; this.did = new did_api_js_1.DidApi({ agent: agent, connectedDid: connectedDid }); this.dwn = new dwn_api_js_1.DwnApi({ agent: agent, connectedDid: connectedDid }); this.vc = new vc_api_js_1.VcApi({ agent: agent, connectedDid: connectedDid }); } /** * Connects to a {@link Web5Agent}. Defaults to creating a local {@link Web5UserAgent} if one * isn't provided. * * @param options - Optional overrides that can be provided when calling {@link Web5.connect}. * @returns A promise that resolves to a {@link Web5} instance and the connected DID. */ Web5.connect = function (_a) { var _b; var _c = _a === void 0 ? {} : _a, agent = _c.agent, agentVault = _c.agentVault, connectedDid = _c.connectedDid, password = _c.password, recoveryPhrase = _c.recoveryPhrase, sync = _c.sync, techPreview = _c.techPreview; return __awaiter(this, void 0, void 0, function () { var userAgent, notConnected, identity, identities, existingIdentityCount, serviceEndpointNodes, _d, _e, _f, web5; var _g; return __generator(this, function (_h) { switch (_h.label) { case 0: if (!(agent === undefined)) return [3 /*break*/, 17]; return [4 /*yield*/, user_agent_1.Web5UserAgent.create({ agentVault: agentVault })]; case 1: userAgent = _h.sent(); agent = userAgent; // Warn the developer and application user of the security risks of using a static password. if (password === undefined) { password = 'insecure-static-phrase'; console.warn('%cSECURITY WARNING:%c ' + 'You have not set a password, which defaults to a static, guessable value. ' + 'This significantly compromises the security of your data. ' + 'Please configure a secure, unique password.', 'font-weight: bold; color: red;', 'font-weight: normal; color: inherit;'); } return [4 /*yield*/, userAgent.firstLaunch()]; case 2: if (!_h.sent()) return [3 /*break*/, 4]; return [4 /*yield*/, userAgent.initialize({ password: password, recoveryPhrase: recoveryPhrase })]; case 3: recoveryPhrase = _h.sent(); _h.label = 4; case 4: return [4 /*yield*/, userAgent.start({ password: password })]; case 5: _h.sent(); notConnected = true; if (!notConnected) return [3 /*break*/, 15]; identity = void 0; return [4 /*yield*/, userAgent.identity.list()]; case 6: identities = _h.sent(); existingIdentityCount = identities.length; if (!(existingIdentityCount === 0)) return [3 /*break*/, 13]; if (!((_b = techPreview === null || techPreview === void 0 ? void 0 : techPreview.dwnEndpoints) !== null && _b !== void 0)) return [3 /*break*/, 7]; _d = _b; return [3 /*break*/, 9]; case 7: return [4 /*yield*/, (0, tech_preview_js_1.getTechPreviewDwnEndpoints)()]; case 8: _d = _h.sent(); _h.label = 9; case 9: serviceEndpointNodes = _d; return [4 /*yield*/, userAgent.identity.create({ didMethod: 'dht', metadata: { name: 'Default' }, didOptions: { services: [ { id: 'dwn', type: 'DecentralizedWebNode', serviceEndpoint: serviceEndpointNodes, enc: '#enc', sig: '#sig', } ], verificationMethods: [ { algorithm: 'Ed25519', id: 'sig', purposes: ['assertionMethod', 'authentication'] }, { algorithm: 'secp256k1', id: 'enc', purposes: ['keyAgreement'] } ] } })]; case 10: // Generate a new Identity for the end-user. identity = _h.sent(); _f = (_e = userAgent.identity).manage; _g = {}; return [4 /*yield*/, identity.export()]; case 11: // The User Agent will manage the Identity, which ensures it will be available on future // sessions. return [4 /*yield*/, _f.apply(_e, [(_g.portableIdentity = _h.sent(), _g)])]; case 12: // The User Agent will manage the Identity, which ensures it will be available on future // sessions. _h.sent(); return [3 /*break*/, 14]; case 13: if (existingIdentityCount === 1) { // An existing identity was found in the User Agent's tenant. identity = identities[0]; } else { throw new Error("connect() failed due to unexpected state: Expected 1 but found ".concat(existingIdentityCount, " stored identities.")); } _h.label = 14; case 14: // Set the stored identity as the connected DID. connectedDid = identity.did.uri; _h.label = 15; case 15: if (!(sync !== 'off')) return [3 /*break*/, 17]; // First, register the user identity for sync. return [4 /*yield*/, userAgent.sync.registerIdentity({ did: connectedDid })]; case 16: // First, register the user identity for sync. _h.sent(); // Enable sync using the specified interval or default. sync !== null && sync !== void 0 ? sync : (sync = '2m'); userAgent.sync.startSync({ interval: sync }) .catch(function (error) { console.error("Sync failed: ".concat(error)); }); _h.label = 17; case 17: web5 = new Web5({ agent: agent, connectedDid: connectedDid }); return [2 /*return*/, { web5: web5, did: connectedDid, recoveryPhrase: recoveryPhrase }]; } }); }); }; return Web5; }()); exports.Web5 = Web5; //# sourceMappingURL=web5.js.map