From 59176ad7cb3a5c641b589b37994a48ab06fe61d4 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 25 Feb 2025 10:38:59 +0000 Subject: [PATCH] Use appropriate cosmos address prefix while signing messages (#22) Part of https://www.notion.so/Revive-attestation-react-app-19da6b22d472809595c2fb9d2970849f Co-authored-by: Shreerang Kale Reviewed-on: https://git.vdb.to/cerc-io/laconic-wallet-web/pulls/22 --- .eslintignore | 1 + src/utils/sign-message.ts | 8 ++++++-- src/utils/wallet-connect/wallet-connect-requests.ts | 10 +++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/src/utils/sign-message.ts b/src/utils/sign-message.ts index 7348b7c..bb3904c 100644 --- a/src/utils/sign-message.ts +++ b/src/utils/sign-message.ts @@ -3,6 +3,7 @@ For more information, "visit https://docs.ethers.org/v5/cookbook/react-native/#c import 'react-native-get-random-values'; import '@ethersproject/shims'; +import { fromBech32 } from '@cosmjs/encoding'; import { Wallet } from 'ethers'; import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; @@ -24,7 +25,7 @@ const signMessage = async ({ case EIP155: return await signEthMessage(message, accountId, chainId); case COSMOS: - return await signCosmosMessage(message, path.path); + return await signCosmosMessage(message, path.path, path.address); default: throw new Error('Invalid wallet type'); } @@ -51,10 +52,13 @@ const signEthMessage = async ( const signCosmosMessage = async ( message: string, path: string, + cosmosAddress: string, ): Promise => { try { const mnemonic = await getMnemonic(); - const cosmosAccount = await getCosmosAccounts(mnemonic, path); + const addressPrefix = fromBech32(cosmosAddress).prefix + + const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix); const address = cosmosAccount.data.address; const cosmosSignature = await cosmosAccount.cosmosWallet.signAmino( address, diff --git a/src/utils/wallet-connect/wallet-connect-requests.ts b/src/utils/wallet-connect/wallet-connect-requests.ts index 01b1d48..db3d8f4 100644 --- a/src/utils/wallet-connect/wallet-connect-requests.ts +++ b/src/utils/wallet-connect/wallet-connect-requests.ts @@ -9,6 +9,7 @@ import { StdFee, MsgSendEncodeObject } from '@cosmjs/stargate'; +import { fromBech32 } from '@cosmjs/encoding'; import { EncodeObject } from '@cosmjs/proto-signing'; import { LaconicClient } from '@cerc-io/registry-sdk'; import { Buffer } from 'buffer'; @@ -19,6 +20,7 @@ import { Account } from '../../types'; import { getMnemonic, getPathKey } from '../misc'; import { getCosmosAccounts } from '../accounts'; import { COSMOS_METHODS } from './COSMOSData'; +import { COSMOS } from '../constants'; interface EthSendTransaction { type: 'eth_sendTransaction'; @@ -80,7 +82,13 @@ export async function approveWalletConnectRequest( const path = (await getPathKey(`${namespace}:${chainId}`, account.index)) .path; const mnemonic = await getMnemonic(); - const cosmosAccount = await getCosmosAccounts(mnemonic, path); + + let addressPrefix: string | undefined + if (namespace === COSMOS) { + addressPrefix = fromBech32(account.address).prefix + } + + const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix); const address = account.address; switch (request.method) {