从操作员地址获取账户地址的方法是什么?

huangapple go评论120阅读模式
英文:

Cosmos how to get account address from an operator address?

问题

我的任务是列出所有验证者以及他们的账户地址。这是一个可以列出所有验证者的RPC,链接在这里:https://buf.build/cosmos/cosmos-sdk/docs/main:cosmos.staking.v1beta1#cosmos.staking.v1beta1.Query.Validators。验证者消息的proto定义如下。我的问题是如何获取验证者的账户地址,因为它没有账户地址。有什么建议吗?

message Validator {
  // operator_address 定义了验证者的操作地址,以bech编码的JSON格式。
  string operator_address = 1;
  // consensus_pubkey 是验证者的共识公钥,作为Protobuf Any类型。
  google.protobuf.Any consensus_pubkey = 2;
  // jailed 定义了验证者是否被监禁,即是否被取消绑定状态。
  bool jailed = 3;
  // status 是验证者的状态(绑定/解绑/未绑定)。
  BondStatus status = 4;
  // tokens 定义了委托给验证者的代币数量(包括自委托)。
  string tokens = 5;
  // delegator_shares 定义了分配给验证者委托人的总份额。
  string delegator_shares = 6;
  // description 定义了验证者的描述信息。
  Description description = 7;
  // unbonding_height 定义了如果解绑,验证者开始解绑的高度。
  int64 unbonding_height = 8;
  // unbonding_time 定义了如果解绑,验证者完成解绑的最小时间。
  google.protobuf.Timestamp unbonding_time = 9;
  // commission 定义了佣金参数。
  Commission commission = 10;
  // min_self_delegation 是验证者自声明的最小自委托数量。
  string min_self_delegation = 11;
}
英文:

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;
}

答案1

得分: 2

在挖掘了几个小时后,我在 Cosmos 地址文档中找到了答案,链接为 https://docs.cosmos.network/main/basics/accounts#addresses,文档中写道:

每个账户都使用地址(Address)来标识,地址是从公钥派生出的字节序列。

然后我知道操作员地址是由公钥派生而来的。但是是否可能将其转换为公钥呢?答案是肯定的,因为它们都是公开信息,只是表示/格式不同。

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

以上是要翻译的内容。

英文:

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... 

huangapple
  • 本文由 发表于 2022年5月9日 16:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/72169183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定