Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Upload profile pic to nostr.build
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jan 29, 2024
1 parent 9110d3f commit a8b07f5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lightning-transaction-sync = { version = "0.0.118", features = ["esplora-async-h
lightning-liquidity = { git = "https://github.com/johncantrell97/ldk-lsp-client.git", rev = "9e01757d20c04aa31c28de8c4ffab5442d547edc" }
chrono = "0.4.22"
futures-util = { version = "0.3", default-features = false }
reqwest = { version = "0.11", default-features = false, features = ["json"] }
reqwest = { version = "0.11", default-features = false, features = ["multipart", "json"] }
async-trait = "0.1.68"
url = { version = "2.3.1", features = ["serde"] }
nostr = { version = "0.27.0", default-features = false, features = ["nip04", "nip05", "nip47", "nip57"] }
Expand Down
44 changes: 44 additions & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ use lightning::{log_error, log_info, log_warn};
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription};
use lnurl::{lnurl::LnUrl, AsyncClient as LnUrlClient, LnUrlResponse, Response};
use nostr_sdk::{Client, RelayPoolNotification};
use reqwest::multipart::{Form, Part};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::sync::Arc;
Expand Down Expand Up @@ -1384,6 +1385,42 @@ impl<S: MutinyStorage> MutinyWallet<S> {
Ok(())
}

/// Uploads a profile pic to nostr.build and returns the uploaded file's URL
pub async fn upload_profile_pic(&self, image_bytes: Vec<u8>) -> Result<String, MutinyError> {
let client = reqwest::Client::new();

let form = Form::new().part("fileToUpload", Part::bytes(image_bytes));
let res: NostrBuildResult = client
.post("https://nostr.build/api/v2/upload/profile")
.multipart(form)
.send()
.await
.map_err(|_| MutinyError::NostrError)?
.json()
.await
.map_err(|_| MutinyError::NostrError)?;

if res.status != "success" {
log_error!(
self.logger,
"Error uploading profile picture: {}",
res.message
);
return Err(MutinyError::NostrError);
}

// get url from response body
if let Some(value) = res.data.first() {
return value
.get("url")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or(MutinyError::NostrError);
}

Err(MutinyError::NostrError)
}

/// Makes a request to the primal api
async fn primal_request(
client: &reqwest::Client,
Expand Down Expand Up @@ -2106,6 +2143,13 @@ pub(crate) async fn create_new_federation<S: MutinyStorage>(
})
}

#[derive(Deserialize)]
struct NostrBuildResult {
status: String,
message: String,
data: Vec<Value>,
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
1 change: 1 addition & 0 deletions mutiny-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ urlencoding = "2.1.2"
once_cell = "1.18.0"
payjoin = { version = "0.13.0", features = ["send", "base64"] }
fedimint-core = "0.2.1"
base64 = "0.13.0"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
6 changes: 6 additions & 0 deletions mutiny-wasm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ impl From<MutinyStorageError> for MutinyJsError {
}
}

impl From<base64::DecodeError> for MutinyJsError {
fn from(_e: base64::DecodeError) -> Self {
Self::InvalidArgumentsError
}
}

impl From<bip39::Error> for MutinyJsError {
fn from(_e: bip39::Error) -> Self {
Self::InvalidMnemonic
Expand Down
6 changes: 6 additions & 0 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,12 @@ impl MutinyWallet {
Ok(event_id.to_hex())
}

/// Uploads a profile pic to nostr.build and returns the uploaded file's URL
pub async fn upload_profile_pic(&self, img_base64: String) -> Result<String, MutinyJsError> {
let bytes = base64::decode(&img_base64)?;
Ok(self.inner.upload_profile_pic(bytes).await?)
}

/// Resets the scorer and network graph. This can be useful if you get stuck in a bad state.
#[wasm_bindgen]
pub async fn reset_router(&self) -> Result<(), MutinyJsError> {
Expand Down

0 comments on commit a8b07f5

Please sign in to comment.