Skip to content

Commit

Permalink
Merge pull request #821 from MutinyWallet/update-wasm-bindgen
Browse files Browse the repository at this point in the history
Update wasm bindgen
  • Loading branch information
TonyGiorgio authored Nov 14, 2023
2 parents 8801819 + 99d38c5 commit da63e5b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 71 deletions.
8 changes: 4 additions & 4 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ async-interface = []
ignored_tests = []

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = { version = "0.4.33" }
web-sys = { version = "0.3.60", features = ["console"] }
js-sys = { version = "0.3.60" }
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = { version = "0.4.38" }
web-sys = { version = "0.3.65", features = ["console"] }
js-sys = "0.3.65"
gloo-net = { version = "0.2.4" }
instant = { version = "0.1", features = ["wasm-bindgen"] }
getrandom = { version = "0.2", features = ["js"] }
Expand Down
9 changes: 5 additions & 4 deletions mutiny-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ crate-type = ["cdylib"]
mutiny-core = { path = "../mutiny-core" }

anyhow = "1.0"
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.33"
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38"
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
bitcoin = { version = "0.29.2", default-features = false, features = ["serde", "secp-recovery", "rand"] }
Expand All @@ -33,7 +33,6 @@ nostr = { version = "0.24.0", default-features = false }
wasm-logger = "0.2.0"
log = "0.4.17"
rexie = "0.4"
js-sys = "0.3.60"
gloo-utils = { version = "0.1.6", features = ["serde"] }
web-sys = { version = "0.3.60", features = ["console"] }
bip39 = { version = "2.0.0" }
Expand All @@ -46,10 +45,12 @@ once_cell = "1.18.0"
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.33"
web-sys = { version = "0.3.65", features = ["console"] }
js-sys = "0.3.65"

[features]
default = []
Expand Down
64 changes: 16 additions & 48 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,8 @@ impl MutinyWallet {
#[wasm_bindgen]
pub fn get_new_address(
&self,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyBip21RawMaterials, MutinyJsError> {
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
let address = self.inner.node_manager.get_new_address(labels.clone())?;
Ok(MutinyBip21RawMaterials {
address: address.to_string(),
Expand Down Expand Up @@ -355,11 +352,8 @@ impl MutinyWallet {
pub async fn create_bip21(
&self,
amount: Option<u64>,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyBip21RawMaterials, MutinyJsError> {
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand All @@ -377,13 +371,10 @@ impl MutinyWallet {
&self,
destination_address: String,
amount: u64,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
fee_rate: Option<f32>,
) -> Result<String, MutinyJsError> {
let send_to = Address::from_str(&destination_address)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand All @@ -400,13 +391,10 @@ impl MutinyWallet {
pub async fn sweep_wallet(
&self,
destination_address: String,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
fee_rate: Option<f32>,
) -> Result<String, MutinyJsError> {
let send_to = Address::from_str(&destination_address)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand Down Expand Up @@ -618,11 +606,8 @@ impl MutinyWallet {
pub async fn create_invoice(
&self,
amount: Option<u64>,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyJsError> {
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand All @@ -640,13 +625,10 @@ impl MutinyWallet {
from_node: String,
invoice_str: String,
amt_sats: Option<u64>,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyJsError> {
let from_node = PublicKey::from_str(&from_node)?;
let invoice = Bolt11Invoice::from_str(&invoice_str)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand All @@ -664,13 +646,10 @@ impl MutinyWallet {
to_node: String,
amt_sats: u64,
message: Option<String>,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyJsError> {
let from_node = PublicKey::from_str(&from_node)?;
let to_node = PublicKey::from_str(&to_node)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand Down Expand Up @@ -716,13 +695,10 @@ impl MutinyWallet {
lnurl: String,
amount_sats: u64,
zap_npub: Option<String>,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyJsError> {
let from_node = PublicKey::from_str(&from_node)?;
let lnurl = LnUrl::from_str(&lnurl)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;

let zap_npub = match zap_npub.filter(|z| !z.is_empty()) {
Some(z) => {
Expand Down Expand Up @@ -1032,12 +1008,9 @@ impl MutinyWallet {
pub fn set_address_labels(
&self,
address: String,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<(), MutinyJsError> {
let address = Address::from_str(&address)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand All @@ -1058,12 +1031,9 @@ impl MutinyWallet {
pub fn set_invoice_labels(
&self,
invoice: String,
labels: &JsValue, /* Vec<String> */
labels: Vec<String>,
) -> Result<(), MutinyJsError> {
let invoice = Bolt11Invoice::from_str(&invoice)?;
let labels: Vec<String> = labels
.into_serde()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
Ok(self
.inner
.node_manager
Expand Down Expand Up @@ -1128,7 +1098,7 @@ impl MutinyWallet {
Ok(self.inner.node_manager.edit_contact(id, contact.into())?)
}

pub fn get_tag_items(&self) -> Result<JsValue /* Vec<TagItem> */, MutinyJsError> {
pub fn get_tag_items(&self) -> Result<Vec<TagItem>, MutinyJsError> {
let mut tags: Vec<TagItem> = self
.inner
.node_manager
Expand All @@ -1139,7 +1109,7 @@ impl MutinyWallet {

tags.sort();

Ok(JsValue::from_serde(&tags)?)
Ok(tags)
}

/// Gets the current bitcoin price in chosen Fiat.
Expand Down Expand Up @@ -1174,13 +1144,13 @@ impl MutinyWallet {

/// Get nostr wallet connect profiles
#[wasm_bindgen]
pub fn get_nwc_profiles(&self) -> Result<JsValue /* Vec<NwcProfile> */, MutinyJsError> {
pub fn get_nwc_profiles(&self) -> Result<Vec<NwcProfile>, MutinyJsError> {
let profiles = self.inner.nostr.profiles();
let p = profiles
.into_iter()
.map(models::NwcProfile::from)
.collect::<Vec<_>>();
Ok(JsValue::from_serde(&p)?)
Ok(p)
}

/// Create a nostr wallet connect profile
Expand Down Expand Up @@ -1305,9 +1275,7 @@ impl MutinyWallet {
}

/// Lists all pending NWC invoices
pub fn get_pending_nwc_invoices(
&self,
) -> Result<JsValue /* Vec<PendingNwcInvoice> */, MutinyJsError> {
pub fn get_pending_nwc_invoices(&self) -> Result<Vec<PendingNwcInvoice>, MutinyJsError> {
let pending: Vec<PendingNwcInvoice> = self
.inner
.nostr
Expand All @@ -1316,7 +1284,7 @@ impl MutinyWallet {
.map(|i| i.into())
.collect();

Ok(JsValue::from_serde(&pending)?)
Ok(pending)
}

/// Approves an invoice and sends the payment
Expand Down
49 changes: 34 additions & 15 deletions mutiny-wasm/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl ActivityItem {
}

#[wasm_bindgen(getter)]
pub fn labels(&self) -> JsValue /* Vec<String> */ {
JsValue::from_serde(&self.labels).unwrap()
pub fn labels(&self) -> Vec<String> {
self.labels.clone()
}

#[wasm_bindgen(getter)]
pub fn contacts(&self) -> JsValue /* Vec<Contact> */ {
JsValue::from_serde(&self.contacts).unwrap()
pub fn contacts(&self) -> Vec<Contact> {
self.contacts.clone()
}
}

Expand Down Expand Up @@ -170,8 +170,8 @@ impl MutinyInvoice {
}

#[wasm_bindgen(getter)]
pub fn labels(&self) -> JsValue /* Vec<String> */ {
JsValue::from_serde(&self.labels).unwrap()
pub fn labels(&self) -> Vec<String> {
self.labels.clone()
}
}

Expand Down Expand Up @@ -486,8 +486,8 @@ impl MutinyBip21RawMaterials {
}

#[wasm_bindgen(getter)]
pub fn labels(&self) -> JsValue /* Vec<String> */ {
JsValue::from_serde(&self.labels).unwrap()
pub fn labels(&self) -> Vec<String> {
self.labels.clone()
}
}

Expand Down Expand Up @@ -523,8 +523,8 @@ impl AuthProfile {
}

#[wasm_bindgen(getter)]
pub fn used_services(&self) -> JsValue /* Vec<String> */ {
JsValue::from_serde(&serde_json::to_value(&self.used_services).unwrap()).unwrap()
pub fn used_services(&self) -> Vec<String> {
self.used_services.clone()
}
}

Expand Down Expand Up @@ -998,8 +998,8 @@ impl NwcProfile {
}

#[wasm_bindgen(getter)]
pub fn active_payments(&self) -> JsValue /* Vec<String> */ {
JsValue::from_serde(&serde_json::to_value(self._active_payments()).unwrap()).unwrap()
pub fn active_payments(&self) -> Vec<String> {
self._active_payments().clone()
}

#[wasm_bindgen(getter)]
Expand Down Expand Up @@ -1045,21 +1045,40 @@ impl From<nostr::nwc::NwcProfile> for NwcProfile {

/// An invoice received over Nostr Wallet Connect that is pending approval or rejection
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[wasm_bindgen]
pub struct PendingNwcInvoice {
/// Index of the profile that received the invoice
pub index: u32,
/// The invoice that awaiting approval
pub invoice: String,
invoice: String,
/// The id of the invoice, this is the payment hash
pub id: String,
id: String,
/// The amount of sats that the invoice is for
pub amount_sats: u64,
/// The description of the invoice
pub invoice_description: Option<String>,
invoice_description: Option<String>,
/// Invoice expire time in seconds since epoch
pub expiry: u64,
}

#[wasm_bindgen]
impl PendingNwcInvoice {
#[wasm_bindgen(getter)]
pub fn invoice(&self) -> String {
self.invoice.clone()
}

#[wasm_bindgen(getter)]
pub fn id(&self) -> String {
self.id.clone()
}

#[wasm_bindgen(getter)]
pub fn invoice_description(&self) -> Option<String> {
self.invoice_description.clone()
}
}

impl From<nostr::nwc::PendingNwcInvoice> for PendingNwcInvoice {
fn from(value: nostr::nwc::PendingNwcInvoice) -> Self {
let invoice_description = match value.invoice.description() {
Expand Down

0 comments on commit da63e5b

Please sign in to comment.