gocosmos-sdk

Cosmos how to get account address from an operator address?


My task is to list all validators in addition with their account address. Here is the RPC, which can list all validators, https://buf.build/cosmos/cosmos-sdk/docs/main:cosmos.staking.v1beta1#cosmos.staking.v1beta1.Query.Validators. The validator message proto is below. My problem is that how could I get validator's account address, it doesn't have account address. Any suggests?

message Validator {
  // operator_address defines the address of the validator's operator; bech encoded in JSON.
  string operator_address = 1;
  // consensus_pubkey is the consensus public key of the validator, as a Protobuf Any.
  google.protobuf.Any consensus_pubkey = 2;
  // jailed defined whether the validator has been jailed from bonded status or not.
  bool jailed = 3;
  // status is the validator status (bonded/unbonding/unbonded).
  BondStatus status = 4;
  // tokens define the delegated tokens (incl. self-delegation).
  string tokens = 5;
  // delegator_shares defines total shares issued to a validator's delegators.
  string delegator_shares = 6;
  // description defines the description terms for the validator.
  Description description = 7;
  // unbonding_height defines, if unbonding, the height at which this validator has begun unbonding.
  int64 unbonding_height = 8;
  // unbonding_time defines, if unbonding, the min time for the validator to complete unbonding.
  google.protobuf.Timestamp unbonding_time = 9;
  // commission defines the commission parameters.
  Commission commission = 10;
  // min_self_delegation is the validator's self declared minimum self delegation.
  string min_self_delegation = 11;
}

Solution

  • After digging several hours, I find the answer in cosmos address docs, https://docs.cosmos.network/main/basics/accounts#addresses, it says.

    Each account is identified using Address which is a sequence of bytes derived from a public key.

    Then I know that the operator address is derived from public key. But is it possible to convert it to public key? The answer is yes because both of them are public information, only the representation/format is different.

    valAddr, _ := sdk.ValAddressFromBech32(v.OperatorAddress)
    accAddr, _ := sdk.AccAddressFromHex(hex.EncodeToString(valAddr.Bytes()))
    fmt.Println(accAddr.String())
    // output: cosmos1q...