Created
December 28, 2021 14:49
-
-
Save Xavier59/b0b216f003b8e54db53c39397e98cd70 to your computer and use it in GitHub Desktop.
Convert Solana private key from/to base58/uint8array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// exporting from a bs58 private key to an Uint8Array | |
// == from phantom private key to solana cli id.json key file | |
// npm install bs58 @solana/web3.js | |
const web3 = require("@solana/web3.js"); | |
const bs58 = require('bs58'); | |
let secretKey = bs58.decode("[base58 private key here]"); | |
console.log(`[${web3.Keypair.fromSecretKey(secretKey).secretKey}]`); | |
// exporting back from Uint8Array to bs58 private key | |
// == from solana cli id.json key file to phantom private key | |
const bs58 = require('bs58'); | |
privkey = new Uint8Array([111, 43, 24, ...]); // content of id.json here | |
console.log(bs58.encode(privkey)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the code to get KeyPair from seed phrase words.
const mnemonic = "...";
const seed = bip39.mnemonicToSeedSync(mnemonic, ""); // (mnemonic, password)
const keypair = Keypair.fromSeed(seed.slice(0, 32));
console.log("keypair: ", keypair);