-2

I have generated accounts using the Solana CLI, but I am getting the public and private keys as

publicKey: Uint8Array(32) [
  102, 255,  46,  44,  90, 176, 207,  98,
  251,  66, 136, 190, 240,  59, 198, 177,
  169,  35, 153,   3, 163,  68, 188, 214,
  225,  46,  55, 111, 159, 157, 182, 111
], 

but I want readable format to sustain key for next transaction.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Please supply a snippet that demonstrates the problem. Without that we have to guess what you tried, and that trick never works. Please read "[ask]" along with "[How do I format my posts...](https://stackoverflow.com/help/formatting)" – the Tin Man Sep 28 '21 at 20:41
  • Thanks to your question, opened up my eyes on how to properly store a publickey on a solana program. For others, its helpful to read publickey documentation: https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html and for rust specific docs: https://docs.rs/solana-program/1.4.17/solana_program/pubkey/struct.Pubkey.html – 0xgio Nov 28 '22 at 22:34

2 Answers2

4

You can try this

console.log(wallet.publicKey.toBase58());

Sudarshan
  • 702
  • 6
  • 24
0

Pubkey is base58 so this (dirty) vanilla (or any base58 lib) should work.

// ref: https://gist.github.com/diafygi/90a3e80ca1c2793220e5/
const MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const encode = function (B) { const A = MAP; var d = [], s = "", i, j, c, n; for (i in B) { j = 0, c = B[i]; s += c || s.length ^ i ? "" : 1; while (j in d || c) { n = d[j]; n = n ? n * 256 + c : c; c = n / 58 | 0; d[j] = n % 58; j++ } } while (j--) s += A[d[j]]; return s };
const decode = function (S) { const A = MAP; var d = [], b = [], i, j, c, n; for (i in S) { j = 0, c = A.indexOf(S[i]); if (c < 0) return undefined; c || b.length ^ i ? i : b.push(0); while (j in d || c) { n = d[j]; n = n ? n * 58 + c : c; c = n >> 8; d[j] = n % 256; j++ } } while (j--) b.push(d[j]); return new Uint8Array(b) };

encode(new Uint8Array([
  102, 255,  46,  44,  90, 176, 207,  98,
  251,  66, 136, 190, 240,  59, 198, 177,
  169,  35, 153,   3, 163,  68, 188, 214,
  225,  46,  55, 111, 159, 157, 182, 111
]))

Output

7w4GYV1BxE9Vvn93BG3NZzZQAYoiTXWnNX8rdXBgQ5Tt
katopz
  • 591
  • 6
  • 14