cashaddr 0.1.1

Cashaddr codec library
Documentation

Implements cashaddr codec for transcoding between cashaddr strings and bytes. The main use of this crate is via the [CashEnc] trait which provides and implementation for encoding any borrowed sequence of u8 into a cashaddr string with arbirary prefix. Also, the library supports parsing cashaddr string via the [Payload] struct, which implements [FromStr]

Encoding Examples

Encoding a sequence of bytes into a cashaddr string is acheived via the [CashEnc] trait. This trait's methods are used to encode the bytes sequence, and is implemented for all types which implement [AsRef<[u8]>]

use hex;
use cashaddr::{CashEnc, HashType};
let payload: Vec<u8> = hex::decode("F5BF48B397DAE70BE82B3CCA4793F8EB2B6CDAC9").unwrap();

// encode the payload bytes as a p2sh cashaddr, using "bchtest" as the prefix
let cashaddr = "bchtest:pr6m7j9njldwwzlg9v7v53unlr4jkmx6eyvwc0uz5t";
assert_eq!(payload.encode_p2sh("bchtest").unwrap(), cashaddr);

// encode the payload bytes as a p2pkh cashaddr, using "bitcoincash" as the prefix
let cashaddr = "bitcoincash:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2";
assert_eq!(payload.encode_p2pkh("bitcoincash").unwrap(), cashaddr);

Decoding Examples

use hex;
use cashaddr::Payload;
let cashaddr = "bitcoincash:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2";
let addr: Payload = cashaddr.parse().unwrap();
let payload = hex::decode("F5BF48B397DAE70BE82B3CCA4793F8EB2B6CDAC9").unwrap();
assert_eq!(payload, addr.payload);

Attribution

Most of this code was forked from bitcoincash-addr. This library was created to both provide a more convenient user interface, as well as support arbitrary prefixes.