redgold_keys/
lib.rs

1#![allow(unused_imports)]
2
3use crate::util::keys::ToPublicKeyFromLib;
4use crate::util::mnemonic_support::MnemonicSupport;
5use bdk::bitcoin::hashes::hex::ToHex;
6use bdk::bitcoin::secp256k1::Secp256k1;
7use redgold_schema::keys::words_pass::WordsPass;
8use redgold_schema::structs::{Address, Hash};
9use redgold_schema::{structs, ErrorInfoContext, RgResult};
10use std::str::FromStr;
11
12pub mod proof_support;
13pub mod request_support;
14pub mod transaction_support;
15pub mod util;
16pub mod debug;
17pub mod xpub_wrapper;
18pub mod address_external;
19pub mod address_support;
20pub mod hw_wallet_wrapper;
21pub mod tx_proof_validate;
22pub mod public_key_parse_support;
23pub mod external_tx_support;
24pub mod solana;
25pub mod monero;
26pub mod gpg;
27pub mod word_pass_support;
28pub mod eth;
29pub mod btc;
30pub mod yubikey;
31
32pub struct TestConstants {
33    pub secret: bdk::bitcoin::secp256k1::SecretKey,
34    pub public: bdk::bitcoin::secp256k1::PublicKey,
35    pub secret2: bdk::bitcoin::secp256k1::SecretKey,
36    pub public2: bdk::bitcoin::secp256k1::PublicKey,
37    pub hash_vec: Vec<u8>,
38    pub address_1: Address,
39    pub rhash_1: Hash,
40    pub rhash_2: Hash,
41    pub words: String,
42    pub words_pass: WordsPass,
43}
44
45impl TestConstants {
46
47    pub fn dev_ci_kp_path() -> String {
48        "m/84'/0'/0'/0/0".to_string()
49    }
50
51
52    pub fn test_words() -> Option<String> {
53        std::env::var("REDGOLD_TEST_WORDS").ok()
54    }
55
56    pub fn test_words_pass() -> Option<WordsPass> {
57        Self::test_words().map(|w| WordsPass::new(w, None))
58    }
59
60    pub fn key_pair(&self) -> KeyPair {
61        KeyPair {
62            secret_key: self.secret,
63            public_key: self.public,
64        }
65    }
66
67    pub fn new() -> TestConstants {
68        let result = WordsPass::from_str_hashed("test_constants");
69        let kp_default = result.default_kp().expect("");
70        let (secret, public) = (kp_default.secret_key, kp_default.public_key);
71        let kp2 = result.keypair_at_change(1).expect("");
72        let (secret2, public2) = (kp2.secret_key, kp2.public_key);
73        let hash_vec = Hash::from_string_calculate("asdf1").vec();
74        let addr = Address::from_struct_public(&public.to_struct_public_key()).expect("");
75        // let addr2 = Address::from_struct_public(&public2.to_struct_public_key()).expect("")
76        return TestConstants {
77            secret,
78            public,
79            secret2,
80            public2,
81            hash_vec,
82            address_1: addr,
83            rhash_1: Hash::from_string_calculate("asdf"),
84            rhash_2: Hash::from_string_calculate("asdf2"),
85            words: "abuse lock pledge crowd pair become ridge alone target viable black plate ripple sad tape victory blood river gloom air crash invite volcano release".to_string(),
86            words_pass: result
87        };
88    }
89}
90
91#[derive(Clone, Copy, PartialEq)]
92pub struct KeyPair {
93    pub secret_key: bdk::bitcoin::secp256k1::SecretKey,
94    pub public_key: bdk::bitcoin::secp256k1::PublicKey,
95}
96
97
98impl KeyPair {
99    pub fn new(
100        secret_key: &bdk::bitcoin::secp256k1::SecretKey,
101        public_key: &bdk::bitcoin::secp256k1::PublicKey,
102    ) -> Self {
103        return Self {
104            secret_key: *secret_key,
105            public_key: *public_key,
106        };
107    }
108
109    pub fn address_typed(&self) -> Address {
110        self.public_key().address().expect("")
111    }
112
113    pub fn public_key(&self) -> structs::PublicKey {
114        self.public_key.to_struct_public_key()
115    }
116
117    pub fn from_private_hex(hex: String) -> RgResult<Self> {
118        let secret_key = bdk::bitcoin::secp256k1::SecretKey::from_str(&*hex)
119            .error_info("Unable to parse private key hex")?;
120        let public_key = bdk::bitcoin::secp256k1::PublicKey::from_secret_key(&Secp256k1::new(), &secret_key);
121        return Ok(Self {
122            secret_key,
123            public_key,
124        });
125    }
126    pub fn to_private_hex(&self) -> String {
127        self.secret_key.secret_bytes().to_hex()
128    }
129}
130
131#[test]
132fn debug_addr() {
133    let tc = TestConstants::new();
134    let kp = tc.key_pair();
135    let addr = kp.address_typed();
136    println!("addr: {:?}", addr);
137    println!("addr: {}", addr.render_string().expect(""));
138    println!("addr: {}", addr.raw_bytes().expect("").to_hex());
139}