Skip to content

Commit

Permalink
Add key::PublicKey to schnorrsig::PublicKey conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Nov 10, 2020
1 parent 09b0456 commit 0c937d0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/schnorrsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,26 @@ impl From<ffi::XOnlyPublicKey> for PublicKey {
}
}

impl From<::key::PublicKey> for PublicKey {
fn from(src: ::key::PublicKey) -> PublicKey {
let mut pk = ffi::XOnlyPublicKey::new();

unsafe {
assert_eq!(
1,
ffi::secp256k1_xonly_pubkey_from_pubkey(
ffi::secp256k1_context_no_precomp,
&mut pk,
ptr::null_mut(),
src.as_c_ptr(),
)
);
}

PublicKey(pk)
}
}

serde_impl_from_slice!(PublicKey);

impl<C: Signing> Secp256k1<C> {
Expand Down Expand Up @@ -707,4 +727,22 @@ mod tests {
assert_eq!(PublicKey::from_keypair(&s, &kp), pk);
}
}

#[test]
fn test_from_key_pubkey() {
let kpk1 = ::key::PublicKey::from_str(
"02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443",
)
.unwrap();
let kpk2 = ::key::PublicKey::from_str(
"0384526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07",
)
.unwrap();

let pk1 = PublicKey::from(kpk1);
let pk2 = PublicKey::from(kpk2);

assert_eq!(pk1.serialize()[..], kpk1.serialize()[1..]);
assert_eq!(pk2.serialize()[..], kpk2.serialize()[1..]);
}
}

0 comments on commit 0c937d0

Please sign in to comment.