mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-07-30 20:11:23 +00:00
Implement PaymentAddress encoding and decoding
This commit is contained in:
@@ -7,4 +7,10 @@ authors = [
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bech32 = "0.6"
|
||||
pairing = { path = "../pairing" }
|
||||
sapling-crypto = { path = "../sapling-crypto" }
|
||||
zcash_primitives = { path = "../zcash_primitives" }
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.4"
|
||||
|
@@ -2,3 +2,11 @@
|
||||
///
|
||||
/// [SLIP 44]: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
||||
pub const COIN_TYPE: u32 = 133;
|
||||
|
||||
/// The HRP for a Bech32-encoded mainnet [`PaymentAddress`].
|
||||
///
|
||||
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
|
||||
///
|
||||
/// [`PaymentAddress`]: sapling_crypto::primitives::PaymentAddress
|
||||
/// [Zcash Protocol Specification]: https://github.com/zcash/zips/blob/master/protocol/protocol.pdf
|
||||
pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "zs";
|
||||
|
@@ -2,3 +2,11 @@
|
||||
///
|
||||
/// [SLIP 44]: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
||||
pub const COIN_TYPE: u32 = 1;
|
||||
|
||||
/// The HRP for a Bech32-encoded testnet [`PaymentAddress`].
|
||||
///
|
||||
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
|
||||
///
|
||||
/// [`PaymentAddress`]: sapling_crypto::primitives::PaymentAddress
|
||||
/// [Zcash Protocol Specification]: https://github.com/zcash/zips/blob/master/protocol/protocol.pdf
|
||||
pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "ztestsapling";
|
||||
|
173
zcash_client_backend/src/encoding.rs
Normal file
173
zcash_client_backend/src/encoding.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
//! Encoding and decoding functions for Zcash key and address structs.
|
||||
//!
|
||||
//! Human-Readable Prefixes (HRPs) for Bech32 encodings are located in the [`constants`]
|
||||
//! module.
|
||||
|
||||
use bech32::{convert_bits, Bech32, Error};
|
||||
use pairing::bls12_381::Bls12;
|
||||
use sapling_crypto::{
|
||||
jubjub::edwards,
|
||||
primitives::{Diversifier, PaymentAddress},
|
||||
};
|
||||
use std::io::{self, Write};
|
||||
use zcash_primitives::JUBJUB;
|
||||
|
||||
fn bech32_encode<F>(hrp: &str, write: F) -> String
|
||||
where
|
||||
F: Fn(&mut dyn Write) -> io::Result<()>,
|
||||
{
|
||||
let mut data: Vec<u8> = vec![];
|
||||
write(&mut data).expect("Should be able to write to a Vec");
|
||||
|
||||
let converted =
|
||||
convert_bits(&data, 8, 5, true).expect("Should be able to convert Vec<u8> to Vec<u5>");
|
||||
let encoded = Bech32::new_check_data(hrp.into(), converted).expect("hrp is not empty");
|
||||
|
||||
encoded.to_string()
|
||||
}
|
||||
|
||||
fn bech32_decode<T, F>(hrp: &str, s: &str, read: F) -> Result<Option<T>, Error>
|
||||
where
|
||||
F: Fn(Vec<u8>) -> Option<T>,
|
||||
{
|
||||
let decoded = s.parse::<Bech32>()?;
|
||||
if decoded.hrp() == hrp {
|
||||
convert_bits(decoded.data(), 5, 8, false).map(|data| read(data))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a [`PaymentAddress`] as a Bech32-encoded string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use pairing::bls12_381::Bls12;
|
||||
/// use rand::{SeedableRng, XorShiftRng};
|
||||
/// use sapling_crypto::{
|
||||
/// jubjub::edwards,
|
||||
/// primitives::{Diversifier, PaymentAddress},
|
||||
/// };
|
||||
/// use zcash_client_backend::{
|
||||
/// constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||
/// encoding::encode_payment_address,
|
||||
/// };
|
||||
/// use zcash_primitives::JUBJUB;
|
||||
///
|
||||
/// let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
|
||||
///
|
||||
/// let pa = PaymentAddress {
|
||||
/// diversifier: Diversifier([0u8; 11]),
|
||||
/// pk_d: edwards::Point::<Bls12, _>::rand(rng, &JUBJUB).mul_by_cofactor(&JUBJUB),
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// encode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &pa),
|
||||
/// "ztestsapling1qqqqqqqqqqqqqqqqqqxrrfaccydp867g6zg7ne5ht37z38jtfyw0ygmp0ja6hhf07twjq6awtaj",
|
||||
/// );
|
||||
/// ```
|
||||
pub fn encode_payment_address(hrp: &str, addr: &PaymentAddress<Bls12>) -> String {
|
||||
bech32_encode(hrp, |w| {
|
||||
w.write_all(&addr.diversifier.0)?;
|
||||
addr.pk_d.write(w)
|
||||
})
|
||||
}
|
||||
|
||||
/// Decodes a [`PaymentAddress`] from a Bech32-encoded string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use pairing::bls12_381::Bls12;
|
||||
/// use rand::{SeedableRng, XorShiftRng};
|
||||
/// use sapling_crypto::{
|
||||
/// jubjub::edwards,
|
||||
/// primitives::{Diversifier, PaymentAddress},
|
||||
/// };
|
||||
/// use zcash_client_backend::{
|
||||
/// constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||
/// encoding::decode_payment_address,
|
||||
/// };
|
||||
/// use zcash_primitives::JUBJUB;
|
||||
///
|
||||
/// let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
|
||||
///
|
||||
/// let pa = PaymentAddress {
|
||||
/// diversifier: Diversifier([0u8; 11]),
|
||||
/// pk_d: edwards::Point::<Bls12, _>::rand(rng, &JUBJUB).mul_by_cofactor(&JUBJUB),
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// decode_payment_address(
|
||||
/// HRP_SAPLING_PAYMENT_ADDRESS,
|
||||
/// "ztestsapling1qqqqqqqqqqqqqqqqqqxrrfaccydp867g6zg7ne5ht37z38jtfyw0ygmp0ja6hhf07twjq6awtaj",
|
||||
/// ),
|
||||
/// Ok(Some(pa)),
|
||||
/// );
|
||||
/// ```
|
||||
pub fn decode_payment_address(hrp: &str, s: &str) -> Result<Option<PaymentAddress<Bls12>>, Error> {
|
||||
bech32_decode(hrp, s, |data| {
|
||||
let mut diversifier = Diversifier([0; 11]);
|
||||
diversifier.0.copy_from_slice(&data[0..11]);
|
||||
edwards::Point::<Bls12, _>::read(&data[11..], &JUBJUB)
|
||||
.ok()?
|
||||
.as_prime_order(&JUBJUB)
|
||||
.map(|pk_d| PaymentAddress { pk_d, diversifier })
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use pairing::bls12_381::Bls12;
|
||||
use rand::{SeedableRng, XorShiftRng};
|
||||
use sapling_crypto::{
|
||||
jubjub::edwards,
|
||||
primitives::{Diversifier, PaymentAddress},
|
||||
};
|
||||
use zcash_primitives::JUBJUB;
|
||||
|
||||
use super::{decode_payment_address, encode_payment_address};
|
||||
use crate::constants;
|
||||
|
||||
#[test]
|
||||
fn payment_address() {
|
||||
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
|
||||
|
||||
let addr = PaymentAddress {
|
||||
diversifier: Diversifier([0u8; 11]),
|
||||
pk_d: edwards::Point::<Bls12, _>::rand(rng, &JUBJUB).mul_by_cofactor(&JUBJUB),
|
||||
};
|
||||
|
||||
let encoded_main =
|
||||
"zs1qqqqqqqqqqqqqqqqqqxrrfaccydp867g6zg7ne5ht37z38jtfyw0ygmp0ja6hhf07twjqj2ug6x";
|
||||
let encoded_test =
|
||||
"ztestsapling1qqqqqqqqqqqqqqqqqqxrrfaccydp867g6zg7ne5ht37z38jtfyw0ygmp0ja6hhf07twjq6awtaj";
|
||||
|
||||
assert_eq!(
|
||||
encode_payment_address(constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS, &addr),
|
||||
encoded_main
|
||||
);
|
||||
assert_eq!(
|
||||
decode_payment_address(
|
||||
constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||
encoded_main
|
||||
)
|
||||
.unwrap(),
|
||||
Some(addr.clone())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
encode_payment_address(constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, &addr),
|
||||
encoded_test
|
||||
);
|
||||
assert_eq!(
|
||||
decode_payment_address(
|
||||
constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||
encoded_test
|
||||
)
|
||||
.unwrap(),
|
||||
Some(addr)
|
||||
);
|
||||
}
|
||||
}
|
@@ -4,4 +4,5 @@
|
||||
//! light clients.
|
||||
|
||||
pub mod constants;
|
||||
pub mod encoding;
|
||||
pub mod keys;
|
||||
|
Reference in New Issue
Block a user