Skip to content

Commit

Permalink
nostr: adapt NIP46 to last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Mar 11, 2024
1 parent 6b370e2 commit c64e552
Show file tree
Hide file tree
Showing 9 changed files with 764 additions and 566 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions crates/nostr-sdk/examples/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ async fn main() -> Result<()> {
client.connect().await;
println!("Connected to relay {}", nwc_uri.relay_url);

let req = nip47::Request {
method: Method::PayInvoice,
params: RequestParams::PayInvoice(PayInvoiceRequestParams {
id: None,
invoice,
amount: None,
}),
};
let req = nip47::Request::pay_invoice(PayInvoiceRequestParams {
id: None,
invoice,
amount: None,
});
let req_event = req.to_event(&nwc_uri).unwrap();

let subscription = Filter::new()
Expand Down
19 changes: 5 additions & 14 deletions crates/nostr-sdk/examples/nostr-connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

// Compose signer
let secret_key = SecretKey::from_bech32(APP_SECRET_KEY)?;
let app_keys = Keys::new(secret_key);
let relay_url = Url::parse("wss://relay.rip")?;
let signer =
Nip46Signer::new(relay_url.clone(), app_keys, None, Duration::from_secs(60)).await?;

let metadata = NostrConnectMetadata::new("Nostr SDK").url(Url::parse("https://example.com")?);
let nostr_connect_uri: NostrConnectURI = signer.nostr_connect_uri(metadata);

println!("\n###############################################\n");
println!("Nostr Connect URI: {nostr_connect_uri}");
println!("\n###############################################\n");
let uri = NostrConnectURI::parse("bunker://.. or nostrconnect://..")?;
let app_keys = Keys::parse(APP_SECRET_KEY)?;
let signer = Nip46Signer::new(uri, Some(app_keys), Duration::from_secs(60), None).await?;

// Compose client
let client = Client::new(signer);
Expand All @@ -33,14 +24,14 @@ async fn main() -> Result<()> {

// Publish events
let id = client
.publish_text_note("Testing nostr-sdk nostr-connect client", [])
.publish_text_note("Testing rust-nostr NIP46 signer [bunker]", [])
.await?;
println!("Published text note: {id}\n");

let receiver =
PublicKey::from_bech32("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet")?;
client
.send_direct_msg(receiver, "Hello from nostr-sdk", None)
.send_direct_msg(receiver, "Hello from rust-nostr", None)
.await?;
println!("Sent DM: {id}");

Expand Down
3 changes: 2 additions & 1 deletion crates/nostr-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ default = ["nip04", "nip07", "nip44", "nip46"]
nip04 = ["nostr/nip04"]
nip07 = ["nostr/nip07"]
nip44 = ["nostr/nip44"]
nip46 = ["nostr/nip46", "dep:nostr-relay-pool"]
nip46 = ["nostr/nip46", "dep:nostr-relay-pool", "dep:tracing"]

[dependencies]
async-utility.workspace = true
nostr = { workspace = true, features = ["std"] }
nostr-relay-pool = { workspace = true, optional = true }
thiserror.workspace = true
tokio = { workspace = true, features = ["sync"] }
tracing = { workspace = true, features = ["std", "attributes"], optional = true }
64 changes: 5 additions & 59 deletions crates/nostr-signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,6 @@ pub enum Error {
#[cfg(feature = "nip46")]
#[error(transparent)]
NIP46(#[from] nip46::Error),
/// Response not match to the request
#[cfg(feature = "nip46")]
#[error("response not match to the request")]
ResponseNotMatchRequest,
/// Not supported yet
#[error("{0}")]
Unsupported(String),
}

#[cfg(feature = "nip44")]
impl Error {
fn unsupported<S>(message: S) -> Self
where
S: Into<String>,
{
Self::Unsupported(message.into())
}
}

/// Nostr Signer Type
Expand Down Expand Up @@ -154,16 +137,7 @@ impl NostrSigner {
#[cfg(all(feature = "nip07", target_arch = "wasm32"))]
Self::NIP07(nip07) => Ok(nip07.sign_event(unsigned).await?),
#[cfg(feature = "nip46")]
Self::NIP46(nip46) => {
let res = nip46
.send_req_to_signer(nostr::nips::nip46::Request::SignEvent(unsigned), None)
.await?;
if let nostr::nips::nip46::Response::SignEvent(event) = res {
Ok(event)
} else {
Err(Error::ResponseNotMatchRequest)
}
}
Self::NIP46(nip46) => Ok(nip46.sign_event(unsigned).await?),
}
}

Expand All @@ -179,19 +153,7 @@ impl NostrSigner {
#[cfg(all(feature = "nip07", target_arch = "wasm32"))]
Self::NIP07(signer) => Ok(signer.nip04_encrypt(public_key, content).await?),
#[cfg(feature = "nip46")]
Self::NIP46(signer) => {
let req = nostr::nips::nip46::Request::Nip04Encrypt {
public_key,
text: String::from_utf8_lossy(content).to_string(),
};
let res: nostr::nips::nip46::Response =
signer.send_req_to_signer(req, None).await?;
if let nostr::nips::nip46::Response::Nip04Encrypt(ciphertext) = res {
Ok(ciphertext)
} else {
Err(Error::ResponseNotMatchRequest)
}
}
Self::NIP46(signer) => Ok(signer.nip04_encrypt(public_key, content).await?),
}
}

Expand All @@ -215,19 +177,7 @@ impl NostrSigner {
#[cfg(all(feature = "nip07", target_arch = "wasm32"))]
Self::NIP07(signer) => Ok(signer.nip04_decrypt(public_key, encrypted_content).await?),
#[cfg(feature = "nip46")]
Self::NIP46(signer) => {
let req = nostr::nips::nip46::Request::Nip04Decrypt {
public_key,
text: encrypted_content.to_string(),
};
let res: nostr::nips::nip46::Response =
signer.send_req_to_signer(req, None).await?;
if let nostr::nips::nip46::Response::Nip04Decrypt(content) = res {
Ok(content)
} else {
Err(Error::ResponseNotMatchRequest)
}
}
Self::NIP46(signer) => Ok(signer.nip04_decrypt(public_key, encrypted_content).await?),
}
}

Expand All @@ -248,9 +198,7 @@ impl NostrSigner {
#[cfg(all(feature = "nip07", target_arch = "wasm32"))]
Self::NIP07(signer) => Ok(signer.nip44_encrypt(public_key, content).await?),
#[cfg(feature = "nip46")]
Self::NIP46(..) => Err(Error::unsupported(
"NIP44 encryption not supported with NIP46 signer yet!",
)),
Self::NIP46(signer) => Ok(signer.nip44_encrypt(public_key, content).await?),
}
}

Expand All @@ -266,9 +214,7 @@ impl NostrSigner {
#[cfg(all(feature = "nip07", target_arch = "wasm32"))]
Self::NIP07(signer) => Ok(signer.nip44_decrypt(public_key, payload).await?),
#[cfg(feature = "nip46")]
Self::NIP46(..) => Err(Error::unsupported(
"NIP44 decryption not supported with NIP46 signer yet!",
)),
Self::NIP46(signer) => Ok(signer.nip44_decrypt(public_key, payload).await?),
}
}
}
Expand Down
Loading

0 comments on commit c64e552

Please sign in to comment.