Skip to content

Commit

Permalink
Update wasm bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Nov 8, 2023
1 parent 46b0194 commit 87226be
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 71 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

5 changes: 2 additions & 3 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ ignored_tests = ["test-utils"]
test-utils = []

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = { version = "0.4.33" }
js-sys = { version = "0.3.60" }
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = { version = "0.4.38" }
gloo-net = { version = "0.2.4" }
instant = { version = "0.1", features = ["wasm-bindgen"] }
getrandom = { version = "0.2", features = ["js"] }
Expand Down
8 changes: 4 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.22.0-bitcoin-v0.29", default-features = false, features =
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"] }
bip39 = { version = "2.0.0" }
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -46,12 +45,13 @@ windowless_sleep = "0.1.1"
# 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]
mutiny-core = { path = "../mutiny-core", features = ["test-utils"] }
wasm-bindgen-test = "0.3.33"
web-sys = { version = "0.3.60", 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 @@ -305,11 +305,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 @@ -356,11 +353,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 @@ -378,13 +372,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 @@ -401,13 +392,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 @@ -619,11 +607,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 @@ -641,13 +626,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 @@ -665,13 +647,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 @@ -717,13 +696,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 @@ -1060,12 +1036,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 @@ -1086,12 +1059,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 @@ -1156,7 +1126,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 @@ -1167,7 +1137,7 @@ impl MutinyWallet {

tags.sort();

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

/// Gets the current bitcoin price in chosen Fiat.
Expand Down Expand Up @@ -1202,13 +1172,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 @@ -1333,9 +1303,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 @@ -1344,7 +1312,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 87226be

Please sign in to comment.