javascriptrustanchorsolanaanchor-solana

Converting string to Public Key


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?


Solution

  • 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();
    

    Javascript:

    import { Keypair, PublicKey } from "@solana/web3.js";
    
    (async () => {
      const publicKey = new PublicKey(
        "PUBLIC_KEYSTRING_HERE"
      );