Skip to content

Commit

Permalink
Run cargo test --release --all
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed Nov 15, 2021
1 parent 02c51c7 commit 4e5546e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 35 deletions.
3 changes: 1 addition & 2 deletions keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ std = [
"scale-info/std",
"sha2/std",

"bitcoin-bech32/std",
"light-bitcoin-crypto/std",
"light-bitcoin-primitives/std",
"light-bitcoin-serialization/std",
Expand All @@ -30,7 +29,7 @@ bs58 = { version = "0.4", default-features = false, features = ["alloc"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
digest = { version = "0.9.0", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
libsecp256k1 = { version = "0.6.0", default-features = false, features = ["hmac", "static-context"] }
libsecp256k1 = { version = "0.3.5", default-features = false, features = ["hmac"] }
musig2 = { git = "https://github.com/chainx-org/Musig2", branch = "lib", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion keys/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Deserializable for Network {
}
}

#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode)]
pub enum AddressTypes {
Expand Down
12 changes: 6 additions & 6 deletions keys/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ impl core::fmt::Display for Error {
}
}

impl From<libsecp256k1::Error> for Error {
fn from(e: libsecp256k1::Error) -> Self {
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
match e {
libsecp256k1::Error::InvalidSignature => Error::InvalidSignature,
libsecp256k1::Error::InvalidPublicKey => Error::InvalidPublic,
libsecp256k1::Error::InvalidSecretKey => Error::InvalidSecret,
libsecp256k1::Error::InvalidMessage => Error::InvalidMessage,
secp256k1::Error::InvalidSignature => Error::InvalidSignature,
secp256k1::Error::InvalidPublicKey => Error::InvalidPublic,
secp256k1::Error::InvalidSecretKey => Error::InvalidSecret,
secp256k1::Error::InvalidMessage => Error::InvalidMessage,
_ => Error::InvalidSignature,
}
}
Expand Down
10 changes: 5 additions & 5 deletions keys/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl KeyPair {
}

pub fn from_private(private: Private) -> Result<KeyPair, Error> {
let secret_key = libsecp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?;
let pub_key = libsecp256k1::PublicKey::from_secret_key(&secret_key);
let secret_key = secp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?;
let pub_key = secp256k1::PublicKey::from_secret_key(&secret_key);
let public = if private.compressed {
let public = H264::from_slice(&pub_key.serialize_compressed());
Public::Compressed(public)
Expand All @@ -46,11 +46,11 @@ impl KeyPair {
}

pub fn from_keypair(
sec: libsecp256k1::SecretKey,
public: libsecp256k1::PublicKey,
sec: secp256k1::SecretKey,
public: secp256k1::PublicKey,
network: Network,
) -> Self {
let sec: libsecp256k1::curve::Scalar = sec.into();
let sec: secp256k1::curve::Scalar = sec.into();
let sec = sec.b32();
let secret = Secret::from_slice(&sec[..]);
let serialized = public.serialize();
Expand Down
12 changes: 6 additions & 6 deletions keys/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ impl str::FromStr for Private {

impl Private {
pub fn sign(&self, message: &Message) -> Result<Signature, Error> {
let secret = libsecp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let (signature, _recovery_id) = libsecp256k1::sign(&message, &secret);
let secret = secp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let (signature, _recovery_id) = secp256k1::sign(&message, &secret);
Ok(signature.serialize_der().as_ref().to_vec().into())
}

pub fn sign_compact(&self, message: &Message) -> Result<CompactSignature, Error> {
let secret = libsecp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let (signature, recovery_id) = libsecp256k1::sign(&message, &secret);
let secret = secp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let (signature, recovery_id) = secp256k1::sign(&message, &secret);
let recovery_id = recovery_id.serialize();
let data = signature.serialize();

Expand Down
30 changes: 15 additions & 15 deletions keys/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ impl Public {

pub fn verify(&self, message: &Message, signature: &Signature) -> Result<bool, Error> {
let public = match self {
Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Compressed(pubkey) => {
libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
}
};
let mut signature = libsecp256k1::Signature::parse_der_lax(&**signature)?;
let mut signature = secp256k1::Signature::parse_der_lax(&**signature)?;
signature.normalize_s();
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
Ok(libsecp256k1::verify(&message, &signature, &public))
let message = secp256k1::Message::parse(message.as_fixed_bytes());
Ok(secp256k1::verify(&message, &signature, &public))
}

pub fn verify_schnorr(&self, message: &Message, signature: [u8; 64]) -> Result<bool, Error> {
Expand All @@ -114,24 +114,24 @@ impl Public {

pub fn verify_compact(&self, message: &Message, signature: &[u8; 64]) -> Result<bool, Error> {
let public = match self {
Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Compressed(pubkey) => {
libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
}
};
let signature = libsecp256k1::Signature::parse_standard(signature)?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
Ok(libsecp256k1::verify(&message, &signature, &public))
let signature = secp256k1::Signature::parse(signature);
let message = secp256k1::Message::parse(message.as_fixed_bytes());
Ok(secp256k1::verify(&message, &signature, &public))
}

pub fn recover_compact(message: &Message, signature: &CompactSignature) -> Result<Self, Error> {
let recovery_id = (signature[0] - 27) & 3;
let compressed = (signature[0] - 27) & 4 != 0;
let recovery_id = libsecp256k1::RecoveryId::parse(recovery_id)?;
let recovery_id = secp256k1::RecoveryId::parse(recovery_id)?;
let sign = H512::from_slice(&signature[1..65]);
let signature = libsecp256k1::Signature::parse_standard(sign.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let pub_key = libsecp256k1::recover(&message, &signature, &recovery_id)?;
let signature = secp256k1::Signature::parse(sign.as_fixed_bytes());
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let pub_key = secp256k1::recover(&message, &signature, &recovery_id)?;

let public = if compressed {
let public = H264::from_slice(&pub_key.serialize_compressed());
Expand All @@ -149,7 +149,7 @@ impl Public {
///
/// X-only public keys become equivalent to a compressed public key
/// that is the X-only key prefixed by the byte 0x02
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode)]
pub struct XOnly(pub [u8; 32]);
Expand Down

0 comments on commit 4e5546e

Please sign in to comment.