1#![doc = "Deterministic, non-mainnet wallet qualification fixtures."]
2#![forbid(unsafe_code)]
3
4use hns_wallet_chain_api::Preimage;
5use hns_wallet_market::{SwapSession, TimeoutPlan, VerifiedEvidence, VerifiedQuote};
6use hns_wallet_types::{Amount, ModuleId, ObjectHash, SessionId, WalletAsset};
7use serde::{Deserialize, Serialize};
8use serde_json::{Value, json};
9use sha2::{Digest, Sha256};
10
11pub const TEST_MNEMONIC: &str =
12 "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
13pub const TEST_NOW_UNIX: u64 = 1_800_000_000;
14
15#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum DeterministicNetwork {
18 HandshakeRegtest,
19 BitcoinRegtest,
20 EthereumLocalDevelopment,
21}
22
23#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
24pub struct QualificationEvidence {
25 pub unit_tests_passed: bool,
26 pub restart_tests_passed: bool,
27 pub reorg_tests_passed: bool,
28 pub refund_tests_passed: bool,
29 pub malicious_input_tests_passed: bool,
30 pub real_network_demonstration_passed: bool,
31 pub storage_benchmark_recorded: bool,
32 pub bandwidth_benchmark_recorded: bool,
33 pub independent_security_review_complete: bool,
34}
35
36impl QualificationEvidence {
37 pub const fn unit_only() -> Self {
38 Self {
39 unit_tests_passed: true,
40 restart_tests_passed: false,
41 reorg_tests_passed: false,
42 refund_tests_passed: false,
43 malicious_input_tests_passed: true,
44 real_network_demonstration_passed: false,
45 storage_benchmark_recorded: false,
46 bandwidth_benchmark_recorded: false,
47 independent_security_review_complete: false,
48 }
49 }
50
51 pub const fn permits_mainnet(&self) -> bool {
52 self.unit_tests_passed
53 && self.restart_tests_passed
54 && self.reorg_tests_passed
55 && self.refund_tests_passed
56 && self.malicious_input_tests_passed
57 && self.real_network_demonstration_passed
58 && self.storage_benchmark_recorded
59 && self.bandwidth_benchmark_recorded
60 && self.independent_security_review_complete
61 }
62}
63
64pub fn deterministic_preimage() -> Preimage {
65 Preimage::new([42; 32])
66}
67
68pub fn deterministic_hashlock() -> ObjectHash {
69 ObjectHash::new(Sha256::digest([42; 32]).into())
70}
71
72pub fn hns_btc_session() -> SwapSession {
73 SwapSession::new(
74 SessionId::new([2; 32]),
75 ModuleId::Handshake,
76 ModuleId::Bitcoin,
77 VerifiedQuote {
78 terms_id: ObjectHash::new([3; 32]),
79 offered: Amount::new(WalletAsset::Hns, 1_000_000),
80 received: Amount::new(WalletAsset::Btc, 10_000),
81 valid_until_unix: TEST_NOW_UNIX + 600,
82 },
83 deterministic_hashlock(),
84 TimeoutPlan {
85 first_chain_refund_at: TEST_NOW_UNIX + 14_400,
86 second_chain_refund_at: TEST_NOW_UNIX + 7_200,
87 minimum_safety_margin: 3_600,
88 },
89 TEST_NOW_UNIX,
90 )
91 .expect("valid deterministic HNS/BTC session")
92}
93
94pub fn verified_funding_evidence(tag: u8) -> VerifiedEvidence {
95 VerifiedEvidence::FirstFundingConfirmed {
96 evidence: ObjectHash::new([tag; 32]),
97 }
98}
99
100pub fn hostile_provider_requests() -> Vec<Value> {
101 vec![
102 json!({"method": "wallet_exportSeed", "params": {}}),
103 json!({"method": "eth_sendTransaction", "params": {"data": "0xdeadbeef"}}),
104 json!({"method": "wallet_addEthereumChain", "params": {}}),
105 json!({"method": "wallet_signRawTransaction", "params": {}}),
106 json!({"method": "nativeHost_execute", "params": {"command": "sh"}}),
107 ]
108}
109
110pub fn malformed_wire_messages() -> Vec<Vec<u8>> {
111 vec![
112 Vec::new(),
113 vec![0xff],
114 vec![0; 1_048_577],
115 b"{\"version\":999999999999999999999999}".to_vec(),
116 ]
117}
118
119#[derive(Clone, Debug, Eq, PartialEq)]
120pub struct ReorgFixture {
121 pub old_tip: u64,
122 pub common_ancestor: u64,
123 pub replacement_tip: u64,
124 pub orphaned_evidence: Vec<ObjectHash>,
125}
126
127pub fn shallow_reorg_fixture() -> ReorgFixture {
128 ReorgFixture {
129 old_tip: 110,
130 common_ancestor: 107,
131 replacement_tip: 112,
132 orphaned_evidence: vec![ObjectHash::new([9; 32]), ObjectHash::new([10; 32])],
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn unit_evidence_never_enables_mainnet() {
142 assert!(!QualificationEvidence::unit_only().permits_mainnet());
143 }
144
145 #[test]
146 fn fixtures_are_non_mainnet_and_deterministic() {
147 assert_eq!(deterministic_hashlock(), deterministic_hashlock());
148 assert_eq!(hns_btc_session(), hns_btc_session());
149 assert_eq!(shallow_reorg_fixture().common_ancestor, 107);
150 }
151
152 #[test]
153 fn hostile_corpus_includes_key_and_generic_evm_requests() {
154 let methods: Vec<_> = hostile_provider_requests()
155 .into_iter()
156 .map(|value| value["method"].as_str().unwrap().to_owned())
157 .collect();
158 assert!(methods.contains(&"wallet_exportSeed".to_owned()));
159 assert!(methods.contains(&"eth_sendTransaction".to_owned()));
160 }
161}