Skip to content

Commit cb89e93

Browse files
committed
browser-signer-proxy: Options for BrowserSignerProxy
Signed-off-by: Awiteb <[email protected]>
1 parent dd8328d commit cb89e93

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

signer/nostr-browser-signer-proxy/examples/browser-signer-proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::{signal, time};
99

1010
#[tokio::main]
1111
async fn main() -> Result<()> {
12-
let proxy = BrowserSignerProxy::new(8080);
12+
let proxy = BrowserSignerProxy::new(BrowserSignerProxyOptions::default());
1313

1414
proxy.start().await?;
1515

signer/nostr-browser-signer-proxy/src/lib.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![warn(rustdoc::bare_urls)]
1313

1414
use std::collections::HashMap;
15-
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
15+
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
1616
use std::sync::Arc;
1717
use std::time::Duration;
1818

@@ -44,8 +44,6 @@ pub use self::error::Error;
4444
const HTML: &str = include_str!("../index.html");
4545
const JS: &str = include_str!("../proxy.js");
4646
const CSS: &str = include_str!("../style.css");
47-
const IP_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
48-
const TIMEOUT: Duration = Duration::from_secs(30);
4947

5048
type PendingResponseMap = HashMap<Uuid, Sender<Result<Value, String>>>;
5149

@@ -158,30 +156,72 @@ struct ProxyState {
158156
pub pending_responses: Mutex<PendingResponseMap>,
159157
}
160158

159+
/// Configuration options for [`BrowserSignerProxy`].
160+
#[derive(Debug, Clone, Serialize, Deserialize)]
161+
pub struct BrowserSignerProxyOptions {
162+
/// Request timeout for the signer extension. Default is 30 seconds.
163+
pub timeout: Duration,
164+
/// Proxy server IP address and port. Default is `127.0.0.1:7400`.
165+
pub addr: SocketAddr,
166+
}
167+
161168
/// Nostr Browser Signer Proxy
162169
///
163170
/// Proxy to use Nostr Browser signer (NIP-07) in native applications.
164171
#[derive(Debug, Clone)]
165172
pub struct BrowserSignerProxy {
166-
port: u16,
173+
/// Configuration options for the proxy
174+
options: BrowserSignerProxyOptions,
175+
/// Internal state of the proxy including request queues
167176
state: Arc<ProxyState>,
177+
/// Handle to the running proxy server (initialized on demand)
168178
handle: OnceCell<Arc<JoinHandle<()>>>,
179+
/// Notification trigger for graceful shutdown
169180
shutdown: Arc<Notify>,
170181
}
171182

183+
impl Default for BrowserSignerProxyOptions {
184+
fn default() -> Self {
185+
Self {
186+
timeout: Duration::from_secs(30),
187+
// 7 for NIP-07 and 400 because the NIP title is 40 bytes :)
188+
addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 7400)),
189+
}
190+
}
191+
}
192+
193+
impl BrowserSignerProxyOptions {
194+
/// Sets the timeout duration.
195+
pub const fn timeout(mut self, timeout: Duration) -> Self {
196+
self.timeout = timeout;
197+
self
198+
}
199+
200+
/// Sets the IP address.
201+
pub const fn ip_addr(mut self, new_ip: IpAddr) -> Self {
202+
self.addr = SocketAddr::new(new_ip, self.addr.port());
203+
self
204+
}
205+
206+
/// Sets the port number.
207+
pub const fn port(mut self, new_port: u16) -> Self {
208+
self.addr = SocketAddr::new(self.addr.ip(), new_port);
209+
self
210+
}
211+
}
212+
172213
// TODO: use atomic-destructor to automatically shutdown this when all instances are dropped
173214

174215
impl BrowserSignerProxy {
175-
// TODO: use a builder instead, to allow to config IP, port, timeout and so on.
176216
/// Construct a new browser signer proxy
177-
pub fn new(port: u16) -> Self {
217+
pub fn new(options: BrowserSignerProxyOptions) -> Self {
178218
let state = ProxyState {
179219
outgoing_requests: Mutex::new(Vec::new()),
180220
pending_responses: Mutex::new(HashMap::new()),
181221
};
182222

183223
Self {
184-
port,
224+
options,
185225
state: Arc::new(state),
186226
handle: OnceCell::new(),
187227
shutdown: Arc::new(Notify::new()),
@@ -191,7 +231,7 @@ impl BrowserSignerProxy {
191231
/// Get the signer proxy webpage URL
192232
#[inline]
193233
pub fn url(&self) -> String {
194-
format!("http://{IP_ADDR}:{}", self.port)
234+
format!("http://{}", self.options.addr)
195235
}
196236

197237
/// Start the proxy
@@ -201,8 +241,7 @@ impl BrowserSignerProxy {
201241
let _handle: &Arc<JoinHandle<()>> = self
202242
.handle
203243
.get_or_try_init(|| async {
204-
let addr: SocketAddr = SocketAddr::new(IP_ADDR, self.port);
205-
let listener = TcpListener::bind(addr).await?;
244+
let listener = TcpListener::bind(self.options.addr).await?;
206245

207246
let state = self.state.clone();
208247
let shutdown = self.shutdown.clone();
@@ -281,7 +320,7 @@ impl BrowserSignerProxy {
281320
self.store_outgoing_request(request).await;
282321

283322
// Wait for response
284-
match time::timeout(TIMEOUT, rx)
323+
match time::timeout(self.options.timeout, rx)
285324
.await
286325
.map_err(|_| Error::Timeout)??
287326
{

0 commit comments

Comments
 (0)