Skip to main content

Address

Struct Address 

Source
pub struct Address { /* private fields */ }
Expand description

A Bitcoin P2PKH address.

Contains the 20-byte public key hash and the network prefix byte. Supports both mainnet (prefix 0x00, addresses start with 1) and testnet (prefix 0x6f, addresses start with m or n).

§Cross-SDK Compatibility

This implementation is compatible with the Go SDK’s script.Address and the TypeScript SDK’s address handling. The same address strings are produced for the same public keys across all three SDKs.

Implementations§

Source§

impl Address

Source

pub fn new_from_string(address: &str) -> Result<Self>

Creates an Address from a Base58Check encoded address string.

Validates the checksum and ensures the address uses a supported version prefix (mainnet 0x00 or testnet 0x6f).

§Arguments
  • address - A P2PKH address string
§Returns

The parsed Address, or an error if the string is invalid.

§Example
use bsv_rs::script::Address;

let addr = Address::new_from_string("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH").unwrap();
assert_eq!(addr.to_string(), "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH");
Source

pub fn new_from_public_key_hash(hash: &[u8], mainnet: bool) -> Result<Self>

Creates an Address from a raw 20-byte public key hash.

§Arguments
  • hash - The 20-byte public key hash (RIPEMD160(SHA256(pubkey)))
  • mainnet - If true, creates a mainnet address; otherwise testnet
§Returns

The Address, or an error if the hash is not 20 bytes.

§Example
use bsv_rs::script::Address;

let hash = [0u8; 20];
let addr = Address::new_from_public_key_hash(&hash, true).unwrap();
Source

pub fn new_from_public_key( public_key: &PublicKey, mainnet: bool, ) -> Result<Self>

Creates an Address from a PublicKey.

Computes the hash160 (RIPEMD160(SHA256(compressed_pubkey))) and creates a mainnet or testnet address.

§Arguments
  • public_key - The public key to derive the address from
  • mainnet - If true, creates a mainnet address; otherwise testnet
§Returns

The Address.

§Example
use bsv_rs::script::Address;
use bsv_rs::primitives::ec::PrivateKey;

let key = PrivateKey::random();
let addr = Address::new_from_public_key(&key.public_key(), true).unwrap();
Source

pub fn public_key_hash(&self) -> &[u8]

Returns the 20-byte public key hash.

This is the RIPEMD160(SHA256(compressed_pubkey)) value that is the same regardless of the network type (mainnet or testnet).

Source

pub fn prefix(&self) -> u8

Returns the network prefix byte.

  • 0x00 for mainnet
  • 0x6f for testnet
Source

pub fn is_mainnet(&self) -> bool

Returns true if this is a mainnet address.

Source

pub fn is_valid_address(address: &str) -> bool

Validates whether a string is a valid P2PKH address.

Returns true if the address can be parsed successfully with a valid checksum and a supported version prefix.

§Arguments
  • address - The address string to validate
§Example
use bsv_rs::script::Address;

assert!(Address::is_valid_address("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH"));
assert!(!Address::is_valid_address("invalid"));

Trait Implementations§

Source§

impl Clone for Address

Source§

fn clone(&self) -> Address

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Address

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Address

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Address

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Address

Source§

fn eq(&self, other: &Address) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Address

Source§

impl StructuralPartialEq for Address

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V