Add encodings for ExtendedSpendingKey and ExtendedFullViewingKey
This commit is contained in:
parent
17f60a0354
commit
dd9c9ffa3f
|
@ -3,6 +3,22 @@
|
|||
/// [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 [`ExtendedSpendingKey`].
|
||||
///
|
||||
/// Defined in [ZIP 32].
|
||||
///
|
||||
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
|
||||
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
|
||||
pub const HRP_SAPLING_EXTENDED_SPENDING_KEY: &str = "secret-extended-key-main";
|
||||
|
||||
/// The HRP for a Bech32-encoded mainnet [`ExtendedFullViewingKey`].
|
||||
///
|
||||
/// Defined in [ZIP 32].
|
||||
///
|
||||
/// [`ExtendedFullViewingKey`]: zcash_primitives::zip32::ExtendedFullViewingKey
|
||||
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
|
||||
pub const HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY: &str = "zxviews";
|
||||
|
||||
/// The HRP for a Bech32-encoded mainnet [`PaymentAddress`].
|
||||
///
|
||||
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
|
||||
|
|
|
@ -3,6 +3,22 @@
|
|||
/// [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 [`ExtendedSpendingKey`].
|
||||
///
|
||||
/// Defined in [ZIP 32].
|
||||
///
|
||||
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
|
||||
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
|
||||
pub const HRP_SAPLING_EXTENDED_SPENDING_KEY: &str = "secret-extended-key-test";
|
||||
|
||||
/// The HRP for a Bech32-encoded testnet [`ExtendedFullViewingKey`].
|
||||
///
|
||||
/// Defined in [ZIP 32].
|
||||
///
|
||||
/// [`ExtendedFullViewingKey`]: zcash_primitives::zip32::ExtendedFullViewingKey
|
||||
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
|
||||
pub const HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY: &str = "zxviewtestsapling";
|
||||
|
||||
/// The HRP for a Bech32-encoded testnet [`PaymentAddress`].
|
||||
///
|
||||
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
|
||||
|
|
|
@ -10,7 +10,10 @@ use sapling_crypto::{
|
|||
primitives::{Diversifier, PaymentAddress},
|
||||
};
|
||||
use std::io::{self, Write};
|
||||
use zcash_primitives::JUBJUB;
|
||||
use zcash_primitives::{
|
||||
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
|
||||
JUBJUB,
|
||||
};
|
||||
|
||||
fn bech32_encode<F>(hrp: &str, write: F) -> String
|
||||
where
|
||||
|
@ -38,6 +41,60 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Writes an [`ExtendedSpendingKey`] as a Bech32-encoded string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zcash_client_backend::{
|
||||
/// constants::testnet::{COIN_TYPE, HRP_SAPLING_EXTENDED_SPENDING_KEY},
|
||||
/// encoding::encode_extended_spending_key,
|
||||
/// keys::spending_key,
|
||||
/// };
|
||||
///
|
||||
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, 0);
|
||||
/// let encoded = encode_extended_spending_key(HRP_SAPLING_EXTENDED_SPENDING_KEY, &extsk);
|
||||
/// ```
|
||||
pub fn encode_extended_spending_key(hrp: &str, extsk: &ExtendedSpendingKey) -> String {
|
||||
bech32_encode(hrp, |w| extsk.write(w))
|
||||
}
|
||||
|
||||
/// Decodes an [`ExtendedSpendingKey`] from a Bech32-encoded string.
|
||||
pub fn decode_extended_spending_key(
|
||||
hrp: &str,
|
||||
s: &str,
|
||||
) -> Result<Option<ExtendedSpendingKey>, Error> {
|
||||
bech32_decode(hrp, s, |data| ExtendedSpendingKey::read(&data[..]).ok())
|
||||
}
|
||||
|
||||
/// Writes an [`ExtendedFullViewingKey`] as a Bech32-encoded string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zcash_client_backend::{
|
||||
/// constants::testnet::{COIN_TYPE, HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY},
|
||||
/// encoding::encode_extended_full_viewing_key,
|
||||
/// keys::spending_key,
|
||||
/// };
|
||||
/// use zcash_primitives::zip32::ExtendedFullViewingKey;
|
||||
///
|
||||
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, 0);
|
||||
/// let extfvk = ExtendedFullViewingKey::from(&extsk);
|
||||
/// let encoded = encode_extended_full_viewing_key(HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, &extfvk);
|
||||
/// ```
|
||||
pub fn encode_extended_full_viewing_key(hrp: &str, extfvk: &ExtendedFullViewingKey) -> String {
|
||||
bech32_encode(hrp, |w| extfvk.write(w))
|
||||
}
|
||||
|
||||
/// Decodes an [`ExtendedFullViewingKey`] from a Bech32-encoded string.
|
||||
pub fn decode_extended_full_viewing_key(
|
||||
hrp: &str,
|
||||
s: &str,
|
||||
) -> Result<Option<ExtendedFullViewingKey>, Error> {
|
||||
bech32_decode(hrp, s, |data| ExtendedFullViewingKey::read(&data[..]).ok())
|
||||
}
|
||||
|
||||
/// Writes a [`PaymentAddress`] as a Bech32-encoded string.
|
||||
///
|
||||
/// # Examples
|
||||
|
|
Loading…
Reference in New Issue