1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

use crate::base::wallet::config::*;
use crate::base::wallet::keypair::*;
use crate::base::wallet::address::WalletAddress;
use crate::base::wallet::seed::Seed;

use crate::WalletType;

//WalletBuilder
#[derive(Debug)]
pub struct WalletBuilder <'a> {
    pub config: &'a WalletConfig,
}

impl <'a> WalletBuilder <'a> {
    pub fn new(config: &'a WalletConfig) -> Self {
        WalletBuilder {
            config: config,
        }
    }

    pub fn build(&self) -> Wallet {
        //seed
        let seed = Seed::build(&self.config.key_type);

        //keypair
        let key_pair = KeypairBuilder::new(&seed, &self.config.key_type).build().unwrap();

        //address
        let address = WalletAddress::build(&key_pair);

        Wallet {
            key_type: self.config.key_type,
            address : address,
            secret  : seed,
            keypair : key_pair,
        }
    }
}

#[derive(Debug)]
pub struct Wallet {
    pub key_type: WalletType,
    pub address : String,
    pub secret  : String,
    pub keypair : Keypair,
}

impl Wallet {
    pub fn new(wtype: WalletType) -> Self {
        let config = WalletConfig::new(wtype);
        WalletBuilder::new(&config).build()
    }
}