Skip to content

Commit 8d74316

Browse files
committed
Support explicit change outputs
Setting the `is_change` flag on an addressee when creating a transaction routes all change to that output. The default behaviour is to generate a change address for the wallet. Only one explicit change address per asset is allowed. If an explicit change address is specified for a given asset it follows that there will not be a generated change address for that asset. The tx output in the transaction json corresponding to the explicit change output will be flagged with `is_change`, just like a generated change address output, however the `change_index` will be set to -1. IOW `change_index` should be interpreted as the index of the generated (implied) change output, if there is one.
1 parent 3c39def commit 8d74316

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/ga_tx.cpp

+36-8
Original file line numberDiff line numberDiff line change
@@ -749,11 +749,7 @@ namespace sdk {
749749
// so compute what we can send (everything minus the
750750
// fee) and exit the loop
751751
required_total = available_total - fee;
752-
if (is_liquid) {
753-
set_tx_output_commitment(net_params, tx, 0, asset_tag, required_total.value());
754-
} else {
755-
tx->outputs[0].satoshi = required_total.value();
756-
}
752+
set_tx_output_value(net_params, tx, 0, asset_tag, required_total.value());
757753
if (num_addressees == 1u) {
758754
addressees_p->at(0)["satoshi"] = required_total.value();
759755
}
@@ -809,9 +805,41 @@ namespace sdk {
809805
continue;
810806
}
811807

812-
// We have more than the dust amount of change. Add a change
813-
// output to collect it, then loop again in case the amount
814-
// this increases the fee by requires more UTXOs.
808+
// We have more than the dust amount of change. First look for an explicit change
809+
// output in the addressees and if present send the change there
810+
amount::value_type change_amount = (total - required_total - fee).value();
811+
bool have_explicit_change = false;
812+
if (num_addressees) {
813+
size_t addressee_index = 0;
814+
for (auto& addressee : *addressees_p) {
815+
const auto addressee_asset_tag
816+
= session.asset_id_from_string(addressee.value("asset_tag", std::string{}));
817+
if (addressee_asset_tag == asset_tag) {
818+
if (json_get_value(addressee, "is_change", false)) {
819+
if (have_explicit_change) {
820+
set_tx_error(result, "Only one explicit change addressee allowed");
821+
break;
822+
}
823+
// There is an assumption that the tx outputs are indexed so they match
824+
// addressees, which is very weakly enforced but needs to be true otherwise
825+
// other things (liquid blinding) breaks.
826+
set_tx_output_value(net_params, tx, addressee_index, asset_tag, change_amount);
827+
addressee["satoshi"] = change_amount;
828+
have_explicit_change = true;
829+
required_total += change_amount;
830+
}
831+
}
832+
++addressee_index;
833+
}
834+
}
835+
836+
if (have_explicit_change) {
837+
// No need for implicit change output
838+
continue;
839+
}
840+
841+
// No explicit change output specified, generate a change output paying back to
842+
// the wallet.
815843
add_tx_output(net_params, result, tx, result.at("change_address").at(asset_tag).at("address"),
816844
is_liquid ? 1 : 0, asset_tag == "btc" ? std::string{} : asset_tag);
817845
have_change_output = true;

src/transaction_utils.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ namespace sdk {
355355
}
356356

357357
// Transactions with outputs below the dust threshold (except OP_RETURN)
358-
// are not relayed by network nodes
359-
if (!result.value("send_all", false) && satoshi.value() < session.get_dust_threshold()) {
358+
// are not relayed by network nodes. send_all and explicit change outputs
359+
// have amounts set to zero because they are calculated
360+
if (!result.value("send_all", false) && !json_get_value(addressee, "is_change", false)
361+
&& satoshi.value() < session.get_dust_threshold()) {
360362
result["error"] = res::id_invalid_amount;
361363
}
362364

@@ -367,6 +369,17 @@ namespace sdk {
367369
net_params, result, tx, address, satoshi.value(), addressee.value("asset_tag", std::string{}));
368370
}
369371

372+
void set_tx_output_value(const network_parameters& net_params, wally_tx_ptr& tx, uint32_t index,
373+
const std::string& asset_tag, amount::value_type satoshi)
374+
{
375+
const bool is_liquid = net_params.liquid();
376+
if (is_liquid) {
377+
set_tx_output_commitment(net_params, tx, index, asset_tag, satoshi);
378+
} else {
379+
tx->outputs[index].satoshi = satoshi;
380+
}
381+
}
382+
370383
void update_tx_size_info(const wally_tx_ptr& tx, nlohmann::json& result)
371384
{
372385
const bool valid = tx->num_inputs != 0u && tx->num_outputs != 0u;
@@ -473,6 +486,14 @@ namespace sdk {
473486
if (net_params.liquid()) {
474487
output["public_key"] = blinding_key_from_addr(address);
475488
}
489+
const bool is_change = json_get_value(addressee, "is_change", false);
490+
if (is_change) {
491+
// This is an explicit change output (as opposed to one generated by the gdk)
492+
// Mark with 'is_change'
493+
// Can be distinguished from a generated change output because the change_index
494+
// will be set to -1
495+
output["is_change"] = true;
496+
}
476497
++addressee_index;
477498
}
478499

src/transaction_utils.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ namespace sdk {
8484
amount add_tx_addressee(ga_session& session, const network_parameters& net_params, nlohmann::json& result,
8585
wally_tx_ptr& tx, nlohmann::json& addressee);
8686

87+
void set_tx_output_value(const network_parameters& net_params, wally_tx_ptr& tx, uint32_t index,
88+
const std::string& asset_tag, amount::value_type satoshi);
89+
8790
vbf_t generate_final_vbf(byte_span_t input_abfs, byte_span_t input_vbfs, uint64_span_t input_values,
8891
const std::vector<abf_t>& output_abfs, const std::vector<vbf_t>& output_vbfs, uint32_t num_inputs);
8992

0 commit comments

Comments
 (0)