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

基于Aptos JS npm模块:从私钥获取地址/公钥的代码需求

使用Aptos JS npm模块实现私钥相关操作

a) 从私钥直接获取地址

基于你提供的从公钥获取地址的逻辑,从私钥获取地址只需先从私钥推导出公钥,再沿用原有流程即可:

const { HexString, TxnBuilderTypes } = require("aptos");

// 替换为你的私钥十六进制字符串
let privateKeyHex = "your-private-key-hex-string";

// 将私钥转换为Uint8Array格式
let privateKeyBytes = HexString.ensure(privateKeyHex).toUint8Array();
// 创建Ed25519私钥实例
const privateKey = new TxnBuilderTypes.Ed25519PrivateKey(privateKeyBytes);
// 从私钥推导对应公钥
const publicKey = privateKey.publicKey();

// 沿用你已有的地址生成逻辑
const authKey = TxnBuilderTypes.AuthenticationKey.fromEd25519PublicKey(publicKey);
console.log(authKey.derivedAddress());

b) 从私钥推导公钥

直接通过私钥实例的publicKey()方法即可获取对应公钥,示例代码如下:

const { HexString, TxnBuilderTypes } = require("aptos");

// 替换为你的私钥十六进制字符串
let privateKeyHex = "your-private-key-hex-string";

// 将私钥转换为Uint8Array格式
let privateKeyBytes = HexString.ensure(privateKeyHex).toUint8Array();
// 创建Ed25519私钥实例
const privateKey = new TxnBuilderTypes.Ed25519PrivateKey(privateKeyBytes);

// 推导公钥并转换为十六进制字符串输出
const publicKey = privateKey.publicKey();
console.log(publicKey.toHexString());

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 18:55:06