From dd9c9ffa3fb65f99dcf2e05c3e6f06bd494b2c0d Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 30 Jan 2019 14:31:32 +0000 Subject: [PATCH] Add encodings for ExtendedSpendingKey and ExtendedFullViewingKey --- zcash_client_backend/src/constants/mainnet.rs | 16 +++++ zcash_client_backend/src/constants/testnet.rs | 16 +++++ zcash_client_backend/src/encoding.rs | 59 ++++++++++++++++++- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/zcash_client_backend/src/constants/mainnet.rs b/zcash_client_backend/src/constants/mainnet.rs index 2850c456b..ed90697d0 100644 --- a/zcash_client_backend/src/constants/mainnet.rs +++ b/zcash_client_backend/src/constants/mainnet.rs @@ -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]. diff --git a/zcash_client_backend/src/constants/testnet.rs b/zcash_client_backend/src/constants/testnet.rs index f4ea3c2a0..f151110e2 100644 --- a/zcash_client_backend/src/constants/testnet.rs +++ b/zcash_client_backend/src/constants/testnet.rs @@ -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]. diff --git a/zcash_client_backend/src/encoding.rs b/zcash_client_backend/src/encoding.rs index 7150621e0..3a201ad39 100644 --- a/zcash_client_backend/src/encoding.rs +++ b/zcash_client_backend/src/encoding.rs @@ -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(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, 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, Error> { + bech32_decode(hrp, s, |data| ExtendedFullViewingKey::read(&data[..]).ok()) +} + /// Writes a [`PaymentAddress`] as a Bech32-encoded string. /// /// # Examples