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
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use serde::{Deserialize, Serialize};
#[cfg(all(feature = "events", any(feature = "ledger_nano", feature = "ledger_nano")))]
use crate::wallet::events::types::{AddressData, WalletEvent};
use crate::{
client::secret::{GenerateAddressOptions, SecretManage, SecretManager},
wallet::account::{
handle::AccountHandle,
types::address::{AccountAddress, AddressWrapper},
},
};
/// Options for address generation
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct AddressGenerationOptions {
pub internal: bool,
pub options: Option<GenerateAddressOptions>,
}
impl AccountHandle {
/// Generate addresses and stores them in the account
/// ```ignore
/// let public_addresses = account_handle.generate_addresses(2, None).await?;
/// // internal addresses are used for remainder outputs, if the RemainderValueStrategy for transactions is set to ChangeAddress
/// let internal_addresses = account_handle
/// .generate_addresses(
/// 1,
/// Some(AddressGenerationOptions {
/// internal: true,
/// ..Default::default()
/// }),
/// )
/// .await?;
/// ```
pub async fn generate_addresses(
&self,
amount: u32,
options: Option<AddressGenerationOptions>,
) -> crate::wallet::Result<Vec<AccountAddress>> {
let options = options.unwrap_or_default();
log::debug!(
"[ADDRESS GENERATION] generating {amount} addresses, internal: {}",
options.internal
);
if amount == 0 {
return Ok(vec![]);
}
let account = self.read().await;
// get the highest index for the public or internal addresses
let highest_current_index_plus_one = if options.internal {
account.internal_addresses.len() as u32
} else {
account.public_addresses.len() as u32
};
// get bech32_hrp
let bech32_hrp = {
match account.public_addresses.first() {
Some(address) => address.address.bech32_hrp.to_string(),
None => self.client.get_bech32_hrp().await?,
}
};
let address_range = highest_current_index_plus_one..highest_current_index_plus_one + amount;
let addresses = match &*self.secret_manager.read().await {
#[cfg(feature = "ledger_nano")]
SecretManager::LedgerNano(ledger_nano) => {
// If we don't sync, then we want to display the prompt on the ledger with the address. But the user
// needs to have it visible on the computer first, so we need to generate it without the
// prompt first
if options.options.clone().unwrap_or_default().ledger_nano_prompt {
#[cfg(feature = "events")]
let changed_options = options.options.clone().map(|mut options| {
// Change options so ledger will not show the prompt the first time
options.ledger_nano_prompt = false;
options
});
let mut addresses = Vec::new();
for address_index in address_range {
#[cfg(feature = "events")]
{
// Generate without prompt to be able to display it
let address = ledger_nano
.generate_addresses(
account.coin_type,
account.index,
address_index..address_index + 1,
options.internal,
changed_options.clone(),
)
.await?;
self.event_emitter.lock().await.emit(
account.index,
WalletEvent::LedgerAddressGeneration(AddressData {
address: address[0].to_bech32(bech32_hrp.clone()),
}),
);
}
// Generate with prompt so the user can verify
let address = ledger_nano
.generate_addresses(
account.coin_type,
account.index,
address_index..address_index + 1,
options.internal,
options.options.clone(),
)
.await?;
addresses.push(address[0]);
}
addresses
} else {
ledger_nano
.generate_addresses(
account.coin_type,
account.index,
address_range.clone(),
options.internal,
options.options,
)
.await?
}
}
#[cfg(feature = "stronghold")]
SecretManager::Stronghold(stronghold) => {
stronghold
.generate_addresses(
account.coin_type,
account.index,
address_range,
options.internal,
options.options.clone(),
)
.await?
}
SecretManager::Mnemonic(mnemonic) => {
mnemonic
.generate_addresses(
account.coin_type,
account.index,
address_range,
options.internal,
options.options.clone(),
)
.await?
}
SecretManager::Placeholder(_) => vec![],
};
drop(account);
let generate_addresses: Vec<AccountAddress> = addresses
.into_iter()
.enumerate()
.map(|(index, address)| AccountAddress {
address: AddressWrapper::new(address, bech32_hrp.clone()),
key_index: highest_current_index_plus_one + index as u32,
internal: options.internal,
used: false,
})
.collect();
self.update_account_addresses(options.internal, generate_addresses.clone())
.await?;
Ok(generate_addresses)
}
/// Generate an internal address and store in the account, internal addresses are used for remainder outputs
pub(crate) async fn generate_remainder_address(&self) -> crate::wallet::Result<AccountAddress> {
let result = self
.generate_addresses(
1,
Some(AddressGenerationOptions {
internal: true,
..Default::default()
}),
)
.await?
.first()
.ok_or(crate::wallet::Error::FailedToGetRemainder)?
.clone();
Ok(result)
}
}