parity-zcash/src/keys/signature.rs

126 lines
2.3 KiB
Rust
Raw Normal View History

2016-08-21 05:58:35 -07:00
//! Bitcoin signatures.
//!
//! http://bitcoin.stackexchange.com/q/12554/40688
2016-08-21 02:12:53 -07:00
use std::fmt;
use std::cmp::PartialEq;
use std::ops::Deref;
use std::str::FromStr;
use hex::{ToHex, FromHex};
use hash::H520;
use keys::Error;
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
}
impl Deref for Signature {
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-08-21 05:58:35 -07:00
impl FromStr for Signature {
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-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-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-08-21 05:58:35 -07:00
self.0.to_hex().fmt(f)
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-08-21 05:58:35 -07:00
self.0.to_hex().fmt(f)
2016-08-21 02:12:53 -07:00
}
}
2016-08-21 05:58:35 -07:00
impl Deref for CompactSignature {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PartialEq for CompactSignature {
fn eq(&self, other: &Self) -> bool {
let s_slice: &[u8] = self;
let o_slice: &[u8] = other;
s_slice == o_slice
}
}
impl FromStr for CompactSignature {
2016-08-21 02:12:53 -07:00
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
let vec = try!(s.from_hex().map_err(|_| Error::InvalidSignature));
2016-08-21 05:58:35 -07:00
match vec.len() {
2016-08-21 02:12:53 -07:00
65 => {
let mut compact = [0u8; 65];
compact.copy_from_slice(&vec);
2016-08-21 05:58:35 -07:00
Ok(CompactSignature(compact))
2016-08-21 02:12:53 -07:00
},
2016-08-21 05:58:35 -07:00
_ => Err(Error::InvalidSignature)
}
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)
}
}