UNPKG

@foreverrbum/ethsign

Version:

This package will allow you to electronically sign documents within your application

633 lines (506 loc) 21.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.updateNetwork = exports.resetContractAndProvider = exports.renderProviderText = exports.initializeProvider = exports.getWallet = exports.connectNetworkRequestResult = exports.connectNetwork = void 0; var _ethers = require("ethers"); var _fortmatic = _interopRequireDefault(require("fortmatic")); var _torusEmbed = _interopRequireDefault(require("@toruslabs/torus-embed")); var _chains = require("../chains"); var _EthSign = _interopRequireDefault(require("../../artifacts/EthSign.json")); var _mexa = require("@biconomy/mexa"); var Sentry = _interopRequireWildcard(require("@sentry/react")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var fortmatic_key = 'pk_live_6D2FFF6E41301BF0'; var fortmatic_test_key = 'pk_test_F4BDDED1F885923E'; var updateNetwork = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(provider, handleNetwork) { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return provider.getNetwork().then(function (network) { var chain = (0, _chains.getChain)(network.chainId); handleNetwork(chain); }); case 3: _context.next = 9; break; case 5: _context.prev = 5; _context.t0 = _context["catch"](0); handleNetwork(null); return _context.abrupt("return"); case 9: case "end": return _context.stop(); } } }, _callee, null, [[0, 5]]); })); return function updateNetwork(_x, _x2) { return _ref.apply(this, arguments); }; }(); exports.updateNetwork = updateNetwork; var initializeProvider = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(handleProvider, handleNetwork, handleWalletName) { var name; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: name = getWallet(); if (name) { window.ethereum.request({ method: 'eth_requestAccounts' }).then( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var provider; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: provider = new _ethers.ethers.providers.Web3Provider(window.ethereum); handleWalletName(name); handleProvider(provider); _context2.next = 5; return updateNetwork(provider, handleNetwork); case 5: case "end": return _context2.stop(); } } }, _callee2); }))); } case 2: case "end": return _context3.stop(); } } }, _callee3); })); return function initializeProvider(_x3, _x4, _x5) { return _ref2.apply(this, arguments); }; }(); exports.initializeProvider = initializeProvider; var getWallet = function getWallet() { if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") { if (window.ethereum.isMetaMask) { return 'Metamask'; } if (window.ethereum.isTorus) { return 'Torus'; } if (window.ethereum.isImToken) { return 'imToken'; } } return null; }; exports.getWallet = getWallet; var renderProviderText = function renderProviderText(account) { if (account && account.address) { var providerTextList = { Metamask: 'Add to Metamask', imToken: 'Add to imToken', Wallet: 'Add to Wallet' }; return providerTextList[getProvider()]; } else { return 'Connect wallet'; } }; exports.renderProviderText = renderProviderText; var toHex = function toHex(num) { return '0x' + num.toString(16); }; var connectNetwork = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(chain, provider, handleProvider, handleContract, handleNetwork, walletName, handleFm, torus, handleTorus, finishedCallback) { var ethAccount, fm, _provider, networkId, deployedNetwork, contract, _provider2, _networkId, _deployedNetwork, _contract, params, res, _res; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: ethAccount = null; _context4.prev = 1; _context4.next = 4; return provider.getSigner().getAddress(); case 4: ethAccount = _context4.sent; _context4.next = 11; break; case 7: _context4.prev = 7; _context4.t0 = _context4["catch"](1); console.log(_context4.t0); return _context4.abrupt("return"); case 11: if (!(walletName === 'Fortmatic')) { _context4.next = 28; break; } fm = null; if (chain.chainId == 1) { fm = new _fortmatic.default(fortmatic_key); } else { fm = new _fortmatic.default((0, _chains.isTestnet)(chain.chainId) ? fortmatic_test_key : fortmatic_key, (0, _chains.getFortmaticChainOptions)(chain.chainId)); } _provider = new _ethers.ethers.providers.Web3Provider(fm.getProvider()); _context4.next = 17; return _provider.getNetwork(); case 17: networkId = _context4.sent.chainId; deployedNetwork = _EthSign.default.networks[networkId]; contract = new _ethers.ethers.Contract(deployedNetwork.address, _EthSign.default.abi, _provider); if (handleFm) { handleFm(fm); } if (handleProvider) { handleProvider(_provider); } if (handleContract) { handleContract(contract); } if (handleNetwork) { handleNetwork(chain); } if (finishedCallback) { finishedCallback(true); } return _context4.abrupt("return", chain); case 28: if (!(walletName === 'Torus')) { _context4.next = 53; break; } if (torus) { _context4.next = 37; break; } torus = new _torusEmbed.default(); _context4.next = 33; return torus.init((0, _chains.getTorusChainOptions)(chain.chainId)); case 33: _context4.next = 35; return torus.login(); case 35: _context4.next = 39; break; case 37: _context4.next = 39; return torus.setProvider((0, _chains.getTorusChainSwitchOptions)(chain.chainId)); case 39: _provider2 = new _ethers.ethers.providers.Web3Provider(torus.provider); _context4.next = 42; return _provider2.getNetwork(); case 42: _networkId = _context4.sent.chainId; _deployedNetwork = _EthSign.default.networks[_networkId]; _contract = new _ethers.ethers.Contract(_deployedNetwork.address, _EthSign.default.abi, _provider2); if (handleTorus) { handleTorus(torus); } if (handleProvider) { handleProvider(_provider2); } if (handleNetwork) { handleNetwork(chain); } if (handleContract) { handleContract(_contract); } if (finishedCallback) { finishedCallback(true); } return _context4.abrupt("return", chain); case 53: params = { chainId: toHex(chain.chainId), // A 0x-prefixed hexadecimal string chainName: chain.name, nativeCurrency: { name: chain.nativeCurrency.name, symbol: chain.nativeCurrency.symbol, // 2-6 characters long decimals: chain.nativeCurrency.decimals }, rpcUrls: chain.rpc, blockExplorerUrls: [chain.explorers && chain.explorers.length > 0 && chain.explorers[0].url ? chain.explorers[0].url : chain.infoURL] }; _context4.prev = 54; _context4.next = 57; return window.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: params.chainId }] }); case 57: res = _context4.sent; connectNetworkRequestResult(chain, handleProvider, handleContract, handleNetwork, finishedCallback); _context4.next = 81; break; case 61: _context4.prev = 61; _context4.t1 = _context4["catch"](54); if (!(_context4.t1.code === 4902)) { _context4.next = 77; break; } _context4.prev = 64; _res = window.ethereum.request({ method: 'wallet_addEthereumChain', params: [params, ethAccount] }); connectNetworkRequestResult(chain, handleProvider, handleContract, handleNetwork, finishedCallback); _context4.next = 75; break; case 69: _context4.prev = 69; _context4.t2 = _context4["catch"](64); console.log(_context4.t2); if (finishedCallback) { finishedCallback(true); } Sentry.captureException(_context4.t2); return _context4.abrupt("return", _context4.t2); case 75: _context4.next = 81; break; case 77: // this is some other network switch error console.log(_context4.t1); if (finishedCallback) { finishedCallback(true); } Sentry.captureException(_context4.t1); return _context4.abrupt("return", _context4.t1); case 81: return _context4.abrupt("return", chain); case 82: case "end": return _context4.stop(); } } }, _callee4, null, [[1, 7], [54, 61], [64, 69]]); })); return function connectNetwork(_x6, _x7, _x8, _x9, _x10, _x11, _x12, _x13, _x14, _x15) { return _ref4.apply(this, arguments); }; }(); exports.connectNetwork = connectNetwork; var connectNetworkRequestResult = /*#__PURE__*/function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(chain, handleProvider, handleContract, handleNetwork, finishedCallback) { var provider, deployedNetwork, contract; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: provider = new _ethers.ethers.providers.Web3Provider(window.ethereum); handleProvider(provider); if (handleContract) { deployedNetwork = _EthSign.default.networks[chain.chainId]; contract = new _ethers.ethers.Contract(deployedNetwork.address, _EthSign.default.abi, provider); handleContract(contract); } if (finishedCallback) { finishedCallback(true); } if (handleNetwork) { handleNetwork(chain); } case 5: case "end": return _context5.stop(); } } }, _callee5); })); return function connectNetworkRequestResult(_x16, _x17, _x18, _x19, _x20) { return _ref5.apply(this, arguments); }; }(); // This function supports only torus, imtoken and metamask wallets because we currently don't support network switch for fortmatic exports.connectNetworkRequestResult = connectNetworkRequestResult; var resetContractAndProvider = /*#__PURE__*/function () { var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(chainId, handleProvider, handleContract, torus) { var biconomy, provider, _getChain, originalProvider, _getChain2, _originalProvider, deployedNetwork, signer, account, contract; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: biconomy = null; provider = null; if (!torus) { _context7.next = 32; break; } _context7.t2 = _chains.getChain; _context7.next = 6; return torus.ethereum.request({ method: 'eth_chainId' }); case 6: _context7.t3 = _context7.sent; _context7.t4 = _getChain = (0, _context7.t2)(_context7.t3); _context7.t1 = _context7.t4 !== null; if (!_context7.t1) { _context7.next = 11; break; } _context7.t1 = _getChain !== void 0; case 11: _context7.t0 = _context7.t1; if (!_context7.t0) { _context7.next = 14; break; } _context7.t0 = _getChain.biconomySupport; case 14: if (!_context7.t0) { _context7.next = 28; break; } originalProvider = new _ethers.ethers.providers.Web3Provider(torus.ethereum); _context7.t5 = _mexa.Biconomy; _context7.t6 = originalProvider; _context7.t7 = _chains.getChain; _context7.next = 21; return torus.ethereum.request({ method: 'eth_chainId' }); case 21: _context7.t8 = _context7.sent; _context7.t9 = (0, _context7.t7)(_context7.t8).biconomyKey; _context7.t10 = { apiKey: _context7.t9 }; biconomy = new _context7.t5(_context7.t6, _context7.t10); provider = new _ethers.ethers.providers.Web3Provider(biconomy); _context7.next = 29; break; case 28: provider = new _ethers.ethers.providers.Web3Provider(torus.ethereum); case 29: handleProvider(provider); _context7.next = 60; break; case 32: if (!(window.ethereum != null && window.ethereum != undefined)) { _context7.next = 60; break; } _context7.t13 = _chains.getChain; _context7.next = 36; return window.ethereum.request({ method: 'eth_chainId' }); case 36: _context7.t14 = _context7.sent; _context7.t15 = _getChain2 = (0, _context7.t13)(_context7.t14); _context7.t12 = _context7.t15 !== null; if (!_context7.t12) { _context7.next = 41; break; } _context7.t12 = _getChain2 !== void 0; case 41: _context7.t11 = _context7.t12; if (!_context7.t11) { _context7.next = 44; break; } _context7.t11 = _getChain2.biconomySupport; case 44: if (!_context7.t11) { _context7.next = 58; break; } _originalProvider = new _ethers.ethers.providers.Web3Provider(window.ethereum); _context7.t16 = _mexa.Biconomy; _context7.t17 = _originalProvider; _context7.t18 = _chains.getChain; _context7.next = 51; return window.ethereum.request({ method: 'eth_chainId' }); case 51: _context7.t19 = _context7.sent; _context7.t20 = (0, _context7.t18)(_context7.t19).biconomyKey; _context7.t21 = { apiKey: _context7.t20 }; biconomy = new _context7.t16(_context7.t17, _context7.t21); provider = new _ethers.ethers.providers.Web3Provider(biconomy); _context7.next = 59; break; case 58: provider = new _ethers.ethers.providers.Web3Provider(window.ethereum); case 59: handleProvider(provider); case 60: // Handle the contract deployedNetwork = _EthSign.default.networks[chainId]; _context7.next = 63; return provider.getSigner(); case 63: signer = _context7.sent; _context7.next = 66; return signer.getAddress(); case 66: account = _context7.sent; contract = null; if (!(biconomy == null)) { _context7.next = 73; break; } contract = new _ethers.ethers.Contract(deployedNetwork.address, _EthSign.default.abi, provider); console.log("EthSign: Initialized normally."); _context7.next = 75; break; case 73: _context7.next = 75; return new Promise(function (resolve, reject) { biconomy.onEvent(biconomy.READY, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: contract = new _ethers.ethers.Contract(deployedNetwork.address, _EthSign.default.abi, biconomy.getSignerByAddress(account)); console.log("EthSign: Initialized with Mexa."); resolve(); case 3: case "end": return _context6.stop(); } } }, _callee6); }))).onEvent(biconomy.ERROR, function (error, message) { console.log("EthSign: Failed to initialize with Mexa."); console.log(message); console.log(error); reject(); }); }); case 75: handleContract(contract); case 76: case "end": return _context7.stop(); } } }, _callee7); })); return function resetContractAndProvider(_x21, _x22, _x23, _x24) { return _ref6.apply(this, arguments); }; }(); exports.resetContractAndProvider = resetContractAndProvider;