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
impl Address
Sourcepub fn new_from_string(address: &str) -> Result<Self>
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");Sourcepub fn new_from_public_key_hash(hash: &[u8], mainnet: bool) -> Result<Self>
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();Sourcepub fn new_from_public_key(
public_key: &PublicKey,
mainnet: bool,
) -> Result<Self>
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 frommainnet- 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();Sourcepub fn public_key_hash(&self) -> &[u8] ⓘ
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).
Sourcepub fn is_mainnet(&self) -> bool
pub fn is_mainnet(&self) -> bool
Returns true if this is a mainnet address.
Sourcepub fn is_valid_address(address: &str) -> bool
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"));