-
Notifications
You must be signed in to change notification settings - Fork 449
Impl Writeable and Readable for Address
#3206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ use crate::prelude::*; | |
| use crate::io::{self, Read, Seek, Write}; | ||
| use crate::io_extras::{copy, sink}; | ||
| use core::hash::Hash; | ||
| use core::str::FromStr; | ||
| use crate::sync::{Mutex, RwLock}; | ||
| use core::cmp; | ||
| use core::ops::Deref; | ||
|
|
@@ -27,6 +28,7 @@ use bitcoin::secp256k1::{PublicKey, SecretKey}; | |
| use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE, SCHNORR_SIGNATURE_SIZE}; | ||
| use bitcoin::secp256k1::ecdsa; | ||
| use bitcoin::secp256k1::schnorr; | ||
| use bitcoin::Address; | ||
| use bitcoin::amount::Amount; | ||
| use bitcoin::blockdata::constants::ChainHash; | ||
| use bitcoin::blockdata::script::{self, ScriptBuf}; | ||
|
|
@@ -689,6 +691,24 @@ impl Readable for WithoutLength<ScriptBuf> { | |
| } | ||
| } | ||
|
|
||
| impl Writeable for WithoutLength<&Address> { | ||
| #[inline] | ||
| fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
| writer.write_all(self.0.to_string().as_bytes()) | ||
| } | ||
| } | ||
|
|
||
| impl Readable for WithoutLength<Address> { | ||
| #[inline] | ||
| fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> { | ||
| let v: WithoutLength<String> = Readable::read(r)?; | ||
| match Address::from_str(&v.0) { | ||
| Ok(addr) => Ok(WithoutLength(addr.assume_checked())), | ||
| Err(_) => Err(DecodeError::InvalidValue), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct Iterable<'a, I: Iterator<Item = &'a T> + Clone, T: 'a>(pub I); | ||
|
|
||
|
|
@@ -946,6 +966,26 @@ impl Readable for ScriptBuf { | |
| } | ||
| } | ||
|
|
||
| impl Writeable for Address { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to do both the WithoutLength version and the with? |
||
| fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> { | ||
| let addr_str = self.to_string(); | ||
| (addr_str.len() as u16).write(w)?; | ||
| w.write_all(self.to_string().as_bytes()) | ||
|
Comment on lines
+972
to
+973
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just write a |
||
| } | ||
| } | ||
|
|
||
| impl Readable for Address { | ||
| fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> { | ||
| let len = <u16 as Readable>::read(r)? as usize; | ||
| let mut buf = vec![0; len]; | ||
| r.read_exact(&mut buf)?; | ||
| match Address::from_str(&String::from_utf8(buf).map_err(|_| DecodeError::InvalidValue)?) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Ok(addr) => Ok(addr.assume_checked()), | ||
| Err(_) => Err(DecodeError::InvalidValue), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Writeable for PublicKey { | ||
| fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> { | ||
| self.serialize().write(w) | ||
|
|
@@ -1584,4 +1624,13 @@ mod tests { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn address_serliazation() { | ||
| use std::str::FromStr; | ||
| let addr = bitcoin::Address::from_str("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq").unwrap().assume_checked(); | ||
| let mut buf = Vec::new(); | ||
| addr.write(&mut buf).unwrap(); | ||
| assert_eq!(bitcoin::Address::read(&mut buf.as_slice()).unwrap(), addr); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm, not sure if we should
assume_checkedhere? Kinda defeats the purpose of the network-checking API, but of course on the flip side there's really not much we could practically do to actually check the network...