Skip to content

Commit

Permalink
Merge branch 'xpubs'
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Dec 18, 2024
2 parents 3226fe5 + 21f5b24 commit 210c5c1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 7 deletions.
27 changes: 27 additions & 0 deletions lwk_common/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ pub enum Bip {
Bip87,
}

impl std::fmt::Display for Bip {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Bip::Bip84 => write!(f, "bip84"),
Bip::Bip49 => write!(f, "bip49"),
Bip::Bip87 => write!(f, "bip87"),
}
}
}

#[derive(Error, Debug)]
#[error("Invalid bip variant '{0}' supported variant are: 'bip84'")]
pub struct InvalidBipVariant(String);
Expand Down Expand Up @@ -214,3 +224,20 @@ impl FromStr for Multisig {
})
}
}

#[cfg(test)]
mod test {
use std::str::FromStr;

use super::Bip;

#[test]
fn roundtrip_bip() {
for el in ["bip49", "bip84", "bip87"] {
let bip = Bip::from_str(el).unwrap();
let bip_str = bip.to_string();
assert_eq!(el, bip_str);
}
Bip::from_str("vattelapesca").unwrap_err();
}
}
4 changes: 4 additions & 0 deletions lwk_signer/src/software.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl SwSigner {
})
}

pub fn is_mainnet(&self) -> bool {
self.xprv.network == bitcoin::NetworkKind::Main
}

pub fn random(is_mainnet: bool) -> Result<(Self, Mnemonic), NewError> {
let mnemonic = Mnemonic::generate(12)?;
Ok((SwSigner::new(&mnemonic.to_string(), is_mainnet)?, mnemonic))
Expand Down
65 changes: 65 additions & 0 deletions lwk_wasm/src/bip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use wasm_bindgen::prelude::*;

/// wrapper over [`lwk_common::Bip`]
#[wasm_bindgen]
pub struct Bip {
inner: lwk_common::Bip,
}

impl std::fmt::Display for Bip {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}

impl From<lwk_common::Bip> for Bip {
fn from(inner: lwk_common::Bip) -> Self {
Self { inner }
}
}

impl From<Bip> for lwk_common::Bip {
fn from(value: Bip) -> Self {
value.inner
}
}

#[wasm_bindgen]
impl Bip {
/// Creates a bip49 variant
pub fn bip49() -> Bip {
lwk_common::Bip::Bip49.into()
}

/// Creates a bip84 variant
pub fn bip84() -> Bip {
lwk_common::Bip::Bip84.into()
}

/// Creates a bip87 variant
pub fn bip87() -> Bip {
lwk_common::Bip::Bip87.into()
}

#[wasm_bindgen(js_name = toString)]
pub fn to_string_js(&self) -> String {
format!("{}", self)
}
}

#[cfg(all(test, target_arch = "wasm32"))]
mod tests {

use wasm_bindgen_test::*;

use crate::Bip;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
async fn test_bip() {
assert_eq!(Bip::bip49().to_string(), "bip49");
assert_eq!(Bip::bip84().to_string(), "bip84");
assert_eq!(Bip::bip87().to_string(), "bip87");
}
}
10 changes: 5 additions & 5 deletions lwk_wasm/src/jade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::{collections::HashMap, str::FromStr};
use crate::{
serial::{get_jade_serial, WebSerial},
signer::FakeSigner,
Error, Network, Pset, WolletDescriptor, Xpub,
Bip, Error, Network, Pset, WolletDescriptor, Xpub,
};
use lwk_common::{Bip, DescriptorBlindingKey, Signer};
use lwk_common::{DescriptorBlindingKey, Signer};
use lwk_jade::{asyncr, protocol::GetXpubParams};
use lwk_jade::{
derivation_path_to_vec,
Expand Down Expand Up @@ -146,14 +146,14 @@ impl Jade {
Ok(serde_wasm_bindgen::to_value(&wallets_str)?)
}

#[wasm_bindgen(js_name = keyoriginXpubBip87)]
pub async fn keyorigin_xpub_bip87(&self) -> Result<String, Error> {
#[wasm_bindgen(js_name = keyoriginXpub)]
pub async fn keyorigin_xpub(&self, bip: Bip) -> Result<String, Error> {
self.inner.unlock().await?;
let signer = self.create_fake_signer().await?;
let is_mainnet = self.inner.network().is_mainnet();

Ok(signer
.keyorigin_xpub(Bip::Bip87, is_mainnet)
.keyorigin_xpub(bip.into(), is_mainnet)
.map_err(Error::Generic)?)
}

Expand Down
2 changes: 2 additions & 0 deletions lwk_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]

mod bip;
mod blockdata;
mod contract;
mod descriptor;
Expand All @@ -21,6 +22,7 @@ mod update;
mod wollet;
mod xpub;

pub use bip::Bip;
pub use blockdata::address::{Address, AddressResult};
pub use blockdata::asset_id::AssetId;
pub use blockdata::out_point::OutPoint;
Expand Down
15 changes: 13 additions & 2 deletions lwk_wasm/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::{Error, Mnemonic, Network, Pset, WolletDescriptor, Xpub};
use crate::{Bip, Error, Mnemonic, Network, Pset, WolletDescriptor, Xpub};
use lwk_wollet::{
bitcoin::bip32, elements::pset::PartiallySignedTransaction, elements_miniscript::slip77,
};
Expand Down Expand Up @@ -49,6 +49,15 @@ impl Signer {
pub fn get_master_xpub(&self) -> Result<Xpub, Error> {
Ok(self.inner.xpub().into())
}

#[wasm_bindgen(js_name = keyoriginXpub)]
pub fn keyorigin_xpub(&self, bip: Bip) -> Result<String, Error> {
Ok(lwk_common::Signer::keyorigin_xpub(
&self.inner,
bip.into(),
self.inner.is_mainnet(),
)?)
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -79,7 +88,7 @@ impl lwk_common::Signer for FakeSigner {

#[cfg(all(test, target_arch = "wasm32"))]
mod tests {
use crate::{Mnemonic, Pset, Signer};
use crate::{Bip, Mnemonic, Pset, Signer};
use lwk_wollet::elements;
use std::str::FromStr;
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -113,5 +122,7 @@ mod tests {
assert_ne!(pset, signed_pset);

assert_eq!(signer.get_master_xpub().unwrap().fingerprint(), "73c5da0a");

assert_eq!(signer.keyorigin_xpub(Bip::bip49()).unwrap(), "[73c5da0a/49h/1h/0h]tpubDD7tXK8KeQ3YY83yWq755fHY2JW8Ha8Q765tknUM5rSvjPcGWfUppDFMpQ1ScziKfW3ZNtZvAD7M3u7bSs7HofjTD3KP3YxPK7X6hwV8Rk2");
}
}

0 comments on commit 210c5c1

Please sign in to comment.