Skip to content

Wasm bindings #49

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

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[env]
AR = "/opt/homebrew/opt/llvm/bin/llvm-ar"
CC = "/opt/homebrew/opt/llvm/bin/clang"
Comment on lines +1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everyone building is going to use homebrew. You probably don't want to commit your own environment here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is something we've been noting in the readme on wasm libs since it's mac specific

44 changes: 43 additions & 1 deletion Cargo.lock

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

21 changes: 20 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ exclude = ["tests"]
[features]
_danger-local-https = ["payjoin/_danger-local-https"]
uniffi = ["uniffi/cli", "bitcoin-ffi/default"]
wasm = [
"wasm-bindgen",
"console_error_panic_hook",
"getrandom",
"ring",
"gloo-timers",
"web-sys",
"serde-wasm-bindgen",
]
default = ["io"]
io = ["payjoin/io"]

[lib]
name = "payjoin_ffi"
Expand All @@ -25,11 +36,19 @@ base64 = "0.22.1"
bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi.git", rev = "4cd8e644dbf4e001d71d5fffb232480fa5ff2246" }
hex = "0.4.3"
ohttp = { package = "bitcoin-ohttp", version = "0.6.0" }
payjoin = { git = "https://github.com/payjoin/rust-payjoin.git", rev = "bb47c8469146f1a9055b7f850d86f58f2b9627c6", features = ["v1", "io"] }
payjoin = { path = "../../rust-payjoin/payjoin", features = ["v1"] }
serde_json = "1.0.128"
thiserror = "1.0.58"
uniffi = { version = "0.28.0", optional = true }
url = "2.5.0"
wasm-bindgen = { version = "0.2.91", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
# Compatibility to compile to WASM
getrandom = { version = "0.2.15", optional = true, features = ["js"] }
ring = { version = "0.17.8", optional = true, features = ["wasm32_unknown_unknown_js"] }
gloo-timers = { version = "0.3.0", optional = true, features = ["futures"] }
web-sys = { version = "0.3", optional = true, features = ["console"] }
serde-wasm-bindgen = { version = "0.2.0", optional = true }

[dev-dependencies]
bdk = { version = "0.29.0", features = ["all-keys", "use-esplora-ureq", "keys-bip39", "rpc"] }
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ cargo test --package payjoin_ffi --test bdk_integration_test v2_to_v2_full_cycl

This project is in active development and currently in its Alpha stage. **Please proceed with caution**, particularly when using real funds.
We encourage thorough review, testing, and contributions to help improve its stability and security before considering production use.

## WASM

```shell
wasm-pack build --no-default-features --features wasm
```
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl From<payjoin::receive::Error> for PayjoinError {
}
}

#[cfg(not(feature = "wasm"))]
impl From<payjoin::io::Error> for PayjoinError {
fn from(value: payjoin::io::Error) -> Self {
PayjoinError::IoError { message: value.to_string() }
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

pub mod bitcoin_ffi;
pub mod error;
#[cfg(not(feature = "wasm"))]
pub mod io;
pub mod ohttp;
pub mod receive;
pub mod request;
#[cfg(feature = "wasm")]
pub mod request_wasm;
pub mod send;
pub mod uri;
mod utils;

pub use utils::*;
pub use crate::bitcoin_ffi::*;
pub use crate::error::PayjoinError;
pub use crate::ohttp::*;
Expand All @@ -20,3 +25,12 @@ pub use crate::send::uni::*;
pub use crate::uri::{PjUri, Uri, Url};
#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
// Initialize WASM logging (optional but helpful for debugging)
#[cfg(feature = "wasm")]
#[wasm_bindgen(start)]
pub fn start() {
console_error_panic_hook::set_once();
}
34 changes: 33 additions & 1 deletion src/ohttp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#[cfg(not(feature = "wasm"))]
use crate::error::PayjoinError;

#[cfg(feature = "wasm")]
use {
crate::utils::result::JsResult,
wasm_bindgen::prelude::*,
};

use std::str::FromStr;

impl From<payjoin::OhttpKeys> for OhttpKeys {
fn from(value: payjoin::OhttpKeys) -> Self {
Self(value)
Expand All @@ -11,21 +20,44 @@ impl From<OhttpKeys> for payjoin::OhttpKeys {
}
}
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Debug, Clone)]
pub struct OhttpKeys(pub payjoin::OhttpKeys);
pub struct OhttpKeys(
#[wasm_bindgen(skip)]
pub payjoin::OhttpKeys
);

#[cfg_attr(feature = "uniffi", uniffi::export)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl OhttpKeys {
/// Decode an OHTTP KeyConfig
#[cfg(not(feature = "wasm"))]
#[cfg_attr(feature = "uniffi", uniffi::constructor)]
pub fn decode(bytes: Vec<u8>) -> Result<Self, PayjoinError> {
payjoin::OhttpKeys::decode(bytes.as_slice()).map(|e| e.into()).map_err(|e| e.into())
}

#[cfg(feature = "wasm")]
#[wasm_bindgen(constructor)]
pub fn decode(bytes: Vec<u8>) -> JsResult<OhttpKeys> {
payjoin::OhttpKeys::decode(bytes.as_slice())
.map(|e| e.into())
.map_err(|e| wasm_bindgen::JsError::new(&e.to_string()))
}

#[cfg(feature = "wasm")]
pub fn parse(s: &str) -> JsResult<OhttpKeys> {
payjoin::OhttpKeys::from_str(s)
.map(|e| e.into())
.map_err(|e| wasm_bindgen::JsError::new(&e.to_string()))
}
}


use std::sync::Mutex;

#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct ClientResponse(Mutex<Option<ohttp::ClientResponse>>);

impl From<&ClientResponse> for ohttp::ClientResponse {
Expand Down
7 changes: 6 additions & 1 deletion src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ use crate::{ClientResponse, Request};
#[cfg(feature = "uniffi")]
pub mod uni;

#[cfg(feature = "wasm")]
pub mod wasm;


#[derive(Clone, Debug)]
pub struct Receiver(pub payjoin::receive::v2::Receiver);

impl From<Receiver> for payjoin::receive::v2::Receiver {
fn from(value: Receiver) -> Self {
value.0
Expand Down Expand Up @@ -246,7 +251,7 @@ impl WantsOutputs {
replacement_outputs.iter().map(|o| o.clone().into()).collect();
self.0
.clone()
.replace_receiver_outputs(replacement_outputs, &drain_script.0)
.replace_receiver_outputs(replacement_outputs.into_iter(), &drain_script.0)
.map(Into::into)
.map_err(Into::into)
}
Expand Down
Loading