From 5b8655031680b19ac5d466794124313ed32633d8 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 8 Aug 2024 17:13:20 +0530 Subject: [PATCH 1/2] Decode message when sending tx to create a validator --- src/screens/ApproveTransaction.tsx | 24 +++++++++++++++---- .../wallet-connect/wallet-connect-requests.ts | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/screens/ApproveTransaction.tsx b/src/screens/ApproveTransaction.tsx index fd74bb7..83229dd 100644 --- a/src/screens/ApproveTransaction.tsx +++ b/src/screens/ApproveTransaction.tsx @@ -1,13 +1,14 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { Image, ScrollView, View } from 'react-native'; import { Button, Text, TextInput } from 'react-native-paper'; +import { MsgCreateValidator } from 'cosmjs-types/cosmos/staking/v1beta1/tx'; import { NativeStackNavigationProp, NativeStackScreenProps, } from '@react-navigation/native-stack'; import { useNavigation } from '@react-navigation/native'; -import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing'; +import { DirectSecp256k1Wallet, EncodeObject } from '@cosmjs/proto-signing'; import { LaconicClient } from '@cerc-io/registry-sdk'; import { GasPrice, calculateFee } from '@cosmjs/stargate'; import { formatJsonRpcError } from '@json-rpc-tools/utils'; @@ -40,7 +41,6 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { const requestName = requestSession.peer.metadata.name; const requestIcon = requestSession.peer.metadata.icons[0]; const requestURL = requestSession.peer.metadata.url; - const transactionMessage = route.params.transactionMessage; const signer = route.params.signer; const requestEvent = route.params.requestEvent; const chainId = requestEvent.params.chainId; @@ -67,6 +67,20 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { const { web3wallet } = useWalletConnect(); + const transactionMessage = useMemo((): EncodeObject => { + const inputTxMsg = route.params.transactionMessage; + + // If it's a MsgCreateValidator, decode the tx msg value using MsgCreateValidator type + if (inputTxMsg.typeUrl.includes('MsgCreateValidator')) { + return { + typeUrl: inputTxMsg.typeUrl, + value: MsgCreateValidator.fromJSON(inputTxMsg.value) + } + } + + return inputTxMsg; + }, [route.params.transactionMessage]); + useEffect(() => { if (namespace !== COSMOS) { return; @@ -138,7 +152,7 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { return; } const gasEstimation = await cosmosStargateClient!.simulate( - transactionMessage.value.participant!, + signer, [transactionMessage], MEMO, ); @@ -160,7 +174,7 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { } }; getCosmosGas(); - }, [cosmosStargateClient, transactionMessage, requestEventId, topic, web3wallet]); + }, [cosmosStargateClient, transactionMessage, requestEventId, topic, web3wallet, signer]); useEffect(() => { const gasPrice = GasPrice.fromString( diff --git a/src/utils/wallet-connect/wallet-connect-requests.ts b/src/utils/wallet-connect/wallet-connect-requests.ts index a676df2..01b1d48 100644 --- a/src/utils/wallet-connect/wallet-connect-requests.ts +++ b/src/utils/wallet-connect/wallet-connect-requests.ts @@ -7,7 +7,7 @@ import { getSdkError } from '@walletconnect/utils'; import { SigningStargateClient, StdFee, - MsgSendEncodeObject, + MsgSendEncodeObject } from '@cosmjs/stargate'; import { EncodeObject } from '@cosmjs/proto-signing'; import { LaconicClient } from '@cerc-io/registry-sdk'; -- 2.45.2 From 8eee46e669b9be268270bfba4f9799135bc85967 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Fri, 9 Aug 2024 13:41:07 +0530 Subject: [PATCH 2/2] Use replacer for displaying pubkey --- src/screens/ApproveTransaction.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/screens/ApproveTransaction.tsx b/src/screens/ApproveTransaction.tsx index 83229dd..c6d2912 100644 --- a/src/screens/ApproveTransaction.tsx +++ b/src/screens/ApproveTransaction.tsx @@ -253,6 +253,13 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { navigation.navigate('Home'); }; + const replacer = (key: string, value: any): any => { + if (value instanceof Uint8Array) { + return Buffer.from(value).toString('hex'); + } + return value; + }; + return ( <> @@ -278,7 +285,7 @@ const ApproveTransaction = ({ route }: ApproveTransactionProps) => { - {JSON.stringify(transactionMessage, null, 2)} + {JSON.stringify(transactionMessage, replacer, 2)} <> -- 2.45.2