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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use epic_wallet_rust_lib::{
    get_wallet_secret_key_pair,
    _build_subscribe_request,
    build_post_slate_request,
    process_received_slates,
    decrypt_epicbox_slates,
    get_epicbox_address,
    EpicBoxConfig,
    open_wallet,
    tx_create,
    tx_post,
    txs_get
};

use pyo3::prelude::*;
use serde_json;


#[pyfunction]
fn build_post_slate_request_py(
    config: String, receiver_address: String,
    slate: String, epicbox_config: String, password: String
) -> PyResult<String> {

    //TODO: epic-box cfg changes, keep an eye
    let box_cfg = serde_json::from_str::<EpicBoxConfig>(&epicbox_config);
    // let box_cfg = EpicBoxConfig::from_str(&epicbox_config).unwrap();

    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let key_pair =
        get_wallet_secret_key_pair(&wallet, Some(keychain.unwrap()), 0);

    let slate_msg = build_post_slate_request(
        &receiver_address.to_string(),
        key_pair.unwrap(),
        slate.to_string(),
        box_cfg.unwrap()
    );
    Ok(slate_msg)
}


#[pyfunction]
fn build_subscribe_request_py(
    config: String, epicbox_config: String, password: String) -> PyResult<String> {

    //TODO: cfg changes, keep an eye
    let box_cfg = serde_json::from_str::<EpicBoxConfig>(&epicbox_config).unwrap();
    // let box_cfg = EpicBoxConfig::from_str(&epicbox_config).unwrap();

    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let key_pair =
        get_wallet_secret_key_pair(&wallet, Some(keychain.unwrap()), 0);
    let subscribe_request =
        _build_subscribe_request(key_pair.unwrap(), box_cfg);
    Ok(subscribe_request)
}


#[pyfunction]
fn decrypt_slates_py(config: String, password: String, encrypted_slates: String) -> PyResult<Vec<String>> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let key_pair =
        get_wallet_secret_key_pair(&wallet, Some(keychain.unwrap()), 0);
    let decrypted_slates =
        decrypt_epicbox_slates(key_pair.unwrap(), &encrypted_slates);
    Ok(decrypted_slates.unwrap())
}


#[pyfunction]
fn process_slate_py(config: String, password: String, slate: String) -> PyResult<String> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();

    let processed_slates = process_received_slates(
        &wallet, Some(keychain.unwrap()), &slate);
    Ok(processed_slates.unwrap())
}


#[pyfunction]
fn get_txs_py(config: String, password: String) -> PyResult<String> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let txs = txs_get(&wallet, keychain, true);
    Ok(txs.unwrap())
}


#[pyfunction]
fn create_slate_py(config: String, password: String, args: (u64, u64, bool)) -> PyResult<String> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let create =
        tx_create(&wallet, keychain, args.0,
                  args.1, args.2).unwrap();

    Ok(create)
}


#[pyfunction]
fn tx_post_py(config: String, password: String, tx_slate_id: &str) -> PyResult<String> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let post_tx = tx_post(&wallet, keychain, tx_slate_id);

    Ok(post_tx.unwrap())
}


#[pyfunction]
fn get_epicbox_address_py(config: String, password: String, domain: String, port: u16) -> PyResult<String> {
    let (wallet, keychain) =
        open_wallet(&config, &password.to_string()).unwrap();
    let key_pair =
        get_wallet_secret_key_pair(&wallet, Some(keychain.unwrap()), 0);
    let address =
        get_epicbox_address(key_pair.unwrap().1, &domain, Some(port));

    Ok(address.to_string())
}

// wrap _py function and make them available as python package
#[pymodule]
fn epic_wallet_rust_python(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(build_post_slate_request_py, m)?)?;
    m.add_function(wrap_pyfunction!(build_subscribe_request_py, m)?)?;
    m.add_function(wrap_pyfunction!(get_epicbox_address_py, m)?)?;
    m.add_function(wrap_pyfunction!(decrypt_slates_py, m)?)?;
    m.add_function(wrap_pyfunction!(process_slate_py, m)?)?;
    m.add_function(wrap_pyfunction!(create_slate_py, m)?)?;
    m.add_function(wrap_pyfunction!(tx_post_py, m)?)?;
    m.add_function(wrap_pyfunction!(get_txs_py, m)?)?;
    Ok(())
}