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

在NEAR协议中如何从私钥或账户ID获取对应公钥?

Great question! When you hold a NEAR private key (like one extracted from NEAR Wallet's local storage) and need to get its matching public key, there are several trustworthy, safe methods to do this. Let's walk through each one:

1. Use the NEAR CLI Command-Line Tool

This is the most straightforward official method, perfect for quick checks:

  • First, make sure you have NEAR CLI installed. If not, run npm install -g near-cli in your terminal.
  • Run the inspection command with your private key:
    near key inspect <your-private-key>
    
  • The output will clearly show the corresponding public key, formatted like ed25519:<public-key-string>.

2. Use the NEAR JavaScript SDK (near-api-js)

For developers integrating this into code, the near-api-js library makes it simple:

  • Install the library first if you haven't: npm install near-api-js
  • Use this snippet to derive the public key:
    const { KeyPair } = require('near-api-js');
    
    // Replace with your actual private key (including the ed25519: prefix)
    const privateKey = "ed25519:abc123...";
    const keyPair = KeyPair.fromString(privateKey);
    const publicKey = keyPair.getPublicKey().toString();
    
    console.log("Corresponding public key:", publicKey);
    

3. Offline Local Calculation (Highest Security)

If you're concerned about private key exposure never use online tools—instead, run an offline script locally. Here's a Python example using cryptography libraries:

  • First install the required package: pip install pycryptodome
  • Use this code (note: remove the ed25519: prefix from your private key first):
    import base64
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
    from cryptography.hazmat.primitives import serialization
    
    # Replace with your private key (without the ed25519: prefix)
    raw_private_key = "abc123..."
    private_key_bytes = base64.b64decode(raw_private_key)
    private_key = Ed25519PrivateKey.from_private_bytes(private_key_bytes)
    
    public_key = private_key.public_key()
    public_key_bytes = public_key.public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw
    )
    
    # Format back to NEAR's standard public key format
    near_public_key = f"ed25519:{base64.b64encode(public_key_bytes).decode('utf-8')}"
    print(near_public_key)
    

This method keeps your private key entirely offline, eliminating any risk of network-based exposure.

Bonus: Check Exported NEAR Wallet Files

If you exported a full key JSON file from NEAR Wallet, you don't need to calculate anything—open the file, and you'll find a public_key field directly included with the private key data. This only works if you have the complete exported key package, though.


内容的提问来源于stack exchange,提问作者Vlad Frolov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 09:12:33