Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
How to create web3.Keypair object for Solana javascript API?

I want users to come and mint tokens on my website. I have followed Solana docs and came across the following code in the Javascript client section.

var web3Connection = new web3.Connection(
    web3.clusterApiUrl("devnet"),
    'confirmed',
);
// Generate a new wallet keypair and airdrop SOL
var fromWallet = web3.Keypair.generate();
var fromAirdropSignature = await web3Connection.requestAirdrop(
    fromWallet.publicKey,
    web3.LAMPORTS_PER_SOL,
);

//wait for airdrop confirmation
await web3Connection.confirmTransaction(fromAirdropSignature);

//create new token mint
let mint = await splToken.Token.createMint(
    web3Connection,
    fromWallet,
    fromWallet.publicKey,
    null,
    9,
    splToken.TOKEN_PROGRAM_ID,
);

Please correct me if wrong. I think ideally, I should create the wallet and keypair from CLI and then use them through env variables or some secured config in code to mint, transfer. etc.

If the above is correct, how can I create the web3.Keypair object with my existing publicKey and secret values instead of generating a random one?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create a Keypair instance using fromSecretKey and passing in the array of bytes from your keypair file: https://solana-labs.github.io/solana-web3.js/classes/Keypair.html#fromSecretKey

There's a nice example doing exactly that: https://github.com/solana-labs/solana/blob/b16f2da44414be6f211352ed336812131622bae7/docs/src/developing/clients/javascript-reference.md#example-usage-2

about 4 years ago · Juan Pablo Isaza Relatório

0

Create a new file to generate the publicKey, call it createKey.js (or related). Use this simple code:

const fs = require('fs')
const anchor = require('@project-serum/anchor')

const account = anchor.web3.Keypair.generate()

fs.writeFileSync('./keypair.json', JSON.stringify(account))

Inside your JavaScript client you can extract this key by using the fromSecretKey method.

import keyPair from './keypair.json';

const tempArraySecret = Object.values(keyPair._keypair.secretKey);
const secret = new Uint8Array(tempArraySecret)
const account = web3.Keypair.fromSecretKey(secret);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda