Skip to content

Commit

Permalink
fix(wallet): allow generating transactions without persisting change …
Browse files Browse the repository at this point in the history
…addresses
  • Loading branch information
aesedepece committed Jul 11, 2023
1 parent d76e7d5 commit cdb646b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
8 changes: 7 additions & 1 deletion wallet/src/actors/app/handlers/create_data_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct CreateDataReqRequest {
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
fee: Fee,
fee_type: Option<FeeType>,
#[serde(default)]
preview: bool,
}

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

let f = fut::result(validated).and_then(move |request, slf: &mut Self, _ctx| {
let params = types::DataReqParams { request, fee };
let params = types::DataReqParams {
request,
fee,
preview: msg.preview,
};

slf.create_data_req(&msg.session_id, &msg.wallet_id, params)
.map_ok(
Expand Down
3 changes: 3 additions & 0 deletions wallet/src/actors/app/handlers/create_vtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct CreateVttRequest {
utxo_strategy: UtxoSelectionStrategy,
#[serde(default)]
selected_utxos: HashSet<OutputPointer>,
#[serde(default)]
preview: bool,
}

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

act.create_vtt(&msg.session_id, &msg.wallet_id, params)
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/actors/worker/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl Worker {
let address = if external {
wallet.gen_external_address(label)?
} else {
wallet.gen_internal_address(label)?
wallet.gen_internal_address(label, false)?
};

Ok(address)
Expand Down
32 changes: 25 additions & 7 deletions wallet/src/repository/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,14 @@ where
}

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

self._gen_internal_address(&mut state, label)
self._gen_internal_address(&mut state, label, preview)
}

/// Return a list of the generated external addresses that.
Expand Down Expand Up @@ -1082,6 +1086,7 @@ where
outputs,
utxo_strategy,
selected_utxos,
preview,
}: types::VttParams,
) -> Result<(model::ExtendedTransaction, AbsoluteFee)> {
let mut state = self.state.write()?;
Expand All @@ -1095,6 +1100,7 @@ where
fee,
&utxo_strategy,
selected_utxos,
preview,
)?;

let pointers_as_inputs = inputs
Expand All @@ -1118,14 +1124,18 @@ where

pub fn create_data_req(
&self,
types::DataReqParams { fee, request }: types::DataReqParams,
types::DataReqParams {
fee,
request,
preview,
}: types::DataReqParams,
) -> Result<(model::ExtendedTransaction, AbsoluteFee)> {
let mut state = self.state.write()?;
let TransactionComponents {
fee,
inputs,
outputs,
} = self.create_dr_transaction_components(&mut state, request.clone(), fee)?;
} = self.create_dr_transaction_components(&mut state, request.clone(), fee, preview)?;

let pointers_as_inputs = inputs
.pointers
Expand Down Expand Up @@ -1190,6 +1200,7 @@ where
fee: Fee,
utxo_strategy: &UtxoSelectionStrategy,
selected_utxos: HashSet<model::OutPtr>,
preview: bool,
) -> Result<TransactionComponents> {
let timestamp = u64::try_from(get_timestamp()).unwrap();

Expand All @@ -1203,6 +1214,7 @@ where
utxo_strategy,
self.params.max_vt_weight,
selected_utxos,
preview,
)
}

Expand All @@ -1211,6 +1223,7 @@ where
state: &mut State,
request: DataRequestOutput,
fee: Fee,
preview: bool,
) -> Result<TransactionComponents> {
let utxo_strategy = UtxoSelectionStrategy::Random { from: None };
let timestamp = u64::try_from(get_timestamp()).unwrap();
Expand All @@ -1225,6 +1238,7 @@ where
&utxo_strategy,
self.params.max_dr_weight,
HashSet::default(),
preview,
)
}

Expand All @@ -1233,14 +1247,15 @@ where
&self,
state: &mut State,
force_input: Option<Input>,
preview: bool,
) -> Result<PublicKeyHash> {
let pkh = if let Some(input) = force_input {
let forced = input.output_pointer();
let key_balance = state.utxo_set.get(&forced.into()).unwrap();

key_balance.pkh
} else {
self._gen_internal_address(state, None)?.pkh
self._gen_internal_address(state, None, preview)?.pkh
};

Ok(pkh)
Expand All @@ -1263,6 +1278,7 @@ where
utxo_strategy: &UtxoSelectionStrategy,
max_weight: u32,
selected_utxos: HashSet<model::OutPtr>,
preview: bool,
) -> Result<TransactionComponents> {
let empty_hashset = HashSet::default();
let unconfirmed_transactions = if self.params.use_unconfirmed_utxos {
Expand Down Expand Up @@ -1298,6 +1314,7 @@ where
let change_pkh = self.calculate_change_address(
state,
dr_output.and_then(|_| inputs.pointers.get(0).cloned().map(Input::new)),
preview,
)?;

let mut outputs = outputs;
Expand All @@ -1318,14 +1335,15 @@ where
&self,
state: &mut State,
label: Option<String>,
preview: bool,
) -> Result<Arc<model::Address>> {
let keychain = constants::INTERNAL_KEYCHAIN;
let account = state.account;
let index = state.next_internal_index;
let parent_key = &state.keychains[keychain as usize];

let (address, next_index) =
self.derive_and_persist_address(label, parent_key, account, keychain, index, true)?;
self.derive_and_persist_address(label, parent_key, account, keychain, index, !preview)?;

state.next_internal_index = next_index;

Expand Down Expand Up @@ -1569,7 +1587,7 @@ where
state.transient_external_addresses.remove(&addr.pkh);
}
for _ in state.next_internal_index..new_internal_index {
let addr = self._gen_internal_address(&mut state, None)?;
let addr = self._gen_internal_address(&mut state, None, false)?;
state.transient_internal_addresses.remove(&addr.pkh);
}

Expand Down
2 changes: 2 additions & 0 deletions wallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ pub struct VttParams {
pub outputs: Vec<ValueTransferOutput>,
pub utxo_strategy: UtxoSelectionStrategy,
pub selected_utxos: HashSet<model::OutPtr>,
pub preview: bool,
}

pub struct DataReqParams {
pub fee: Fee,
pub request: DataRequestOutput,
pub preview: bool,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down

0 comments on commit cdb646b

Please sign in to comment.