Derive pubkey from partial

This commit is contained in:
Adw8 2024-08-08 15:02:48 +05:30
parent 18590a7d65
commit 833cd53ff5
2 changed files with 47 additions and 35 deletions

View File

@ -4,6 +4,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"@cerc-io/registry-sdk": "^0.2.5", "@cerc-io/registry-sdk": "^0.2.5",
"@cosmjs/crypto": "^0.32.4",
"@cosmjs/encoding": "^0.32.4", "@cosmjs/encoding": "^0.32.4",
"@cosmjs/proto-signing": "^0.32.4", "@cosmjs/proto-signing": "^0.32.4",
"@cosmjs/stargate": "^0.32.4", "@cosmjs/stargate": "^0.32.4",

View File

@ -4,19 +4,22 @@ import { enqueueSnackbar } from 'notistack';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { MsgCreateValidatorEncodeObject } from '@cosmjs/stargate'; 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 { LoadingButton } from '@mui/lab';
import { encodePubkey } from '@cosmjs/proto-signing'; import { 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();
const navigate = useNavigate(); const navigate = useNavigate();
const [cosmosAddress, setCosmosAddress] = useState(''); const [cosmosAddress, setCosmosAddress] = useState('laconic1z4l4556v8jnk7k456ujme720nk8mzjwhka7qvw');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [pubkey, setPubkey] = useState('d5aOEYaCIHaDn6kG0tAg699D7sLgAgkJI5reTMl0o5U='); const [pubkey, setPubkey] = useState('d5aOEYaCIHaDn6kG0tAg699D7sLgAgkJI5reTMl0o5U=');
const [participant, setParticipant] = useState<Participant | null>(null); const [participant, setParticipant] = useState<Participant | null>(null);
@ -25,15 +28,18 @@ const CreateValidator = () => {
navigate("/connect-wallet") navigate("/connect-wallet")
} }
const changePrefix = (address: string, newPrefix: string): string => { const pubkeyProto = CosmosCryptoEd25519Pubkey.fromPartial({
return toBech32(newPrefix, fromBech32(address).data); 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(() => { useMemo(() => {
return { return {
typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator",
value: {
description: { description: {
moniker: "dockerNode", moniker: "dockerNode",
identity: "", identity: "",
@ -48,19 +54,23 @@ const CreateValidator = () => {
}, },
minSelfDelegation: "1", minSelfDelegation: "1",
delegatorAddress: cosmosAddress, delegatorAddress: cosmosAddress,
// validatorAddress: changePrefix(cosmosAddress, "laconicvaloper"), validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data),
validatorAddress: "laconicvaloper1ru0s5tu0cj3xmt8zdfrmz74p0c6lj73nqfpt2q", // 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, pubkey]); }, [cosmosAddress, updatedPubKey]);
const msgCreateValidatorEncodeObject: MsgCreateValidatorEncodeObject = {
typeUrl: '/cosmos.staking.v1beta1.MsgCreateValidator',
value: msgCreateValidator
};
const sendTransaction = async ( const sendTransaction = async (
transactionMessage: MsgCreateValidatorEncodeObject transactionMessage: MsgCreateValidatorEncodeObject
@ -193,14 +203,15 @@ const CreateValidator = () => {
}} }}
> >
<pre style={{ whiteSpace: "pre-wrap", margin: 0 }}> <pre style={{ whiteSpace: "pre-wrap", margin: 0 }}>
{JSON.stringify(createValidatorMessage, null, 2)} {JSON.stringify(msgCreateValidator, null, 2)}
</pre> </pre>
</Box> </Box>
<Box marginTop={1}> <Box marginTop={1}>
<LoadingButton <LoadingButton
variant="contained" variant="contained"
onClick={async () => { onClick={async () => {
await sendTransaction(createValidatorMessage); console.log(msgCreateValidatorEncodeObject);
await sendTransaction(msgCreateValidatorEncodeObject);
}} }}
loading={isLoading} loading={isLoading}
> >