Solana - 如何从 JavaScript / Node.js 中的本地密钥对获取帐户?

Posted

技术标签:

【中文标题】Solana - 如何从 JavaScript / Node.js 中的本地密钥对获取帐户?【英文标题】:Solana - How to get the account from my local keypair in JavaScript / Node.js? 【发布时间】:2021-12-07 02:24:11 【问题描述】:

我正在尝试编写一个 node.js 程序,将一个 SOL 空投到我的 devnet 帐户中(我知道我可以为此使用 CLI,但我想在成功处理空投后继续使用该程序)。

在许多在线示例中,他们首先为此使用 let account = Keypair.generate();. This worked for me too, but I want to use my existing file system wallet / account with the pubkey: DNuqHBGxzm96VLkLWCUctjYW9CX68DBY6jQ1cVuYP2Ai 生成一个新的密钥对/帐户。 首先,我尝试通过运行来获取对帐户的引用:

let accountFromSeed = Keypair.fromSeed("raw present ... <rest of my seed>"); 但这引发了这个错误:UnhandledPromiseRejectionWarning: TypeError: unexpected type, use Uint8Array

然后我尝试将我的 pubkey 直接传递给 requestAirdrop() 命令:

const web3 = require("@solana/web3.js");
(async () => 
    // Connect to cluster
    console.log(web3.clusterApiUrl('devnet'))
    const connection = new web3.Connection(
        web3.clusterApiUrl('devnet'),
        'confirmed',
    );
const airdropSignature = await connection.requestAirdrop(
        "DNuqHBGxzm96VLkLWCUctjYW9CX68DBY6jQ1cVuYP2Ai",   // passing my pubkey directly into the requestAirdrop function
        web3.LAMPORTS_PER_SOL,
    );
    await connection.confirmTransaction(airdropSignature);
)();

使用node solaris 启动脚本后的错误消息:

$ node solaris
https://api.devnet.solana.com
(node:33672) UnhandledPromiseRejectionWarning: TypeError: to.toBase58 is not a function
    at Connection.requestAirdrop (C:\Users\...\workspace\privat\solana\Solaris\node_modules\@solana\web3.js\lib\index.cjs.js:4716:68)
    at C:\Users\...\workspace\privat\solana\Solaris\solaris.js:38:47
    at Object.<anonymous> (C:\Users\...\workspace\privat\solana\Solaris\solaris.js:63:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:33672) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:33672) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the 
Node.js process with a non-zero exit code.

【问题讨论】:

【参考方案1】:

以下代码获取我的密钥(来自我的本地密钥对的完整内容)并从中获取帐户。然后空投 1 个 SOL 到账户中,并将 0.01 个 SOL 转移到另一个随机账户:

const web3 = require("@solana/web3.js");
const Keypair, Transaction, SystemProgram, LAMPORTS_PER_SOL, sendAndConfirmTransaction, clusterApiUrl = require("@solana/web3.js");

let secretKey = Uint8Array.from([233,65,...]); // my secret key...
let fromKeypair  = Keypair.fromSecretKey(secretKey);
// let fromKeypair = Keypair.generate();
let toKeypair = Keypair.generate();
let transaction = new Transaction();

transaction.add(
  SystemProgram.transfer(
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: 10000000
  )
);

let connection = new web3.Connection(clusterApiUrl('devnet'));

connection.requestAirdrop(
    fromKeypair.publicKey,   
    web3.LAMPORTS_PER_SOL,
);

sendAndConfirmTransaction(
  connection,
  transaction,
  [fromKeypair]
);

node solaris执行

【讨论】:

以上是关于Solana - 如何从 JavaScript / Node.js 中的本地密钥对获取帐户?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 @solana/web3.js 从 Solana 中的自定义令牌中删除铸币权限?

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

如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据

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

如何通过“@solana/web3.js”传输自定义令牌

Solana开发入门源码