Skip to content
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

Musig2 #33

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 app/bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ path = "src/main.rs"
[dependencies]
bitcoin = { path = "rust-bitcoin/bitcoin", default-features = false, features = ["no-std"] }
byteorder = { version = "1.4.3", default-features = false }
digest = "0.10.7"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
hex-literal = { version = "0.3.4" }
nom = { version = "7.1.3", default-features = false, features = ["alloc"] }
quick-protobuf = { version = "0.8.1", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
schnorr_fun = { path = "secp256kfun/schnorr_fun", default-features = false, features = ["alloc"] }
subtle = { version = "2.5.0", default-features = false }
vanadium_sdk = { path = "../../rust-sdk" }

Expand Down
83 changes: 45 additions & 38 deletions app/bitcoin/client_py/message_pb2.py

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

1 change: 1 addition & 0 deletions app/bitcoin/prepare_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

[ ! -d "rust-bitcoin" ] && git clone https://github.com/LedgerHQ/vanadium-rust-bitcoin.git rust-bitcoin
[ ! -d "rust-secp256k1" ] && git clone https://github.com/LedgerHQ/vanadium-rust-secp256k1.git rust-secp256k1
[ ! -d "secp256kfun" ] && git clone https://github.com/LedgerHQ/vanadium-secp256kfun.git secp256kfun
32 changes: 31 additions & 1 deletion app/bitcoin/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::string::{String, ToString};
use alloc::{fmt, format};

use vanadium_sdk::SdkError;
use vanadium_sdk::{SdkError, secp256k1};

#[derive(Debug)]
pub struct AppError {
Expand Down Expand Up @@ -34,6 +34,36 @@ impl From<quick_protobuf::Error> for AppError {
}
}

impl From<bitcoin::psbt::Error> for AppError {
fn from(err: bitcoin::psbt::Error) -> Self {
AppError::new(&err.to_string())
}
}

impl From<bitcoin::bip32::Error> for AppError {
fn from(err: bitcoin::bip32::Error) -> Self {
AppError::new(&err.to_string())
}
}

impl From<bitcoin::key::Error> for AppError {
fn from(err: bitcoin::key::Error) -> Self {
AppError::new(&err.to_string())
}
}

impl From<bitcoin::secp256k1::Error> for AppError {
fn from(err: bitcoin::secp256k1::Error) -> Self {
AppError::new(&err.to_string())
}
}

impl From<bitcoin::taproot::SigFromSliceError> for AppError {
fn from(err: bitcoin::taproot::SigFromSliceError) -> Self {
AppError::new(&err.to_string())
}
}

impl From<SdkError> for AppError {
fn from(err: SdkError) -> Self {
AppError::new(&format!("sdk error: {}", err))
Expand Down
47 changes: 45 additions & 2 deletions app/bitcoin/src/handlers/get_wallet_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ pub fn handle_get_wallet_address<'a>(

let script = wallet_policy
.to_script(req.change, req.address_index)
.map_err(|_| AppError::new("Failed to produce script"))?;
.map_err(|err| AppError::new(&format!("Failed to produce script: {}", err)))?;

let addr: Address<NetworkChecked> =
Address::from_script(script.as_script(), bitcoin::Network::Testnet)
.map_err(|_| AppError::new("Failed to produce address"))?;
.map_err(|err| AppError::new(&format!("Failed to produce address: {}", err)))?;

Ok(ResponseGetWalletAddress {
address: Cow::Owned(format!("{}", addr)),
Expand Down Expand Up @@ -99,4 +99,47 @@ mod tests {

assert_eq!(resp.address, "tb1qzdr7s2sr0dwmkwx033r4nujzk86u0cy6fmzfjk");
}

#[test]
fn test_get_wallet_address_musig_keypath() {
let req = RequestGetWalletAddress {
name: "Musig in keypath".into(),
descriptor_template: "tr(musig(@0,@1)/**)".into(),
keys_info: vec![
"[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT".into(),
"tpubDCwYjpDhUdPGQWG6wG6hkBJuWFZEtrn7j3xwG3i8XcQabcGC53xWZm1hSXrUPFS5UvZ3QhdPSjXWNfWmFGTioARHuG5J7XguEjgg7p8PxAm".into()
],
wallet_hmac: Cow::Owned(DUMMY_HMAC.into()),
change: false,
address_index: 3,
display: false,
};

let resp = handle_get_wallet_address(req).unwrap();

assert_eq!(resp.address, "tb1p7j9azx5xwt5fp3t99wnj4885yfcqynhz645ntflxr9dxr2g8ndnq32xa2m");
}

#[test]
fn test_get_wallet_address_musig_scriptpath() {
let req = RequestGetWalletAddress {
name: "Musig in script path".into(),
descriptor_template: "tr(@0/**,pk(musig(@1,@2)/**))".into(),
keys_info: vec![
"tpubD6NzVbkrYhZ4WLczPJWReQycCJdd6YVWXubbVUFnJ5KgU5MDQrD998ZJLSmaB7GVcCnJSDWprxmrGkJ6SvgQC6QAffVpqSvonXmeizXcrkN".into(),
"[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT".into(),
"tpubDCwYjpDhUdPGQWG6wG6hkBJuWFZEtrn7j3xwG3i8XcQabcGC53xWZm1hSXrUPFS5UvZ3QhdPSjXWNfWmFGTioARHuG5J7XguEjgg7p8PxAm".into()
],
wallet_hmac: Cow::Owned(DUMMY_HMAC.into()),
change: false,
address_index: 3,
display: false,
};

let resp = handle_get_wallet_address(req).unwrap();

assert_eq!(resp.address, "tb1pmx5syrz67lwdy8dsmvlta5h5ahfn6k9pg8qw3y0jn698xj0duxpq3k94zq");
}

}

Loading