pub struct Payload { /* private fields */ }
Expand description

Representation of a parsed cashaddr payload (i.e. the hash) and a hash type.

This type deliberately has private fields to guarantee that it can only be instantiated by parseing a valid cashaddr str. As such, all Payload instances represent a deserialized, valid, cashaddr.

Decoding

This type provides the main interface for decoding cashaddr strings via the FromStr trait.

use cashaddr::{Payload, HashType};

// Parse a cashaddr `str` as a Payload using trait FromStr
let payload: Payload = "foobar:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eyde268tla".parse().unwrap();

// The payload exposes the hash (via AsRef, or payload())
assert_eq!(payload.as_ref(),  b"\xf5\xbfH\xb3\x97\xda\xe7\x0b\xe8+<\xcaG\x93\xf8\xeb+l\xda\xc9");
assert_eq!(payload.payload(), b"\xf5\xbfH\xb3\x97\xda\xe7\x0b\xe8+<\xcaG\x93\xf8\xeb+l\xda\xc9");
// the payload exposes the hash type via hashtype
assert_eq!(payload.hash_type(), HashType::P2PKH);

Encoding

Payload supports encoding back to a cashaddr string via the fmt::Display, and CashEnc traits, as well as the Payload::to_string_no_prefix method.

use cashaddr::{Payload, HashType, CashEnc};
let payload: Payload = "foobar:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eyde268tla".parse().unwrap();

// For convenience, `Payload` imlements `trait Display` for encoding the payload using the
// "bitcoincash" prefix, which is the standard prefix for the Bitcoin Cash mainnet:
assert_eq!(payload.to_string(), "bitcoincash:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2");

// For convenience, `Payload` provides to_string_no_prefix method which, which does the same
// but omits the prefix, as is common as most application imply the "bitcoincash" prefix if it
// is absent
assert_eq!(payload.to_string_no_prefix(), "qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2");

// Because `Payload` implements `AsRef<[u8]>`, it also implements CashEnc so it can easily be
// encoded back into a cashaddr string
assert_eq!(
    payload.encode_p2pkh("foobar").unwrap(),
    "foobar:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eyde268tla"
);

Implementations

Return a cashaddr String for the payload, using "bitcoincash" as the prefix, but without including the prefix in the output.

get a reference to the raw bytes comprising the payload

Get the HashType

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more

Format the payload as as a cashaddr using "bitcoincash", the default prefix for the bitcoin cash mainnet, as the prefix

The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.