use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{LazyLock, RwLock};
use std::time::Duration;
use nostr_sdk::prelude::*;
use nostr_connect::prelude::{AuthUrlHandler, NostrConnect, NostrConnectUri};
pub type BoxedFuture<'a, T> =
std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignerError(String);
impl SignerError {
#[inline]
pub fn backend<E>(e: E) -> Self
where
E: core::fmt::Display,
{
Self(e.to_string())
}
}
impl core::fmt::Display for SignerError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl core::error::Error for SignerError {}
impl From<&str> for SignerError {
#[inline]
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<String> for SignerError {
#[inline]
fn from(s: String) -> Self {
Self(s)
}
}
pub trait VectorSigner:
AsyncGetPublicKey<Error = SignerError>
+ AsyncSignEvent<Error = SignerError>
+ AsyncNip04<Error = SignerError>
+ AsyncNip44<Error = SignerError>
{
}
impl<T> VectorSigner for T
where
T: ?Sized
+ AsyncGetPublicKey<Error = SignerError>
+ AsyncSignEvent<Error = SignerError>
+ AsyncNip04<Error = SignerError>
+ AsyncNip44<Error = SignerError>,
{
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)] pub enum ActiveSigner {
Local(crate::crypto::GuardedSigner),
Bunker(WatchedBunkerSigner),
Nip55(crate::nip55::Nip55Signer),
Nip07(crate::nip07::Nip07Signer),
Keys(Keys),
}
macro_rules! dispatch {
($self:ident, $method:ident $(, $arg:expr)*) => {
match $self {
ActiveSigner::Local(s) => s.$method($($arg),*).await.map_err(SignerError::backend),
ActiveSigner::Bunker(s) => s.$method($($arg),*).await.map_err(SignerError::backend),
ActiveSigner::Nip55(s) => s.$method($($arg),*).await.map_err(SignerError::backend),
ActiveSigner::Nip07(s) => s.$method($($arg),*).await.map_err(SignerError::backend),
ActiveSigner::Keys(s) => s.$method($($arg),*).await.map_err(SignerError::backend),
}
};
}
impl AsyncGetPublicKey for ActiveSigner {
type Error = SignerError;
fn get_public_key_async(&self) -> BoxedFuture<'_, Result<PublicKey, Self::Error>> {
Box::pin(async move { dispatch!(self, get_public_key_async) })
}
}
impl AsyncSignEvent for ActiveSigner {
type Error = SignerError;
fn sign_event_async(&self, unsigned: UnsignedEvent) -> BoxedFuture<'_, Result<Event, Self::Error>> {
let unsigned = crate::clock::restamp(unsigned);
Box::pin(async move { dispatch!(self, sign_event_async, unsigned) })
}
}
impl AsyncNip04 for ActiveSigner {
type Error = SignerError;
fn nip04_encrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { dispatch!(self, nip04_encrypt_async, public_key, content) })
}
fn nip04_decrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
encrypted_content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { dispatch!(self, nip04_decrypt_async, public_key, encrypted_content) })
}
}
impl AsyncNip44 for ActiveSigner {
type Error = SignerError;
fn nip44_encrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { dispatch!(self, nip44_encrypt_async, public_key, content) })
}
fn nip44_decrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
payload: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { dispatch!(self, nip44_decrypt_async, public_key, payload) })
}
}
#[cfg(test)]
static TEST_SIGNER: LazyLock<RwLock<Option<ActiveSigner>>> =
LazyLock::new(|| RwLock::new(None));
#[cfg(test)]
pub(crate) fn set_test_signer(signer: Option<ActiveSigner>) {
if let Ok(mut g) = TEST_SIGNER.write() {
*g = signer;
}
}
pub async fn unwrap_gift_wrap(gift_wrap: &Event) -> Result<UnwrappedGift, String> {
#[cfg(test)]
let overridden = TEST_SIGNER.read().is_ok_and(|g| g.is_some());
#[cfg(not(test))]
let overridden = false;
if !overridden && signer_kind() == SignerKind::Local {
return crate::crypto::GuardedSigner::unwrap_gift_wrap(gift_wrap).map_err(|e| e.to_string());
}
let signer = active_signer()?;
UnwrappedGift::from_gift_wrap_async(&signer, gift_wrap).await.map_err(|e| e.to_string())
}
pub async fn seal_rumor(rumor: UnsignedEvent, receiver: &PublicKey) -> Result<Event, String> {
#[cfg(test)]
let overridden = TEST_SIGNER.read().is_ok_and(|g| g.is_some());
#[cfg(not(test))]
let overridden = false;
if !overridden && signer_kind() == SignerKind::Local {
if let Some(identity) = crate::state::my_public_key() {
return crate::crypto::GuardedSigner::seal(&identity, rumor, receiver).map_err(|e| e.to_string());
}
}
let signer = active_signer()?;
GiftWrapSealBuilder::new(rumor, *receiver)
.finalize_async(&signer)
.await
.map_err(|e| e.to_string())
}
pub async fn gift_wrap(
receiver: &PublicKey,
rumor: UnsignedEvent,
extra_tags: impl IntoIterator<Item = Tag>,
) -> Result<Event, String> {
#[cfg(test)]
let overridden = TEST_SIGNER.read().is_ok_and(|g| g.is_some());
#[cfg(not(test))]
let overridden = false;
crate::event_size::check_rumor(&rumor)?;
let extra_tags: Vec<Tag> = extra_tags.into_iter().collect();
let local = if !overridden && signer_kind() == SignerKind::Local { crate::state::my_public_key() } else { None };
let wrap = match local {
Some(identity) => crate::crypto::GuardedSigner::gift_wrap(&identity, receiver, rumor, extra_tags).map_err(|e| e.to_string())?,
None => GiftWrapBuilder::new(*receiver, rumor)
.extra_tags(extra_tags)
.finalize_async(&active_signer()?)
.await
.map_err(|e| e.to_string())?,
};
crate::event_size::check(&wrap)?;
Ok(wrap)
}
pub async fn unwrap_gift_wraps(gift_wraps: &[&Event]) -> Vec<Result<UnwrappedGift, String>> {
#[cfg(test)]
let overridden = TEST_SIGNER.read().is_ok_and(|g| g.is_some());
#[cfg(not(test))]
let overridden = false;
if !overridden && signer_kind() == SignerKind::Local {
return crate::crypto::GuardedSigner::unwrap_batch(gift_wraps)
.into_iter()
.map(|r| r.map_err(|e| e.to_string()))
.collect();
}
let mut out = Vec::with_capacity(gift_wraps.len());
for wrap in gift_wraps {
out.push(unwrap_gift_wrap(wrap).await);
}
out
}
static LOCAL_KEY_OWNER: std::sync::Mutex<Option<(usize, PublicKey)>> = std::sync::Mutex::new(None);
fn local_key_owner() -> Option<PublicKey> {
let count = crate::crypto::guarded_key::write_count();
let settled = count & 1 == 0;
if settled {
if let Some((at, pk)) = *LOCAL_KEY_OWNER.lock().unwrap_or_else(|e| e.into_inner()) {
if at == count {
return Some(pk);
}
}
}
let Some(pk) = crate::state::MY_SECRET_KEY.with_keys(|keys| keys.public_key()) else {
*LOCAL_KEY_OWNER.lock().unwrap_or_else(|e| e.into_inner()) = None;
return None;
};
if settled && crate::crypto::guarded_key::write_count() == count {
*LOCAL_KEY_OWNER.lock().unwrap_or_else(|e| e.into_inner()) = Some((count, pk));
}
Some(pk)
}
pub fn active_signer() -> Result<ActiveSigner, String> {
#[cfg(test)]
if let Some(s) = TEST_SIGNER.read().ok().and_then(|g| g.clone()) {
return Ok(s);
}
match signer_kind() {
SignerKind::Bunker => {
let inner = match bunker_signer() {
Some(b) => b,
None if bunker_suspended() => return Err("Your signer isn't connected yet.".into()),
None => return Err("bunker account has no live signer (not yet connected)".into()),
};
Ok(ActiveSigner::Bunker(WatchedBunkerSigner::new(inner)))
}
SignerKind::Nip55 => {
let pk = crate::state::my_public_key().ok_or("no active identity")?;
Ok(ActiveSigner::Nip55(crate::nip55::Nip55Signer::new(pk)))
}
SignerKind::Nip07 => {
let pk = crate::state::my_public_key().ok_or("no active identity")?;
Ok(ActiveSigner::Nip07(crate::nip07::Nip07Signer::new(pk)))
}
SignerKind::Local => {
if !crate::state::MY_SECRET_KEY.has_key() {
return Err("no signer available (no local key)".to_string());
}
if let Some(pk) = crate::state::my_public_key() {
if local_key_owner() != Some(pk) {
return Err("local key does not match the active identity (remote-signer account with no live signer)".to_string());
}
return Ok(ActiveSigner::Local(crate::crypto::GuardedSigner::new(pk)));
}
let pk = local_key_owner().ok_or("no signer available (no local key)")?;
Ok(ActiveSigner::Local(crate::crypto::GuardedSigner::new(pk)))
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum SignerKind {
Local = 0,
Bunker = 1,
Nip55 = 2,
Nip07 = 3,
}
impl SignerKind {
#[inline]
pub fn as_setting_str(self) -> &'static str {
match self {
SignerKind::Local => "local",
SignerKind::Bunker => "bunker",
SignerKind::Nip55 => "nip55",
SignerKind::Nip07 => "nip07",
}
}
#[inline]
pub fn from_setting_str(s: &str) -> Self {
match s {
"bunker" => SignerKind::Bunker,
"nip55" => SignerKind::Nip55,
"nip07" => SignerKind::Nip07,
_ => SignerKind::Local,
}
}
}
static SIGNER_KIND: AtomicU8 = AtomicU8::new(SignerKind::Local as u8);
#[inline]
pub fn signer_kind() -> SignerKind {
match SIGNER_KIND.load(Ordering::Acquire) {
1 => SignerKind::Bunker,
2 => SignerKind::Nip55,
3 => SignerKind::Nip07,
_ => SignerKind::Local,
}
}
#[inline]
pub fn set_signer_kind(kind: SignerKind) {
SIGNER_KIND.store(kind as u8, Ordering::Release);
}
#[inline]
pub fn is_bunker() -> bool {
signer_kind() == SignerKind::Bunker
}
#[inline]
pub fn is_keyless() -> bool {
signer_kind() != SignerKind::Local
}
pub static BUNKER_SIGNER: LazyLock<RwLock<Option<NostrConnect>>> =
LazyLock::new(|| RwLock::new(None));
#[inline]
pub fn bunker_signer() -> Option<NostrConnect> {
BUNKER_SIGNER.read().ok().and_then(|g| g.as_ref().cloned())
}
#[inline]
pub fn set_bunker_signer(signer: NostrConnect) {
if let Ok(mut g) = BUNKER_SIGNER.write() {
*g = Some(signer);
}
}
#[inline]
pub fn take_bunker_signer() -> Option<NostrConnect> {
BUNKER_SIGNER.write().ok().and_then(|mut g| g.take())
}
pub fn parse_bunker_relays(bunker_url: &str) -> Vec<String> {
match NostrConnectUri::parse(bunker_url) {
Ok(NostrConnectUri::Bunker { relays, .. }) => {
relays.into_iter().map(|r| r.to_string()).collect()
}
_ => Vec::new(),
}
}
pub fn parse_bunker_remote_pubkey(bunker_url: &str) -> Result<String, String> {
let uri = NostrConnectUri::parse(bunker_url)
.map_err(|e| format!("Invalid bunker URL: {}", e))?;
match uri {
NostrConnectUri::Bunker { remote_signer_public_key, .. } => {
Ok(remote_signer_public_key.to_hex().to_ascii_lowercase())
}
NostrConnectUri::Client { .. } => {
Err("Client-initiated URIs not supported here; use a bunker:// URL".into())
}
}
}
pub const VECTOR_APP_NAME: &str = "Vector";
pub const VECTOR_APP_URL: &str = "https://vectorapp.io";
pub const VECTOR_APP_ICON: &str = "https://raw.githubusercontent.com/VectorPrivacy/Vector/master/src-tauri/icons/icon.png";
pub const VECTOR_NIP46_PERMS: &[&str] = &[
"get_public_key",
"sign_event",
"nip04_encrypt",
"nip04_decrypt",
"nip44_encrypt",
"nip44_decrypt",
];
pub fn vector_metadata() -> NostrConnectMetadata {
let mut md = NostrConnectMetadata::new(VECTOR_APP_NAME);
if let Ok(url) = Url::parse(VECTOR_APP_URL) {
md = md.url(url);
}
if let Ok(icon) = Url::parse(VECTOR_APP_ICON) {
md = md.icons(vec![icon]);
}
md
}
pub fn build_nostrconnect_uri(
client_pubkey: PublicKey,
relays: Vec<RelayUrl>,
) -> NostrConnectUri {
NostrConnectUri::Client {
public_key: client_pubkey,
relays,
metadata: vector_metadata(),
secret: random_connect_secret(),
}
}
fn random_connect_secret() -> String {
use ::rand::RngCore;
let mut bytes = [0u8; 16];
::rand::rngs::OsRng.fill_bytes(&mut bytes);
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
pub fn build_nostrconnect_session(
client_keys: Keys,
relays: Vec<RelayUrl>,
timeout: Duration,
) -> Result<(NostrConnect, String), String> {
let uri = build_nostrconnect_uri(client_keys.public_key(), relays);
let mut uri_string = uri.to_string();
let perms = VECTOR_NIP46_PERMS.join(",");
if !perms.is_empty() {
uri_string.push_str("&perms=");
uri_string.push_str(&perms);
}
let mut nc = NostrConnect::new(uri, client_keys, timeout, Some(crate::transport_relay_options()))
.map_err(|e| format!("Bunker init failed: {}", e))?;
nc.auth_url_handler(VectorAuthUrlHandler);
Ok((nc, uri_string))
}
pub fn build_bunker_signer(
bunker_url: &str,
client_keys: Keys,
timeout: Duration,
) -> Result<NostrConnect, String> {
let uri = NostrConnectUri::parse(bunker_url)
.map_err(|e| format!("Invalid bunker URL: {}", e))?;
NostrConnect::new(uri, client_keys, timeout, Some(crate::transport_relay_options()))
.map_err(|e| format!("Bunker init failed: {}", e))
}
pub async fn prewarm_bunker(signer: &NostrConnect) -> Result<PublicKey, String> {
signer
.get_public_key_async()
.await
.map_err(|e| format!("Bunker prewarm failed: {}", e))
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum BunkerConnectionState {
Idle = 0,
Connecting = 1,
Online = 2,
Offline = 3,
}
impl BunkerConnectionState {
pub fn as_label(self) -> &'static str {
match self {
BunkerConnectionState::Idle => "idle",
BunkerConnectionState::Connecting => "connecting",
BunkerConnectionState::Online => "online",
BunkerConnectionState::Offline => "offline",
}
}
}
static BUNKER_STATE: AtomicU8 = AtomicU8::new(BunkerConnectionState::Idle as u8);
#[inline]
pub fn bunker_state() -> BunkerConnectionState {
match BUNKER_STATE.load(Ordering::Acquire) {
1 => BunkerConnectionState::Connecting,
2 => BunkerConnectionState::Online,
3 => BunkerConnectionState::Offline,
_ => BunkerConnectionState::Idle,
}
}
pub fn set_bunker_state(new_state: BunkerConnectionState) {
let prev = BUNKER_STATE.swap(new_state as u8, Ordering::AcqRel);
if prev == new_state as u8 {
return;
}
crate::traits::emit_event_json(
"bunker_state",
serde_json::json!({ "state": new_state.as_label() }),
);
}
#[derive(Debug, Clone)]
pub struct WatchedBunkerSigner {
inner: NostrConnect,
session: std::sync::Arc<crate::db::Session>,
}
impl WatchedBunkerSigner {
pub fn new(inner: NostrConnect) -> Self {
Self { inner, session: crate::db::current_session() }
}
#[inline]
fn flip(&self, state: BunkerConnectionState) {
if self.session.is_live() {
set_bunker_state(state);
}
}
#[cfg(test)]
pub(crate) fn session_id_for_test(&self) -> u64 {
self.session.id()
}
}
impl WatchedBunkerSigner {
#[inline]
fn watch<T, E>(&self, res: Result<T, E>) -> Result<T, SignerError>
where
E: core::fmt::Display,
{
match res {
Ok(v) => {
self.flip(BunkerConnectionState::Online);
Ok(v)
}
Err(e) => {
self.flip(BunkerConnectionState::Offline);
Err(SignerError::backend(e))
}
}
}
}
impl AsyncGetPublicKey for WatchedBunkerSigner {
type Error = SignerError;
fn get_public_key_async(&self) -> BoxedFuture<'_, Result<PublicKey, Self::Error>> {
Box::pin(async move { self.watch(self.inner.get_public_key_async().await) })
}
}
impl AsyncSignEvent for WatchedBunkerSigner {
type Error = SignerError;
fn sign_event_async(&self, unsigned: UnsignedEvent) -> BoxedFuture<'_, Result<Event, Self::Error>> {
Box::pin(async move { self.watch(self.inner.sign_event_async(unsigned).await) })
}
}
impl AsyncNip04 for WatchedBunkerSigner {
type Error = SignerError;
fn nip04_encrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.watch(self.inner.nip04_encrypt_async(public_key, content).await) })
}
fn nip04_decrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
encrypted_content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move {
self.watch(self.inner.nip04_decrypt_async(public_key, encrypted_content).await)
})
}
}
impl AsyncNip44 for WatchedBunkerSigner {
type Error = SignerError;
fn nip44_encrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.watch(self.inner.nip44_encrypt_async(public_key, content).await) })
}
fn nip44_decrypt_async<'a>(
&'a self,
public_key: &'a PublicKey,
payload: &'a str,
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.watch(self.inner.nip44_decrypt_async(public_key, payload).await) })
}
}
#[derive(Debug, Clone, Default)]
pub struct VectorAuthUrlHandler;
impl AuthUrlHandler for VectorAuthUrlHandler {
fn on_auth_url<'a>(&'a self, auth_url: Url) -> BoxedFuture<'a, std::result::Result<(), nostr_connect::error::Error>> {
Box::pin(async move {
crate::traits::emit_event_json(
"bunker_auth_url",
serde_json::json!({ "url": auth_url.to_string() }),
);
Ok(())
})
}
}
pub async fn attempt_bunker_login(
bunker_url: &str,
client_keys: Keys,
timeout: Duration,
) -> Result<PublicKey, String> {
set_bunker_state(BunkerConnectionState::Connecting);
let mut nc = match build_bunker_signer(bunker_url, client_keys, timeout) {
Ok(nc) => nc,
Err(e) => {
set_bunker_state(BunkerConnectionState::Offline);
return Err(e);
}
};
nc.auth_url_handler(VectorAuthUrlHandler);
let pairing = track_pairing(&nc);
match prewarm_bunker(&nc).await {
Ok(remote_pk) => match pairing.install(nc) {
Ok(old) => {
if let Some(old) = old {
crate::rt::spawn(async move { let _ = old.shutdown().await; });
}
set_bunker_state(BunkerConnectionState::Online);
Ok(remote_pk)
}
Err(PairingAborted) => {
set_bunker_state(BunkerConnectionState::Offline);
Err(NETWORK_CHANGED.into())
}
},
Err(e) => {
set_bunker_state(BunkerConnectionState::Offline);
Err(if pairing.aborted() { NETWORK_CHANGED.into() } else { e })
}
}
}
struct Suspended {
uri: NostrConnectUri,
keys: Keys,
user: Option<PublicKey>,
}
static SUSPENDED: std::sync::Mutex<Option<Suspended>> = std::sync::Mutex::new(None);
pub const NETWORK_CHANGED: &str = "The network changed. Try again.";
struct Pairing {
id: u64,
nc: NostrConnect,
aborted: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
static PAIRINGS: std::sync::Mutex<Vec<Pairing>> = std::sync::Mutex::new(Vec::new());
static PAIRING_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PairingAborted;
#[must_use = "the pairing is forgotten when the guard drops"]
pub struct PairingGuard {
id: u64,
aborted: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl PairingGuard {
pub fn aborted(&self) -> bool {
self.aborted.load(Ordering::Acquire)
}
pub fn install(self, nc: NostrConnect) -> Result<Option<NostrConnect>, PairingAborted> {
let mut pairings = PAIRINGS.lock().unwrap_or_else(|e| e.into_inner());
if self.aborted() {
return Err(PairingAborted);
}
pairings.retain(|p| p.id != self.id);
let old = take_bunker_signer();
set_bunker_signer(nc);
*SUSPENDED.lock().unwrap_or_else(|e| e.into_inner()) = None;
Ok(old)
}
}
impl Drop for PairingGuard {
fn drop(&mut self) {
PAIRINGS.lock().unwrap_or_else(|e| e.into_inner()).retain(|p| p.id != self.id);
}
}
pub fn track_pairing(nc: &NostrConnect) -> PairingGuard {
let id = PAIRING_SEQ.fetch_add(1, Ordering::Relaxed);
let aborted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
PAIRINGS.lock().unwrap_or_else(|e| e.into_inner()).push(Pairing { id, nc: nc.clone(), aborted: aborted.clone() });
PairingGuard { id, aborted }
}
fn abort_pairings() -> Vec<NostrConnect> {
PAIRINGS
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.map(|p| {
p.aborted.store(true, Ordering::Release);
p.nc
})
.collect()
}
async fn rebuild_uri(nc: &NostrConnect) -> Option<NostrConnectUri> {
use futures_util::FutureExt;
let stored = match crate::db::get_bunker_url().await {
Ok(Some(url)) => Some(url),
_ => crate::state::pending_bunker_setup().map(|(url, _)| url),
};
if let Some(NostrConnectUri::Bunker { remote_signer_public_key, relays, .. }) =
stored.and_then(|url| NostrConnectUri::parse(&url).ok())
{
return Some(NostrConnectUri::Bunker { remote_signer_public_key, relays, secret: None });
}
match nc.bunker_uri().now_or_never() {
Some(Ok(uri)) => Some(uri),
_ => None,
}
}
pub async fn suspend_bunker() {
use futures_util::FutureExt;
for nc in abort_pairings() {
nc.shutdown().await;
}
let Some(nc) = bunker_signer() else { return };
if nc.status().now_or_never().is_some_and(|relays| relays.is_empty()) {
return;
}
let Some(nc) = take_bunker_signer() else { return };
match rebuild_uri(&nc).await {
Some(uri) => {
let user = crate::state::my_public_key();
*SUSPENDED.lock().unwrap_or_else(|e| e.into_inner()) = Some(Suspended { uri, keys: nc.local_keys().clone(), user });
set_bunker_state(BunkerConnectionState::Connecting);
}
None => set_bunker_state(BunkerConnectionState::Offline),
}
nc.shutdown().await;
}
static RESUMING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
struct Resuming;
impl Drop for Resuming {
fn drop(&mut self) {
RESUMING.store(false, Ordering::Release);
}
}
pub async fn resume_bunker() {
if bunker_signer().is_some() || RESUMING.swap(true, Ordering::AcqRel) {
return;
}
let resuming = Resuming;
let Some((uri, keys, user)) = SUSPENDED
.lock()
.unwrap_or_else(|e| e.into_inner())
.as_ref()
.map(|s| (s.uri.clone(), s.keys.clone(), s.user))
else {
return;
};
let mut nc = match NostrConnect::new(uri, keys, Duration::from_secs(60), Some(crate::transport_relay_options())) {
Ok(nc) => nc,
Err(e) => {
crate::log_warn!("[Transport] could not rebuild the signer connection: {e}");
set_bunker_state(BunkerConnectionState::Offline);
return;
}
};
nc.auth_url_handler(VectorAuthUrlHandler);
if let Some(pk) = user {
let _ = nc.non_secure_set_user_public_key(pk);
}
let pairing = track_pairing(&nc);
crate::db::spawn_bound(async move {
let _resuming = resuming;
let bootstrapped = nc.bunker_uri().await.is_ok();
if pairing.aborted() || !crate::db::session_is_live() || !bunker_suspended() {
nc.shutdown().await;
return;
}
if pairing.install(nc).is_ok() {
set_bunker_state(if bootstrapped { BunkerConnectionState::Online } else { BunkerConnectionState::Offline });
}
});
}
pub fn bunker_suspended() -> bool {
SUSPENDED.lock().unwrap_or_else(|e| e.into_inner()).is_some()
}
pub fn pairing_pending() -> bool {
(bunker_signer().is_some() && bunker_state() == BunkerConnectionState::Connecting)
|| !PAIRINGS.lock().unwrap_or_else(|e| e.into_inner()).is_empty()
}
pub fn drain_bunker_state() -> Option<NostrConnect> {
set_signer_kind(SignerKind::Local);
set_bunker_state(BunkerConnectionState::Idle);
*SUSPENDED.lock().unwrap_or_else(|e| e.into_inner()) = None;
take_bunker_signer()
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn a_signer_that_is_not_the_vault_seals_and_wraps_itself() {
let _guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
let (vault_key, remote, peer) = (Keys::generate(), Keys::generate(), Keys::generate());
crate::state::MY_SECRET_KEY.store_from_keys(&vault_key, &[]);
set_test_signer(Some(ActiveSigner::Keys(remote.clone())));
let rumor = EventBuilder::new(Kind::PrivateDirectMessage, "x").finalize_unsigned(remote.public_key());
let seal = seal_rumor(rumor.clone(), &peer.public_key()).await;
let wrap = gift_wrap(&peer.public_key(), rumor, []).await;
set_test_signer(None);
crate::state::MY_SECRET_KEY.clear(&[]);
assert_eq!(seal.unwrap().pubkey, remote.public_key(), "sealed by the signer");
let opened = UnwrappedGift::from_gift_wrap(&peer, &wrap.unwrap()).unwrap();
assert_eq!(opened.sender, remote.public_key(), "wrapped by the signer");
}
#[test]
fn resolving_a_local_signer_refuses_a_vault_key_that_is_not_the_identity() {
let _guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
set_signer_kind(SignerKind::Local);
let (me, other) = (Keys::generate(), Keys::generate());
crate::state::set_my_public_key(me.public_key());
crate::state::MY_SECRET_KEY.store_from_keys(&other, &[]);
let refused = active_signer().is_err();
crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
let accepted = matches!(active_signer(), Ok(ActiveSigner::Local(_)));
crate::state::MY_SECRET_KEY.clear(&[]);
crate::state::clear_my_public_key();
assert!(refused, "a key that isn't the identity is refused before any work");
assert!(accepted, "the identity's own key resolves");
}
#[test]
fn the_vault_keys_owner_follows_every_set_and_clear() {
let _guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
let (me, other) = (Keys::generate(), Keys::generate());
crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
assert_eq!(local_key_owner(), Some(me.public_key()));
assert_eq!(local_key_owner(), Some(me.public_key()), "answered again from the cache");
crate::state::MY_SECRET_KEY.store_from_keys(&other, &[]);
assert_eq!(local_key_owner(), Some(other.public_key()), "a set is never answered from a stale cache");
crate::state::ENCRYPTION_KEY.set([7u8; 32], &[&crate::state::MY_SECRET_KEY]);
assert_eq!(local_key_owner(), Some(other.public_key()));
crate::state::ENCRYPTION_KEY.clear(&[&crate::state::MY_SECRET_KEY]);
crate::state::MY_SECRET_KEY.clear(&[]);
assert_eq!(local_key_owner(), None);
}
#[test]
fn setting_roundtrip() {
assert_eq!(SignerKind::from_setting_str("local"), SignerKind::Local);
assert_eq!(SignerKind::from_setting_str("bunker"), SignerKind::Bunker);
assert_eq!(SignerKind::from_setting_str("nip55"), SignerKind::Nip55);
assert_eq!(SignerKind::from_setting_str("nip07"), SignerKind::Nip07);
assert_eq!(SignerKind::Nip07.as_setting_str(), "nip07");
assert_eq!(SignerKind::Local.as_setting_str(), "local");
assert_eq!(SignerKind::Bunker.as_setting_str(), "bunker");
assert_eq!(SignerKind::Nip55.as_setting_str(), "nip55");
assert_eq!(SignerKind::from_setting_str(""), SignerKind::Local);
assert_eq!(SignerKind::from_setting_str("garbage"), SignerKind::Local);
}
#[test]
fn atomic_state_round_trips_and_drains() {
let _guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
set_signer_kind(SignerKind::Local);
set_bunker_state(BunkerConnectionState::Idle);
set_signer_kind(SignerKind::Bunker);
assert_eq!(signer_kind(), SignerKind::Bunker);
assert!(is_bunker());
assert!(is_keyless());
set_signer_kind(SignerKind::Local);
assert_eq!(signer_kind(), SignerKind::Local);
assert!(!is_bunker());
assert!(!is_keyless());
set_signer_kind(SignerKind::Nip55);
assert_eq!(signer_kind(), SignerKind::Nip55);
assert!(!is_bunker());
assert!(is_keyless());
set_signer_kind(SignerKind::Local);
set_signer_kind(SignerKind::Bunker);
set_bunker_state(BunkerConnectionState::Online);
let drained = drain_bunker_state();
assert!(drained.is_none());
assert_eq!(signer_kind(), SignerKind::Local);
assert_eq!(bunker_state(), BunkerConnectionState::Idle);
let drained_again = drain_bunker_state();
assert!(drained_again.is_none());
assert_eq!(signer_kind(), SignerKind::Local);
assert_eq!(bunker_state(), BunkerConnectionState::Idle);
}
#[test]
fn bunker_state_label_covers_all_variants() {
assert_eq!(BunkerConnectionState::Idle.as_label(), "idle");
assert_eq!(BunkerConnectionState::Connecting.as_label(), "connecting");
assert_eq!(BunkerConnectionState::Online.as_label(), "online");
assert_eq!(BunkerConnectionState::Offline.as_label(), "offline");
}
#[test]
fn parse_bunker_relays_returns_relays_from_bunker_uri() {
let signer_keys = Keys::generate();
let r1 = RelayUrl::parse("wss://relay1.example").unwrap();
let r2 = RelayUrl::parse("wss://relay2.example").unwrap();
let uri = NostrConnectUri::Bunker {
remote_signer_public_key: signer_keys.public_key(),
relays: vec![r1.clone(), r2.clone()],
secret: None,
};
let relays = parse_bunker_relays(&uri.to_string());
assert_eq!(relays.len(), 2);
assert!(relays.iter().any(|r| r.contains("relay1.example")));
assert!(relays.iter().any(|r| r.contains("relay2.example")));
}
#[test]
fn parse_bunker_relays_returns_empty_on_invalid_input() {
assert!(parse_bunker_relays("").is_empty());
assert!(parse_bunker_relays("not a url").is_empty());
assert!(parse_bunker_relays("http://example.com").is_empty());
let client_keys = Keys::generate();
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let client_uri = build_nostrconnect_uri(client_keys.public_key(), vec![relay]);
assert!(parse_bunker_relays(&client_uri.to_string()).is_empty(),
"client URI must not surface as a bunker relay list");
}
#[test]
fn parse_bunker_remote_pubkey_invalid_url() {
assert!(parse_bunker_remote_pubkey("not a url").is_err());
assert!(parse_bunker_remote_pubkey("").is_err());
assert!(parse_bunker_remote_pubkey("http://example.com").is_err());
}
#[test]
fn parse_bunker_remote_pubkey_rejects_client_uri() {
let client_keys = Keys::generate();
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let uri = build_nostrconnect_uri(client_keys.public_key(), vec![relay]);
let err = parse_bunker_remote_pubkey(&uri.to_string())
.expect_err("client URI must be rejected");
assert!(err.contains("Client-initiated"), "unexpected error: {}", err);
}
#[test]
fn parse_bunker_remote_pubkey_normalizes_lowercase() {
let signer_keys = Keys::generate();
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let uri = NostrConnectUri::Bunker {
remote_signer_public_key: signer_keys.public_key(),
relays: vec![relay],
secret: None,
};
let parsed = parse_bunker_remote_pubkey(&uri.to_string())
.expect("valid bunker URI");
assert_eq!(parsed, signer_keys.public_key().to_hex().to_ascii_lowercase());
assert_eq!(parsed, parsed.to_ascii_lowercase(),
"callers may compare with == — output must already be lowercase");
}
#[test]
fn vector_metadata_carries_app_name_and_icon() {
let md = vector_metadata();
let json = serde_json::to_string(&md).expect("metadata serializes");
assert!(json.contains(VECTOR_APP_NAME),
"metadata must include app name for the signer's approval prompt; got {}", json);
assert!(json.contains("vectorapp.io"),
"metadata must reference the app URL for the signer's 'More info' link");
}
#[test]
fn nip46_perms_list_excludes_get_private_key() {
for perm in VECTOR_NIP46_PERMS {
assert!(!perm.contains("get_private_key"),
"VECTOR_NIP46_PERMS must never include get_private_key (found: {})", perm);
assert!(!perm.contains("private_key"),
"perm string looks dangerous: {}", perm);
}
}
#[test]
fn build_nostrconnect_session_appends_perms_query_param() {
let client_keys = Keys::generate();
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let (_nc, uri) = build_nostrconnect_session(
client_keys,
vec![relay],
std::time::Duration::from_secs(1),
).expect("session builds");
assert!(uri.contains("perms="),
"URI must carry perms query param so signers can scope the pairing; got: {}", uri);
for perm in VECTOR_NIP46_PERMS {
assert!(uri.contains(perm),
"URI missing permission '{}': {}", perm, uri);
}
assert!(!uri.contains("get_private_key"),
"URI must never request get_private_key: {}", uri);
}
#[test]
fn build_nostrconnect_session_rejects_empty_uri() {
let client_keys = Keys::generate();
let session = build_nostrconnect_session(
client_keys,
vec![],
std::time::Duration::from_secs(1),
);
let _ = session;
}
#[test]
fn watched_signer_session_gate_and_state_transitions() {
let _db = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
let client_keys = Keys::generate();
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let signer_keys = Keys::generate();
let uri = NostrConnectUri::Bunker {
remote_signer_public_key: signer_keys.public_key(),
relays: vec![relay],
secret: None,
};
let nc = NostrConnect::new(
uri,
client_keys,
std::time::Duration::from_secs(1),
None,
).expect("NostrConnect builds");
let gen_before = crate::db::current_session_id();
let watched = WatchedBunkerSigner::new(nc);
assert_eq!(watched.session_id_for_test(), gen_before,
"WatchedBunkerSigner must capture the live session generation at construction");
set_bunker_state(BunkerConnectionState::Idle);
watched.flip(BunkerConnectionState::Online);
assert_eq!(bunker_state(), BunkerConnectionState::Online,
"flip with valid session must update bunker_state");
crate::db::close_database();
set_bunker_state(BunkerConnectionState::Online);
watched.flip(BunkerConnectionState::Offline);
assert_eq!(bunker_state(), BunkerConnectionState::Online,
"flip with stale session must be a no-op");
set_bunker_state(BunkerConnectionState::Idle);
}
}