UNPKG

@test-org122/hypernet-core

Version:

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

67 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EthersBlockchainProvider = void 0; const objects_1 = require("@interfaces/objects"); const errors_1 = require("@interfaces/objects/errors"); const ethers_1 = require("ethers"); const neverthrow_1 = require("neverthrow"); // This is just a code of avoiding errors in mobile app. // An actuall non metamask provider set up should be implemented in this class. class EthersBlockchainProvider { constructor(externalProvider) { this.externalProvider = externalProvider; this.provider = null; this.signer = null; this.initializationPromise = null; } initialize() { var _a, _b; if (this.externalProvider) { this.provider = (_a = this.externalProvider) === null || _a === void 0 ? void 0 : _a.provider; this.signer = this.provider.getSigner((_b = this.externalProvider) === null || _b === void 0 ? void 0 : _b.address); this.initializationPromise = neverthrow_1.okAsync(undefined); } else { window.ethereum.autoRefreshOnNetworkChange = false; this.initializationPromise = objects_1.ResultAsync.fromPromise(window.ethereum.enable(), (e) => { return new errors_1.BlockchainUnavailableError("Unable to initialize ethereum provider from the window"); }) .map(() => { // A Web3Provider wraps a standard Web3 provider, which is // what Metamask injects as window.ethereum into each page this.provider = new ethers_1.ethers.providers.Web3Provider(window.ethereum); // The Metamask plugin also allows signing transactions to // send ether and pay to change state within the blockchain. // For this, you need the account signer... this.signer = this.provider.getSigner(); return null; }) .map(() => { return; }); } return this.initializationPromise; } /** * getProvider * @return ethers.providers.Web3Provider */ getProvider() { return this.initialize().map(() => { if (this.provider == null) { throw new errors_1.BlockchainUnavailableError("No provider available!"); } return this.provider; }); } getSigner() { return this.initialize().map(() => { if (this.signer == null) { throw new errors_1.BlockchainUnavailableError("No signer available!"); } return this.signer; }); } } exports.EthersBlockchainProvider = EthersBlockchainProvider; //# sourceMappingURL=BlockchainProvider.js.map