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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! sender and issuer features input selection
use std::collections::HashSet;
use crypto::keys::slip10::Chain;
use crate::{
client::{
api::{
address::search_address,
block_builder::input_selection::core::{
error::Error as InputSelectionError, requirement::alias::is_alias_transition,
},
ClientBlockBuilder,
},
constants::HD_WALLET_TYPE,
secret::types::InputSigningData,
Error, Result,
},
types::block::{
address::Address,
output::{dto::OutputDto, feature::Features, AliasOutput, NftOutput, Output, OutputMetadata},
},
};
impl<'a> ClientBlockBuilder<'a> {
pub(crate) async fn get_inputs_for_sender_and_issuer(
&self,
utxo_chain_inputs: &[InputSigningData],
) -> Result<Vec<InputSigningData>> {
log::debug!("[get_inputs_for_sender_and_issuer]");
let mut required_inputs = Vec::new();
let bech32_hrp = self.client.get_bech32_hrp().await?;
let current_time = self.client.get_time_checked().await?;
let token_supply = self.client.get_token_supply().await?;
let required_sender_or_issuer_addresses =
get_required_addresses_for_sender_and_issuer(&[], &self.outputs, current_time)?;
for sender_or_issuer_address in required_sender_or_issuer_addresses {
match sender_or_issuer_address {
Address::Ed25519(_) => {
// Check if the address is derived from the seed
let (address_index, internal) = search_address(
self.secret_manager.ok_or(Error::MissingParameter("secret manager"))?,
&bech32_hrp,
self.coin_type,
self.account_index,
self.input_range.clone(),
&sender_or_issuer_address,
)
.await?;
let address_outputs = self
.basic_address_outputs(sender_or_issuer_address.to_bech32(&bech32_hrp))
.await?;
let mut found_output = false;
for output_response in address_outputs {
let output = Output::try_from_dto(&output_response.output, token_supply)?;
// We can ignore the unlocked_alias_or_nft_address, since we only requested basic outputs
let (required_unlock_address, _unlocked_alias_or_nft_address) = output
.required_and_unlocked_address(
current_time,
&output_response.metadata.output_id()?,
None,
)?;
if required_unlock_address == sender_or_issuer_address {
required_inputs.push(InputSigningData {
output,
output_metadata: OutputMetadata::try_from(&output_response.metadata)?,
chain: Some(Chain::from_u32_hardened(vec![
HD_WALLET_TYPE,
self.coin_type,
self.account_index,
internal as u32,
address_index,
])),
});
found_output = true;
break;
}
}
if !found_output {
return Err(InputSelectionError::MissingInputWithEd25519Address)?;
}
}
Address::Alias(alias_address) => {
// Check if output is alias address.
let alias_id = alias_address.alias_id();
// check if already found or request new.
if !utxo_chain_inputs.iter().chain(required_inputs.iter()).any(|input| {
if let Output::Alias(alias_output) = &input.output {
*alias_id == alias_output.alias_id_non_null(input.output_id())
} else {
false
}
}) {
let output_id = self.client.alias_output_id(*alias_id).await?;
let output_response = self.client.get_output(&output_id).await?;
if let OutputDto::Alias(alias_output_dto) = &output_response.output {
let alias_output = AliasOutput::try_from_dto(alias_output_dto, token_supply)?;
// State transition if we add them to inputs
let unlock_address = alias_output.state_controller_address();
let address_index_internal = match self.secret_manager {
Some(secret_manager) => {
match unlock_address {
Address::Ed25519(_) => Some(
search_address(
secret_manager,
&bech32_hrp,
self.coin_type,
self.account_index,
self.input_range.clone(),
unlock_address,
)
.await?,
),
// Alias and NFT addresses can't be generated from a private key
_ => None,
}
}
// Assuming default for offline signing
None => Some((0, false)),
};
required_inputs.push(InputSigningData {
output: Output::try_from_dto(&output_response.output, token_supply)?,
output_metadata: OutputMetadata::try_from(&output_response.metadata)?,
chain: address_index_internal.map(|(address_index, internal)| {
Chain::from_u32_hardened(vec![
HD_WALLET_TYPE,
self.coin_type,
self.account_index,
internal as u32,
address_index,
])
}),
});
}
}
}
Address::Nft(nft_address) => {
// Check if output is nft address.
let nft_id = nft_address.nft_id();
// Check if already found or request new.
if !utxo_chain_inputs.iter().chain(required_inputs.iter()).any(|input| {
if let Output::Nft(nft_output) = &input.output {
nft_id == nft_output.nft_id()
} else {
false
}
}) {
let output_id = self.client.nft_output_id(*nft_id).await?;
let output_response = self.client.get_output(&output_id).await?;
if let OutputDto::Nft(nft_output) = &output_response.output {
let nft_output = NftOutput::try_from_dto(nft_output, token_supply)?;
let unlock_address = nft_output
.unlock_conditions()
.locked_address(nft_output.address(), current_time);
let address_index_internal = match self.secret_manager {
Some(secret_manager) => {
match unlock_address {
Address::Ed25519(_) => Some(
search_address(
secret_manager,
&bech32_hrp,
self.coin_type,
self.account_index,
self.input_range.clone(),
unlock_address,
)
.await?,
),
// Alias and NFT addresses can't be generated from a private key.
_ => None,
}
}
// Assuming default for offline signing.
None => Some((0, false)),
};
required_inputs.push(InputSigningData {
output: Output::try_from_dto(&output_response.output, token_supply)?,
output_metadata: OutputMetadata::try_from(&output_response.metadata)?,
chain: address_index_internal.map(|(address_index, internal)| {
Chain::from_u32_hardened(vec![
HD_WALLET_TYPE,
self.coin_type,
self.account_index,
internal as u32,
address_index,
])
}),
});
}
}
}
}
}
// Check required Alias and NFT outputs with new added outputs.
// No need to check for sender and issuer again, since these outputs already exist and we don't set new features
// for them.
let utxo_chain_inputs = self
.get_utxo_chains_inputs(required_inputs.iter().map(|i| &i.output))
.await?;
required_inputs.extend(utxo_chain_inputs.into_iter());
Ok(required_inputs)
}
}
// Returns required addresses for sender and issuer features that aren't already unlocked with the selected_inputs
fn get_required_addresses_for_sender_and_issuer(
selected_inputs: &[InputSigningData],
outputs: &Vec<Output>,
current_time: u32,
) -> crate::client::Result<HashSet<Address>> {
log::debug!("[get_required_addresses_for_sender_and_issuer]");
// Addresses in the inputs that will be unlocked in the transaction
let mut unlocked_addresses = HashSet::new();
for input_signing_data in selected_inputs {
let alias_transition = is_alias_transition(input_signing_data, outputs);
let (required_unlock_address, unlocked_alias_or_nft_address) =
input_signing_data.output.required_and_unlocked_address(
current_time,
input_signing_data.output_id(),
alias_transition.map(|(alias_transition, _)| alias_transition),
)?;
unlocked_addresses.insert(required_unlock_address);
if let Some(unlocked_alias_or_nft_address) = unlocked_alias_or_nft_address {
unlocked_addresses.insert(unlocked_alias_or_nft_address);
}
}
let mut required_sender_or_issuer_addresses = HashSet::new();
for output in outputs {
if let Some(sender_feature) = output.features().and_then(Features::sender) {
if !required_sender_or_issuer_addresses.contains(sender_feature.address()) {
// Only add if not already present in the selected inputs.
if !unlocked_addresses.contains(sender_feature.address()) {
required_sender_or_issuer_addresses.insert(*sender_feature.address());
}
}
}
// Issuer address only needs to be unlocked when the utxo chain is newly created.
let utxo_chain_creation = match &output {
Output::Alias(alias_output) => alias_output.alias_id().is_null(),
Output::Nft(nft_output) => nft_output.nft_id().is_null(),
_ => false,
};
if utxo_chain_creation {
if let Some(issuer_feature) = output.immutable_features().and_then(Features::issuer) {
if !required_sender_or_issuer_addresses.contains(issuer_feature.address()) {
// Only add if not already present in the selected inputs.
if !unlocked_addresses.contains(issuer_feature.address()) {
required_sender_or_issuer_addresses.insert(*issuer_feature.address());
}
}
}
}
}
Ok(required_sender_or_issuer_addresses)
}