221 lines
8.0 KiB
Rust
221 lines
8.0 KiB
Rust
use std::collections::HashMap;
|
|
use std::str::FromStr;
|
|
|
|
use sdk_client::api::{
|
|
create_device_from_sp_wallet, create_update_transaction, dump_device, dump_process_cache,
|
|
get_address, get_outputs, get_update_proposals, pair_device, parse_cipher, reset_device,
|
|
restore_device, set_process_cache, setup, ApiReturn,
|
|
};
|
|
use sdk_common::log::debug;
|
|
use sdk_common::pcd::{Member, RoleDefinition};
|
|
use sdk_common::sp_client::bitcoin::OutPoint;
|
|
use sdk_common::sp_client::spclient::OwnedOutput;
|
|
use serde_json::{json, Value};
|
|
|
|
use tsify::JsValueSerdeExt;
|
|
use wasm_bindgen_test::*;
|
|
|
|
mod utils;
|
|
|
|
use utils::*;
|
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
#[wasm_bindgen_test]
|
|
fn test_pairing() {
|
|
setup();
|
|
debug!("==============================================\nStarting test_pairing\n==============================================");
|
|
|
|
// ========================= Alice
|
|
reset_device().unwrap();
|
|
create_device_from_sp_wallet(ALICE_LOGIN_WALLET.to_owned()).unwrap();
|
|
|
|
// we get our own address
|
|
let device_address = get_address().unwrap();
|
|
|
|
// we scan the qr code or get the address by any other means
|
|
let paired_device = helper_get_bob_address();
|
|
|
|
// Alice creates the new member with Bob address
|
|
let new_member = Member::new(vec![
|
|
device_address.as_str().try_into().unwrap(),
|
|
paired_device.as_str().try_into().unwrap(),
|
|
])
|
|
.unwrap();
|
|
|
|
let pairing_init_state = json!({
|
|
"html": "",
|
|
"style": "",
|
|
"script": "",
|
|
"roles": {
|
|
"owner": {
|
|
"members":
|
|
[
|
|
new_member
|
|
],
|
|
"validation_rules":
|
|
[
|
|
{
|
|
"quorum": 0.0,
|
|
"fields": [
|
|
"roles",
|
|
"pairing_tx"
|
|
],
|
|
"min_sig_member": 0.0
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"pairing_tx": OutPoint::null(),
|
|
});
|
|
|
|
debug!("Alice pairs her device");
|
|
// we can update our local device now, first with an empty txid
|
|
pair_device(OutPoint::null().to_string(), vec![helper_get_bob_address()]).unwrap();
|
|
|
|
debug!("Alice sends a transaction commiting to an update prd to Bob");
|
|
let alice_pairing_return =
|
|
create_update_transaction(None, pairing_init_state.to_string(), 1).unwrap();
|
|
|
|
// debug!("{:?}", alice_pairing_return);
|
|
|
|
// todo must take all the necessary validation before commiting
|
|
|
|
// debug!("Alice prepares the commit message for the relay");
|
|
// let (outpoint, process) = alice_pairing_return.updated_process.unwrap();
|
|
// let to_commit = process.get_status_at(0).unwrap().encrypted_pcd;
|
|
// let init_return = create_commit_message(serde_json::to_string(&to_commit).unwrap(), RELAY_ADDRESS.to_owned(), None, 1).unwrap();
|
|
|
|
// todo send the commit message to the relay
|
|
|
|
let pairing_tx = alice_pairing_return.new_tx_to_send.unwrap();
|
|
|
|
// We can update the local device with the actual pairing outpoint
|
|
pair_device(
|
|
OutPoint::new(pairing_tx.txid(), 0).to_string(),
|
|
vec![helper_get_bob_address()],
|
|
)
|
|
.unwrap();
|
|
|
|
// This is only for testing, the relay takes care of that in prod
|
|
let get_outputs_result = get_outputs().unwrap();
|
|
|
|
let alice_outputs: HashMap<OutPoint, OwnedOutput> = get_outputs_result.into_serde().unwrap();
|
|
|
|
let alice_pairing_tweak_data = helper_get_tweak_data(&pairing_tx, alice_outputs);
|
|
|
|
// End of the test only part
|
|
|
|
// Alice parses her own transaction
|
|
helper_parse_transaction(&pairing_tx, &alice_pairing_tweak_data);
|
|
|
|
// Notify user that we're waiting for confirmation from the other device
|
|
|
|
let alice_device = dump_device().unwrap();
|
|
let alice_processes = dump_process_cache().unwrap();
|
|
debug!("Alice processes: {:#?}", alice_processes);
|
|
|
|
// ======================= Bob
|
|
reset_device().unwrap();
|
|
create_device_from_sp_wallet(BOB_LOGIN_WALLET.to_owned()).unwrap();
|
|
|
|
// Bob receives Alice pairing transaction
|
|
debug!("Bob parses Alice pairing transaction");
|
|
helper_parse_transaction(&pairing_tx, &alice_pairing_tweak_data);
|
|
|
|
debug!("Bob receives the prd");
|
|
let mut bob_retrieved_prd: ApiReturn = ApiReturn::default();
|
|
for cipher in alice_pairing_return.ciphers_to_send.iter() {
|
|
// debug!("Parsing cipher: {:#?}", cipher);
|
|
match parse_cipher(cipher.clone()) {
|
|
Ok(res) => bob_retrieved_prd = res,
|
|
Err(e) => {
|
|
debug!("Error parsing cipher: {:#?}", e);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
assert!(bob_retrieved_prd != ApiReturn::default());
|
|
|
|
debug!("Bob retrieved prd: {:#?}", bob_retrieved_prd);
|
|
|
|
debug!("Bob sends a Confirm Prd to Alice");
|
|
|
|
let bob_device = dump_device().unwrap();
|
|
let bob_processes = dump_process_cache().unwrap();
|
|
|
|
// ======================= Alice
|
|
reset_device().unwrap();
|
|
restore_device(alice_device).unwrap();
|
|
set_process_cache(alice_processes).unwrap();
|
|
|
|
debug!("Alice receives the Confirm Prd");
|
|
let alice_parsed_prd = parse_cipher(bob_retrieved_prd.ciphers_to_send[0].clone()).unwrap();
|
|
|
|
debug!("Alice parsed Bob's Confirm Prd: {:#?}", alice_parsed_prd);
|
|
|
|
// ======================= Bob
|
|
reset_device().unwrap();
|
|
restore_device(bob_device).unwrap();
|
|
set_process_cache(bob_processes).unwrap();
|
|
|
|
let bob_parsed_pcd_return = parse_cipher(alice_parsed_prd.ciphers_to_send[0].clone()).unwrap();
|
|
|
|
debug!("bob_parsed_pcd: {:#?}", bob_parsed_pcd_return);
|
|
|
|
// At this point, user must validate the pairing proposal received from Alice
|
|
// We decrypt the content of the pcd so that we can display to user what matters
|
|
let alice_proposal =
|
|
get_update_proposals(bob_parsed_pcd_return.updated_process.unwrap().0).unwrap();
|
|
|
|
debug!("Alice proposal: {:#?}", alice_proposal);
|
|
|
|
// get the pairing tx from the proposal
|
|
let proposal = Value::from_str(&alice_proposal.get(0).unwrap()).unwrap();
|
|
let pairing_tx = proposal.get("pairing_tx").unwrap().as_str().unwrap();
|
|
let roles: RoleDefinition =
|
|
serde_json::from_str(proposal.get("roles").unwrap().as_str().unwrap()).unwrap();
|
|
|
|
// we check that the proposal contains only one member
|
|
assert!(roles.members.len() == 1);
|
|
|
|
// we get all the addresses of the members of the proposal
|
|
let proposal_members = roles
|
|
.members
|
|
.iter()
|
|
.flat_map(|member| member.get_addresses())
|
|
.collect::<Vec<String>>();
|
|
|
|
// we can automatically check that a pairing member contains local device address + the one that sent the proposal
|
|
assert!(proposal_members.contains(&device_address));
|
|
assert!(proposal_members.contains(&paired_device));
|
|
assert!(proposal_members.len() == 2); // no free riders
|
|
|
|
// we can now show all the addresses + pairing tx to the user on device to prompt confirmation
|
|
|
|
pair_device(pairing_tx.to_owned(), proposal_members).unwrap();
|
|
|
|
// Bob signs the proposal and send a prd response to Alice
|
|
|
|
// debug!("Bob pairs device with Alice");
|
|
// let process = lock_processes().unwrap();
|
|
// let prd: Prd = serde_json::from_str(&bob_retrieved_prd.prd.unwrap()).unwrap();
|
|
// let relevant_process = process.get(&Uuid::parse_str(&prd.process_uuid).unwrap()).unwrap();
|
|
// // decrypt the pcd and update bob device
|
|
// let pairing_tx: Txid;
|
|
// if let Some(initial_state) = relevant_process.get_status_at(0) {
|
|
// let keys = initial_state.keys;
|
|
// let mut pcd = initial_state.encrypted_pcd;
|
|
// pcd.decrypt_fields(&keys).unwrap();
|
|
// debug!("decrypted pcd: {:?}", pcd);
|
|
// pairing_tx = Txid::from_str(pcd.get("pairing_tx").unwrap().as_str().unwrap()).unwrap();
|
|
// pair_device(relevant_process.get_process().uuid, vec![device_address]).unwrap();
|
|
// }
|
|
|
|
// To make the pairing effective, alice and bob must now spend their respective output into a new transaction
|
|
// login();
|
|
|
|
// Once we know this tx id, we can commit to the relay
|
|
}
|