pub mod messages;
use bitcoin::key::Parity;
use bitcoin::secp256k1::PublicKey as BitcoinPublicKey;
use nostr_rs::{Kind, PublicKey};
pub const DLC_MESSAGE_KIND: Kind = Kind::Custom(8_888);
pub const ORACLE_ANNOUNCMENT_KIND: Kind = Kind::Custom(88);
pub const ORACLE_ATTESTATION_KIND: Kind = Kind::Custom(89);
pub fn bitcoin_to_nostr_pubkey(bitcoin_pk: &BitcoinPublicKey) -> PublicKey {
let (xonly, _parity) = bitcoin_pk.x_only_public_key();
PublicKey::from_slice(xonly.serialize().as_slice())
.expect("Could not convert Bitcoin key to nostr key.")
}
pub fn nostr_to_bitcoin_pubkey(nostr_pk: &PublicKey) -> BitcoinPublicKey {
let xonly = nostr_pk.xonly().expect("Could not get xonly public key.");
BitcoinPublicKey::from_x_only_public_key(xonly, Parity::Even)
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
#[test]
fn test_nostr_to_bitcoin_pubkey() {
let nostr_pk = "7622b0ca2b5ad4d7441784a97bfc50c69d09853a640ad793a4fb9d238c7e0b15";
let bitcoin_pk = "027622b0ca2b5ad4d7441784a97bfc50c69d09853a640ad793a4fb9d238c7e0b15";
let nostr_pk_2 = bitcoin_to_nostr_pubkey(&BitcoinPublicKey::from_str(bitcoin_pk).unwrap());
assert_eq!(nostr_pk_2.to_string(), nostr_pk);
let nostr = PublicKey::from_str(nostr_pk).unwrap();
let btc_pk = nostr_to_bitcoin_pubkey(&nostr);
assert_eq!(btc_pk.to_string(), bitcoin_pk);
}
}