UNPKG

@soundsright/connector

Version:

soundsright wallet connector

66 lines (65 loc) 3.03 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConnectorHelper = void 0; let requestHandler = ({ url }) => __awaiter(void 0, void 0, void 0, function* () { const res = yield window.fetch(url); return res.json(); }); const setRequestHandler = (handler) => { requestHandler = handler; }; const openUri = (uri) => { window.open(uri, '_blank'); }; const openAppUri = (uri, { universalLink, deepLink }) => { const encodedUri = encodeURIComponent(uri); if (universalLink) { openUri(`${universalLink}/wc?uri=${encodedUri}`); } else if (deepLink) { openUri(`${deepLink}${deepLink.endsWith(':') ? '//' : '/'}wc?uri=${encodedUri}`); } }; const formatMobileRegistryEntry = (wallet, platform) => { var _a; return ({ name: wallet.name || '', shortName: wallet.metadata.shortName || '', color: wallet.metadata.colors.primary || '', logo: (_a = wallet.image_url.sm) !== null && _a !== void 0 ? _a : '', universalLink: wallet[platform].universal || '', deepLink: wallet[platform].native || '', }); }; const getWallets = () => __awaiter(void 0, void 0, void 0, function* () { const res = yield requestHandler({ url: 'https://registry.walletconnect.com/api/v2/wallets' }); return res.listings; }); const getWalletConfigsByPlatform = (platform, whitelist) => __awaiter(void 0, void 0, void 0, function* () { const wallets = yield getWallets(); let configs = Object.values(wallets); if (whitelist) { configs = whitelist.map((name) => configs.find((config) => config.name.toLowerCase() === name)); } return configs .filter((wallet) => !!wallet[platform].universal || !!wallet[platform].native) .map((wallet) => formatMobileRegistryEntry(wallet, platform)); }); const getIOSWalletConfigs = (whitelist) => __awaiter(void 0, void 0, void 0, function* () { return getWalletConfigsByPlatform('mobile', whitelist); }); const getDesktopWalletConfigs = (whitelist) => __awaiter(void 0, void 0, void 0, function* () { return getWalletConfigsByPlatform('desktop', whitelist); }); exports.ConnectorHelper = { setRequestHandler, openUri, openAppUri, getIOSWalletConfigs, getDesktopWalletConfigs, };