1#[macro_use]
10extern crate tracing;
11
12mod cashnotes;
13mod error;
14mod genesis;
15mod transfers;
16mod wallet;
17
18pub use cashnotes::{
20    CashNote, DerivationIndex, DerivedSecretKey, Hash, MainPubkey, MainSecretKey, NanoTokens,
21    SignedSpend, Spend, SpendAddress, SpendReason, UniquePubkey,
22};
23pub use error::{Result, TransferError};
24pub use genesis::{
26    calculate_royalties_fee, create_first_cash_note_from_key, get_faucet_data_dir, get_genesis_sk,
27    is_genesis_spend, load_genesis_wallet, Error as GenesisError, GENESIS_CASHNOTE,
28    GENESIS_INPUT_DERIVATION_INDEX, GENESIS_OUTPUT_DERIVATION_INDEX, GENESIS_PK,
29    GENESIS_SPEND_UNIQUE_KEY, TOTAL_SUPPLY,
30};
31pub use transfers::{CashNoteRedemption, SignedTransaction, Transfer, UnsignedTransaction};
32pub use wallet::{
33    bls_secret_from_hex, wallet_lockfile_name, Error as WalletError, HotWallet, Payment,
34    PaymentQuote, QuotingMetrics, Result as WalletResult, WalletApi, WatchOnlyWallet,
35    QUOTE_EXPIRATION_SECS, WALLET_DIR_NAME,
36};
37
38use bls::SecretKey;
39use lazy_static::lazy_static;
40
41const DEFAULT_FOUNDATION_PK_STR: &str = "8f73b97377f30bed96df1c92daf9f21b4a82c862615439fab8095e68860a5d0dff9f97dba5aef503a26c065e5cb3c7ca"; const DEFAULT_NETWORK_ROYALTIES_STR: &str = "b4243ec9ceaec374ef992684cd911b209758c5de53d1e406b395bc37ebc8ce50e68755ea6d32da480ae927e1af4ddadb"; const DEFAULT_PAYMENT_FORWARD_STR: &str = "a585839f0502713a0ed6a327f3bd0c301f9e8fe298c93dd00ed7869d8e6804244f0d3014e90df45cd344a7ccd702865c"; const DEFAULT_PAYMENT_FORWARD_SK_STR: &str =
51    "49113d2083f57a976076adbe85decb75115820de1e6e74b47e0429338cef124a"; lazy_static! {
54    pub static ref FOUNDATION_PK: MainPubkey = {
55        let compile_time_key = option_env!("FOUNDATION_PK").unwrap_or(DEFAULT_FOUNDATION_PK_STR);
56        let runtime_key =
57            std::env::var("FOUNDATION_PK").unwrap_or_else(|_| compile_time_key.to_string());
58
59        if runtime_key == DEFAULT_FOUNDATION_PK_STR {
60            warn!("Using default FOUNDATION_PK: {}", DEFAULT_FOUNDATION_PK_STR);
61        } else if runtime_key == compile_time_key {
62            warn!("Using compile-time FOUNDATION_PK: {}", compile_time_key);
63        } else {
64            warn!("Overridden by runtime FOUNDATION_PK: {}", runtime_key);
65        }
66
67        match MainPubkey::from_hex(&runtime_key) {
68            Ok(pk) => pk,
69            Err(err) => panic!("Failed to parse foundation PK: {err:?}"),
70        }
71    };
72}
73
74lazy_static! {
75    pub static ref NETWORK_ROYALTIES_PK: MainPubkey = {
76        let compile_time_key =
77            option_env!("NETWORK_ROYALTIES_PK").unwrap_or(DEFAULT_NETWORK_ROYALTIES_STR);
78        let runtime_key =
79            std::env::var("NETWORK_ROYALTIES_PK").unwrap_or_else(|_| compile_time_key.to_string());
80
81        if runtime_key == DEFAULT_NETWORK_ROYALTIES_STR {
82            warn!(
83                "Using default NETWORK_ROYALTIES_PK: {}",
84                DEFAULT_NETWORK_ROYALTIES_STR
85            );
86        } else if runtime_key == compile_time_key {
87            warn!(
88                "Using compile-time NETWORK_ROYALTIES_PK: {}",
89                compile_time_key
90            );
91        } else {
92            warn!(
93                "Overridden by runtime NETWORK_ROYALTIES_PK: {}",
94                runtime_key
95            );
96        }
97
98        match MainPubkey::from_hex(&runtime_key) {
99            Ok(pk) => pk,
100            Err(err) => panic!("Failed to parse network royalties PK: {err:?}"),
101        }
102    };
103    pub static ref DEFAULT_NETWORK_ROYALTIES_PK: MainPubkey = {
104        match MainPubkey::from_hex(DEFAULT_NETWORK_ROYALTIES_STR) {
105            Ok(pk) => pk,
106            Err(err) => panic!("Failed to parse default network royalties PK: {err:?}"),
107        }
108    };
109}
110
111lazy_static! {
112    pub static ref PAYMENT_FORWARD_PK: MainPubkey = {
113        let compile_time_key =
114            option_env!("PAYMENT_FORWARD_PK").unwrap_or(DEFAULT_PAYMENT_FORWARD_STR);
115        let runtime_key =
116            std::env::var("PAYMENT_FORWARD_PK").unwrap_or_else(|_| compile_time_key.to_string());
117
118        if runtime_key == DEFAULT_PAYMENT_FORWARD_STR {
119            warn!(
120                "Using default PAYMENT_FORWARD_PK: {}",
121                DEFAULT_PAYMENT_FORWARD_STR
122            );
123        } else if runtime_key == compile_time_key {
124            warn!(
125                "Using compile-time PAYMENT_FORWARD_PK: {}",
126                compile_time_key
127            );
128        } else {
129            warn!("Overridden by runtime PAYMENT_FORWARD_PK: {}", runtime_key);
130        }
131
132        match MainPubkey::from_hex(&runtime_key) {
133            Ok(pk) => pk,
134            Err(err) => panic!("Failed to parse payment forward PK: {err:?}"),
135        }
136    };
137    pub static ref DEFAULT_PAYMENT_FORWARD_SK: SecretKey = {
138        match SecretKey::from_hex(DEFAULT_PAYMENT_FORWARD_SK_STR) {
139            Ok(sk) => sk,
140            Err(err) => panic!("Failed to parse default payment forward SK: {err:?}"),
141        }
142    };
143}
144
145pub use bls::{self, rand, Ciphertext, Signature};
147
148pub mod rng {
153    use crate::rand::{
154        rngs::{StdRng, ThreadRng},
155        SeedableRng,
156    };
157    use tiny_keccak::{Hasher, Sha3};
158
159    pub fn thread_rng() -> ThreadRng {
160        crate::rand::thread_rng()
161    }
162
163    pub fn from_seed(seed: <StdRng as SeedableRng>::Seed) -> StdRng {
164        StdRng::from_seed(seed)
165    }
166
167    pub fn from_vec(vec: &[u8]) -> StdRng {
170        let mut sha3 = Sha3::v256();
171        sha3.update(vec);
172        let mut hash = [0u8; 32];
173        sha3.finalize(&mut hash);
174
175        from_seed(hash)
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182    use crate::rng::from_vec;
183
184    #[test]
185    fn confirm_generating_same_key() {
186        let rng_seed = b"testing generating same key";
187        let content = b"some context to try with";
188
189        let mut rng_1 = from_vec(rng_seed);
190        let reward_key_1 = MainSecretKey::random_from_rng(&mut rng_1);
191        let sig = reward_key_1.sign(content);
192
193        let mut rng_2 = from_vec(rng_seed);
194        let reward_key_2 = MainSecretKey::random_from_rng(&mut rng_2);
195
196        assert!(reward_key_2.main_pubkey().verify(&sig, content));
197    }
198}