Skip to content

Commit faba025

Browse files
committed
fix rebase issues
1 parent fe6bd38 commit faba025

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

crates/nostr/src/event/builder.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#[cfg(feature = "alloc")]
66
use alloc::{
77
string::{String, ToString},
8+
vec,
89
vec::Vec,
910
};
1011

@@ -33,6 +34,7 @@ pub enum Error {
3334
#[error(transparent)]
3435
Key(#[from] key::Error),
3536
/// Secp256k1 error
37+
3638
#[error("Secp256k1 Error: {0}")]
3739
Secp256k1(secp256k1::Error),
3840
/// JSON error
@@ -84,6 +86,12 @@ impl EventBuilder {
8486
#[cfg(feature = "std")]
8587
pub fn to_event(self, keys: &Keys) -> Result<Event, Error> {
8688
let pubkey: XOnlyPublicKey = keys.public_key();
89+
Ok(self.to_unsigned_event(pubkey).sign(keys)?)
90+
}
91+
92+
/// Build [`UnsignedEvent`]
93+
#[cfg(feature = "std")]
94+
pub fn to_unsigned_event(self, pubkey: XOnlyPublicKey) -> UnsignedEvent {
8795
let created_at: Timestamp = Timestamp::now();
8896
let id = EventId::new(&pubkey, created_at, &self.kind, &self.tags, &self.content);
8997
let message = Message::from_slice(id.as_bytes())?;
@@ -102,19 +110,42 @@ impl EventBuilder {
102110
) -> Result<Event, Error> {
103111
let pubkey: XOnlyPublicKey = keys.public_key();
104112
let id = EventId::new(&pubkey, created_at, &self.kind, &self.tags, &self.content);
105-
UnsignedEvent {
113+
let message = Message::from_slice(id.as_bytes())?;
114+
let signature = keys.sign_schnorr_with_secp(&message, secp)?;
115+
116+
Self::to_event_internal(self, keys, created_at, id, signature)
117+
}
118+
119+
fn to_event_internal(
120+
self,
121+
keys: &Keys,
122+
created_at: Timestamp,
123+
id: EventId,
124+
sig: Signature,
125+
) -> Result<Event, Error> {
126+
let pubkey: XOnlyPublicKey = keys.public_key();
127+
128+
Ok(Event {
106129
id,
107130
pubkey,
108131
created_at,
109132
kind: self.kind,
110133
tags: self.tags,
111134
content: self.content,
112-
}
135+
sig,
136+
})
113137
}
114138

115139
/// Build POW [`Event`]
116140
#[cfg(feature = "std")]
117141
pub fn to_pow_event(self, keys: &Keys, difficulty: u8) -> Result<Event, Error> {
142+
let pubkey: XOnlyPublicKey = keys.public_key();
143+
Ok(self.to_unsigned_pow_event(pubkey, difficulty).sign(keys)?)
144+
}
145+
146+
/// Build unsigned POW [`Event`]
147+
#[cfg(feature = "std")]
148+
pub fn to_unsigned_pow_event(self, pubkey: XOnlyPublicKey, difficulty: u8) -> UnsignedEvent {
118149
#[cfg(target_arch = "wasm32")]
119150
use instant::Instant;
120151
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
@@ -123,7 +154,9 @@ impl EventBuilder {
123154
let now = Instant::now();
124155

125156
use secp256k1::SECP256K1;
126-
self.to_pow_event_with_time_supplier_with_secp::<Instant, _>(keys, difficulty, &now, SECP256K1)
157+
self.to_pow_event_with_time_supplier_with_secp::<Instant, _>(
158+
keys, difficulty, &now, SECP256K1,
159+
)
127160
}
128161

129162
/// Build POW [`Event`] using the given time supplier
@@ -158,8 +191,6 @@ impl EventBuilder {
158191
#[cfg(all(feature = "alloc", not(feature = "std")))]
159192
use core::cmp;
160193

161-
/// Build unsigned POW [`Event`]
162-
pub fn to_unsigned_pow_event(self, pubkey: XOnlyPublicKey, difficulty: u8) -> UnsignedEvent {
163194
let mut nonce: u128 = 0;
164195
let mut tags: Vec<Tag> = self.tags.clone();
165196

@@ -196,7 +227,6 @@ impl EventBuilder {
196227
#[cfg(all(feature = "std", not(feature = "alloc")))]
197228
let sig = keys.sign_schnorr(&message)?;
198229

199-
200230
return self.to_event_internal(keys, created_at, id, sig);
201231
}
202232

@@ -247,6 +277,7 @@ impl EventBuilder {
247277
///
248278
/// # Example
249279
/// ```rust,no_run
280+
/// use nostr::url::Url;
250281
/// use nostr::{EventBuilder, Metadata};
251282
///
252283
/// let metadata = Metadata::new()

crates/nostr/src/event/tag.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
#[cfg(all(feature = "alloc", not(feature = "std")))]
77
use alloc::{
8+
borrow::ToOwned,
89
fmt, format,
910
str::FromStr,
1011
string::{String, ToString},

crates/nostr/src/key/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl Keys {
220220
}
221221

222222
/// Get [`PublicKey`]
223+
#[cfg(feature = "std")]
223224
pub fn normalized_public_key(&self) -> Result<PublicKey, Error> {
224225
Ok(self.secret_key()?.public_key(SECP256K1))
225226
}

crates/nostr/src/nips/nip26.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn sign_delegation_with_signer<C: Signing>(
109109
) -> Result<Signature, Error> {
110110
let unhashed_token = DelegationToken::new(delegatee_pk, conditions);
111111
let hashed_token = Sha256Hash::hash(unhashed_token.as_bytes());
112-
let message = Message::from_slice(&hashed_token)?;
112+
let message = Message::from_slice(hashed_token.as_byte_array())?;
113113
Ok(delegator_keys.sign_schnorr_with_secp(&message, secp)?)
114114
}
115115

@@ -139,7 +139,7 @@ pub fn verify_delegation_signature<C: Verification>(
139139
) -> Result<(), Error> {
140140
let unhashed_token = DelegationToken::new(delegatee_public_key, conditions);
141141
let hashed_token = Sha256Hash::hash(unhashed_token.as_bytes());
142-
let message = Message::from_slice(&hashed_token)?;
142+
let message = Message::from_slice(hashed_token.as_byte_array())?;
143143
secp.verify_schnorr(&signature, &message, &delegator_public_key)?;
144144
Ok(())
145145
}

0 commit comments

Comments
 (0)