Skip to content

Commit 6465f07

Browse files
committed
fix(wallet): allow generating transactions without persisting change addresses
1 parent 081dac6 commit 6465f07

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

wallet/src/actors/app/handlers/create_data_req.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub struct CreateDataReqRequest {
3333
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
3434
fee: Fee,
3535
fee_type: Option<FeeType>,
36+
#[serde(default)]
37+
preview: bool,
3638
}
3739

3840
#[derive(Debug, Serialize, Deserialize)]
@@ -81,7 +83,11 @@ impl Handler<CreateDataReqRequest> for app::App {
8183
let fee = fee_compat(msg.fee, msg.fee_type);
8284

8385
let f = fut::result(validated).and_then(move |request, slf: &mut Self, _ctx| {
84-
let params = types::DataReqParams { request, fee };
86+
let params = types::DataReqParams {
87+
request,
88+
fee,
89+
preview: msg.preview,
90+
};
8591

8692
slf.create_data_req(&msg.session_id, &msg.wallet_id, params)
8793
.map_ok(

wallet/src/actors/app/handlers/create_vtt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub struct CreateVttRequest {
6565
utxo_strategy: UtxoSelectionStrategy,
6666
#[serde(default)]
6767
selected_utxos: HashSet<OutputPointer>,
68+
#[serde(default)]
69+
preview: bool,
6870
}
6971

7072
/// Part of CreateVttResponse struct, containing additional data to be displayed in clients
@@ -124,6 +126,7 @@ impl Handler<CreateVttRequest> for app::App {
124126
outputs,
125127
utxo_strategy: msg.utxo_strategy.clone(),
126128
selected_utxos: msg.selected_utxos.iter().map(|x| x.into()).collect(),
129+
preview: msg.preview,
127130
};
128131

129132
act.create_vtt(&msg.session_id, &msg.wallet_id, params)

wallet/src/actors/worker/methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl Worker {
352352
let address = if external {
353353
wallet.gen_external_address(label)?
354354
} else {
355-
wallet.gen_internal_address(label)?
355+
wallet.gen_internal_address(label, false)?
356356
};
357357

358358
Ok(address)

wallet/src/repository/wallet/mod.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,14 @@ where
501501
}
502502

503503
/// Generate an address in the internal keychain (WIP-0001).
504-
pub fn gen_internal_address(&self, label: Option<String>) -> Result<Arc<model::Address>> {
504+
pub fn gen_internal_address(
505+
&self,
506+
label: Option<String>,
507+
preview: bool,
508+
) -> Result<Arc<model::Address>> {
505509
let mut state = self.state.write()?;
506510

507-
self._gen_internal_address(&mut state, label)
511+
self._gen_internal_address(&mut state, label, preview)
508512
}
509513

510514
/// Return a list of the generated external addresses that.
@@ -1082,6 +1086,7 @@ where
10821086
outputs,
10831087
utxo_strategy,
10841088
selected_utxos,
1089+
preview,
10851090
}: types::VttParams,
10861091
) -> Result<(model::ExtendedTransaction, AbsoluteFee)> {
10871092
let mut state = self.state.write()?;
@@ -1095,6 +1100,7 @@ where
10951100
fee,
10961101
&utxo_strategy,
10971102
selected_utxos,
1103+
preview,
10981104
)?;
10991105

11001106
let pointers_as_inputs = inputs
@@ -1118,14 +1124,18 @@ where
11181124

11191125
pub fn create_data_req(
11201126
&self,
1121-
types::DataReqParams { fee, request }: types::DataReqParams,
1127+
types::DataReqParams {
1128+
fee,
1129+
request,
1130+
preview,
1131+
}: types::DataReqParams,
11221132
) -> Result<(model::ExtendedTransaction, AbsoluteFee)> {
11231133
let mut state = self.state.write()?;
11241134
let TransactionComponents {
11251135
fee,
11261136
inputs,
11271137
outputs,
1128-
} = self.create_dr_transaction_components(&mut state, request.clone(), fee)?;
1138+
} = self.create_dr_transaction_components(&mut state, request.clone(), fee, preview)?;
11291139

11301140
let pointers_as_inputs = inputs
11311141
.pointers
@@ -1190,6 +1200,7 @@ where
11901200
fee: Fee,
11911201
utxo_strategy: &UtxoSelectionStrategy,
11921202
selected_utxos: HashSet<model::OutPtr>,
1203+
preview: bool,
11931204
) -> Result<TransactionComponents> {
11941205
let timestamp = u64::try_from(get_timestamp()).unwrap();
11951206

@@ -1203,6 +1214,7 @@ where
12031214
utxo_strategy,
12041215
self.params.max_vt_weight,
12051216
selected_utxos,
1217+
preview,
12061218
)
12071219
}
12081220

@@ -1211,6 +1223,7 @@ where
12111223
state: &mut State,
12121224
request: DataRequestOutput,
12131225
fee: Fee,
1226+
preview: bool,
12141227
) -> Result<TransactionComponents> {
12151228
let utxo_strategy = UtxoSelectionStrategy::Random { from: None };
12161229
let timestamp = u64::try_from(get_timestamp()).unwrap();
@@ -1225,6 +1238,7 @@ where
12251238
&utxo_strategy,
12261239
self.params.max_dr_weight,
12271240
HashSet::default(),
1241+
preview,
12281242
)
12291243
}
12301244

@@ -1233,14 +1247,15 @@ where
12331247
&self,
12341248
state: &mut State,
12351249
force_input: Option<Input>,
1250+
preview: bool,
12361251
) -> Result<PublicKeyHash> {
12371252
let pkh = if let Some(input) = force_input {
12381253
let forced = input.output_pointer();
12391254
let key_balance = state.utxo_set.get(&forced.into()).unwrap();
12401255

12411256
key_balance.pkh
12421257
} else {
1243-
self._gen_internal_address(state, None)?.pkh
1258+
self._gen_internal_address(state, None, preview)?.pkh
12441259
};
12451260

12461261
Ok(pkh)
@@ -1263,6 +1278,7 @@ where
12631278
utxo_strategy: &UtxoSelectionStrategy,
12641279
max_weight: u32,
12651280
selected_utxos: HashSet<model::OutPtr>,
1281+
preview: bool,
12661282
) -> Result<TransactionComponents> {
12671283
let empty_hashset = HashSet::default();
12681284
let unconfirmed_transactions = if self.params.use_unconfirmed_utxos {
@@ -1298,6 +1314,7 @@ where
12981314
let change_pkh = self.calculate_change_address(
12991315
state,
13001316
dr_output.and_then(|_| inputs.pointers.get(0).cloned().map(Input::new)),
1317+
preview,
13011318
)?;
13021319

13031320
let mut outputs = outputs;
@@ -1318,14 +1335,15 @@ where
13181335
&self,
13191336
state: &mut State,
13201337
label: Option<String>,
1338+
preview: bool,
13211339
) -> Result<Arc<model::Address>> {
13221340
let keychain = constants::INTERNAL_KEYCHAIN;
13231341
let account = state.account;
13241342
let index = state.next_internal_index;
13251343
let parent_key = &state.keychains[keychain as usize];
13261344

13271345
let (address, next_index) =
1328-
self.derive_and_persist_address(label, parent_key, account, keychain, index, true)?;
1346+
self.derive_and_persist_address(label, parent_key, account, keychain, index, !preview)?;
13291347

13301348
state.next_internal_index = next_index;
13311349

@@ -1569,7 +1587,7 @@ where
15691587
state.transient_external_addresses.remove(&addr.pkh);
15701588
}
15711589
for _ in state.next_internal_index..new_internal_index {
1572-
let addr = self._gen_internal_address(&mut state, None)?;
1590+
let addr = self._gen_internal_address(&mut state, None, false)?;
15731591
state.transient_internal_addresses.remove(&addr.pkh);
15741592
}
15751593

wallet/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ pub struct VttParams {
149149
pub outputs: Vec<ValueTransferOutput>,
150150
pub utxo_strategy: UtxoSelectionStrategy,
151151
pub selected_utxos: HashSet<model::OutPtr>,
152+
pub preview: bool,
152153
}
153154

154155
pub struct DataReqParams {
155156
pub fee: Fee,
156157
pub request: DataRequestOutput,
158+
pub preview: bool,
157159
}
158160

159161
#[derive(Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)