4

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.

1
  • I figure out ! I use bs58 library.
    – Jerry
    Commented Jul 28, 2022 at 8:50

2 Answers 2

5

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.

2
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 and acknowledge you have read our privacy policy.

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