Add functionality to create a validator #28

Merged
nabarun merged 14 commits from deep-stack/testnet-onboarding-app:ag-validator-ui into main 2024-08-09 10:18:14 +00:00
Showing only changes of commit 36bc969804 - Show all commits

View File

@ -3,17 +3,14 @@ import React, { useEffect, useMemo, useState } from 'react'
import { enqueueSnackbar } from 'notistack'; import { enqueueSnackbar } from 'notistack';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { MsgCreateValidatorEncodeObject } from '@cosmjs/stargate'; import { MsgCreateValidator } from 'cosmjs-types/cosmos/staking/v1beta1/tx';
import {MsgCreateValidator} from '@cerc-io/registry-sdk'; import { fromBech32, toBech32 } from '@cosmjs/encoding';
import { fromBase64, fromBech32, toBech32 } from '@cosmjs/encoding';
import { LoadingButton } from '@mui/lab'; 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 { Registry } from '@cerc-io/registry-sdk';
import { useWalletConnectContext } from '../context/WalletConnectContext' import { useWalletConnectContext } from '../context/WalletConnectContext'
import { Participant } from '../types'; 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 CreateValidator = () => {
const {session, signClient} = useWalletConnectContext(); const {session, signClient} = useWalletConnectContext();
@ -28,15 +25,6 @@ const CreateValidator = () => {
navigate("/connect-wallet") 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 = const msgCreateValidator: MsgCreateValidator =
useMemo(() => { useMemo(() => {
return { return {
@ -55,25 +43,28 @@ const CreateValidator = () => {
minSelfDelegation: "1", minSelfDelegation: "1",
delegatorAddress: cosmosAddress, delegatorAddress: cosmosAddress,
validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data), validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data),
// pubkey: encodePubkey({ pubkey: encodePubkey({
// type: "tendermint/PubKeyEd25519", type: "tendermint/PubKeyEd25519",
// value: pubkey, value: pubkey,
// }), }),
pubkey: updatedPubKey,
value: { value: {
amount: process.env.REACT_APP_STAKING_AMOUNT!, amount: process.env.REACT_APP_STAKING_AMOUNT!,
denom: process.env.REACT_APP_LACONICD_DENOM!, 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', typeUrl: '/cosmos.staking.v1beta1.MsgCreateValidator',
value: msgCreateValidator value: MsgCreateValidator.toJSON(msgCreateValidator)
// value: msgCreateValidator
}; };
const sendTransaction = async ( const sendTransaction = async (
transactionMessage: MsgCreateValidatorEncodeObject transactionMessage: EncodeObject
) => { ) => {
try { try {
setIsLoading(true); setIsLoading(true);