Merge pull request #507 from stevenroose/pubkey-write-result

Change PublicKey::write_into to return Result
This commit is contained in:
Andrew Poelstra 2020-11-05 19:21:39 +00:00 committed by GitHub
commit d3210b39fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 9 deletions

View File

@ -35,6 +35,7 @@
#![deny(dead_code)] #![deny(dead_code)]
#![deny(unused_imports)] #![deny(unused_imports)]
#![deny(missing_docs)] #![deny(missing_docs)]
#![deny(unused_must_use)]
// Re-exported dependencies. // Re-exported dependencies.
#[macro_use] pub extern crate bitcoin_hashes as hashes; #[macro_use] pub extern crate bitcoin_hashes as hashes;

View File

@ -222,7 +222,7 @@ impl Address {
#[inline] #[inline]
pub fn p2pkh(pk: &key::PublicKey, network: Network) -> Address { pub fn p2pkh(pk: &key::PublicKey, network: Network) -> Address {
let mut hash_engine = PubkeyHash::engine(); let mut hash_engine = PubkeyHash::engine();
pk.write_into(&mut hash_engine); pk.write_into(&mut hash_engine).expect("engines don't error");
Address { Address {
network: network, network: network,
@ -250,7 +250,7 @@ impl Address {
} }
let mut hash_engine = WPubkeyHash::engine(); let mut hash_engine = WPubkeyHash::engine();
pk.write_into(&mut hash_engine); pk.write_into(&mut hash_engine).expect("engines don't error");
Ok(Address { Ok(Address {
network: network, network: network,
@ -271,7 +271,7 @@ impl Address {
} }
let mut hash_engine = WPubkeyHash::engine(); let mut hash_engine = WPubkeyHash::engine();
pk.write_into(&mut hash_engine); pk.write_into(&mut hash_engine).expect("engines don't error");
let builder = script::Builder::new() let builder = script::Builder::new()
.push_int(0) .push_int(0)

View File

@ -575,7 +575,7 @@ impl ExtendedPubKey {
/// Returns the HASH160 of the public key of the xpub /// Returns the HASH160 of the public key of the xpub
pub fn identifier(&self) -> XpubIdentifier { pub fn identifier(&self) -> XpubIdentifier {
let mut engine = XpubIdentifier::engine(); let mut engine = XpubIdentifier::engine();
self.public_key.write_into(&mut engine); self.public_key.write_into(&mut engine).expect("engines don't error");
XpubIdentifier::from_engine(engine) XpubIdentifier::from_engine(engine)
} }

View File

@ -101,18 +101,18 @@ impl PublicKey {
} }
/// Write the public key into a writer /// Write the public key into a writer
pub fn write_into<W: io::Write>(&self, mut writer: W) { pub fn write_into<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
let _: io::Result<()> = if self.compressed { if self.compressed {
writer.write_all(&self.key.serialize()) writer.write_all(&self.key.serialize())
} else { } else {
writer.write_all(&self.key.serialize_uncompressed()) writer.write_all(&self.key.serialize_uncompressed())
}; }
} }
/// Serialize the public key to bytes /// Serialize the public key to bytes
pub fn to_bytes(&self) -> Vec<u8> { pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::new(); let mut buf = Vec::new();
self.write_into(&mut buf); self.write_into(&mut buf).expect("vecs don't error");
buf buf
} }

View File

@ -63,7 +63,7 @@ impl Deserialize for Script {
impl Serialize for PublicKey { impl Serialize for PublicKey {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new(); let mut buf = Vec::new();
self.write_into(&mut buf); self.write_into(&mut buf).expect("vecs don't error");
buf buf
} }
} }