You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hedera Hashgraph中查询与指定公钥关联的所有账户ID方法问询

实现方案:通过Hedera Mirror Node查询公钥关联的账户ID

核心原理

Hedera共识节点不提供公钥到账户ID的反向查询能力,唯一可行的方式是利用Mirror Node REST API——镜像节点会同步并存储全网所有账户的完整数据,包括密钥信息,因此可以通过它过滤出匹配目标公钥的所有账户。

具体步骤

1. 选择对应网络的镜像节点端点

根据你连接的Hedera网络选择API地址:

  • 主网:https://mainnet-public.mirrornode.hedera.com
  • 测试网:https://testnet.mirrornode.hedera.com
  • 预览网:https://previewnet.mirrornode.hedera.com

2. 调用账户查询API

使用镜像节点的/api/v1/accounts接口,通过public_key参数过滤匹配结果。

curl命令示例

# 替换为目标公钥和对应的镜像节点地址
curl "https://testnet.mirrornode.hedera.com/api/v1/accounts?public_key=302a300506032b6570032100abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"

参数说明

  • public_key:传入DER格式的公钥十六进制字符串(不要带任何前缀,直接使用原始DER编码内容)
  • 可选参数:limit(控制单次返回结果数量,默认100)、order(排序方式,asc/desc)

3. 解析返回结果

接口返回的JSON数据中,accounts数组包含所有匹配的账户记录,每个对象的account_id字段即为你需要的账户ID,同时还会附带账户余额、创建时间等信息。

示例返回片段:

{
  "accounts": [
    {
      "account_id": "0.0.123456",
      "public_key": "302a300506032b6570032100abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
      "balance": 100000000,
      "created_timestamp": "1620000000.000000000"
    }
  ]
}

代码示例(JavaScript)

使用Node.js的fetch API实现查询逻辑:

async function getAccountsByPublicKey(publicKey, network = 'testnet') {
  const baseUrlMap = {
    mainnet: 'https://mainnet-public.mirrornode.hedera.com',
    testnet: 'https://testnet.mirrornode.hedera.com',
    previewnet: 'https://previewnet.mirrornode.hedera.com'
  };
  const baseUrl = baseUrlMap[network];
  if (!baseUrl) throw new Error('无效的网络参数');

  const url = new URL(`${baseUrl}/api/v1/accounts`);
  url.searchParams.set('public_key', publicKey);

  const response = await fetch(url);
  if (!response.ok) throw new Error(`API请求失败: ${response.statusText}`);
  
  const data = await response.json();
  return data.accounts.map(account => account.account_id);
}

// 使用示例
const targetPublicKey = '302a300506032b6570032100abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
getAccountsByPublicKey(targetPublicKey)
  .then(accountIds => console.log('关联的账户ID:', accountIds))
  .catch(err => console.error('查询失败:', err));

注意事项

  • 公钥格式必须为DER编码的十六进制字符串,若你的公钥是PEM格式,需先通过OpenSSL或Hedera SDK转换为DER格式。
  • 镜像节点API有请求速率限制,高频查询需控制请求频率。
  • 同一公钥可关联多个账户,接口会返回所有匹配结果。

内容的提问来源于stack exchange,提问作者Sina Nouri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 12:03:31