wallet_gen/
wallet.rs

1/*
2 * wallet.rs
3 *
4 * Copyright 2018 Standard Mining
5 *
6 * Available to be used and modified under the terms of the MIT License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
11 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
12 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
13 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 */
15
16//! A representation of a cryptocurrency wallet. Stores in that
17//! coin's native WIF, or "wallet import format".
18
19use super::prelude::*;
20use {bitcoin, cryptonote, ethereum};
21use std::collections::HashMap;
22
23/// The actual wallet structure.
24#[derive(Debug, Clone, PartialEq)]
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26pub struct Wallet {
27    /// Which cryptocurrency this wallet is for
28    pub coin: Coin,
29
30    /// The wallet address as a human-readable string
31    pub address: String,
32
33    /// The wallet's public key in WIF format
34    pub public_key: String,
35
36    /// The wallet's private key in WIF format
37    pub private_key: String,
38
39    /// Extra fields, depending on the wallet
40    pub other: Option<HashMap<String, String>>,
41}
42
43impl Wallet {
44    /// Generates a new random wallet for the given coin.
45    /// The private key will be sourced from a source of
46    /// cryptographically-secure randomness. See OpenSSL
47    /// documentation for more details.
48    pub fn generate(coin: Coin) -> Result<Self> {
49        use self::Coin::*;
50
51        match coin {
52            Ethereum | EthereumClassic => ethereum::new_wallet(coin),
53            Monero | Aeon => cryptonote::new_wallet(coin),
54            coin if bitcoin::wif_data(coin).is_some() => bitcoin::new_wallet(coin),
55            _ => Err(Error::CoinNotSupported(coin)),
56        }
57    }
58}
59
60#[test]
61fn gen_all_wallets() {
62    use coin::COINS;
63
64    println!("Generating wallets for all coins...");
65    for coin in COINS.iter() {
66        let wallet = match Wallet::generate(*coin) {
67            Ok(wallet) => wallet,
68            Err(Error::CoinNotSupported(_)) => continue,
69            Err(e) => {
70                panic!(
71                    "Error generating wallet for {:?} ({}): {}",
72                    coin,
73                    coin.symbol(),
74                    e,
75                )
76            },
77        };
78
79        println!("Coin: {:?} ({})", coin, coin.symbol());
80        println!("Address: {}", &wallet.address);
81        println!("Public key: {}", &wallet.public_key);
82        println!("Private key: {}", &wallet.private_key);
83        if let Some(ref other) = wallet.other {
84            println!("Other: {:#?}", other);
85        }
86        println!();
87    }
88}