UNPKG

@aeternity/aepp-sdk

Version:
135 lines (124 loc) 6.34 kB
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"; import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter"; import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"; import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"; import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties"; import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property"; import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator"; import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty"; import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context2; _forEachInstanceProperty(_context2 = ownKeys(Object(source), true)).call(_context2, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context3; _forEachInstanceProperty(_context3 = ownKeys(Object(source))).call(_context3, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; } /* * ISC License (ISC) * Copyright (c) 2018 aeternity developers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /** * Wallet Detector * * This is the complement to {@link module:@aeternity/aepp-sdk/es/utils}. * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/wallet-detector * @export WalletDetector */ import stampit from '@stamp/it'; import BrowserWindowMessageConnection from './connection/browser-window-message'; import { MESSAGE_DIRECTION, METHODS } from './schema'; /** * WalletDetector stamp * @function * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/wallet-detector * @rtype Stamp * @param {Object} params={} - Initializer object * @param {WalletConnection} params.connection - Connection for listening for wallets * @return {WalletDetector} */ export default stampit({ init: function init() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, connection = _ref.connection; if (!window) throw new Error('Window object not found, you can run wallet detector only in browser'); this.connection = connection || BrowserWindowMessageConnection({ connectionInfo: { id: 'spy' } }); this.wallets = {}; }, methods: { /** * Start scanning * @function scan * @instance * @param {Function} onDetected Call-back function which trigger on new wallet * @return {void} */ scan: function scan(onDetected) { var wallets = this.wallets; this.connection.connect(function (_ref2, origin, source) { var method = _ref2.method, params = _ref2.params; if (!method || !params || method !== METHODS.wallet.readyToConnect || wallets[params.id]) return; var wallet = _objectSpread(_objectSpread({}, params), {}, { getConnection: function getConnection() { var _this = this; return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var isExtension, origin; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: // if detect extension wallet or page wallet isExtension = _this.type === 'extension'; origin = isExtension ? window.origin : _this.origin; return _context.abrupt("return", BrowserWindowMessageConnection({ connectionInfo: _this, sendDirection: isExtension ? MESSAGE_DIRECTION.to_waellet : undefined, receiveDirection: isExtension ? MESSAGE_DIRECTION.to_aepp : undefined, target: source, origin: origin })); case 3: case "end": return _context.stop(); } } }, _callee); }))(); } }); wallets[wallet.id] = wallet; onDetected({ wallets: wallets, newWallet: wallet }); }); if (_Object$keys(wallets).length) onDetected({ wallets: wallets }); }, /** * Stop scanning * @function stopScan * @instance * @return {void} */ stopScan: function stopScan() { this.connection.disconnect(); } } }); //# sourceMappingURL=wallet-detector.js.map