Boommeister
Boommeister

Reputation: 2127

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

I'm trying to write a node.js program that airdrops one SOL into my devnet account (I know I can use the CLI for this, but I want to continue with the program after I managed to handle an airdrop).

In many examples online they first generate a new keypair / account for this with let account = Keypair.generate();. This worked for me too, but I want to use my existing file system wallet / account with the pubkey: DNuqHBGxzm96VLkLWCUctjYW9CX68DBY6jQ1cVuYP2Ai`. First I tried to get a reference to the account by running:

let accountFromSeed = Keypair.fromSeed("raw present ... <rest of my seed>"); but this threw this error: UnhandledPromiseRejectionWarning: TypeError: unexpected type, use Uint8Array

Then I tried it with passsing my pubkey directly into the requestAirdrop() command:

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);
})();

Error message after starting the script with 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.

Upvotes: 0

Views: 1822

Answers (1)

Boommeister
Boommeister

Reputation: 2127

Following code takes my secret key (complete content from my local keypair) and gets the account from it. Then it airdrops 1 SOL into the account and transfers 0.01 SOL to another random account:

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]
);

execute with node solaris

Upvotes: 2

Related Questions