Trait crypto_addr::Address

source ·
pub trait Address {
    // Required method
    fn addr(&self, opts: &AddressFormat) -> Result<String, Error>;
}
Expand description

Generate an Address string from existing Public Key

This trait provides the main functionality of the crate. The addr method implements encoding a public key as an address.

Usage

Addresses are generated from public keys using Address::addr

use crypto_addr::{Address, AddressFormat as AF, Error};
use hex_literal::hex;

// This is a compressed public key in SEC 1 standard format
const PUBKEY_BYTES: [u8; 33] =
    hex!("029d08f65e6aac041328edaeddba9c682356f81b37b052728fd8637c14eddb7ff1");

// Generate a Bitcoin P2PKH address

// Derive a legacy P2PKH Bitcoin Address from the Public Key (this is the Default AddressFormat)
assert_eq!(PUBKEY_BYTES.addr(&AF::default()).as_deref(), Ok("14cWWeAYZmA9oGgLpjekay9xLwHew3Ni34"));

// Derive Addresses from public key bytes in various other formats
assert_eq!(PUBKEY_BYTES.addr(&AF::Litecoin).as_deref(), Ok("LNqTmrUNeRQD45NVzse3rzDiZ9ew7HMYDY"));
assert_eq!(PUBKEY_BYTES.addr(&AF::Dogecoin).as_deref(), Ok("D8kc3u7BsB4SLGrwZKeK8jKZE51xKNc3FF"));
assert_eq!(PUBKEY_BYTES.addr(&AF::Ripple).as_deref(),   Ok("rhcWWewYZmw9oGgLFjek2y9xLAHeAs45sh"));
assert_eq!(PUBKEY_BYTES.addr(&AF::Dash).as_deref(),     Ok("XeJMLtpSXUNjxDGvgcxySVqkBGsM1d5LRg"));
assert_eq!(PUBKEY_BYTES.addr(&AF::ZCash).as_deref(),    Ok("t1MV7WyagY5wkPujEmATsinFsbbUjoMy6b3"));
assert_eq!(PUBKEY_BYTES.addr(&AF::BitcoinGold).as_deref(),   Ok("GMTRvmVVYcmSsjydkgJs1jVrG75W2Zc7dR"));

// Generate a Bitcoin Cash P2PKH address with elided "bitcoincash" prefix
assert_eq!(
    PUBKEY_BYTES.addr(&AF::BitcoinCash(None)).as_deref(),
    Ok("qqnelysjjjyrn4eywr04vf4v9nydrqdk6vep2tp2g9")
);

// Generate a Bitcoin Cash P2PKH address with a custom prefix
assert_eq!(
    PUBKEY_BYTES.addr(&AF::BitcoinCash(Some("foo".to_owned()))).as_deref(),
    Ok("foo:qqnelysjjjyrn4eywr04vf4v9nydrqdk6v730zmhs6")
);

// Errors are detected
let partial_pubkey = &PUBKEY_BYTES[..30];
assert_eq!(
    partial_pubkey.addr(&Default::default()).unwrap_err(),
    Error::InvalidLength{expected: 33, received: 30}
);

Required Methods§

source

fn addr(&self, opts: &AddressFormat) -> Result<String, Error>

Derive an address string from a public key (self). Parameters for the address format are given by opts. Returns Err if public key is not able to derive an address, i.e. if it is not the correct length.

Implementations on Foreign Types§

source§

impl Address for PublicKey

Available on crate feature secp256k1 only.
source§

impl Address for [u8]

Slice of input bytes is interpreted as a compressed public key encoded with encoding described in SEC 1: Elliptic Curve Cryptography (Version 2.0) section 2.3.3 (page 10).

source§

impl Address for PublicKey

Available on crate feature k256 only.
source§

impl Address for VerifyingKey

Available on crate feature k256 only.

Implementors§

source§

impl Address for PubkeyBytes

Array of input bytes is interpreted as a compressed public key encoded with encoding described in SEC 1: Elliptic Curve Cryptography (Version 2.0) section 2.3.3 (page 10).