5
5
#[ cfg( feature = "alloc" ) ]
6
6
use alloc:: {
7
7
string:: { String , ToString } ,
8
+ vec,
8
9
vec:: Vec ,
9
10
} ;
10
11
@@ -33,6 +34,7 @@ pub enum Error {
33
34
#[ error( transparent) ]
34
35
Key ( #[ from] key:: Error ) ,
35
36
/// Secp256k1 error
37
+
36
38
#[ error( "Secp256k1 Error: {0}" ) ]
37
39
Secp256k1 ( secp256k1:: Error ) ,
38
40
/// JSON error
@@ -84,6 +86,12 @@ impl EventBuilder {
84
86
#[ cfg( feature = "std" ) ]
85
87
pub fn to_event ( self , keys : & Keys ) -> Result < Event , Error > {
86
88
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 {
87
95
let created_at: Timestamp = Timestamp :: now ( ) ;
88
96
let id = EventId :: new ( & pubkey, created_at, & self . kind , & self . tags , & self . content ) ;
89
97
let message = Message :: from_slice ( id. as_bytes ( ) ) ?;
@@ -102,19 +110,42 @@ impl EventBuilder {
102
110
) -> Result < Event , Error > {
103
111
let pubkey: XOnlyPublicKey = keys. public_key ( ) ;
104
112
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 {
106
129
id,
107
130
pubkey,
108
131
created_at,
109
132
kind : self . kind ,
110
133
tags : self . tags ,
111
134
content : self . content ,
112
- }
135
+ sig,
136
+ } )
113
137
}
114
138
115
139
/// Build POW [`Event`]
116
140
#[ cfg( feature = "std" ) ]
117
141
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 {
118
149
#[ cfg( target_arch = "wasm32" ) ]
119
150
use instant:: Instant ;
120
151
#[ cfg( all( feature = "std" , not( target_arch = "wasm32" ) ) ) ]
@@ -123,7 +154,9 @@ impl EventBuilder {
123
154
let now = Instant :: now ( ) ;
124
155
125
156
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
+ )
127
160
}
128
161
129
162
/// Build POW [`Event`] using the given time supplier
@@ -158,8 +191,6 @@ impl EventBuilder {
158
191
#[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
159
192
use core:: cmp;
160
193
161
- /// Build unsigned POW [`Event`]
162
- pub fn to_unsigned_pow_event ( self , pubkey : XOnlyPublicKey , difficulty : u8 ) -> UnsignedEvent {
163
194
let mut nonce: u128 = 0 ;
164
195
let mut tags: Vec < Tag > = self . tags . clone ( ) ;
165
196
@@ -196,7 +227,6 @@ impl EventBuilder {
196
227
#[ cfg( all( feature = "std" , not( feature = "alloc" ) ) ) ]
197
228
let sig = keys. sign_schnorr ( & message) ?;
198
229
199
-
200
230
return self . to_event_internal ( keys, created_at, id, sig) ;
201
231
}
202
232
@@ -247,6 +277,7 @@ impl EventBuilder {
247
277
///
248
278
/// # Example
249
279
/// ```rust,no_run
280
+ /// use nostr::url::Url;
250
281
/// use nostr::{EventBuilder, Metadata};
251
282
///
252
283
/// let metadata = Metadata::new()
0 commit comments