sdk_client/tests/prd.rs
2024-08-20 12:22:03 +02:00

82 lines
2.8 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::process::{Process, Role, ValidationRules};
use sdk_common::sp_client::bitcoin::hashes::Hash;
use sdk_common::sp_client::bitcoin::{OutPoint, Txid};
use sdk_common::sp_client::spclient::OwnedOutput;
use serde_json::Value;
use tsify::JsValueSerdeExt;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
mod utils;
use utils::*;
#[wasm_bindgen_test]
fn test_alice_sends_prd_message_to_bob() {
reset_device().unwrap();
setup();
debug!("==============================================\nStarting test_alice_sends_prd_message_to_bob\n==============================================");
// ============================ Alice
restore_device(ALICE_LOGGED_DEVICE.to_owned()).unwrap();
// Alice first puts her message in a pcd
let pcd = Pcd::new("TEST".to_owned());
debug!("Alice notified Bob about a message it sent");
let empty_process = Process::new(
"empty".to_owned(),
vec![],
ValidationRules::new(0.0, Role::User),
Txid::all_zeros(),
String::default(),
String::default(),
String::default(),
Value::Null
);
let notification_tx =
create_notification_transaction(helper_get_bob_address(), empty_process, pcd, 1).unwrap();
let get_outputs_result = get_outputs().unwrap();
let alice_outputs: HashMap<OutPoint, OwnedOutput> = get_outputs_result.into_serde().unwrap();
let notification_tweak_data = helper_get_tweak_data(
&notification_tx.transaction,
alice_outputs
);
debug!("Alice parses her own notification transaction");
// Alice parse her own transaction to update her utxos
helper_parse_transaction(
&notification_tx.transaction,
&notification_tweak_data,
);
reset_device().unwrap();
restore_device(BOB_LOGGED_DEVICE.to_owned()).unwrap();
debug!("bob parses the transaction and the message");
helper_parse_transaction(&notification_tx.transaction, &notification_tweak_data);
helper_parse_cipher(notification_tx.new_network_msg.prd_cipher.unwrap());
let bob_notification_msg = helper_parse_cipher(notification_tx.new_network_msg.pcd_cipher.unwrap());
let msg_dump = dump_message_cache().unwrap();
debug!("bob_wallet: {:?}", dump_device());
debug!("bob_notification_msg: {:?}", msg_dump);
debug!("commited_in: {:?}", msg_dump.get(0).unwrap().find("0x71b37cede4655932a5ce97bb8c4a7845adce96d4f85b64bc699bf74942c19f89"));
assert!(bob_notification_msg.status == CachedMessageStatus::ReceivedMustConfirm);
}