Search code examples
node.jsblockchainwalletsolana

Failing in convert a mnemonic to a seed for a solana wallet


I'm trying to use a wallet seed on the backend, using nodejs, to connect in blokchain. The Keypair module from "@solana/web3.js" library has a fromSeed method, that I use with input from another method, from the bip39 library, that converts my mnemonic to a seed. This my code

const mnemonic = <My-mnemonic>
console.log(bip39.validateMnemonic(mnemonic)) // true
bip39.mnemonicToSeed(mnemonic).then(buffer => Keypair.fromSeed(buffer)).catch(err => console.log(err))

This is my error

Error: bad seed size
    at Function.nacl.sign.keyPair.fromSeed (/home/diazrock/Carrera/Elasbit/NFT's/mint-nft-solana/node_modules/tweetnacl/nacl-fast.js:2329:11)
    at Function.fromSeed (/home/diazrock/Carrera/Elasbit/NFT's/mint-nft-solana/node_modules/@solana/web3.js/lib/index.cjs.js:5625:53)
    at bip39.mnemonicToSeed.then.buffer (repl:1:55)

Solution

  • Keypair.fromSeed() takes a Unit8Array check

    buffer.toJSON().data this returns a 64 length array, So this contains both the public key and private key respectively. Check this

    let a = new Uint8Array(buffer.toJSON().data.slice(0,32))
    const key = Keypair.fromSeed(a);
    

    I tried the above and this works. key has a public key and private key component.