From 67b1f41dddd01bc998f6d3877953b5aafb9cab6f Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Mon, 22 Apr 2024 20:22:25 +0530 Subject: [PATCH 1/4] fix cosmos tx sign --- .../signTx/cosmosEIP/signCosmosEIP.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/transactions/signTx/cosmosEIP/signCosmosEIP.ts b/transactions/signTx/cosmosEIP/signCosmosEIP.ts index 4b6b017e..856546e1 100644 --- a/transactions/signTx/cosmosEIP/signCosmosEIP.ts +++ b/transactions/signTx/cosmosEIP/signCosmosEIP.ts @@ -26,6 +26,7 @@ import { signTypedData, type SignTypedDataArgs, } from "@wagmi/core"; +import { areEqualAddresses } from "@/utils/address"; export async function signCosmosEIPTx( tx: Transaction @@ -114,13 +115,25 @@ async function signAndBroadcastCosmosTransaction( context.chain.chainId, eipPayload ); + if(!window || !window.ethereum){ + return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); + } + const connectedAccounts = await window.ethereum.request({ method: "eth_accounts" }) + if(!connectedAccounts || connectedAccounts.length === 0){ + return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); + } + const index = connectedAccounts.findIndex(address => areEqualAddresses(address, context.ethAddress)); + if(index === -1){ + return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); + } // check public key on sender object, if none, create one if (!context.sender.pubkey) { // create a public key for the user IFF EIP712 Canto is used (since through metamask) try { - const signature = await signMessage({ - message:"Welcome to Canto! \n\nPlease sign this message to generate your Canto account.", + const signature = await window.ethereum.request({ + method: "personal_sign", + params: [context.ethAddress, "generate_pubkey"], }); context.sender.pubkey = signatureToPubkey( signature, @@ -152,7 +165,12 @@ async function signAndBroadcastCosmosTransaction( ); // get signature from metamask - const signature = await signTypedData(eipToSign as SignTypedDataArgs); + + const signature = await window.ethereum.request({ + method: "eth_signTypedData_v4", + params: [context.ethAddress, JSON.stringify(eipToSign)], + }); + const signedTx = createTxRawEIP712( cosmosPayload.legacyAmino.body, cosmosPayload.legacyAmino.authInfo, From 6349cd7da93780114cd0e5f323c521fef864a788 Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Mon, 22 Apr 2024 20:31:17 +0530 Subject: [PATCH 2/4] fix lint --- transactions/signTx/cosmosEIP/signCosmosEIP.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transactions/signTx/cosmosEIP/signCosmosEIP.ts b/transactions/signTx/cosmosEIP/signCosmosEIP.ts index 856546e1..20f45e88 100644 --- a/transactions/signTx/cosmosEIP/signCosmosEIP.ts +++ b/transactions/signTx/cosmosEIP/signCosmosEIP.ts @@ -118,7 +118,7 @@ async function signAndBroadcastCosmosTransaction( if(!window || !window.ethereum){ return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); } - const connectedAccounts = await window.ethereum.request({ method: "eth_accounts" }) + const connectedAccounts: string[] = await window.ethereum.request({ method: "eth_accounts" }) if(!connectedAccounts || connectedAccounts.length === 0){ return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); } From 20b48cf71af477c57e5fe0690f14191b1951905f Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Fri, 26 Apr 2024 17:01:33 +0530 Subject: [PATCH 3/4] check chain id of cnnected wallets --- transactions/signTx/cosmosEIP/signCosmosEIP.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/transactions/signTx/cosmosEIP/signCosmosEIP.ts b/transactions/signTx/cosmosEIP/signCosmosEIP.ts index 20f45e88..dbe0b5e4 100644 --- a/transactions/signTx/cosmosEIP/signCosmosEIP.ts +++ b/transactions/signTx/cosmosEIP/signCosmosEIP.ts @@ -115,9 +115,11 @@ async function signAndBroadcastCosmosTransaction( context.chain.chainId, eipPayload ); + if(!window || !window.ethereum){ return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); } + const connectedAccounts: string[] = await window.ethereum.request({ method: "eth_accounts" }) if(!connectedAccounts || connectedAccounts.length === 0){ return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); @@ -127,6 +129,11 @@ async function signAndBroadcastCosmosTransaction( return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); } + const connectedChainId = await window.ethereum.request({ method: "eth_chainId" }); + if(!connectedChainId || parseInt(connectedChainId, 16) !== context.chain.chainId){ + return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); + } + // check public key on sender object, if none, create one if (!context.sender.pubkey) { // create a public key for the user IFF EIP712 Canto is used (since through metamask) From d7a32fe61155ebc9b128084833e0b34d6386d1a7 Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Mon, 6 May 2024 13:15:34 +0530 Subject: [PATCH 4/4] add function to check wallet support --- .../signTx/cosmosEIP/signCosmosEIP.ts | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/transactions/signTx/cosmosEIP/signCosmosEIP.ts b/transactions/signTx/cosmosEIP/signCosmosEIP.ts index dbe0b5e4..9330ebdf 100644 --- a/transactions/signTx/cosmosEIP/signCosmosEIP.ts +++ b/transactions/signTx/cosmosEIP/signCosmosEIP.ts @@ -92,6 +92,12 @@ async function signAndBroadcastCosmosTransaction( tx: UnsignedCosmosMessages ): PromiseWithError { try { + // check if wallet is supported + const {error} = await checkWalletSupport(context.chain.chainId, context.ethAddress) + if (error) { + return NEW_ERROR("signAndBroadcastCosmosTransaction", error); + } + // create correct fee object for EIP712 const feeObj = generateFeeObj(tx.fee, context.sender.accountAddress); @@ -115,24 +121,6 @@ async function signAndBroadcastCosmosTransaction( context.chain.chainId, eipPayload ); - - if(!window || !window.ethereum){ - return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); - } - - const connectedAccounts: string[] = await window.ethereum.request({ method: "eth_accounts" }) - if(!connectedAccounts || connectedAccounts.length === 0){ - return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); - } - const index = connectedAccounts.findIndex(address => areEqualAddresses(address, context.ethAddress)); - if(index === -1){ - return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); - } - - const connectedChainId = await window.ethereum.request({ method: "eth_chainId" }); - if(!connectedChainId || parseInt(connectedChainId, 16) !== context.chain.chainId){ - return NEW_ERROR("signAndBroadcastCosmosTransaction", "Wallet not supported"); - } // check public key on sender object, if none, create one if (!context.sender.pubkey) { @@ -318,3 +306,37 @@ export async function getCosmosTxDetailsFromHash( return NEW_ERROR("getCosmosTxDetailsFromHash", err); } } + +export async function checkWalletSupport( + chainId: number, + ethAddress: string +): PromiseWithError<{ + isSupported: boolean; +}> { + try{ + // check ethereum provider + if(!window || !window.ethereum){ + throw new Error("No provider found::Wallet not supported"); + } + + // check matching account + const connectedAccounts: string[] = await window.ethereum.request({ method: "eth_accounts" }) + if(!connectedAccounts || connectedAccounts.length === 0){ + throw new Error("No connected accounts::Wallet not supported"); + } + const matchingAccountIndex = connectedAccounts.findIndex(address => areEqualAddresses(address, ethAddress)); + if(matchingAccountIndex === -1){ + throw new Error(`No matching account::${connectedAccounts.join(', ')}::${ethAddress}::Wallet not supported`); + } + + // check chainId + const connectedChainId = await window.ethereum.request({ method: "eth_chainId" }); + if(!connectedChainId || parseInt(connectedChainId, 16) !== chainId){ + throw new Error(`ChainId not matched::${parseInt(connectedChainId, 16)}::${chainId}::Wallet not supported`); + } + + return NO_ERROR({ isSupported: true }); + } catch(err){ + return NEW_ERROR("checkWalletSupport", err); + } +} \ No newline at end of file