簡體   English   中英

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: . 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執行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM