如何使用现有钱包签署使用节点 JS 脚本运行的 solana 交易?

dus*_*88c 1 node.js web3js solana

我需要通过使用 solana/web3.js 构建的节点脚本从我的现有地址(从 Phantom 钱包创建)发送一些 SOL。我的钱包里有一把私钥。但我无法用我的私钥字符串签署创建密钥对。

const from = web3.Keypair.generate();
// how to get my wallet account from private key to from account
Run Code Online (Sandbox Code Playgroud)

谢谢

Jon*_*n C 8

如果您有字节数组形式的密钥对,则可以用来Keypair.fromSecretKey获取密钥对:https://github.com/solana-labs/solana/blob/82a6bbe06891bc58ed1fa0586ae5d168e68516bd/web3.js/src/keypair.ts#L60

如果您只有 base58 编码的字符串,则必须首先使用 bs58 节点包对其进行解码:

import bs58 from `bs58`;
const decoded = bs58.decode(keypairString);
Run Code Online (Sandbox Code Playgroud)