UNPKG

ribbit-wallet-connect

Version:

Next-generation multi-chain wallet and payments app that makes crypto simple, secure, and usable in daily life.

141 lines (140 loc) 6.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransportMessageType = exports.SupraChainId = void 0; const types_1 = require("./core/types"); const ribbit_wallet_sdk_1 = require("./sdk/ribbit-wallet-sdk"); const window_transport_1 = require("./transport/window-transport"); const detect_transport_1 = require("./utils/detect-transport"); const sdk = new ribbit_wallet_sdk_1.RibbitWalletSDK((0, detect_transport_1.detectTransport)()); const windowTransport = new window_transport_1.WindowTransport(); function isFlutterInAppWebView() { return typeof window.flutter_inappwebview !== 'undefined'; } function sendWindowTransportMessage(type, payload) { return windowTransport.sendMessage(type, payload); } function isExtensionInjected() { return new Promise((resolve) => { try { // Check if we're in a browser environment if (typeof window === 'undefined') { resolve(false); return; } windowTransport .sendMessage(types_1.TransportMessageType.PING, {}) .then((response) => { resolve(!!response); }) .catch(() => { resolve(false); }); // Add a timeout in case the message hangs setTimeout(() => resolve(false), 500); } catch (err) { // Catch any other errors if (err && typeof err === 'object' && 'message' in err) { console.log('Error checking extension:', err.message); } else { console.log('Error checking extension:', err); } resolve(false); } }); } const readyPromise = (async () => { if (isFlutterInAppWebView()) { return true; } return await isExtensionInjected(); })(); if (typeof window !== 'undefined') { sdk.initialize(); const ribit = { connectToWallet: async (dapp) => { if (isFlutterInAppWebView()) { return sdk.connectToWallet(); } if (await isExtensionInjected()) { const session = await sdk.connectToWallet(); const payload = Object.assign(Object.assign({}, dapp), { sessionId: session.sessionId }); return sendWindowTransportMessage(types_1.TransportMessageType.CONNECT_REQUEST, payload); } }, createRawTransactionBuffer: async (rawTxnRequest) => { return await sdk.createRawTransactionBuffer(rawTxnRequest); }, signAndSendRawTransaction: async (req) => { if (isFlutterInAppWebView()) { return await sdk.signAndSendRawTransaction(req); } if (await isExtensionInjected()) { const payload = Object.assign(Object.assign({}, req), { sessionId: await sdk.getSession() }); return sendWindowTransportMessage(types_1.TransportMessageType.SEND_TRANSACTION, payload); } }, signMessage: async (messageRequest) => { if (isFlutterInAppWebView()) { return sdk.signMessage(messageRequest); } if (await isExtensionInjected()) { const payload = Object.assign(Object.assign({}, messageRequest), { sessionId: await sdk.getSession() }); return sendWindowTransportMessage(types_1.TransportMessageType.SIGN_MESSAGE, payload); } }, getSessionStatus: async () => { if (isFlutterInAppWebView()) { return sdk.getSession(); } if (await isExtensionInjected()) { return sendWindowTransportMessage(types_1.TransportMessageType.SESSION_STATUS, sdk.getSession()); } }, getWalletAddress: async (chainId) => { if (isFlutterInAppWebView()) { return sdk.getWalletAddress(chainId); } if (await isExtensionInjected()) { return sendWindowTransportMessage(types_1.TransportMessageType.GET_WALLET_ADDRESS, { chainId }); } }, getWalletBalance: async (walletBalanceRequest) => { if (isFlutterInAppWebView()) { return sdk.getWalletBalance(walletBalanceRequest.chainId, walletBalanceRequest.resourceType, walletBalanceRequest.decimals); } if (await isExtensionInjected()) { return sendWindowTransportMessage(types_1.TransportMessageType.GET_WALLET_BALANCE, { walletBalanceRequest }); } }, disconnect: async () => { if (isFlutterInAppWebView()) { return sdk.disconnect(); } if (await isExtensionInjected()) { return sendWindowTransportMessage(types_1.TransportMessageType.DISCONNECT_REQUEST, {}); } }, ready: readyPromise }; window.ribbit = ribit; } var types_2 = require("./core/types"); Object.defineProperty(exports, "SupraChainId", { enumerable: true, get: function () { return types_2.SupraChainId; } }); Object.defineProperty(exports, "TransportMessageType", { enumerable: true, get: function () { return types_2.TransportMessageType; } }); __exportStar(require("./utils/bcs-utils"), exports);