-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbtc_address.rs
198 lines (172 loc) · 6.49 KB
/
btc_address.rs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::api::{
AddressParam, AddressResult, BitcoinWallet, ExternalAddress, ExternalAddressParam,
};
use crate::error_handling::Result;
use crate::message_handler::encode_message;
use bitcoin::Network;
use coin_bitcoin::address::BtcAddress;
use coin_bitcoin::btcapi::{BtcXpubReq, BtcXpubRes};
use prost::Message;
pub fn get_btc_xpub(data: &[u8]) -> Result<Vec<u8>> {
let input: BtcXpubReq = BtcXpubReq::decode(data).expect("imkey_illegal_param");
let network = match input.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let xpub = BtcAddress::get_xpub(network, input.path.as_ref())?;
let address_message = BtcXpubRes { xpub };
encode_message(address_message)
}
pub fn get_address(param: &AddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let account_path = param.path.to_string();
let main_address: String;
let receive_address: String;
match param.seg_wit.to_uppercase().as_ref() {
"NONE" => {
main_address =
BtcAddress::get_address(network, format!("{}/0/0", account_path).as_str())?;
receive_address =
BtcAddress::get_address(network, format!("{}/0/1", account_path).as_str())?;
}
"P2WPKH" => {
main_address =
BtcAddress::get_segwit_address(network, format!("{}/0/0", account_path).as_str())?;
receive_address =
BtcAddress::get_segwit_address(network, format!("{}/0/1", account_path).as_str())?;
}
"BECH32" => {
main_address = BtcAddress::get_native_segwit_address(
network,
format!("{}/0/0", account_path).as_str(),
)?;
receive_address = BtcAddress::get_native_segwit_address(
network,
format!("{}/0/1", account_path).as_str(),
)?;
}
_ => {
main_address = BtcAddress::get_native_segwit_address(
network,
format!("{}/0/0", account_path).as_str(),
)?;
receive_address = BtcAddress::get_native_segwit_address(
network,
format!("{}/0/1", account_path).as_str(),
)?;
}
};
let enc_xpub = get_enc_xpub(network, param.path.as_ref())?;
let external_address = ExternalAddress {
address: receive_address,
derived_path: "0/1".to_string(),
r#type: "EXTERNAL".to_string(),
};
let address_message = BitcoinWallet {
path: param.path.to_owned(),
chain_type: param.chain_type.to_string(),
address: main_address,
enc_x_pub: enc_xpub,
external_address: Some(external_address),
};
encode_message(address_message)
}
pub fn calc_external_address(param: &ExternalAddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let account_path = param.path.to_string();
let external_path = format!("{}/0/{}", account_path, param.external_idx);
let receive_address: String;
match param.seg_wit.to_uppercase().as_ref() {
"NONE" => {
receive_address = BtcAddress::get_address(network, external_path.as_str())?;
}
"P2WPKH" => {
receive_address = BtcAddress::get_segwit_address(network, external_path.as_str())?;
}
"BECH32" => {
receive_address =
BtcAddress::get_native_segwit_address(network, external_path.as_str())?;
}
_ => {
receive_address =
BtcAddress::get_native_segwit_address(network, external_path.as_str())?;
}
};
let external_address = ExternalAddress {
address: receive_address,
derived_path: format!("0/{}", param.external_idx),
r#type: "EXTERNAL".to_string(),
};
encode_message(external_address)
}
pub fn get_enc_xpub(network: Network, path: &str) -> Result<String> {
let xpub = BtcAddress::get_xpub(network, path)?;
let key = common::XPUB_COMMON_KEY_128.read();
let iv = common::XPUB_COMMON_IV.read();
let key_bytes = hex::decode(&*key)?;
let iv_bytes = hex::decode(&*iv)?;
let encrypted = common::aes::cbc::encrypt_pkcs7(&xpub.as_bytes(), &key_bytes, &iv_bytes)?;
Ok(base64::encode(&encrypted))
}
pub fn register_btc_address(param: &AddressParam) -> Result<Vec<u8>> {
match param.seg_wit.to_uppercase().as_ref() {
"NONE" => display_btc_legacy_address(param),
"P2WPKH" => display_segwit_address(param),
"BECH32" => display_native_segwit_address(param),
_ => display_native_segwit_address(param),
}
}
pub fn display_btc_legacy_address(param: &AddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let path = format!("{}/0/0", param.path);
let address = BtcAddress::display_address(network, &path)?;
let address_message = AddressResult {
address,
path: param.path.to_string(),
chain_type: param.chain_type.to_string(),
};
encode_message(address_message)
}
pub fn display_segwit_address(param: &AddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let path = format!("{}/0/0", param.path);
let address = BtcAddress::display_segwit_address(network, &path)?;
let address_message = AddressResult {
path: param.path.to_string(),
chain_type: param.chain_type.to_string(),
address,
};
encode_message(address_message)
}
pub fn display_native_segwit_address(param: &AddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let path = format!("{}/0/0", param.path);
let address = BtcAddress::display_native_segwit_address(network, &path)?;
let address_message = AddressResult {
path: param.path.to_string(),
chain_type: param.chain_type.to_string(),
address,
};
encode_message(address_message)
}