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

如何在Node.js脚本中使用已有钱包私钥为Solana交易签名

实现步骤

  1. 安装必要依赖:
npm install @solana/web3.js bs58
  1. 替换原有生成密钥对的逻辑,代码示例如下:
const web3 = require('@solana/web3.js');
const bs58 = require('bs58');

// 替换为你从Phantom钱包导出的私钥字符串
const privateKeyStr = '你的Phantom导出的私钥字符串';
// 将base58编码的私钥字符串转为Uint8Array格式
const secretKey = bs58.decode(privateKeyStr);
// 生成对应钱包的Keypair实例
const from = web3.Keypair.fromSecretKey(secretKey);

注意事项

  • 不要将私钥明文硬编码到代码中,建议通过 .env 环境变量加载私钥,生产环境可搭配专用密钥管理服务使用
  • 转账前请确认当前账户内SOL余额足够覆盖转账金额+链上手续费,单笔SOL转账手续费约为0.000005 SOL
  • 如果你的私钥导出后是数字数组格式,可直接传入 web3.Keypair.fromSecretKey() 方法,无需额外bs58解码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:54:03