UNPKG

@test-org122/hypernet-core

Version:

Hypernet Core. Represents the SDK for running the Hypernet Protocol.

93 lines 4.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserNodeProvider = void 0; const vector_browser_node_1 = require("@connext/vector-browser-node"); const errors_1 = require("@interfaces/objects/errors"); const objects_1 = require("@interfaces/objects"); const utils_1 = require("@test-org122/utils"); class BrowserNodeProvider { constructor(configProvider, contextProvider, blockchainProvider, logUtils, storageUtils, browserNodeFactory) { this.configProvider = configProvider; this.contextProvider = contextProvider; this.blockchainProvider = blockchainProvider; this.logUtils = logUtils; this.storageUtils = storageUtils; this.browserNodeFactory = browserNodeFactory; this.browserNodeResult = null; this.browserNode = null; } initialize() { if (this.browserNodeResult == null) { let config; let signer; let context; this.browserNodeResult = utils_1.ResultUtils.combine([ this.configProvider.getConfig(), this.blockchainProvider.getSigner(), this.contextProvider.getContext(), this.browserNodeFactory.factoryBrowserNode(), ]) .andThen((vals) => { [config, signer, context, this.browserNode] = vals; // If no account is set in the context, then we have to error out. // Normally we would use getInitializedContext, but in this case, you need // the browser node up and running to get the publicIdentifier, which // is part of what qualifies for getInitializedContext. I do not see // a better way that doesn't involve a huge refactor. if (context.account == null) { return objects_1.errAsync(new errors_1.CoreUninitializedError("Account is not set in BrowserNodeProvider.initialize")); } // Check if the user has a signature in local storage for this account const storedSignature = this.storageUtils.getSessionItem(`account-${context.account}-signature`); if (storedSignature != null) { return objects_1.okAsync([context.account, storedSignature]); } return utils_1.ResultUtils.combine([ objects_1.ResultAsync.fromPromise(signer.getAddress(), (e) => { return e; }), objects_1.ResultAsync.fromPromise(signer.signMessage(vector_browser_node_1.NonEIP712Message), (e) => { return e; }), ]); }) .andThen((vals) => { const [account, signature] = vals; // Store the signature so you don't have to sign again this.storageUtils.setSessionItem(`account-${account}-signature`, signature); // Initialize the browser node if (this.browserNode == null) { throw new Error("Something is badly broken"); } return this.browserNode.init(signature, account); }) .orElse((e) => { var _a, _b; console.log(e); const shouldAttemptRestore = ((_b = (_a = e.context) === null || _a === void 0 ? void 0 : _a.validationError) !== null && _b !== void 0 ? _b : "").includes("Channel is already setup"); if (shouldAttemptRestore && this.browserNode != null) { return this.browserNode .getStateChannelByParticipants(config.routerPublicIdentifier, config.chainId) .andThen((channelState) => { if (channelState == null && this.browserNode != null) { return this.browserNode.restoreState(config.routerPublicIdentifier, config.chainId); } return objects_1.okAsync(undefined); }); } else { return objects_1.errAsync(e); } }) .map(() => { return this.browserNode; }); } return this.browserNodeResult; } getBrowserNode() { return this.initialize(); } } exports.BrowserNodeProvider = BrowserNodeProvider; //# sourceMappingURL=BrowserNodeProvider.js.map