solana/web3.js has a function PublicKey.toBuffer(key). How do I get the public key back from that buffer?

"decode()" only returns the correct key if i use "encode()" to turn it into a buffer.

1 Answer 1


You can get the correct key by calling new PublicKey(buffer).

The constructor for PublicKey takes a number of different types: number | string | Buffer | Uint8Array | number[]


import * as web3 from '@solana/web3.js'

const pubkey = new web3.PublicKey('CoG95Mk72pkbhpZ5b4WiBseX6a5Dv19HfTp6reLzjfyM');

const buffer = pubkey.toBuffer();

const origPubkey = new web3.PublicKey(buffer);

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.