2

I want to encode my secretKey to Uint8Array.

I use TextEncoder().

const secret = new TextEncoder().encode(secretKey);

I get secretKey from my phantom wallet.

it returns Uint8Array which is length 88. I need to Uint8Array which is length 64.

I want to convert it to Uint8Array which has 64 length.

2
  • I figure out ! I use bs58 library.
    – Jerry
    Jul 28 at 8:50
  • don't forget to mark an answer accepted or post your own if none applied to the solution
    – trent.sol
    Jul 28 at 17:21

2 Answers 2

4

Here's the script I use for this:

import pkg from 'bs58';
const { decode } = pkg;

const args = process.argv.slice(2);
if (!args.length) {
  throw new Error('No key provided')
}

const key = args[0];
const decoded = decode(key)

console.log(JSON.stringify(Array.from(decoded)))

You can call it with eg. node decode.js 5Ab... > key.json where 5aB... is your base-58 encoded private key from eg. Phantom

And then it'll save in key.json the 64-length Uint8array in the format the Solana CLI expects, ie a JSON array.

0
let bs58 = require("bs58");
  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
  const bs = bs58.decode(secretKey);

  const wallet = Keypair.fromSecretKey(bs);

I can call it with base58 library !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.