2024-06-06 18:52:39 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2024-06-18 11:19:36 +01:00
|
|
|
use bft_json_crdt::json_crdt::SignedOp;
|
2024-06-18 17:43:32 +01:00
|
|
|
use bitcoin::{absolute, key::Keypair, transaction::Version, TxIn, TxOut};
|
2024-06-07 18:42:28 +01:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
2024-06-18 16:32:32 +01:00
|
|
|
pub(crate) const BITCOIN_KEY_FILE: &str = "bitcoin_keys.pem";
|
2024-06-18 16:00:02 +01:00
|
|
|
pub(crate) const BFT_CRDT_KEY_FILE: &str = "keys.pem";
|
2024-06-06 18:52:39 +01:00
|
|
|
pub(crate) const CONFIG_FILE: &str = "config.toml";
|
|
|
|
|
|
2024-06-07 17:03:05 +01:00
|
|
|
/// Returns the path to the key file and config for this host OS.
|
2024-06-18 16:32:32 +01:00
|
|
|
pub(crate) fn side_paths(prefix: PathBuf) -> (PathBuf, PathBuf, PathBuf) {
|
|
|
|
|
let mut bft_crdt_key_path = prefix.clone();
|
|
|
|
|
bft_crdt_key_path.push(BFT_CRDT_KEY_FILE);
|
|
|
|
|
|
|
|
|
|
let mut bitcoin_key_path = prefix.clone();
|
|
|
|
|
bitcoin_key_path.push(BITCOIN_KEY_FILE);
|
2024-06-06 18:52:39 +01:00
|
|
|
|
|
|
|
|
let mut config_path = prefix.clone();
|
|
|
|
|
config_path.push(CONFIG_FILE);
|
|
|
|
|
|
2024-06-18 16:32:32 +01:00
|
|
|
(bft_crdt_key_path, bitcoin_key_path, config_path)
|
2024-06-06 18:52:39 +01:00
|
|
|
}
|
2024-06-07 17:03:05 +01:00
|
|
|
|
|
|
|
|
pub(crate) fn home(name: &String) -> std::path::PathBuf {
|
|
|
|
|
let mut path = dirs::home_dir().unwrap();
|
|
|
|
|
path.push(".side");
|
|
|
|
|
path.push(name);
|
|
|
|
|
path
|
|
|
|
|
}
|
2024-06-07 18:42:28 +01:00
|
|
|
|
|
|
|
|
/// Generate a fake transaction with customizable from_pubkey String
|
2024-06-18 17:12:05 +01:00
|
|
|
pub fn fake_generic_transaction_json(from: String) -> Value {
|
2024-06-07 18:42:28 +01:00
|
|
|
json!({
|
2024-06-17 15:25:22 +01:00
|
|
|
"from": from,
|
2024-06-07 18:42:28 +01:00
|
|
|
"to": "Bob",
|
|
|
|
|
"amount": 1
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-06-18 11:19:36 +01:00
|
|
|
|
2024-06-18 17:43:32 +01:00
|
|
|
/// Generate a Bitcoin transaction from this node's bitcoin_keys
|
|
|
|
|
pub fn fake_bitcoin_transaction(key_pair: Keypair) -> bitcoin::Transaction {
|
|
|
|
|
let from = key_pair.public_key().to_string();
|
|
|
|
|
let to = "Bob";
|
|
|
|
|
let amount = 1;
|
|
|
|
|
let lock_time = absolute::LockTime::from_height(0).expect("couldn't format btc lock time");
|
|
|
|
|
let input = TxIn {
|
|
|
|
|
previous_output: todo!(),
|
|
|
|
|
script_sig: todo!(),
|
|
|
|
|
sequence: todo!(),
|
|
|
|
|
witness: todo!(),
|
|
|
|
|
};
|
|
|
|
|
let output = TxOut {
|
|
|
|
|
value: todo!(),
|
|
|
|
|
script_pubkey: todo!(),
|
|
|
|
|
};
|
|
|
|
|
let tx = bitcoin::Transaction {
|
|
|
|
|
version: Version(1),
|
|
|
|
|
lock_time,
|
|
|
|
|
input,
|
|
|
|
|
output,
|
|
|
|
|
};
|
|
|
|
|
tx
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 11:19:36 +01:00
|
|
|
pub fn shappy(op: SignedOp) -> String {
|
|
|
|
|
let b = serde_json::to_string(&op).unwrap().into_bytes();
|
|
|
|
|
sha256::digest(b).to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn shassy(text: String) -> String {
|
|
|
|
|
let b = text.into_bytes();
|
|
|
|
|
sha256::digest(b).to_string()
|
|
|
|
|
}
|