UNPKG

@leordev-web5/api

Version:

SDK for accessing the features and capabilities of Web5

179 lines 9.69 kB
"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 }; } }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web5 = void 0; var ms_1 = __importDefault(require("ms")); 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"); var dids_1 = require("@web5/dids"); /** * 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. * * @beta */ var Web5 = /** @class */ (function () { function Web5(options) { var agent = options.agent, connectedDid = options.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 @web5/agent#Web5Agent}. Defaults to creating a local {@link @web5/user-agent#Web5UserAgent} * if one isn't provided. * * @param options - optional overrides * @returns */ Web5.connect = function (options) { var _a; if (options === void 0) { options = {}; } return __awaiter(this, void 0, void 0, function () { var agent, appData, connectedDid, sync, techPreview, userAgent, notConnected, identities, storedIdentities, serviceEndpointNodes, _b, didOptions, identity, _c, identity, web5; return __generator(this, function (_d) { switch (_d.label) { case 0: agent = options.agent, appData = options.appData, connectedDid = options.connectedDid, sync = options.sync, techPreview = options.techPreview; if (!(agent === undefined)) return [3 /*break*/, 13]; return [4 /*yield*/, user_agent_1.Web5UserAgent.create({ appData: appData })]; case 1: userAgent = _d.sent(); agent = userAgent; // Start the agent. return [4 /*yield*/, userAgent.start({ passphrase: 'insecure-static-phrase' })]; case 2: // Start the agent. _d.sent(); notConnected = true; if (!notConnected) return [3 /*break*/, 11]; return [4 /*yield*/, userAgent.identityManager.list()]; case 3: identities = _d.sent(); storedIdentities = identities.length; if (!(storedIdentities === 0)) return [3 /*break*/, 10]; if (!((_a = techPreview === null || techPreview === void 0 ? void 0 : techPreview.dwnEndpoints) !== null && _a !== void 0)) return [3 /*break*/, 4]; _b = _a; return [3 /*break*/, 6]; case 4: return [4 /*yield*/, (0, tech_preview_js_1.getTechPreviewDwnEndpoints)()]; case 5: _b = _d.sent(); _d.label = 6; case 6: serviceEndpointNodes = _b; return [4 /*yield*/, dids_1.DidIonMethod.generateDwnOptions({ serviceEndpointNodes: serviceEndpointNodes })]; case 7: didOptions = _d.sent(); return [4 /*yield*/, userAgent.identityManager.create({ name: 'Default', didMethod: 'ion', didOptions: didOptions, kms: 'local' })]; case 8: identity = _d.sent(); /** Import the Identity metadata to the User Agent's tenant so that it can be restored * on subsequent launches or page reloads. */ return [4 /*yield*/, userAgent.identityManager.import({ identity: identity, context: userAgent.agentDid })]; case 9: /** Import the Identity metadata to the User Agent's tenant so that it can be restored * on subsequent launches or page reloads. */ _d.sent(); // Set the newly created identity as the connected DID. connectedDid = identity.did; return [3 /*break*/, 11]; case 10: if (storedIdentities === 1) { _c = __read(identities, 1), identity = _c[0]; // Set the stored identity as the connected DID. connectedDid = identity.did; } else { throw new Error("connect() failed due to unexpected state: ".concat(storedIdentities, " stored identities")); } _d.label = 11; case 11: if (!(sync !== 'off')) return [3 /*break*/, 13]; // First, register the user identity for sync. return [4 /*yield*/, userAgent.syncManager.registerIdentity({ did: connectedDid })]; case 12: // First, register the user identity for sync. _d.sent(); // Enable sync using the specified interval or default. sync !== null && sync !== void 0 ? sync : (sync = '2m'); userAgent.syncManager.startSync({ interval: (0, ms_1.default)(sync) }) .catch(function (error) { console.error("Sync failed: ".concat(error)); }); _d.label = 13; case 13: web5 = new Web5({ agent: agent, connectedDid: connectedDid }); return [2 /*return*/, { web5: web5, did: connectedDid }]; } }); }); }; return Web5; }()); exports.Web5 = Web5; //# sourceMappingURL=web5.js.map