如何使用我的私钥文件加载我的 Solana 钱包?

问题描述 投票:0回答:4

我正在尝试使用通过 Solana 命令行生成的私钥在 JavaScript / Node.js 中创建钱包。 我想使用

web3.Keypair.fromSeed()
方法。

这是我迄今为止采取的步骤。

  1. 创建了 solana 钱包:
    solana-keygen new -o keyfile.json
  2. 显示该文件中的内容——它是一个 64 字节数组(这是一个测试密钥,因此不用担心这是私钥

[237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]

但是,对

fromSeed()
的调用只需要 32 个字节。 3.检查 solana 地址,以便我知道一切何时正常工作:

>  solana address -->
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH

这就是公钥

如何调用

web3.Keypair.fromSeed()
加载该私钥并获取我的公共地址(也称为公钥)?

blockchain smartcontracts private-key solana
4个回答
7
投票
let web3 = require('@solana/web3.js');
let splToken = require('@solana/spl-token');

// load up the first 32 bytes of the 64 byte array that was in our keyfile.json
// Only need the first 32 bytes so I use slice() just to make sure it's the correct length
let firstWinPrivKey = [237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]
    .slice(0,32);
  // print the length of the array so we know it is correct
  // the fromSeed() method requires 32 bytes

 console.log(firstWinPrivKey.length);
  let firstWinWallet = web3.Keypair.fromSeed(Uint8Array.from(firstWinPrivKey));
  console.log(firstWinWallet.secretKey);
  console.log(firstWinWallet.publicKey.toString());

请注意,您必须将数组转换为 Uint8Array (Uint8Array.from()) 当我们打印出 SecretKey 时,您将看到传入的相同字节。

最后,当我们打印出公钥时,您将看到与我们在命令行中看到的相同的值

> solana address

现在您可以在代码中使用钱包了。

这是这个简短脚本的最终输出:

32
Uint8Array(64) [
  237, 158,  92, 107, 132, 192,   1,  57,   8,  20, 213,
  108,  29, 227,  37,   8,   3, 105, 196, 244,   8, 221,
  184, 199,  62, 253,  98, 131,  33, 165, 165, 215,  14,
    7,  46,  23, 221, 242, 240, 226,  94,  79, 161,  31,
  192, 163,  13,  25, 106,  53,  34, 215,  83, 124, 162,
  156,   8,  97, 194, 180, 213, 179,  33,  68
]
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH

2
投票

如果你想使用“.json”文件,你可以这样做:

import Fs from "@supercharge/fs";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";

const decodedKey = new Uint8Array(
JSON.parse(
  //replace with actual path from home dir. For example '.config/solana/devnet.json'
  Fs.readFileSync(Fs.homeDir("path to key.json")).toString();
));

let keyPair = Keypair.fromSecretKey(decodedKey);

我使用附加包 https://www.npmjs.com/package/@supercharge/fs 来处理文件。


0
投票
import { Keypair } from "@solana/web3.js";
import fs from "fs";

function loadKeypairFromFile(filename: string): Keypair {
  
  const secret = JSON.parse(fs.readFileSync(filename).toString()) as number[];
  const secretKey = Uint8Array.from(secret);
  return Keypair.fromSecretKey(secretKey);
}

0
投票

2023年答案

Solana 基金会的一些人制作了一个小包,让这一切变得更容易。

npm i @solana-developers/node-helpers @digitak/esrun

然后在

show-pubkey.ts

import { getKeypairFromFile } from "@solana-developers/node-helpers";
const keypair = await getKeypairFromFile();

您可以使用

npx esrun load-pubkey.ts

来运行它
© www.soinside.com 2019 - 2024. All rights reserved.