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

Solana - How to get the account from my local keypair in JavaScript / Node.js?

我正在尝试编写一个 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

然后我尝试将我的公钥直接传递给 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 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