sdk_client/tests/challenge.rs
2024-08-15 16:19:29 +02:00

153 lines
5.2 KiB
Rust

use std::collections::HashMap;
use log::debug;
use sdk_client::api::{
create_confirmation_transaction, create_notification_transaction, dump_device, dump_message_cache, get_outputs, reset_device, restore_device, set_message_cache, setup
};
use sdk_common::network::{
CachedMessage, CachedMessageStatus, Pcd
};
use sdk_common::sp_client::bitcoin::OutPoint;
use sdk_common::sp_client::spclient::OwnedOutput;
use tsify::JsValueSerdeExt;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
mod utils;
use utils::*;
#[wasm_bindgen_test]
fn test_bob_challenges_alice() {
reset_device().unwrap();
debug!("==============================================\nStarting test_bob_challenges_alice\n==============================================");
// =============================== Bob
setup();
restore_device(BOB_CHALLENGE_DEVICE.to_owned()).unwrap();
set_message_cache(
serde_json::from_str::<Vec<CachedMessage>>(BOB_CHALLENGE_CACHE)
.unwrap()
.into_iter()
.map(|v| v.to_string())
.collect(),
)
.unwrap();
let notification_msg: CachedMessage = serde_json::from_str(dump_message_cache().unwrap().get(0).unwrap()).unwrap();
let get_outputs_result = get_outputs().unwrap();
let bob_outputs: HashMap<OutPoint, OwnedOutput> = get_outputs_result.into_serde().unwrap();
debug!("Bob sends a challenge transaction back to Alice");
debug!("outpoints: {:?}", bob_outputs);
let confirmation_tx = create_confirmation_transaction(notification_msg.id, 1).unwrap();
let confirmation_tweak_data = helper_get_tweak_data(
&confirmation_tx.transaction,
bob_outputs
);
debug!("Bob parsing its own confirmation tx");
helper_parse_transaction(
&confirmation_tx.transaction,
&confirmation_tweak_data,
);
// ========================== Alice
reset_device().unwrap();
restore_device(ALICE_CHALLENGE_DEVICE.to_owned()).unwrap();
set_message_cache(
serde_json::from_str::<Vec<CachedMessage>>(ALICE_CHALLENGE_CACHE)
.unwrap()
.into_iter()
.map(|v| v.to_string())
.collect(),
)
.unwrap();
debug!("Alice parsing confirmation tx");
helper_parse_transaction(&confirmation_tx.transaction, &confirmation_tweak_data);
}
// #[wasm_bindgen_test]
// fn test_alice_answers_bob() {
// reset_device().unwrap();
// setup();
// debug!("==============================================\nStarting test_alice_answers_bob\n==============================================");
// restore_device(ALICE_ANSWER_DEVICE.to_owned()).unwrap();
// set_message_cache(
// serde_json::from_str::<Vec<CachedMessage>>(ALICE_ANSWER_CACHE)
// .unwrap()
// .into_iter()
// .map(|v| v.to_string())
// .collect(),
// )
// .unwrap();
// let challenge_msg: CachedMessage = serde_json::from_str(dump_message_cache().unwrap().get(0).unwrap()).unwrap();
// debug!("Alice answers bob's challenge");
// let answer_tx = answer_confirmation_transaction(challenge_msg.id, 1).unwrap();
// let get_outputs_result = get_outputs().unwrap();
// let alice_outputs: HashMap<OutPoint, OwnedOutput> = get_outputs_result.into_serde().unwrap();
// let answer_tweak_data = helper_get_tweak_data(
// &answer_tx.transaction,
// alice_outputs,
// );
// debug!("Alice parses her own transaction");
// helper_parse_transaction(&answer_tx.transaction, &answer_tweak_data);
// reset_device().unwrap();
// restore_device(BOB_ANSWER_DEVICE.to_owned()).unwrap();
// set_message_cache(
// serde_json::from_str::<Vec<CachedMessage>>(BOB_ANSWER_CACHE)
// .unwrap()
// .into_iter()
// .map(|v| v.to_string())
// .collect(),
// )
// .unwrap();
// debug!("Bob parses answer transaction {}", answer_tx.txid);
// helper_parse_transaction(&answer_tx.transaction, &answer_tweak_data);
// }
// #[wasm_bindgen_test]
// fn test_alice_sends_message_through_trusted_channel() {
// reset_device().unwrap();
// setup();
// debug!("==============================================\nStarting test_alice_sends_message_through_trusted_channel\n==============================================");
// restore_device(ALICE_FINAL_DEVICE.to_owned()).unwrap();
// set_message_cache(
// serde_json::from_str::<Vec<CachedMessage>>(ALICE_FINAL_CACHE)
// .unwrap()
// .into_iter()
// .map(|v| v.to_string())
// .collect(),
// )
// .unwrap();
// let answered_msg: CachedMessage = serde_json::from_str(dump_message_cache().unwrap().get(0).unwrap()).unwrap();
// // Bob sends a message to Alice
// debug!("Bob Sends a message to Alice");
// let secret_msg = "Hello Alice";
// let cipher =
// encrypt_with_key(secret_msg.to_owned(), answered_msg.shared_secret.unwrap()).unwrap();
// let bob_msg = Envelope::new(sdk_common::network::AnkFlag::Cipher, &cipher);
// // Alice can find the message
// let mut result = helper_parse_cipher(bob_msg.to_string());
// assert!(result.plaintext.pop() == Some(secret_msg.to_owned()));
// }