Skip to content

Commit

Permalink
Update the tests and boltz API.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed May 4, 2024
1 parent 8dfec13 commit 436e22b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 93 deletions.
1 change: 1 addition & 0 deletions src/swaps/boltzv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub struct CreateReverseRequest {
pub from: String,
pub to: String,
pub preimage_hash: sha256::Hash,
pub address: String,
pub address_signature: String,
pub claim_public_key: PublicKey,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
11 changes: 9 additions & 2 deletions src/swaps/magic_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{error::Error, network::Chain};
use bitcoin::{
hashes::{sha256, Hash},
hex::FromHex,
key::Secp256k1,
secp256k1::Message,
key::{Keypair, Secp256k1},
secp256k1::{schnorr::Signature, Message},
PublicKey,
};
use elements::hex::ToHex;
Expand Down Expand Up @@ -116,6 +116,13 @@ pub fn check_for_mrh(
}
}

/// Sign the address signature by a priv key.
pub fn sign_address(addr: &str, keys: &Keypair) -> Signature {
let address_hash = sha256::Hash::hash(&Vec::from_hex(&addr).unwrap());
let msg = Message::from_digest_slice(address_hash.as_byte_array()).unwrap();
Secp256k1::new().sign_schnorr(&msg, &keys)
}

#[test]
fn test_bip21_parsing() {
let uri = "liquidtestnet:tlq1qqt3sgky7zert7237tred5rqmmx0eargp625zkyhr2ldw6yqdvh5fusnm5xk0qfjpejvgm37q7mqtv5epfksv78jweytmqgpd8?amount=0.00005122&assetid=144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a4";
Expand Down
2 changes: 0 additions & 2 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{error::Error, network::electrum::ElectrumConfig};
pub mod ec;
pub mod secrets;


/// Setup function that will only run once, even if called multiple times.

pub fn liquid_genesis_hash(electrum_config: &ElectrumConfig) -> Result<elements::BlockHash, Error> {
Expand Down Expand Up @@ -38,4 +37,3 @@ pub fn setup_logger() {
.init();
});
}

59 changes: 12 additions & 47 deletions tests/bitcoin_v2.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::{str::FromStr, time::Duration};

use bip21::Uri;
use boltz_client::{
network::electrum::ElectrumConfig,
swaps::boltzv2::{
BoltzApiClientV2, CreateReverseRequest, CreateSubmarineRequest, Subscription, SwapUpdate,
BOLTZ_TESTNET_URL_V2,
network::{electrum::ElectrumConfig, Chain},
swaps::{
boltzv2::{
BoltzApiClientV2, CreateReverseRequest, CreateSubmarineRequest, Subscription,
SwapUpdate, BOLTZ_TESTNET_URL_V2,
},
magic_routing::{check_for_mrh, sign_address},
},
util::{find_magic_routing_hint, secrets::Preimage, setup_logger},
util::{secrets::Preimage, setup_logger},
Bolt11Invoice, BtcSwapScriptV2, BtcSwapTxV2, Secp256k1,
};

use bitcoin::{
hashes::{sha256, Hash},
hex::FromHex,
key::rand::thread_rng,
secp256k1::{Keypair, Message},
secp256k1::Keypair,
PublicKey,
};

Expand All @@ -40,33 +42,7 @@ fn bitcoin_v2_submarine() {

let boltz_api_v2 = BoltzApiClientV2::new(BOLTZ_TESTNET_URL_V2);

// Check for magic routing hint in invoice.
if let Some(route_hint) = find_magic_routing_hint(&invoice).unwrap() {
let mrh_resp = boltz_api_v2.get_mrh_bip21(&invoice).unwrap();

let bip21 = mrh_resp
.bip21
.parse::<Uri<'_, _>>()
.unwrap()
.require_network(bitcoin::Network::Testnet)
.unwrap();
let address_hash = sha256::Hash::hash(&Vec::from_hex(&bip21.address.to_string()).unwrap());
let msg = Message::from_digest_slice(address_hash.as_byte_array()).unwrap();

let receiver_sig = bitcoin::secp256k1::schnorr::Signature::from_slice(
&Vec::from_hex(&mrh_resp.signature).unwrap(),
)
.unwrap();

let receiver_pubkey = PublicKey::from_str(&route_hint.src_node_id.to_string())
.unwrap()
.inner;

secp.verify_schnorr(&receiver_sig, &msg, &receiver_pubkey.x_only_public_key().0)
.unwrap();

log::info!("Magic Routing hint found and verification succeeded");
}
let _ = check_for_mrh(&boltz_api_v2, &invoice, Chain::BitcoinTestnet).unwrap();

// Initiate the swap with Boltz
let create_swap_req = CreateSubmarineRequest {
Expand Down Expand Up @@ -262,17 +238,14 @@ fn bitcoin_v2_reverse() {
// Give a valid claim address or else funds will be lost.
let claim_address = "tb1qq20a7gqewc0un9mxxlqyqwn7ut7zjrj9y3d0mu".to_string();

// Sign the address signature by claim priv key.
let address_hash = sha256::Hash::hash(&Vec::from_hex(&claim_address).unwrap());
let msg = Message::from_digest_slice(address_hash.as_byte_array()).unwrap();
let addrs_sig = Secp256k1::new().sign_schnorr(&msg, &our_keys);

let addrs_sig = sign_address(&claim_address, &our_keys);
let create_reverse_req = CreateReverseRequest {
invoice_amount,
from: "BTC".to_string(),
to: "BTC".to_string(),
preimage_hash: preimage.sha256,
address_signature: addrs_sig.to_string(),
address: claim_address.clone(),
claim_public_key,
referral_id: None, // Add address signature here.
};
Expand All @@ -283,14 +256,6 @@ fn bitcoin_v2_reverse() {

log::debug!("Got Reverse swap response: {:?}", reverse_resp);

if let Some(route_hint) = find_magic_routing_hint(&reverse_resp.invoice).unwrap() {
assert_eq!(
route_hint.src_node_id.to_string(),
our_keys.public_key().to_string()
);
log::info!("Magic Routing Hint spotted in invoice {:?}", route_hint);
}

let swap_script =
BtcSwapScriptV2::reverse_from_swap_resp(&reverse_resp, claim_public_key).unwrap();

Expand Down
53 changes: 11 additions & 42 deletions tests/liquid_v2.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::{str::FromStr, time::Duration};

use bip21::Uri;
use boltz_client::{
network::{electrum::ElectrumConfig, Chain},
swaps::boltzv2::{
BoltzApiClientV2, CreateReverseRequest, CreateSubmarineRequest, Subscription, SwapUpdate,
BOLTZ_MAINNET_URL_V2, BOLTZ_TESTNET_URL_V2,
swaps::{
boltzv2::{
BoltzApiClientV2, CreateReverseRequest, CreateSubmarineRequest, Subscription,
SwapUpdate, BOLTZ_MAINNET_URL_V2, BOLTZ_TESTNET_URL_V2,
},
magic_routing::{check_for_mrh, sign_address},
},
util::{find_magic_routing_hint, secrets::Preimage, setup_logger},
util::{secrets::Preimage, setup_logger},
Bolt11Invoice, LBtcSwapScriptV2, LBtcSwapTxV2, Secp256k1,
};

use bitcoin::{
hashes::{sha256, Hash},
hex::FromHex,
key::rand::thread_rng,
secp256k1::{Keypair, Message},
secp256k1::Keypair,
Amount, PublicKey,
};

Expand All @@ -40,38 +42,7 @@ fn liquid_v2_submarine() {

let boltz_api_v2 = BoltzApiClientV2::new(BOLTZ_TESTNET_URL_V2);

// Check for magic routing hint in invoice.
if let Some(route_hint) = find_magic_routing_hint(&invoice).unwrap() {
let mrh_resp = boltz_api_v2.get_mrh_bip21(&invoice).unwrap();

let bip21 = mrh_resp
.bip21
.parse::<Uri<'_, _>>()
.unwrap()
.require_network(bitcoin::Network::Testnet)
.unwrap();

log::info!("Received BIP21: {:?}", bip21);

let address_hash = sha256::Hash::hash(&Vec::from_hex(&bip21.address.to_string()).unwrap());
let msg = Message::from_digest_slice(address_hash.as_byte_array()).unwrap();

let receiver_sig = bitcoin::secp256k1::schnorr::Signature::from_slice(
&Vec::from_hex(&mrh_resp.signature).unwrap(),
)
.unwrap();

let receiver_pubkey = PublicKey::from_str(&route_hint.src_node_id.to_string())
.unwrap()
.inner;

secp.verify_schnorr(&receiver_sig, &msg, &receiver_pubkey.x_only_public_key().0)
.unwrap();

// TODO: verify liquid asset id.

log::info!("Magic Routing hint found and verification succeeded");
}
let _ = check_for_mrh(&boltz_api_v2, &invoice, Chain::LiquidTestnet).unwrap();

// Initiate the swap with Boltz
let create_swap_req = CreateSubmarineRequest {
Expand Down Expand Up @@ -268,17 +239,15 @@ fn liquid_v2_reverse() {
// Give a valid claim address or else funds will be lost.
let claim_address = "tlq1qqv4z28utgwunvn62s3aw0qjuw3sqgfdq6q8r8fesnawwnuctl70kdyedxw6tmxgqpq83x6ldsyr4n6cj0dm875k8g9k85w2s7".to_string();

// Sign the address signature by claim priv key.
let address_hash = sha256::Hash::hash(&Vec::from_hex(&claim_address).unwrap());
let msg = Message::from_digest_slice(address_hash.as_byte_array()).unwrap();
let addrs_sig = Secp256k1::new().sign_schnorr(&msg, &our_keys);
let addrs_sig = sign_address(&claim_address, &our_keys);

let create_reverse_req = CreateReverseRequest {
invoice_amount,
from: "BTC".to_string(),
to: "L-BTC".to_string(),
preimage_hash: preimage.sha256,
address_signature: addrs_sig.to_string(),
address: claim_address.clone(),
claim_public_key,
referral_id: None,
};
Expand Down

0 comments on commit 436e22b

Please sign in to comment.