Make wasm crate

This commit is contained in:
benthecarman
2023-04-21 19:41:03 -05:00
parent ff1caba403
commit 38f0402dc0
12 changed files with 224 additions and 31 deletions

View File

@@ -28,11 +28,10 @@ jobs:
cargo-${{ runner.os }}- cargo-${{ runner.os }}-
- name: Cargo Publish - name: Cargo Publish
run: cargo publish run: cargo publish -p waila
env: env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
npm: npm:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@@ -67,7 +66,7 @@ jobs:
version: 'latest' version: 'latest'
- name: Build wasm - 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 - name: Publish wasm
run: wasm-pack publish --access public -t web run: wasm-pack publish --access public -t web

View File

@@ -1,31 +1,12 @@
[package] [workspace]
name = "bitcoin-waila" resolver = "2"
version = "0.1.3"
edition = "2018"
authors = ["Ben Carman <benthecarman@live.com>", "Paul Miller <paul@pauljmiller.com>"]
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] members = [
crate-type = ["cdylib", "rlib"] "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] # Tell `rustc` to optimize for small code size.
default = ["std"] [profile.release.package.waila-wasm]
std = ["bitcoin/std", "lightning-invoice/std", "lightning/std"] opt-level = "s"
no-std = ["bitcoin/no-std", "lightning-invoice/no-std", "lightning/no-std"]
[package.metadata.wasm-pack.profile.release]
wasm-opt = true

19
waila-wasm/Cargo.toml Normal file
View File

@@ -0,0 +1,19 @@
[package]
name = "waila-wasm"
version = "0.1.4"
edition = "2018"
authors = ["Ben Carman <benthecarman@live.com>", "Paul Miller <paul@pauljmiller.com>"]
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"

21
waila-wasm/README.md Normal file
View File

@@ -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

63
waila-wasm/src/lib.rs Normal file
View File

@@ -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<PaymentParams, JsValue> {
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<String> {
self.params.memo()
}
#[wasm_bindgen(getter)]
pub fn network(&self) -> Option<String> {
self.params.network().map(|n| n.to_string())
}
#[wasm_bindgen(getter)]
pub fn amount_sats(&self) -> Option<u64> {
self.params.amount().map(|amount| amount.to_sat())
}
#[wasm_bindgen(getter)]
pub fn amount_msats(&self) -> Option<u64> {
self.params.amount_msats()
}
#[wasm_bindgen(getter)]
pub fn address(&self) -> Option<String> {
self.params.address().map(|addr| addr.to_string())
}
#[wasm_bindgen(getter)]
pub fn invoice(&self) -> Option<String> {
self.params.invoice().map(|invoice| invoice.to_string())
}
#[wasm_bindgen(getter)]
pub fn node_pubkey(&self) -> Option<String> {
self.params.node_pubkey().map(|pubkey| pubkey.to_string())
}
#[wasm_bindgen(getter)]
pub fn lnurl(&self) -> Option<String> {
self.params.lnurl().map(|lnurl| lnurl.to_string())
}
}

2
waila/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
/Cargo.lock

31
waila/Cargo.toml Normal file
View File

@@ -0,0 +1,31 @@
[package]
name = "bitcoin-waila"
version = "0.1.3"
edition = "2018"
authors = ["Ben Carman <benthecarman@live.com>", "Paul Miller <paul@pauljmiller.com>"]
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

21
waila/LICENSE Normal file
View File

@@ -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.

56
waila/README.md Normal file
View File

@@ -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()));
```