如何从 NEAR 协议中的私钥 and/or 帐户 ID 获取 public 密钥?

How to get a public key from private key and/or account id in NEAR protocol?

在多种情况下,您可能拥有帐户 ID(这是 public 信息)或拥有私钥(例如,从 NEAR 钱包本地存储中提取),并且您想知道相应的 public 键。

获取相关 public 密钥的选项是什么?

NEAR 协议使用 EdDSA signature, and thus ed25519 keys are used. Having a private (secret) key, you can derive the corresponding public key (but you cannot derive the private key from the public key). Most of the libraries that implement EdDSA signing allows to derive a public key from a secret key, and here is how you can do that with near-api-js:

const nearApi = require('near-api-js')

// NEVER SHARE your private (secret) keys! (The key I used below is not used anywhere)
const keyPair = nearApi.KeyPair.fromString('ed25519:43qKAz3LfCTWpTAZPgA1DGsuwbiAjyosXpDrw24efAGP8Q3TcrnoUzTQHNRF5EbNTR38GRVdsHai9sRnzVu755gU')

console.log(keyPair.getPublicKey().toString())

因此,有了秘钥,就可以随时离线获取对应的public秘钥。

如果您没有密钥,您将无法签署交易,但您可能仍想知道属于某个用户的 public 密钥,在这种情况下您可以通过RPC查询NEAR协议网络:

http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare method=query \
  params:='{
    "request_type": "view_access_key_list",
    "finality": "final",
    "account_id": "near.test"
  }'

或使用 cURL:

curl -X POST https://rpc.testnet.near.org -H 'content-type: application/json' --data \
  '{
    "jsonrpc": "2.0",
    "id": "dontcare",
    "method": "query",
    "params": {
      "request_type": "view_access_key_list",
      "finality": "final",
      "account_id": "near.test"
    }
  }'

作为响应,您将找到所有 public 密钥及其对给定 (near.test) 帐户 ID 的权限。