Skip to main content

solana_address/
lib.rs

1//! Address representation for Solana.
2//!
3//! An address is a sequence of 32 bytes, often shown as a base58 encoded string
4//! (e.g. 14grJpemFaf88c8tiVb77W7TYg2W3ir6pfkKz3YjhhZ5).
5
6#![no_std]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
9#![allow(clippy::arithmetic_side_effects)]
10
11#[cfg(feature = "sha2")]
12mod derive;
13#[cfg(feature = "error")]
14pub mod error;
15#[cfg(feature = "rand")]
16mod hasher;
17#[cfg(any(feature = "curve25519", feature = "syscalls"))]
18pub mod syscalls;
19
20#[cfg(feature = "sha2")]
21use crate::error::AddressError;
22#[cfg(feature = "decode")]
23use crate::error::ParseAddressError;
24#[cfg(all(feature = "rand", not(any(target_os = "solana", target_arch = "bpf"))))]
25pub use crate::hasher::{AddressHasher, AddressHasherBuilder};
26
27#[cfg(feature = "alloc")]
28extern crate alloc;
29#[cfg(feature = "std")]
30extern crate std;
31#[cfg(feature = "alloc")]
32use alloc::vec::Vec;
33#[cfg(feature = "dev-context-only-utils")]
34use arbitrary::Arbitrary;
35#[cfg(feature = "bytemuck")]
36use bytemuck_derive::{Pod, Zeroable};
37#[cfg(feature = "decode")]
38use core::str::FromStr;
39use core::{
40    array,
41    convert::TryFrom,
42    hash::{Hash, Hasher},
43    ptr::read_unaligned,
44};
45#[cfg(feature = "serde")]
46use serde_derive::{Deserialize, Serialize};
47#[cfg(feature = "wincode")]
48use wincode::{SchemaRead, SchemaWrite};
49#[cfg(feature = "borsh")]
50use {
51    alloc::string::ToString,
52    borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
53};
54
55/// Number of bytes in an address.
56pub const ADDRESS_BYTES: usize = 32;
57/// maximum length of derived `Address` seed
58pub const MAX_SEED_LEN: usize = 32;
59/// Maximum number of seeds
60pub const MAX_SEEDS: usize = 16;
61#[cfg(feature = "decode")]
62/// Maximum string length of a base58 encoded address.
63const MAX_BASE58_LEN: usize = 44;
64
65/// Marker used to find program derived addresses (PDAs).
66#[cfg(target_arch = "bpf")]
67pub static PDA_MARKER: &[u8; 21] = b"ProgramDerivedAddress";
68/// Marker used to find program derived addresses (PDAs).
69#[cfg(not(target_arch = "bpf"))]
70pub const PDA_MARKER: &[u8; 21] = b"ProgramDerivedAddress";
71
72/// The address of a [Solana account][acc].
73///
74/// Some account addresses are [ed25519] public keys, with corresponding secret
75/// keys that are managed off-chain. Often, though, account addresses do not
76/// have corresponding secret keys — as with [_program derived
77/// addresses_][pdas] — or the secret key is not relevant to the operation
78/// of a program, and may have even been disposed of. As running Solana programs
79/// can not safely create or manage secret keys, the full [`Keypair`] is not
80/// defined in `solana-program` but in `solana-sdk`.
81///
82/// [acc]: https://solana.com/docs/core/accounts
83/// [ed25519]: https://ed25519.cr.yp.to/
84/// [pdas]: https://solana.com/docs/core/cpi#program-derived-addresses
85/// [`Keypair`]: https://docs.rs/solana-sdk/latest/solana_sdk/signer/keypair/struct.Keypair.html
86#[repr(transparent)]
87#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
88#[cfg_attr(
89    feature = "borsh",
90    derive(BorshSerialize, BorshDeserialize),
91    borsh(crate = "borsh")
92)]
93#[cfg_attr(feature = "borsh", derive(BorshSchema))]
94#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
95#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
96#[cfg_attr(feature = "wincode", derive(SchemaWrite, SchemaRead))]
97#[cfg_attr(feature = "dev-context-only-utils", derive(Arbitrary))]
98#[cfg_attr(not(feature = "decode"), derive(Debug))]
99#[cfg_attr(feature = "copy", derive(Copy))]
100#[derive(Clone, Default, Eq, Ord, PartialEq, PartialOrd)]
101pub struct Address(pub(crate) [u8; 32]);
102
103#[cfg(feature = "sanitize")]
104impl solana_sanitize::Sanitize for Address {}
105
106#[cfg(feature = "decode")]
107impl FromStr for Address {
108    type Err = ParseAddressError;
109
110    fn from_str(s: &str) -> Result<Self, Self::Err> {
111        use five8::DecodeError;
112        if s.len() > MAX_BASE58_LEN {
113            return Err(ParseAddressError::WrongSize);
114        }
115        let mut bytes = [0; ADDRESS_BYTES];
116        five8::decode_32(s, &mut bytes).map_err(|e| match e {
117            DecodeError::InvalidChar(_) => ParseAddressError::Invalid,
118            DecodeError::TooLong
119            | DecodeError::TooShort
120            | DecodeError::LargestTermTooHigh
121            | DecodeError::OutputTooLong => ParseAddressError::WrongSize,
122        })?;
123        Ok(Address(bytes))
124    }
125}
126
127/// Custom impl of Hash for Address.
128///
129/// This allows us to skip hashing the length of the address
130/// which is always the same anyway.
131impl Hash for Address {
132    fn hash<H: Hasher>(&self, state: &mut H) {
133        state.write(self.as_array());
134    }
135}
136
137impl From<&Address> for Address {
138    #[inline]
139    fn from(value: &Address) -> Self {
140        Self(value.0)
141    }
142}
143
144impl From<[u8; 32]> for Address {
145    #[inline]
146    fn from(from: [u8; 32]) -> Self {
147        Self(from)
148    }
149}
150
151impl TryFrom<&[u8]> for Address {
152    type Error = array::TryFromSliceError;
153
154    #[inline]
155    fn try_from(address: &[u8]) -> Result<Self, Self::Error> {
156        <[u8; 32]>::try_from(address).map(Self::from)
157    }
158}
159
160#[cfg(feature = "alloc")]
161impl TryFrom<Vec<u8>> for Address {
162    type Error = Vec<u8>;
163
164    #[inline]
165    fn try_from(address: Vec<u8>) -> Result<Self, Self::Error> {
166        <[u8; 32]>::try_from(address).map(Self::from)
167    }
168}
169
170#[cfg(feature = "decode")]
171impl TryFrom<&str> for Address {
172    type Error = ParseAddressError;
173    fn try_from(s: &str) -> Result<Self, Self::Error> {
174        Address::from_str(s)
175    }
176}
177
178/// Check whether the given bytes represent a valid curve point.
179#[cfg(any(feature = "curve25519", feature = "syscalls"))]
180#[inline(always)]
181pub fn bytes_are_curve_point<T: AsRef<[u8]>>(bytes: T) -> bool {
182    #[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
183    {
184        #[cfg(feature = "curve25519")]
185        {
186            let Ok(compressed_edwards_y) =
187                curve25519_dalek::edwards::CompressedEdwardsY::from_slice(bytes.as_ref())
188            else {
189                return false;
190            };
191            compressed_edwards_y.decompress().is_some()
192        }
193
194        #[cfg(not(feature = "curve25519"))]
195        {
196            core::hint::black_box(bytes);
197            panic!("bytes_are_curve_point is only available with the `curve25519` feature enabled on this crate")
198        }
199    }
200
201    #[cfg(any(target_os = "solana", target_arch = "bpf"))]
202    {
203        // ID for the Ed25519 as defined in `solana-curve25519::CURVE25519_EDWARDS`.
204        const CURVE25519_EDWARDS: u64 = 0;
205
206        // The syscall return `0` when the point is valid – i.e., on the curve;
207        // otherwise it returns `1`
208        //
209        // SAFETY: The syscall validates the input.
210        let result = unsafe {
211            syscalls::sol_curve_validate_point(
212                CURVE25519_EDWARDS,
213                bytes.as_ref() as *const _ as *const u8,
214                core::ptr::null_mut(),
215            )
216        };
217
218        result == 0
219    }
220}
221
222impl Address {
223    pub const fn new_from_array(address_array: [u8; 32]) -> Self {
224        Self(address_array)
225    }
226
227    #[cfg(feature = "decode")]
228    /// Decode a string into an `Address`, usable in a const context
229    pub const fn from_str_const(s: &str) -> Self {
230        let id_array = five8_const::decode_32_const(s);
231        Address::new_from_array(id_array)
232    }
233
234    #[cfg(feature = "atomic")]
235    /// Create a unique `Address` for tests and benchmarks.
236    pub fn new_unique() -> Self {
237        use solana_atomic_u64::AtomicU64;
238        static I: AtomicU64 = AtomicU64::new(1);
239        type T = u32;
240        const COUNTER_BYTES: usize = core::mem::size_of::<T>();
241        let mut b = [0u8; ADDRESS_BYTES];
242        #[cfg(feature = "std")]
243        let mut i = I.fetch_add(1) as T;
244        #[cfg(not(feature = "std"))]
245        let i = I.fetch_add(1) as T;
246        // use big endian representation to ensure that recent unique addresses
247        // are always greater than less recent unique addresses.
248        b[0..COUNTER_BYTES].copy_from_slice(&i.to_be_bytes());
249        // fill the rest of the address with pseudorandom numbers to make
250        // data statistically similar to real addresses.
251        #[cfg(feature = "std")]
252        {
253            let mut hash = std::hash::DefaultHasher::new();
254            for slice in b[COUNTER_BYTES..].chunks_mut(COUNTER_BYTES) {
255                hash.write_u32(i);
256                i += 1;
257                slice.copy_from_slice(&hash.finish().to_ne_bytes()[0..COUNTER_BYTES]);
258            }
259        }
260        // if std is not available, just replicate last byte of the counter.
261        // this is not as good as a proper hash, but at least it is uniform
262        #[cfg(not(feature = "std"))]
263        {
264            for b in b[COUNTER_BYTES..].iter_mut() {
265                *b = (i & 0xFF) as u8;
266            }
267        }
268        Self::from(b)
269    }
270
271    // If target_os = "solana" or target_arch = "bpf", then the
272    // `solana_sha256_hasher` crate will use syscalls which bring no
273    // dependencies; otherwise, this should be opt-in so users don't
274    // need the sha2 dependency.
275    #[cfg(feature = "sha2")]
276    pub fn create_with_seed(
277        base: &Address,
278        seed: &str,
279        owner: &Address,
280    ) -> Result<Address, AddressError> {
281        if seed.len() > MAX_SEED_LEN {
282            return Err(AddressError::MaxSeedLengthExceeded);
283        }
284
285        let owner = owner.as_ref();
286        if owner.len() >= PDA_MARKER.len() {
287            let slice = &owner[owner.len() - PDA_MARKER.len()..];
288            if slice == PDA_MARKER {
289                return Err(AddressError::IllegalOwner);
290            }
291        }
292        let hash = solana_sha256_hasher::hashv(&[base.as_ref(), seed.as_ref(), owner]);
293        Ok(Address::from(hash.to_bytes()))
294    }
295
296    pub const fn to_bytes(&self) -> [u8; 32] {
297        self.0
298    }
299
300    /// Return a reference to the `Address`'s byte array.
301    #[inline(always)]
302    pub const fn as_array(&self) -> &[u8; 32] {
303        &self.0
304    }
305
306    /// Checks whether the given address lies on the Ed25519 curve.
307    ///
308    /// On-curve addresses correspond to valid Ed25519 public keys (and therefore
309    /// can have associated private keys). Off-curve addresses are typically used
310    /// for program-derived addresses (PDAs).
311    #[cfg(any(feature = "curve25519", feature = "syscalls"))]
312    #[inline(always)]
313    pub fn is_on_curve(&self) -> bool {
314        bytes_are_curve_point(self)
315    }
316
317    /// Log an `Address` value.
318    #[cfg(all(not(any(target_os = "solana", target_arch = "bpf")), feature = "std"))]
319    pub fn log(&self) {
320        std::println!("{}", std::string::ToString::to_string(&self));
321    }
322}
323
324impl AsRef<[u8]> for Address {
325    fn as_ref(&self) -> &[u8] {
326        &self.0[..]
327    }
328}
329
330impl AsMut<[u8]> for Address {
331    fn as_mut(&mut self) -> &mut [u8] {
332        &mut self.0[..]
333    }
334}
335
336#[cfg(feature = "decode")]
337fn write_as_base58(f: &mut core::fmt::Formatter, p: &Address) -> core::fmt::Result {
338    let mut out = [0u8; MAX_BASE58_LEN];
339    let len = five8::encode_32(&p.0, &mut out) as usize;
340    // any sequence of base58 chars is valid utf8
341    let as_str = unsafe { core::str::from_utf8_unchecked(&out[..len]) };
342    f.write_str(as_str)
343}
344
345#[cfg(feature = "decode")]
346impl core::fmt::Debug for Address {
347    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
348        write_as_base58(f, self)
349    }
350}
351
352#[cfg(feature = "decode")]
353impl core::fmt::Display for Address {
354    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
355        write_as_base58(f, self)
356    }
357}
358
359/// Custom implementation of equality for `Address`.
360///
361/// The implementation compares the address in 4 chunks of 8 bytes (`u64` values),
362/// which is currently more efficient (CU-wise) than the default implementation.
363///
364/// This isn't the implementation for the `PartialEq` trait because we can't do
365/// structural equality with a trait implementation.
366///
367/// [Issue #345](https://github.com/anza-xyz/solana-sdk/issues/345) contains
368/// more information about the problem.
369#[inline(always)]
370pub fn address_eq(a1: &Address, a2: &Address) -> bool {
371    let p1_ptr = a1.0.as_ptr().cast::<u64>();
372    let p2_ptr = a2.0.as_ptr().cast::<u64>();
373
374    unsafe {
375        read_unaligned(p1_ptr) == read_unaligned(p2_ptr)
376            && read_unaligned(p1_ptr.add(1)) == read_unaligned(p2_ptr.add(1))
377            && read_unaligned(p1_ptr.add(2)) == read_unaligned(p2_ptr.add(2))
378            && read_unaligned(p1_ptr.add(3)) == read_unaligned(p2_ptr.add(3))
379    }
380}
381
382#[cfg(feature = "decode")]
383/// Convenience macro to define a static `Address` value.
384///
385/// Input: a single literal base58 string representation of an `Address`.
386///
387/// # Example
388///
389/// ```
390/// use std::str::FromStr;
391/// use solana_address::{address, Address};
392///
393/// static ID: Address = address!("My11111111111111111111111111111111111111111");
394///
395/// let my_id = Address::from_str("My11111111111111111111111111111111111111111").unwrap();
396/// assert_eq!(ID, my_id);
397/// ```
398#[macro_export]
399macro_rules! address {
400    ($input:literal) => {
401        $crate::Address::from_str_const($input)
402    };
403}
404
405/// Convenience macro to declare a static address and functions to interact with it.
406///
407/// Input: a single literal base58 string representation of a program's ID.
408///
409/// # Example
410///
411/// ```
412/// # // wrapper is used so that the macro invocation occurs in the item position
413/// # // rather than in the statement position which isn't allowed.
414/// use std::str::FromStr;
415/// use solana_address::{declare_id, Address};
416///
417/// # mod item_wrapper {
418/// #   use solana_address::declare_id;
419/// declare_id!("My11111111111111111111111111111111111111111");
420/// # }
421/// # use item_wrapper::id;
422///
423/// let my_id = Address::from_str("My11111111111111111111111111111111111111111").unwrap();
424/// assert_eq!(id(), my_id);
425/// ```
426#[cfg(feature = "decode")]
427#[macro_export]
428macro_rules! declare_id {
429    ($address:expr) => {
430        #[cfg(not(target_arch = "bpf"))]
431        /// The const program ID.
432        pub const ID: $crate::Address = $crate::Address::from_str_const($address);
433        #[cfg(target_arch = "bpf")]
434        /// The const program ID.
435        pub static ID: $crate::Address = $crate::Address::from_str_const($address);
436
437        /// Returns `true` if given address is the ID.
438        // TODO make this const once `derive_const` makes it out of nightly
439        // and we can `derive_const(PartialEq)` on `Address`.
440        pub fn check_id(id: &$crate::Address) -> bool {
441            id == &ID
442        }
443
444        /// Returns the ID.
445        pub const fn id() -> $crate::Address {
446            #[cfg(not(target_arch = "bpf"))]
447            {
448                ID
449            }
450            #[cfg(target_arch = "bpf")]
451            $crate::Address::from_str_const($address)
452        }
453
454        #[cfg(test)]
455        #[test]
456        fn test_id() {
457            assert!(check_id(&id()));
458        }
459    };
460}
461
462/// Same as [`declare_id`] except that it reports that this ID has been deprecated.
463#[cfg(feature = "decode")]
464#[macro_export]
465macro_rules! declare_deprecated_id {
466    ($address:expr) => {
467        #[cfg(not(target_arch = "bpf"))]
468        /// The const ID.
469        pub const ID: $crate::Address = $crate::Address::from_str_const($address);
470        #[cfg(target_arch = "bpf")]
471        /// The const ID.
472        pub static ID: $crate::Address = $crate::Address::from_str_const($address);
473
474        /// Returns `true` if given address is the ID.
475        // TODO make this const once `derive_const` makes it out of nightly
476        // and we can `derive_const(PartialEq)` on `Address`.
477        #[deprecated()]
478        pub fn check_id(id: &$crate::Address) -> bool {
479            id == &ID
480        }
481
482        /// Returns the ID.
483        #[deprecated()]
484        pub const fn id() -> $crate::Address {
485            #[cfg(not(target_arch = "bpf"))]
486            {
487                ID
488            }
489            #[cfg(target_arch = "bpf")]
490            $crate::Address::from_str_const($address)
491        }
492
493        #[cfg(test)]
494        #[test]
495        #[allow(deprecated)]
496        fn test_id() {
497            assert!(check_id(&id()));
498        }
499    };
500}
501
502#[cfg(test)]
503mod tests {
504    use {super::*, core::str::from_utf8, std::string::String};
505
506    fn encode_address(address: &[u8; 32]) -> String {
507        let mut buffer = [0u8; 44];
508        let count = five8::encode_32(address, &mut buffer);
509        from_utf8(&buffer[..count as usize]).unwrap().to_string()
510    }
511
512    #[test]
513    fn test_new_unique() {
514        assert!(Address::new_unique() != Address::new_unique());
515    }
516
517    #[test]
518    fn address_fromstr() {
519        let address = Address::new_unique();
520        let mut address_base58_str = encode_address(&address.0);
521
522        assert_eq!(address_base58_str.parse::<Address>(), Ok(address));
523
524        address_base58_str.push_str(&encode_address(&address.0));
525        assert_eq!(
526            address_base58_str.parse::<Address>(),
527            Err(ParseAddressError::WrongSize)
528        );
529
530        address_base58_str.truncate(address_base58_str.len() / 2);
531        assert_eq!(address_base58_str.parse::<Address>(), Ok(address));
532
533        address_base58_str.truncate(address_base58_str.len() / 2);
534        assert_eq!(
535            address_base58_str.parse::<Address>(),
536            Err(ParseAddressError::WrongSize)
537        );
538
539        let mut address_base58_str = encode_address(&address.0);
540        assert_eq!(address_base58_str.parse::<Address>(), Ok(address));
541
542        // throw some non-base58 stuff in there
543        address_base58_str.replace_range(..1, "I");
544        assert_eq!(
545            address_base58_str.parse::<Address>(),
546            Err(ParseAddressError::Invalid)
547        );
548
549        // too long input string
550        // longest valid encoding
551        let mut too_long = encode_address(&[255u8; ADDRESS_BYTES]);
552        // and one to grow on
553        too_long.push('1');
554        assert_eq!(
555            too_long.parse::<Address>(),
556            Err(ParseAddressError::WrongSize)
557        );
558    }
559
560    #[test]
561    fn test_create_with_seed() {
562        assert!(
563            Address::create_with_seed(&Address::new_unique(), "☉", &Address::new_unique()).is_ok()
564        );
565        assert_eq!(
566            Address::create_with_seed(
567                &Address::new_unique(),
568                from_utf8(&[127; MAX_SEED_LEN + 1]).unwrap(),
569                &Address::new_unique()
570            ),
571            Err(AddressError::MaxSeedLengthExceeded)
572        );
573        assert!(Address::create_with_seed(
574            &Address::new_unique(),
575            "\
576             \u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
577             ",
578            &Address::new_unique()
579        )
580        .is_ok());
581        // utf-8 abuse ;)
582        assert_eq!(
583            Address::create_with_seed(
584                &Address::new_unique(),
585                "\
586                 x\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
587                 ",
588                &Address::new_unique()
589            ),
590            Err(AddressError::MaxSeedLengthExceeded)
591        );
592
593        assert!(Address::create_with_seed(
594            &Address::new_unique(),
595            from_utf8(&[0; MAX_SEED_LEN]).unwrap(),
596            &Address::new_unique(),
597        )
598        .is_ok());
599
600        assert!(
601            Address::create_with_seed(&Address::new_unique(), "", &Address::new_unique(),).is_ok()
602        );
603
604        assert_eq!(
605            Address::create_with_seed(
606                &Address::default(),
607                "limber chicken: 4/45",
608                &Address::default(),
609            ),
610            Ok("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq"
611                .parse()
612                .unwrap())
613        );
614    }
615
616    #[test]
617    fn test_create_program_address() {
618        let exceeded_seed = &[127; MAX_SEED_LEN + 1];
619        let max_seed = &[0; MAX_SEED_LEN];
620        let exceeded_seeds: &[&[u8]] = &[
621            &[1],
622            &[2],
623            &[3],
624            &[4],
625            &[5],
626            &[6],
627            &[7],
628            &[8],
629            &[9],
630            &[10],
631            &[11],
632            &[12],
633            &[13],
634            &[14],
635            &[15],
636            &[16],
637            &[17],
638        ];
639        let max_seeds: &[&[u8]] = &[
640            &[1],
641            &[2],
642            &[3],
643            &[4],
644            &[5],
645            &[6],
646            &[7],
647            &[8],
648            &[9],
649            &[10],
650            &[11],
651            &[12],
652            &[13],
653            &[14],
654            &[15],
655            &[16],
656        ];
657        let program_id = Address::from_str("BPFLoaderUpgradeab1e11111111111111111111111").unwrap();
658        let public_key = Address::from_str("SeedPubey1111111111111111111111111111111111").unwrap();
659
660        assert_eq!(
661            Address::create_program_address(&[exceeded_seed], &program_id),
662            Err(AddressError::MaxSeedLengthExceeded)
663        );
664        assert_eq!(
665            Address::create_program_address(&[b"short_seed", exceeded_seed], &program_id),
666            Err(AddressError::MaxSeedLengthExceeded)
667        );
668        assert!(Address::create_program_address(&[max_seed], &program_id).is_ok());
669        assert_eq!(
670            Address::create_program_address(exceeded_seeds, &program_id),
671            Err(AddressError::MaxSeedLengthExceeded)
672        );
673        assert!(Address::create_program_address(max_seeds, &program_id).is_ok());
674        assert_eq!(
675            Address::create_program_address(&[b"", &[1]], &program_id),
676            Ok("BwqrghZA2htAcqq8dzP1WDAhTXYTYWj7CHxF5j7TDBAe"
677                .parse()
678                .unwrap())
679        );
680        assert_eq!(
681            Address::create_program_address(&["☉".as_ref(), &[0]], &program_id),
682            Ok("13yWmRpaTR4r5nAktwLqMpRNr28tnVUZw26rTvPSSB19"
683                .parse()
684                .unwrap())
685        );
686        assert_eq!(
687            Address::create_program_address(&[b"Talking", b"Squirrels"], &program_id),
688            Ok("2fnQrngrQT4SeLcdToJAD96phoEjNL2man2kfRLCASVk"
689                .parse()
690                .unwrap())
691        );
692        assert_eq!(
693            Address::create_program_address(&[public_key.as_ref(), &[1]], &program_id),
694            Ok("976ymqVnfE32QFe6NfGDctSvVa36LWnvYxhU6G2232YL"
695                .parse()
696                .unwrap())
697        );
698        assert_ne!(
699            Address::create_program_address(&[b"Talking", b"Squirrels"], &program_id).unwrap(),
700            Address::create_program_address(&[b"Talking"], &program_id).unwrap(),
701        );
702    }
703
704    #[test]
705    fn test_address_off_curve() {
706        // try a bunch of random input, all successful generated program
707        // addresses must land off the curve and be unique
708        let mut addresses = std::vec![];
709        for _ in 0..1_000 {
710            let program_id = Address::new_unique();
711            let bytes1 = rand::random::<[u8; 10]>();
712            let bytes2 = rand::random::<[u8; 32]>();
713            if let Ok(program_address) =
714                Address::create_program_address(&[&bytes1, &bytes2], &program_id)
715            {
716                assert!(!program_address.is_on_curve());
717                assert!(!addresses.contains(&program_address));
718                addresses.push(program_address);
719            }
720        }
721    }
722
723    #[test]
724    fn test_find_program_address() {
725        for _ in 0..1_000 {
726            let program_id = Address::new_unique();
727            let (address, bump_seed) =
728                Address::find_program_address(&[b"Lil'", b"Bits"], &program_id);
729            assert_eq!(
730                address,
731                Address::create_program_address(&[b"Lil'", b"Bits", &[bump_seed]], &program_id)
732                    .unwrap()
733            );
734        }
735    }
736
737    fn address_from_seed_by_marker(marker: &[u8]) -> Result<Address, AddressError> {
738        let key = Address::new_unique();
739        let owner = Address::default();
740
741        let mut to_fake = owner.to_bytes().to_vec();
742        to_fake.extend_from_slice(marker);
743
744        let seed = from_utf8(&to_fake[..to_fake.len() - 32]).expect("not utf8");
745        let base = &Address::try_from(&to_fake[to_fake.len() - 32..]).unwrap();
746
747        Address::create_with_seed(&key, seed, base)
748    }
749
750    #[test]
751    fn test_create_with_seed_rejects_illegal_owner() {
752        assert_eq!(
753            address_from_seed_by_marker(PDA_MARKER),
754            Err(AddressError::IllegalOwner)
755        );
756        assert!(address_from_seed_by_marker(&PDA_MARKER[1..]).is_ok());
757    }
758
759    #[test]
760    fn test_as_array() {
761        let bytes = [1u8; 32];
762        let key = Address::from(bytes);
763        assert_eq!(key.as_array(), &bytes);
764        assert_eq!(key.as_array(), &key.to_bytes());
765        // Sanity check: ensure the pointer is the same.
766        assert_eq!(key.as_array().as_ptr(), key.0.as_ptr());
767    }
768
769    #[test]
770    fn test_address_macro() {
771        const ADDRESS: Address =
772            Address::from_str_const("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq");
773        assert_eq!(
774            address!("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq"),
775            ADDRESS
776        );
777        assert_eq!(
778            Address::from_str("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq").unwrap(),
779            ADDRESS
780        );
781    }
782
783    #[test]
784    fn test_address_eq_matches_default_eq() {
785        for i in 0..u8::MAX {
786            let p1 = Address::from([i; ADDRESS_BYTES]);
787            let p2 = Address::from([i; ADDRESS_BYTES]);
788
789            // Identical addresses must be equal.
790            assert!(p1 == p2);
791            assert!(p1.eq(&p2));
792            assert_eq!(p1.eq(&p2), p1.0 == p2.0);
793            assert!(address_eq(&p1, &p2));
794
795            let p3 = Address::from([u8::MAX - i; ADDRESS_BYTES]);
796
797            // Different addresses must not be equal.
798            assert!(p1 != p3);
799            assert!(!p1.eq(&p3));
800            assert_eq!(!p1.eq(&p3), p1.0 != p3.0);
801            assert!(!address_eq(&p1, &p3));
802        }
803    }
804}