Converting string to Public Key

Viewed 102

I am trying to convert a public key saved as a string to an anchor_lang PubKey object in rust. What is the procedure to convert a string to a PubKey object?

2 Answers

Here is an exact example (the string will vary with what you have) that converts a base58 string to Solana Pubkey:

let pk_key = Pubkey::from_str("SDMHqNqN82QSjEaEuqybmpXsjtX98YuTsX6YCdT99to").unwrap();

If your public key can be static

pub const YOUR_PUBKEY: Pubkey = solana_program::pubkey!("base58_pubkey")

Otherwise

let your_pubkey = Pubkey::from_str("base58_pubkey").unwrap();
Related