UNPKG

@0xfutbol/id

Version:

React component library with shared providers for 0xFutbol ID

282 lines (281 loc) 10.3 kB
'use strict';var index=require('./index-DwkZmKdT.js');require('react'),require('react/jsx-runtime'),require('@0xfutbol/id-sign'),require('react-use'),require('@0xfutbol/constants'),require('thirdweb'),require('@matchain/matchid-sdk-react'),require('@tanstack/react-query'),require('@matchain/matchid-sdk-react/index.css'),require('react-dom');/** * @internal */ async function handleSendRawTransactionRequest(options) { const { account, chainId, params: [rawTransaction], } = options; if (!account.sendRawTransaction) { throw new Error("The current account does not support sending raw transactions"); } const txResult = await account.sendRawTransaction({ rawTransaction, chainId, }); return txResult.transactionHash; }/** * @internal */ function validateAccountAddress(account, address) { if (index.bf(account.address) !== index.bf(address)) { throw new Error(`Failed to validate account address (${account.address}), differs from ${address}`); } } /** * @internal */ function parseEip155ChainId(chainId) { const chainIdParts = chainId.split(":"); const chainIdAsNumber = Number.parseInt(chainIdParts[1] ?? "0"); if (chainIdParts.length !== 2 || chainIdParts[0] !== "eip155" || chainIdAsNumber === 0 || !chainIdAsNumber) { throw new Error(`Invalid chainId ${chainId}, should have the format 'eip155:1'`); } return chainIdAsNumber; }/** * @internal */ async function handleSendTransactionRequest(options) { const { account, chainId, thirdwebClient, params: [transaction], } = options; if (transaction.from !== undefined) { validateAccountAddress(account, transaction.from); } const preparedTransaction = index.bg({ gas: transaction.gas ? index.an(transaction.gas) : undefined, gasPrice: transaction.gasPrice ? index.an(transaction.gasPrice) : undefined, value: transaction.value ? index.an(transaction.value) : undefined, to: transaction.to, data: transaction.data, chain: index.h(chainId), client: thirdwebClient, }); const txResult = await index.aE({ transaction: preparedTransaction, account, }); return txResult.transactionHash; }/** * @internal */ async function handleSignTransactionRequest(options) { const { account, params: [transaction], } = options; if (!account.signTransaction) { throw new Error("The current account does not support signing transactions"); } if (transaction.from !== undefined) { validateAccountAddress(account, transaction.from); } return account.signTransaction({ gas: transaction.gas ? index.an(transaction.gas) : undefined, gasPrice: transaction.gasPrice ? index.an(transaction.gasPrice) : undefined, value: transaction.value ? index.an(transaction.value) : undefined, nonce: transaction.nonce ? index.b2(transaction.nonce) : undefined, to: transaction.to, data: transaction.data, }); }/** * @internal */ async function handleSignTypedDataRequest(options) { const { account, params } = options; validateAccountAddress(account, params[0]); return account.signTypedData( // The data could be sent to us as a string or object, depending on the level of parsing on the client side typeof params[1] === "string" ? JSON.parse(params[1]) : params[1]); }/** * @internal */ async function handleSignRequest(options) { const { account, params } = options; validateAccountAddress(account, params[1]); return account.signMessage({ message: { raw: params[0] } }); }/** * @internal */ async function fulfillRequest(options) { const { wallet, walletConnectClient, thirdwebClient, event: { topic, id, params: { chainId: rawChainId, request }, }, handlers, } = options; const account = wallet.getAccount(); if (!account) { throw new Error("No account connected to provided wallet"); } let result; try { switch (request.method) { case "personal_sign": { if (handlers?.personal_sign) { result = await handlers.personal_sign({ account, params: request.params, }); } else { result = await handleSignRequest({ account, params: request.params, }); } break; } case "eth_sign": { if (handlers?.eth_sign) { result = await handlers.eth_sign({ account, params: request.params, }); } else { result = await handleSignRequest({ account, params: request.params, }); } break; } case "eth_signTypedData": { if (handlers?.eth_signTypedData) { result = await handlers.eth_signTypedData({ account, params: request.params, }); } else { result = await handleSignTypedDataRequest({ account, params: request.params, }); } break; } case "eth_signTypedData_v4": { if (handlers?.eth_signTypedData_v4) { result = await handlers.eth_signTypedData_v4({ account, params: request.params, }); } else { result = await handleSignTypedDataRequest({ account, params: request.params, }); } break; } case "eth_signTransaction": { if (handlers?.eth_signTransaction) { result = await handlers.eth_signTransaction({ account, params: request.params, }); } else { result = await handleSignTransactionRequest({ account, params: request.params, }); } break; } case "eth_sendTransaction": { const chainId = parseEip155ChainId(rawChainId); if (handlers?.eth_sendTransaction) { result = await handlers.eth_sendTransaction({ account, chainId, params: request.params, }); } else { result = await handleSendTransactionRequest({ account, chainId, thirdwebClient, params: request.params, }); } break; } case "eth_sendRawTransaction": { const chainId = parseEip155ChainId(rawChainId); if (handlers?.eth_sendRawTransaction) { result = await handlers.eth_sendRawTransaction({ account, chainId, params: request.params, }); } else { result = await handleSendRawTransactionRequest({ account, chainId, params: request.params, }); } break; } case "wallet_addEthereumChain": { if (handlers?.wallet_addEthereumChain) { result = await handlers.wallet_addEthereumChain({ wallet, params: request.params, }); } else { throw new Error("Unsupported request method: wallet_addEthereumChain"); } break; } case "wallet_switchEthereumChain": { if (handlers?.wallet_switchEthereumChain) { result = await handlers.wallet_switchEthereumChain({ wallet, params: request.params, }); } else { const { handleSwitchChain } = await Promise.resolve().then(function(){return require('./switch-chain-BT5JMZnf.js')}); result = await handleSwitchChain({ wallet, params: request.params, }); } break; } default: { const potentialHandler = handlers?.[request.method]; if (potentialHandler) { result = await potentialHandler({ account, chainId: parseEip155ChainId(rawChainId), params: request.params, }); } else { throw new Error(`Unsupported request method: ${request.method}`); } } } } catch (error) { result = { code: typeof error === "object" && error !== null && "code" in error ? error.code : 500, message: typeof error === "object" && error !== null && "message" in error ? error.message : "Unknown error", }; } walletConnectClient.respond({ topic, response: { id, jsonrpc: "2.0", result, }, }); }exports.fulfillRequest=fulfillRequest;//# sourceMappingURL=session-request-BP6HauXd.js.map