Add explicit serialize and deserialize methods to ExternalPubKey

The serialization defined by HDWallet for the fields of ExtendedPubKey
is in the opposite field order from what is defined in ZIP 316.
This commit is contained in:
Kris Nuttycombe 2022-01-24 16:16:55 -07:00
parent 8b0c1c4ab2
commit 72c2e54a7b
1 changed files with 18 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use crate::{legacy::TransparentAddress, sapling::keys::prf_expand_vec};
use hdwallet::{traits::Deserialize, ExtendedPrivKey, ExtendedPubKey};
use hdwallet::{ExtendedPrivKey, ExtendedPubKey};
use secp256k1::PublicKey;
use sha2::{Digest, Sha256};
use std::convert::TryInto;
@ -35,8 +36,7 @@ impl std::convert::TryFrom<&[u8; 65]> for ExternalPubKey {
type Error = hdwallet::error::Error;
fn try_from(data: &[u8; 65]) -> Result<Self, Self::Error> {
let ext_pub_key = ExtendedPubKey::deserialize(data)?;
Ok(Self(ext_pub_key))
ExternalPubKey::deserialize(data)
}
}
@ -83,6 +83,21 @@ impl ExternalPubKey {
pub fn external_ovk(&self) -> ExternalOvk {
self.ovk_for_shielding().1
}
pub fn serialize(&self) -> Vec<u8> {
let mut buf = self.0.chain_code.clone();
buf.extend(self.0.public_key.serialize().to_vec());
buf
}
pub fn deserialize(data: &[u8; 65]) -> Result<Self, hdwallet::error::Error> {
let chain_code = data[..32].to_vec();
let public_key = PublicKey::from_slice(&data[32..])?;
Ok(ExternalPubKey(ExtendedPubKey {
public_key,
chain_code,
}))
}
}
/// Internal ovk used for autoshielding.