#solana #constant #anchor #pubkey #string #const #string-literal

const_str_to_pubkey

A crate that helps you derive constant Solana Pubkey with constant string

3 unstable releases

0.2.0 Nov 25, 2024
0.1.1 Mar 6, 2024
0.1.0 Mar 6, 2024

#6 in #pubkey

Download history 91/week @ 2024-08-19 87/week @ 2024-08-26 2/week @ 2024-09-02 16/week @ 2024-09-09 4/week @ 2024-09-16 43/week @ 2024-09-23 35/week @ 2024-09-30 47/week @ 2024-10-07 19/week @ 2024-10-14 50/week @ 2024-10-21 126/week @ 2024-10-28 73/week @ 2024-11-04 14/week @ 2024-11-11 107/week @ 2024-11-18 221/week @ 2024-11-25 25/week @ 2024-12-02

370 downloads per month

WTFPL license

10KB
94 lines

const_str_to_pubkey

A Rust library that helps developers derive a constant Solana Pubkey with a constant string. (Base58 decode algorithm is the same with bs58-rs)

We sometimes want to declare a constant Pubkey (e.g., to store the admin's public key). We can use pubkey! macro provided by Solana official library. However, this macro only works with string literals. If we have a constant &'static str (e.g., from the env! macro), we can't derive the constant Pubkey with pubkey! macro.

This crate addresses this issue.

Usage

use const_str_to_pubkey::str_to_pubkey;

const ADMIN_PUBKEY: Pubkey = str_to_pubkey(env!("ADMIN_PUBKEY"));

To compile, run:

ADMIN_PUBKEY=AdminPubkey11111111111111111111111111111111 cargo build-sbf
# or
ADMIN_PUBKEY=AdminPubkey11111111111111111111111111111111 anchor build
# or exports the environment variable

Or create a cargo configuration file and store the admin's public key there (see the following section).

Cargo check error

When running cargo check (sometimes rust-analyzer runs for us), we may get an error, saying the environment variable is not set. This is because cargo doesn't recognize the environment variables we've set.

To solve this issue, we can simply add a cargo configuration file .cargo/config.toml at project/workspace root (for details, refer to The Cargo Book), with content:

[env]
ADMIN_PUBKEY = "AdminPubkey11111111111111111111111111111111"

Dependencies

~10–18MB
~264K SLoC