Skip to content

Add solana-address crate #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ opt-level = 1
members = [
"account",
"account-info",
"address",
"address-lookup-table-interface",
"atomic-u64",
"big-mod-exp",
Expand Down Expand Up @@ -210,6 +211,7 @@ signal-hook = "0.3.17"
siphasher = "0.3.11"
solana-account = { path = "account", version = "2.2.1" }
solana-account-info = { path = "account-info", version = "2.2.1" }
solana-address = { path = "address", version = "0.0.0" }
solana-address-lookup-table-interface = { path = "address-lookup-table-interface", version = "2.2.2" }
solana-atomic-u64 = { path = "atomic-u64", version = "2.2.1" }
solana-big-mod-exp = { path = "big-mod-exp", version = "2.2.1" }
Expand Down
20 changes: 20 additions & 0 deletions address/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "solana-address"
description = "Solana account addresses"
documentation = "https://docs.rs/solana-address"
version = "0.0.0"
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[lints]
workspace = true

[features]
syscalls = ["dep:solana-define-syscall", "dep:solana-program-error"]

[dependencies]
solana-define-syscall = { workspace = true, optional = true }
solana-program-error = { version = "2.2.1", git = "https://github.com/kevinheavey/solana-sdk.git", branch = "program-error-pinocchio-compat", optional = true }
71 changes: 71 additions & 0 deletions address/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Define how a Solana account address is represented.

#![no_std]

#[cfg(feature = "syscalls")]
mod syscalls;
#[cfg(feature = "syscalls")]
pub use syscalls::*;

/// Number of bytes in an address.
pub const ADDRESS_BYTES: usize = 32;

/// Maximum length of derived `Address` seed.
pub const MAX_SEED_LEN: usize = 32;

/// Maximum number of seeds for address derivation.
pub const MAX_SEEDS: usize = 16;

/// The address of a [Solana account][account].
///
/// Some account addresses are [ed25519] public keys, with corresponding secret
/// keys that are managed off-chain. Often, though, account addresses do not
/// have corresponding secret keys — as with [_program derived
/// addresses_][pdas] — or the secret key is not relevant to the operation
/// of a program, and may have even been disposed of.
///
/// [account]: https://solana.com/docs/core/accounts
/// [ed25519]: https://ed25519.cr.yp.to/
/// [pdas]: https://solana.com/docs/core/cpi#program-derived-addresses
pub type Address = [u8; ADDRESS_BYTES];

/// Convenience macro to declare a static address and functions to interact with it.
///
/// This macro is useful to declare a constant representing a program ID for a
/// Solana program.
///
/// # Example
///
/// ```
/// # // wrapper is used so that the macro invocation occurs in the item position
/// # // rather than in the statement position which isn't allowed.
/// use solana_address::{declare_id, Address};
///
/// # mod program {
/// # use solana_address::declare_id;
/// declare_id!([0; 32]);
/// # }
/// # use program::id;
///
/// let address = [0; 32];
/// assert_eq!(id(), address);
/// ```
#[macro_export]
macro_rules! declare_id {
( $id:expr ) => {
#[doc = "The constant program ID."]
pub const ID: $crate::Address = $id;

#[doc = "Returns `true` if the given address is equal to the program ID."]
#[inline]
pub fn check_id(id: &$crate::Address) -> bool {
id == &ID
}

#[doc = "Returns the program ID."]
#[inline]
pub const fn id() -> $crate::Address {
ID
}
};
}
Loading
Loading