Send serialized message for validator creation

This commit is contained in:
Prathamesh Musale 2024-08-08 16:53:01 +05:30 committed by Adw8
parent 6d68436c76
commit 36bc969804

View File

@ -3,17 +3,14 @@ import React, { useEffect, useMemo, useState } from 'react'
import { enqueueSnackbar } from 'notistack';
import { useNavigate } from 'react-router-dom';
import { MsgCreateValidatorEncodeObject } from '@cosmjs/stargate';
import {MsgCreateValidator} from '@cerc-io/registry-sdk';
import { fromBase64, fromBech32, toBech32 } from '@cosmjs/encoding';
import { MsgCreateValidator } from 'cosmjs-types/cosmos/staking/v1beta1/tx';
import { fromBech32, toBech32 } from '@cosmjs/encoding';
import { LoadingButton } from '@mui/lab';
import { encodePubkey } from '@cosmjs/proto-signing';
import { EncodeObject, encodePubkey } from '@cosmjs/proto-signing';
import { Registry } from '@cerc-io/registry-sdk';
import { useWalletConnectContext } from '../context/WalletConnectContext'
import { Participant } from '../types';
import { PubKey as CosmosCryptoEd25519Pubkey } from 'cosmjs-types/cosmos/crypto/ed25519/keys';
import { Any } from 'cosmjs-types/google/protobuf/any';
const CreateValidator = () => {
const {session, signClient} = useWalletConnectContext();
@ -28,15 +25,6 @@ const CreateValidator = () => {
navigate("/connect-wallet")
}
const pubkeyProto = CosmosCryptoEd25519Pubkey.fromPartial({
key: fromBase64(pubkey)
});
const updatedPubKey = Any.fromPartial({
typeUrl: '/cosmos.crypto.ed25519.PubKey',
value: Uint8Array.from(CosmosCryptoEd25519Pubkey.encode(pubkeyProto).finish())
});
const msgCreateValidator: MsgCreateValidator =
useMemo(() => {
return {
@ -55,25 +43,28 @@ const CreateValidator = () => {
minSelfDelegation: "1",
delegatorAddress: cosmosAddress,
validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data),
// pubkey: encodePubkey({
// type: "tendermint/PubKeyEd25519",
// value: pubkey,
// }),
pubkey: updatedPubKey,
pubkey: encodePubkey({
type: "tendermint/PubKeyEd25519",
value: pubkey,
}),
value: {
amount: process.env.REACT_APP_STAKING_AMOUNT!,
denom: process.env.REACT_APP_LACONICD_DENOM!,
},
};
}, [cosmosAddress, updatedPubKey]);
}, [cosmosAddress, pubkey]);
const msgCreateValidatorEncodeObject: MsgCreateValidatorEncodeObject = {
// When sending a typed message value, the pubkey value type (Uint8Array) gets lost when wallet connect JSON-stringifies the tx message;
// resulting into a type error from the chain
// Workaround: Encode the tx value instead of sending a typed message directly, has to be decoded using MsgCreateValidator.fromJSON on the wallet side
const msgCreateValidatorEncodeObject: EncodeObject = {
typeUrl: '/cosmos.staking.v1beta1.MsgCreateValidator',
value: msgCreateValidator
value: MsgCreateValidator.toJSON(msgCreateValidator)
// value: msgCreateValidator
};
const sendTransaction = async (
transactionMessage: MsgCreateValidatorEncodeObject
transactionMessage: EncodeObject
) => {
try {
setIsLoading(true);