Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically convert npub into hex #315

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/nostr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ::bitcoin::hashes::hex::ToHex;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -53,9 +54,33 @@ fn test_validate_32bytes_pubkey() -> Result<()> {
Ok(())
}

fn parse_npub(pubkey: &str) -> Result<String> {
use nostr_sdk::prelude::*;

if pubkey.starts_with("npub") {
let key = XOnlyPublicKey::from_bech32(pubkey)?;
Ok(key.to_hex())
} else {
Ok(pubkey.to_owned())
}
}

#[test]
fn test_parse_npub() -> Result<()> {
let result = parse_npub("npub1qqqqqqqx2tj99mng5qgc07cgezv5jm95dj636x4qsq7svwkwmwnse3rfkq")?;

assert_eq!(
result,
"000000000652e452ee68a01187fb08c899496cb46cb51d1aa0803d063acedba7"
);

Ok(())
}

/// Add a new nostr pubkey to a user
pub async fn new_nostr_pubkey(pubkey: &str, token: &str) -> Result<Response> {
let pubkey = validate_pubkey(pubkey)?;
let pubkey = parse_npub(pubkey)?;
let pubkey = validate_pubkey(&pubkey)?;

let endpoint = LNDHUB_ENDPOINT.read().await;
let pubkey = Nostr {
Expand All @@ -71,7 +96,8 @@ pub async fn new_nostr_pubkey(pubkey: &str, token: &str) -> Result<Response> {

/// Update the user nostr pubkey
pub async fn update_nostr_pubkey(pubkey: &str, token: &str) -> Result<Response> {
let pubkey = validate_pubkey(pubkey)?;
let pubkey = parse_npub(pubkey)?;
let pubkey = validate_pubkey(&pubkey)?;

let endpoint = LNDHUB_ENDPOINT.read().await;
let pubkey = Nostr {
Expand Down
Loading