ark/test/
dummy.rs

1use std::str::FromStr;
2
3use bitcoin::{Amount, OutPoint, Txid, TxIn, TxOut, Transaction, Witness, ScriptBuf, Sequence};
4use bitcoin::absolute::LockTime;
5use bitcoin::hashes::Hash;
6use bitcoin::transaction::Version;
7use bitcoin::secp256k1::Keypair;
8
9use bitcoin_ext::{BlockHeight, BlockDelta};
10
11use crate::Vtxo;
12use crate::board::BoardBuilder;
13
14lazy_static! {
15	pub static ref DUMMY_USER_KEY: Keypair = Keypair::from_str(
16		"76f78cc00278817fe65fd81cb962782d2625834d08b66edbf2cd60f6c520db63",
17	).unwrap();
18
19	pub static ref DUMMY_SERVER_KEY: Keypair = Keypair::from_str(
20		"f2a9eaeba1cdb8411dfda3c9ca391e4ad29938c4f0b9c0709376fc4a1cbf0e5d",
21	).unwrap();
22}
23
24/// Just a utility to write unit tests
25/// It can quickly create a vtxo that matches
26/// some desired parameters
27pub struct DummyTestVtxoSpec {
28	pub amount: Amount,
29	pub expiry_height: BlockHeight,
30	pub exit_delta: BlockDelta,
31	pub user_keypair: Keypair,
32	pub server_keypair: Keypair,
33}
34
35impl Default for DummyTestVtxoSpec {
36	fn default() -> Self {
37		Self {
38			amount: Amount::ONE_BTC,
39			expiry_height: 10_000,
40			exit_delta: 144,
41			user_keypair: *DUMMY_USER_KEY,
42			server_keypair: *DUMMY_SERVER_KEY,
43		}
44	}
45}
46
47impl DummyTestVtxoSpec {
48	pub fn build(&self) -> (Transaction, Vtxo) {
49		// The board-builder that is used by the user
50		let user_builder = BoardBuilder::new(
51			self.user_keypair.public_key(),
52			self.expiry_height,
53			self.server_keypair.public_key(),
54			self.exit_delta,
55		);
56
57		let funding_tx = Transaction {
58			version: Version(3),
59			lock_time: LockTime::ZERO,
60			input: vec![TxIn {
61				previous_output: OutPoint::null(),
62				script_sig: ScriptBuf::new(),
63				sequence: Sequence::ZERO,
64				witness: Witness::new(),
65			}],
66			output: vec![
67				TxOut {
68					value: self.amount,
69					script_pubkey: user_builder.funding_script_pubkey(),
70				}
71			],
72		};
73
74		let funding_outpoint = OutPoint::new(funding_tx.compute_txid(), 0);
75
76		let user_builder = user_builder
77			.set_funding_details(self.amount, funding_outpoint)
78			.generate_user_nonces();
79
80		let user_pub_nonce = user_builder.user_pub_nonce();
81
82		// The server builder
83		let server_builder = BoardBuilder::new_for_cosign(
84			self.user_keypair.public_key(),
85			self.expiry_height,
86			self.server_keypair.public_key(),
87			self.exit_delta,
88			self.amount,
89			funding_outpoint,
90			*user_pub_nonce,
91		);
92
93		let server_cosign_response = server_builder.server_cosign(&self.server_keypair);
94
95		let vtxo = user_builder.build_vtxo(&server_cosign_response, &self.user_keypair).unwrap();
96		(funding_tx, vtxo)
97	}
98}
99
100pub fn random_utxo() -> OutPoint {
101	OutPoint::new(Txid::from_byte_array(rand::random()), rand::random())
102}
103
104#[test]
105fn create_dummy_output() {
106	let board = DummyTestVtxoSpec {
107		amount: Amount::from_sat(1000),
108		expiry_height: 100000,
109		exit_delta: 1000,
110		user_keypair: Keypair::new(&crate::SECP, &mut bitcoin::secp256k1::rand::thread_rng()),
111		server_keypair: Keypair::new(&crate::SECP, &mut bitcoin::secp256k1::rand::thread_rng()),
112	};
113
114	board.build();
115}
116
117#[test]
118fn default_dummy_vtxo() {
119	DummyTestVtxoSpec::default().build();
120}