@0xfutbol/id
Version:
React component library with shared providers for 0xFutbol ID
282 lines (281 loc) • 10.4 kB
JavaScript
import {bf as checksumAddress,bg as prepareTransaction,h as getCachedChain,an as hexToBigInt,aE as sendTransaction,b2 as hexToNumber}from'./index-BJCJzM2_.js';import'react';import'react/jsx-runtime';import'@0xfutbol/id-sign';import'react-use';import'@0xfutbol/constants';import'thirdweb';import'@matchain/matchid-sdk-react';import'@tanstack/react-query';import'@matchain/matchid-sdk-react/index.css';import'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 (checksumAddress(account.address) !== checksumAddress(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 = prepareTransaction({
gas: transaction.gas ? hexToBigInt(transaction.gas) : undefined,
gasPrice: transaction.gasPrice
? hexToBigInt(transaction.gasPrice)
: undefined,
value: transaction.value ? hexToBigInt(transaction.value) : undefined,
to: transaction.to,
data: transaction.data,
chain: getCachedChain(chainId),
client: thirdwebClient,
});
const txResult = await sendTransaction({
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 ? hexToBigInt(transaction.gas) : undefined,
gasPrice: transaction.gasPrice
? hexToBigInt(transaction.gasPrice)
: undefined,
value: transaction.value ? hexToBigInt(transaction.value) : undefined,
nonce: transaction.nonce ? hexToNumber(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 import('./switch-chain-CFc8IJLc.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,
},
});
}export{fulfillRequest};//# sourceMappingURL=session-request-5VlCPnAw.js.map