Skip to content

Commit

Permalink
nostr: add sign_schnorr_with_ctx and `normalized_public_key_with_ct…
Browse files Browse the repository at this point in the history
…x` methods
  • Loading branch information
yukibtc committed Sep 6, 2023
1 parent d06510b commit 6edce14
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/nostr/src/event/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl UnsignedEvent {
kind: self.kind,
tags: self.tags,
content: self.content,
sig: keys.sign_schnorr(secp, &message, rng)?,
sig: keys.sign_schnorr_with_ctx(secp, &message, rng)?,
#[cfg(feature = "nip03")]
ots: None,
})
Expand Down
20 changes: 16 additions & 4 deletions crates/nostr/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ impl Keys {

/// Generate new random [`Keys`]
pub fn generate() -> Self {
let mut rng = OsRng;
Self::generate_with_ctx(&SECP256K1, &mut rng)
Self::generate_with_ctx(&SECP256K1, &mut OsRng)
}

/// Generate random [`Keys`] with custom [`Rng`]
Expand All @@ -117,6 +116,19 @@ impl Keys {
{
Self::generate_without_keypair_with_ctx(&SECP256K1, rng)
}

/// Get [`PublicKey`]
pub fn normalized_public_key<C>(&self) -> Result<PublicKey, Error>
where
C: Signing,
{
self.normalized_public_key_with_ctx(&SECP256K1)
}

/// Sign schnorr [`Message`]
pub fn sign_schnorr(&self, message: &Message) -> Result<Signature, Error> {
self.sign_schnorr_with_ctx(&SECP256K1, message, &mut OsRng)
}
}

impl Keys {
Expand Down Expand Up @@ -185,7 +197,7 @@ impl Keys {
}

/// Get [`PublicKey`]
pub fn normalized_public_key<C>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error>
pub fn normalized_public_key_with_ctx<C>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error>
where
C: Signing,
{
Expand All @@ -208,7 +220,7 @@ impl Keys {
}

/// Sign schnorr [`Message`]
pub fn sign_schnorr<C, R>(
pub fn sign_schnorr_with_ctx<C, R>(
&self,
secp: &Secp256k1<C>,
message: &Message,
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/nips/nip26.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ where
let unhashed_token = DelegationToken::new(delegatee_pk, conditions);
let hashed_token = Sha256Hash::hash(unhashed_token.as_bytes());
let message = Message::from_slice(hashed_token.as_byte_array())?;
Ok(delegator_keys.sign_schnorr(secp, &message, rng)?)
Ok(delegator_keys.sign_schnorr_with_ctx(secp, &message, rng)?)
}

/// Verify delegation signature
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/nips/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl Request {
Self::SignSchnorr(value) => {
let hash = Sha256Hash::hash(value.as_bytes());
let message = Secp256k1Message::from(hash);
let sig: Signature = keys.sign_schnorr(secp, &message, rng)?;
let sig: Signature = keys.sign_schnorr_with_ctx(secp, &message, rng)?;
Some(Response::SignSchnorr(sig))
}
};
Expand Down

0 comments on commit 6edce14

Please sign in to comment.