diff --git a/keys/Cargo.toml b/keys/Cargo.toml index 12e8f95..005829a 100644 --- a/keys/Cargo.toml +++ b/keys/Cargo.toml @@ -23,7 +23,7 @@ std = [ bs58 = { version = "0.4", default-features = false, features = ["alloc"] } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } hex = { version = "0.4", default-features = false } -libsecp256k1 = { version = "0.3.5", default-features = false, features = ["hmac"] } +libsecp256k1 = { version = "0.6.0", default-features = false, features = ["hmac", "static-context"] } serde = { version = "1.0", features = ["derive"], optional = true } light-bitcoin-crypto = { path = "../crypto", default-features = false } diff --git a/keys/src/error.rs b/keys/src/error.rs index 5c8b6cc..cbfe225 100644 --- a/keys/src/error.rs +++ b/keys/src/error.rs @@ -32,13 +32,13 @@ impl core::fmt::Display for Error { } } -impl From for Error { - fn from(e: secp256k1::Error) -> Self { +impl From for Error { + fn from(e: libsecp256k1::Error) -> Self { match e { - secp256k1::Error::InvalidSignature => Error::InvalidSignature, - secp256k1::Error::InvalidPublicKey => Error::InvalidPublic, - secp256k1::Error::InvalidSecretKey => Error::InvalidSecret, - secp256k1::Error::InvalidMessage => Error::InvalidMessage, + libsecp256k1::Error::InvalidSignature => Error::InvalidSignature, + libsecp256k1::Error::InvalidPublicKey => Error::InvalidPublic, + libsecp256k1::Error::InvalidSecretKey => Error::InvalidSecret, + libsecp256k1::Error::InvalidMessage => Error::InvalidMessage, _ => Error::InvalidSignature, } } diff --git a/keys/src/keypair.rs b/keys/src/keypair.rs index 8b87f25..2b4ba96 100644 --- a/keys/src/keypair.rs +++ b/keys/src/keypair.rs @@ -32,8 +32,8 @@ impl KeyPair { } pub fn from_private(private: Private) -> Result { - let secret_key = secp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?; - let pub_key = secp256k1::PublicKey::from_secret_key(&secret_key); + let secret_key = libsecp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?; + let pub_key = libsecp256k1::PublicKey::from_secret_key(&secret_key); let public = if private.compressed { let public = H264::from_slice(&pub_key.serialize_compressed()); Public::Compressed(public) @@ -46,11 +46,11 @@ impl KeyPair { } pub fn from_keypair( - sec: secp256k1::SecretKey, - public: secp256k1::PublicKey, + sec: libsecp256k1::SecretKey, + public: libsecp256k1::PublicKey, network: Network, ) -> Self { - let sec: secp256k1::curve::Scalar = sec.into(); + let sec: libsecp256k1::curve::Scalar = sec.into(); let sec = sec.b32(); let secret = Secret::from_slice(&sec[..]); let serialized = public.serialize(); diff --git a/keys/src/private.rs b/keys/src/private.rs index cd54fc8..6e2d9f0 100644 --- a/keys/src/private.rs +++ b/keys/src/private.rs @@ -43,16 +43,16 @@ impl str::FromStr for Private { impl Private { pub fn sign(&self, message: &Message) -> Result { - 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 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); Ok(signature.serialize_der().as_ref().to_vec().into()) } pub fn sign_compact(&self, message: &Message) -> Result { - 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 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 recovery_id = recovery_id.serialize(); let data = signature.serialize(); diff --git a/keys/src/public.rs b/keys/src/public.rs index df97fdd..6c44c12 100644 --- a/keys/src/public.rs +++ b/keys/src/public.rs @@ -73,37 +73,37 @@ impl Public { pub fn verify(&self, message: &Message, signature: &Signature) -> Result { let public = match self { - Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?, + Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?, Public::Compressed(pubkey) => { - secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())? + libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())? } }; - let mut signature = secp256k1::Signature::parse_der_lax(&**signature)?; + let mut signature = libsecp256k1::Signature::parse_der_lax(&**signature)?; signature.normalize_s(); - let message = secp256k1::Message::parse(message.as_fixed_bytes()); - Ok(secp256k1::verify(&message, &signature, &public)) + let message = libsecp256k1::Message::parse(message.as_fixed_bytes()); + Ok(libsecp256k1::verify(&message, &signature, &public)) } pub fn verify_compact(&self, message: &Message, signature: &[u8; 64]) -> Result { let public = match self { - Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?, + Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?, Public::Compressed(pubkey) => { - secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())? + libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())? } }; - let signature = secp256k1::Signature::parse(signature); - let message = secp256k1::Message::parse(message.as_fixed_bytes()); - Ok(secp256k1::verify(&message, &signature, &public)) + let signature = libsecp256k1::Signature::parse_standard(signature)?; + let message = libsecp256k1::Message::parse(message.as_fixed_bytes()); + Ok(libsecp256k1::verify(&message, &signature, &public)) } pub fn recover_compact(message: &Message, signature: &CompactSignature) -> Result { let recovery_id = (signature[0] - 27) & 3; let compressed = (signature[0] - 27) & 4 != 0; - let recovery_id = secp256k1::RecoveryId::parse(recovery_id)?; + let recovery_id = libsecp256k1::RecoveryId::parse(recovery_id)?; let sign = H512::from_slice(&signature[1..65]); - 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 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 public = if compressed { let public = H264::from_slice(&pub_key.serialize_compressed());