From 6d68436c76545b097cd950a9aafc6dfc3ed1537a Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 8 Aug 2024 15:02:48 +0530 Subject: [PATCH] Derive pubkey from partial --- package.json | 1 + src/pages/CreateValidator.tsx | 81 ++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 0a61602..c538f3d 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "@cerc-io/registry-sdk": "^0.2.5", + "@cosmjs/crypto": "^0.32.4", "@cosmjs/encoding": "^0.32.4", "@cosmjs/proto-signing": "^0.32.4", "@cosmjs/stargate": "^0.32.4", diff --git a/src/pages/CreateValidator.tsx b/src/pages/CreateValidator.tsx index 06fead2..c131f50 100644 --- a/src/pages/CreateValidator.tsx +++ b/src/pages/CreateValidator.tsx @@ -4,19 +4,22 @@ import { enqueueSnackbar } from 'notistack'; import { useNavigate } from 'react-router-dom'; import { MsgCreateValidatorEncodeObject } from '@cosmjs/stargate'; -import { fromBech32, toBech32 } from '@cosmjs/encoding'; +import {MsgCreateValidator} from '@cerc-io/registry-sdk'; +import { fromBase64, fromBech32, toBech32 } from '@cosmjs/encoding'; import { LoadingButton } from '@mui/lab'; import { 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(); const navigate = useNavigate(); - const [cosmosAddress, setCosmosAddress] = useState(''); + const [cosmosAddress, setCosmosAddress] = useState('laconic1z4l4556v8jnk7k456ujme720nk8mzjwhka7qvw'); const [isLoading, setIsLoading] = useState(false); const [pubkey, setPubkey] = useState('d5aOEYaCIHaDn6kG0tAg699D7sLgAgkJI5reTMl0o5U='); const [participant, setParticipant] = useState(null); @@ -25,42 +28,49 @@ const CreateValidator = () => { navigate("/connect-wallet") } - const changePrefix = (address: string, newPrefix: string): string => { - return toBech32(newPrefix, fromBech32(address).data); - } + const pubkeyProto = CosmosCryptoEd25519Pubkey.fromPartial({ + key: fromBase64(pubkey) + }); - const createValidatorMessage: MsgCreateValidatorEncodeObject = + const updatedPubKey = Any.fromPartial({ + typeUrl: '/cosmos.crypto.ed25519.PubKey', + value: Uint8Array.from(CosmosCryptoEd25519Pubkey.encode(pubkeyProto).finish()) + }); + + const msgCreateValidator: MsgCreateValidator = useMemo(() => { return { - typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + description: { + moniker: "dockerNode", + identity: "", + website: "", + securityContact: "", + details: "", + }, + commission: { + maxChangeRate: "10000000000000000", // 0.01 + maxRate: "200000000000000000", // 0.2 + rate: "100000000000000000", // 0.1 + }, + minSelfDelegation: "1", + delegatorAddress: cosmosAddress, + validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data), + // pubkey: encodePubkey({ + // type: "tendermint/PubKeyEd25519", + // value: pubkey, + // }), + pubkey: updatedPubKey, value: { - description: { - moniker: "dockerNode", - identity: "", - website: "", - securityContact: "", - details: "", - }, - commission: { - maxChangeRate: "10000000000000000", // 0.01 - maxRate: "200000000000000000", // 0.2 - rate: "100000000000000000", // 0.1 - }, - minSelfDelegation: "1", - delegatorAddress: cosmosAddress, - // validatorAddress: changePrefix(cosmosAddress, "laconicvaloper"), - validatorAddress: "laconicvaloper1ru0s5tu0cj3xmt8zdfrmz74p0c6lj73nqfpt2q", - pubkey: encodePubkey({ - type: "tendermint/PubKeyEd25519", - value: pubkey, - }), - value: { - amount: process.env.REACT_APP_STAKING_AMOUNT!, - denom: process.env.REACT_APP_LACONICD_DENOM!, - }, + amount: process.env.REACT_APP_STAKING_AMOUNT!, + denom: process.env.REACT_APP_LACONICD_DENOM!, }, }; - }, [cosmosAddress, pubkey]); + }, [cosmosAddress, updatedPubKey]); + + const msgCreateValidatorEncodeObject: MsgCreateValidatorEncodeObject = { + typeUrl: '/cosmos.staking.v1beta1.MsgCreateValidator', + value: msgCreateValidator + }; const sendTransaction = async ( transactionMessage: MsgCreateValidatorEncodeObject @@ -193,14 +203,15 @@ const CreateValidator = () => { }} >
-                  {JSON.stringify(createValidatorMessage, null, 2)}
+                  {JSON.stringify(msgCreateValidator, null, 2)}
                 
{ - await sendTransaction(createValidatorMessage); + console.log(msgCreateValidatorEncodeObject); + await sendTransaction(msgCreateValidatorEncodeObject); }} loading={isLoading} > @@ -215,4 +226,4 @@ const CreateValidator = () => { ) } -export default CreateValidator \ No newline at end of file +export default CreateValidator