cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
645 stars 330 forks source link

How to create a validator using @cosmjs/stargate library? #1475

Open NodeNavigator opened 12 months ago

NodeNavigator commented 12 months ago

I am trying to create a validator using cosmjs/stargate library. But I am stuck at an error: { code: 2, codespace: 'sdk', log: 'unable to resolve type URL /tendermint.PubKeyEd25519: tx parse error' }

Please take a look at the code below and help me find out the appropriate path to create a validator through client library using Node.js.

//========================================================================================== import { DirectSecp256k1HdWallet,Registry } from "@cosmjs/proto-signing"; import { SigningStargateClient , defaultRegistryTypes,GasPrice} from "@cosmjs/stargate"; import {MsgCreateValidator} from "cosmjs-types/cosmos/staking/v1beta1/tx" import {PublicKey} from "cosmjs-types/tendermint/crypto/keys"

async function createValidator() { // Replace the following values with your actual data const mnemonic = "forward melody elevator label awkward uniform deposit day pen office explain crack ghost armor elite drop ramp direct girl blush much brisk chief train"; const chainId = "sim_904-3"; const validatorMoniker = "MyValidator"; const validatorIdentity = "Identity information"; const validatorWebsite = "https://myvalidator.com"; const validatorSecurityContact = "security@myvalidator.com"; const validatorDetails = "Details about my validator"; const commissionRate = "0.1"; const commissionMaxRate = "0.2"; const commissionMaxChangeRate = "0.01"; const delegationAmount = "1000000";

const coin = { denom: "stake", amount: "200000000000", }; // The amount of tokens you want to delegate to your own validator (you can adjust this)

const registry = new Registry(); const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "cosmos", }); const [{ address: senderAddress }] = await wallet.getAccounts();

const myRegistry = new Registry(defaultRegistryTypes); myRegistry.register("/cosmos.staking.v1beta1.MsgCreateValidator", MsgCreateValidator); myRegistry.register("/tendermint.crypto.PublicKey", PublicKey); const gasPrice = GasPrice.fromString("0.0000001stake"); const client = await SigningStargateClient.connectWithSigner( "http://localhost:26657", wallet, { gasPrice: gasPrice, registry: myRegistry, } );

// Sign and broadcast the transaction const fee = { amount: [{ denom: "stake", amount: "500" }], gas: "200000", };

const sendMsg = { typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", value: MsgCreateValidator.fromPartial({ description: { moniker: validatorMoniker, identity: validatorIdentity, website: validatorWebsite, securityContact: validatorSecurityContact, details: validatorDetails, }, commission: { rate: commissionRate, maxRate: commissionMaxRate, maxChangeRate: commissionMaxChangeRate, }, minSelfDelegation: delegationAmount, delegatorAddress: senderAddress, validatorAddress: "cosmosvaloper1eayalkqh7cpeaf4kgkg9gtt9qlplh7fnfjwk6z", pubkey: { typeUrl: "/tendermint.crypto.PublicKey", value: Uint8Array.from(Buffer.from("KboqFFQffL+9c5Wj45hoySwlYd5OtmbREZ7kEUPuIgs=", "base64")), // The public key will be filled automatically by the library }, value: coin, }), };

const txResult = await client.signAndBroadcast(senderAddress, [sendMsg], fee,"Create Validator"); return txResult; }

createValidator() .then((txResult) => { console.log("Validator creation transaction result:", txResult); }) .catch((error) => console.error("Error creating validator:", error));

//======================================================================================================