From 38f0402dc017eabfe232aa806de204763b84f822 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Fri, 21 Apr 2023 19:41:03 -0500 Subject: [PATCH] Make wasm crate --- .github/workflows/release.yml | 5 ++- Cargo.toml | 37 +++++--------------- waila-wasm/Cargo.toml | 19 +++++++++++ LICENSE => waila-wasm/LICENSE | 0 waila-wasm/README.md | 21 ++++++++++++ waila-wasm/src/lib.rs | 63 +++++++++++++++++++++++++++++++++++ waila/.gitignore | 2 ++ waila/Cargo.toml | 31 +++++++++++++++++ waila/LICENSE | 21 ++++++++++++ waila/README.md | 56 +++++++++++++++++++++++++++++++ {src => waila/src}/bip21.rs | 0 {src => waila/src}/lib.rs | 0 12 files changed, 224 insertions(+), 31 deletions(-) create mode 100644 waila-wasm/Cargo.toml rename LICENSE => waila-wasm/LICENSE (100%) create mode 100644 waila-wasm/README.md create mode 100644 waila-wasm/src/lib.rs create mode 100644 waila/.gitignore create mode 100644 waila/Cargo.toml create mode 100644 waila/LICENSE create mode 100644 waila/README.md rename {src => waila/src}/bip21.rs (100%) rename {src => waila/src}/lib.rs (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3db9ecd..93b5748 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -28,11 +28,10 @@ jobs: cargo-${{ runner.os }}- - name: Cargo Publish - run: cargo publish + run: cargo publish -p waila env: CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} - npm: runs-on: ubuntu-latest steps: @@ -67,7 +66,7 @@ jobs: version: 'latest' - name: Build wasm - run: wasm-pack build --release --target web --scope mutinywallet + run: wasm-pack build ./waila-wasm --release --target web --scope mutinywallet - name: Publish wasm run: wasm-pack publish --access public -t web diff --git a/Cargo.toml b/Cargo.toml index dcf4188..146a273 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,31 +1,12 @@ -[package] -name = "bitcoin-waila" -version = "0.1.3" -edition = "2018" -authors = ["Ben Carman ", "Paul Miller "] -license = "MIT" -homepage = "https://github.com/MutinyWallet/bitcoin-waila/" -repository = "https://github.com/MutinyWallet/bitcoin-waila/" -readme = "README.md" -documentation = "https://docs.rs/bitcoin-waila/" -description = "\"What am I looking at?\" A tool for decoding bitcoin-related strings." -keywords = ["lightning", "bitcoin", "bip21", "lnurl"] +[workspace] +resolver = "2" -[lib] -crate-type = ["cdylib", "rlib"] +members = [ + "waila", + "waila-wasm", +] -[dependencies] -bitcoin = { version = "0.29.2", default-features = false, features = ["serde"] } -bip21 = "0.2.0" -lnurl-rs = { version = "0.2.1", default-features = false } -lightning-invoice = { version = "0.22.0", default-features = false } -lightning = { version = "0.0.114", default-features = false } -wasm-bindgen = { version = "0.2", optional = true } -[features] -default = ["std"] -std = ["bitcoin/std", "lightning-invoice/std", "lightning/std"] -no-std = ["bitcoin/no-std", "lightning-invoice/no-std", "lightning/no-std"] - -[package.metadata.wasm-pack.profile.release] -wasm-opt = true +# Tell `rustc` to optimize for small code size. +[profile.release.package.waila-wasm] +opt-level = "s" diff --git a/waila-wasm/Cargo.toml b/waila-wasm/Cargo.toml new file mode 100644 index 0000000..0a31835 --- /dev/null +++ b/waila-wasm/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "waila-wasm" +version = "0.1.4" +edition = "2018" +authors = ["Ben Carman ", "Paul Miller "] +license = "MIT" +homepage = "https://github.com/MutinyWallet/bitcoin-waila/" +repository = "https://github.com/MutinyWallet/bitcoin-waila/" +readme = "README.md" +documentation = "https://docs.rs/bitcoin-waila/" +description = "\"What am I looking at?\" A tool for decoding bitcoin-related strings." +keywords = ["lightning", "bitcoin", "bip21", "lnurl"] + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +bitcoin-waila = { path = "../waila" } +wasm-bindgen = "0.2.84" diff --git a/LICENSE b/waila-wasm/LICENSE similarity index 100% rename from LICENSE rename to waila-wasm/LICENSE diff --git a/waila-wasm/README.md b/waila-wasm/README.md new file mode 100644 index 0000000..e1fb59c --- /dev/null +++ b/waila-wasm/README.md @@ -0,0 +1,21 @@ +# bitcoin-waila + +"What am I looking at?" A tool for decoding bitcoin-related strings. + +--- + +## What is this? + +This is a tool for decoding bitcoin-related strings. +The goal is to be able to give it any string, and it will decode it for you while giving you all the relevant payment +information. + +Currently supported: + +- Bitcoin address +- BIP-21 URI +- Lightning invoice +- Lightning Offer +- Node Pubkey +- LNURL +- Lightning Address diff --git a/waila-wasm/src/lib.rs b/waila-wasm/src/lib.rs new file mode 100644 index 0000000..716a611 --- /dev/null +++ b/waila-wasm/src/lib.rs @@ -0,0 +1,63 @@ +use std::str::FromStr; +use wasm_bindgen::prelude::*; + +#[derive(Debug)] +#[wasm_bindgen] +pub struct PaymentParams { + string: String, + params: bitcoin_waila::PaymentParams<'static>, +} + +#[wasm_bindgen] +impl PaymentParams { + #[wasm_bindgen(constructor)] + pub fn from_string(string: String) -> Result { + let params = bitcoin_waila::PaymentParams::from_str(&string).map_err(|_| JsValue::NULL)?; + Ok(PaymentParams { string, params }) + } + + #[wasm_bindgen(getter)] + pub fn string(&self) -> String { + self.string.clone() + } + + #[wasm_bindgen(getter)] + pub fn memo(&self) -> Option { + self.params.memo() + } + + #[wasm_bindgen(getter)] + pub fn network(&self) -> Option { + self.params.network().map(|n| n.to_string()) + } + + #[wasm_bindgen(getter)] + pub fn amount_sats(&self) -> Option { + self.params.amount().map(|amount| amount.to_sat()) + } + + #[wasm_bindgen(getter)] + pub fn amount_msats(&self) -> Option { + self.params.amount_msats() + } + + #[wasm_bindgen(getter)] + pub fn address(&self) -> Option { + self.params.address().map(|addr| addr.to_string()) + } + + #[wasm_bindgen(getter)] + pub fn invoice(&self) -> Option { + self.params.invoice().map(|invoice| invoice.to_string()) + } + + #[wasm_bindgen(getter)] + pub fn node_pubkey(&self) -> Option { + self.params.node_pubkey().map(|pubkey| pubkey.to_string()) + } + + #[wasm_bindgen(getter)] + pub fn lnurl(&self) -> Option { + self.params.lnurl().map(|lnurl| lnurl.to_string()) + } +} diff --git a/waila/.gitignore b/waila/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/waila/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/waila/Cargo.toml b/waila/Cargo.toml new file mode 100644 index 0000000..dcf4188 --- /dev/null +++ b/waila/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "bitcoin-waila" +version = "0.1.3" +edition = "2018" +authors = ["Ben Carman ", "Paul Miller "] +license = "MIT" +homepage = "https://github.com/MutinyWallet/bitcoin-waila/" +repository = "https://github.com/MutinyWallet/bitcoin-waila/" +readme = "README.md" +documentation = "https://docs.rs/bitcoin-waila/" +description = "\"What am I looking at?\" A tool for decoding bitcoin-related strings." +keywords = ["lightning", "bitcoin", "bip21", "lnurl"] + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +bitcoin = { version = "0.29.2", default-features = false, features = ["serde"] } +bip21 = "0.2.0" +lnurl-rs = { version = "0.2.1", default-features = false } +lightning-invoice = { version = "0.22.0", default-features = false } +lightning = { version = "0.0.114", default-features = false } +wasm-bindgen = { version = "0.2", optional = true } + +[features] +default = ["std"] +std = ["bitcoin/std", "lightning-invoice/std", "lightning/std"] +no-std = ["bitcoin/no-std", "lightning-invoice/no-std", "lightning/no-std"] + +[package.metadata.wasm-pack.profile.release] +wasm-opt = true diff --git a/waila/LICENSE b/waila/LICENSE new file mode 100644 index 0000000..8412527 --- /dev/null +++ b/waila/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Mutiny Wallet Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/waila/README.md b/waila/README.md new file mode 100644 index 0000000..50758a0 --- /dev/null +++ b/waila/README.md @@ -0,0 +1,56 @@ +# bitcoin-waila + +"What am I looking at?" A tool for decoding bitcoin-related strings. + +--- + +## What is this? + +This is a tool for decoding bitcoin-related strings. +The goal is to be able to give it any string, and it will decode it for you while giving you all the relevant payment +information. + +Currently supported: + +- Bitcoin address +- BIP-21 URI +- Lightning invoice +- Lightning Offer +- Node Pubkey +- LNURL +- Lightning Address + +## Examples + +Bitcoin Address: + +```rust +let string = "1andreas3batLhQa2FawWjeyjCqyBzypd"; + +let decoded = bitcoin_waila::PaymentParams::from_str(string).unwrap(); + +assert_eq!(decoded.address, Some(Address::from_str("1andreas3batLhQa2FawWjeyjCqyBzypd").unwrap())); +assert_eq!(parsed.network(), Some(Network::Bitcoin)); +``` + +BIP 21: + +```rust +let string = "bitcoin:BC1QYLH3U67J673H6Y6ALV70M0PL2YZ53TZHVXGG7U?amount=0.00001&label=sbddesign%3A%20For%20lunch%20Tuesday&message=For%20lunch%20Tuesday&lightning=LNBC10U1P3PJ257PP5YZTKWJCZ5FTL5LAXKAV23ZMZEKAW37ZK6KMV80PK4XAEV5QHTZ7QDPDWD3XGER9WD5KWM36YPRX7U3QD36KUCMGYP282ETNV3SHJCQZPGXQYZ5VQSP5USYC4LK9CHSFP53KVCNVQ456GANH60D89REYKDNGSMTJ6YW3NHVQ9QYYSSQJCEWM5CJWZ4A6RFJX77C490YCED6PEMK0UPKXHY89CMM7SCT66K8GNEANWYKZGDRWRFJE69H9U5U0W57RRCSYSAS7GADWMZXC8C6T0SPJAZUP6"; + +let decoded = bitcoin_waila::PaymentParams::from_str(string).unwrap(); + +assert_eq!(parsed.amount(), Some(Amount::from_btc(0.00001).unwrap())); +assert_eq!(parsed.address(), Some(Address::from_str("BC1QYLH3U67J673H6Y6ALV70M0PL2YZ53TZHVXGG7U").unwrap())); +assert_eq!(parsed.memo(), Some("For lunch Tuesday".to_string())); +assert_eq!(parsed.network(), Some(Network::Bitcoin)); +assert_eq!(parsed.invoice(), Some(Invoice::from_str("LNBC10U1P3PJ257PP5YZTKWJCZ5FTL5LAXKAV23ZMZEKAW37ZK6KMV80PK4XAEV5QHTZ7QDPDWD3XGER9WD5KWM36YPRX7U3QD36KUCMGYP282ETNV3SHJCQZPGXQYZ5VQSP5USYC4LK9CHSFP53KVCNVQ456GANH60D89REYKDNGSMTJ6YW3NHVQ9QYYSSQJCEWM5CJWZ4A6RFJX77C490YCED6PEMK0UPKXHY89CMM7SCT66K8GNEANWYKZGDRWRFJE69H9U5U0W57RRCSYSAS7GADWMZXC8C6T0SPJAZUP6").unwrap())); +assert_eq!(parsed.node_pubkey(), Some(PublicKey::from_str("037cc5f9f1da20ac0d60e83989729a204a33cc2d8e80438969fadf35c1c5f1233b").unwrap())); +``` + +Lightning Address: + +```rust +let parsed = bitcoin_waila::PaymentParams::from_str("ben@opreturnbot.com").unwrap(); +assert_eq!(parsed.lnurl(), Some(LnUrl::from_str("lnurl1dp68gurn8ghj7mmswfjhgatjde3x7apwvdhk6tewwajkcmpdddhx7amw9akxuatjd3cz7cn9dc94s6d4").unwrap())); +``` diff --git a/src/bip21.rs b/waila/src/bip21.rs similarity index 100% rename from src/bip21.rs rename to waila/src/bip21.rs diff --git a/src/lib.rs b/waila/src/lib.rs similarity index 100% rename from src/lib.rs rename to waila/src/lib.rs