2016-08-21 05:58:35 -07:00
|
|
|
//! Bitcoin signatures.
|
|
|
|
//!
|
|
|
|
//! http://bitcoin.stackexchange.com/q/12554/40688
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
use std::{fmt, ops, str};
|
2016-08-21 02:12:53 -07:00
|
|
|
use hex::{ToHex, FromHex};
|
|
|
|
use hash::H520;
|
2016-09-19 06:39:57 -07:00
|
|
|
use Error;
|
2016-08-21 02:12:53 -07:00
|
|
|
|
2016-08-21 05:58:35 -07:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub struct Signature(Vec<u8>);
|
|
|
|
|
|
|
|
impl fmt::Debug for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.to_hex().fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.to_hex().fmt(f)
|
|
|
|
}
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
impl ops::Deref for Signature {
|
2016-08-21 02:12:53 -07:00
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-08-21 05:58:35 -07:00
|
|
|
&self.0
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
impl str::FromStr for Signature {
|
2016-08-21 05:58:35 -07:00
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Error> {
|
|
|
|
let vec = try!(s.from_hex().map_err(|_| Error::InvalidSignature));
|
|
|
|
Ok(Signature(vec))
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 05:58:35 -07:00
|
|
|
impl From<&'static str> for Signature {
|
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
s.parse().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for Signature {
|
|
|
|
fn from(v: Vec<u8>) -> Self {
|
|
|
|
Signature(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 13:20:01 -07:00
|
|
|
impl From<Signature> for Vec<u8> {
|
|
|
|
fn from(s: Signature) -> Self {
|
|
|
|
s.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 09:23:32 -07:00
|
|
|
impl Signature {
|
|
|
|
pub fn check_low_s(&self) -> bool {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a [u8]> for Signature {
|
|
|
|
fn from(v: &'a [u8]) -> Self {
|
|
|
|
Signature(v.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
#[derive(PartialEq)]
|
2016-08-21 05:58:35 -07:00
|
|
|
pub struct CompactSignature(H520);
|
|
|
|
|
|
|
|
impl fmt::Debug for CompactSignature {
|
2016-08-21 02:12:53 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-09-19 02:58:53 -07:00
|
|
|
f.write_str(&self.0.to_hex())
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 05:58:35 -07:00
|
|
|
impl fmt::Display for CompactSignature {
|
2016-08-21 02:12:53 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-09-19 02:58:53 -07:00
|
|
|
f.write_str(&self.0.to_hex())
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
impl ops::Deref for CompactSignature {
|
2016-08-21 05:58:35 -07:00
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-09-19 02:58:53 -07:00
|
|
|
&*self.0
|
2016-08-21 05:58:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:58:53 -07:00
|
|
|
impl str::FromStr for CompactSignature {
|
2016-08-21 02:12:53 -07:00
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Error> {
|
2016-09-19 02:58:53 -07:00
|
|
|
match s.parse() {
|
|
|
|
Ok(hash) => Ok(CompactSignature(hash)),
|
|
|
|
_ => Err(Error::InvalidSignature),
|
2016-08-21 05:58:35 -07:00
|
|
|
}
|
2016-08-21 02:12:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 05:58:35 -07:00
|
|
|
impl From<&'static str> for CompactSignature {
|
2016-08-21 02:12:53 -07:00
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
s.parse().unwrap()
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 05:58:35 -07:00
|
|
|
|
|
|
|
impl From<H520> for CompactSignature {
|
|
|
|
fn from(h: H520) -> Self {
|
|
|
|
CompactSignature(h)
|
|
|
|
}
|
|
|
|
}
|