Skip to content

Commit dd02648

Browse files
committed
Rename asset_tag in addressee JSON to asset_id
Remove asset_id_from_string from the session interface and replace with asset_id_from_json. Fixes gdk internal naming to avoid confusing asset_tag and asset_id. asset_tag: May be empty, or is a 01-prefixed non-blinded asset hash or otherwise a prefixed blinded asset commitment. Only present in JSON when returned from the Green server in utxos or transaction data endpoints. (When present in returned data it is unblinded into asset_id as needed). asset_id: May be empty, "btc" or the the policy asset hash (All of which represent the policy asset), or alternately an unblinded asset hash. There remain places where the treatment of an asset_id value may not take into account its various representations, these should be standardised in due course following further interface simplification.
1 parent 988695d commit dd02648

File tree

8 files changed

+54
-52
lines changed

8 files changed

+54
-52
lines changed

src/ga_auth_handlers.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,7 @@ namespace sdk {
10561056

10571057
if (!m_hw_device.empty()) {
10581058
// Use the blinding key returned by the HW
1059-
const std::string asset_tag = it.key();
1060-
addr["blinding_key"] = args.at("blinding_keys").at(asset_tag);
1059+
addr["blinding_key"] = args.at("blinding_keys").at(it.key());
10611060
}
10621061

10631062
auto& address = addr.at("address");

src/ga_rust.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ namespace sdk {
438438
if (bip21_params.contains("amount") && !bip21_params.contains("assetid")) {
439439
throw std::runtime_error("in liquid amount without assetid is not valid"); // fixme return error
440440
} else if (bip21_params.contains("assetid")) {
441-
addressee["asset_tag"] = bip21_params["assetid"];
441+
addressee["asset_id"] = bip21_params["assetid"];
442442
}
443443
}
444444
}

src/ga_session.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ namespace sdk {
27142714
const bool is_relevant = json_get_value(ep, "is_relevant", false);
27152715

27162716
if (is_relevant && ep.find("error") == ep.end()) {
2717-
const auto asset_id = asset_id_from_string(ep.value("asset_id", std::string{}));
2717+
const auto asset_id = asset_id_from_json(m_net_params, ep);
27182718
unique_asset_ids.emplace(asset_id);
27192719

27202720
// Compute the effect of the input/output on the wallets balance
@@ -3031,8 +3031,8 @@ namespace sdk {
30313031
const bool confidential_utxo = is_liquid && utxo.at("confidential");
30323032
// Either return all or only confidential UTXOs
30333033
if (!confidential_only || confidential_utxo) {
3034-
const auto utxo_asset_tag = asset_id_from_string(utxo.value("asset_id", std::string{}));
3035-
asset_utxos[utxo_asset_tag].emplace_back(utxo);
3034+
const auto utxo_asset_id = asset_id_from_json(m_net_params, utxo);
3035+
asset_utxos[utxo_asset_id].emplace_back(utxo);
30363036
}
30373037
}
30383038
}

src/ga_session.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ namespace sdk {
213213
std::vector<unsigned char> output_script_from_utxo(const nlohmann::json& utxo);
214214
std::vector<pub_key_t> pubkeys_from_utxo(const nlohmann::json& utxo);
215215

216-
std::string asset_id_from_string(const std::string& tag)
217-
{
218-
return tag.empty() || tag == m_net_params.policy_asset() ? "btc" : tag;
219-
}
220-
221216
private:
222217
void reset();
223218

src/ga_tx.cpp

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ namespace sdk {
547547
std::vector<nlohmann::json> used_utxos;
548548
used_utxos.reserve(utxos.size());
549549

550-
std::set<std::string> asset_tags;
550+
std::set<std::string> asset_ids;
551551
bool addressees_have_assets = json_get_value(result, "addressees_have_assets", false);
552552
if (num_addressees) {
553553
for (auto& addressee : *addressees_p) {
@@ -571,15 +571,15 @@ namespace sdk {
571571
addressees_have_assets = true;
572572

573573
if (assetid_hex == net_params.policy_asset()) {
574-
addressee["asset_tag"] = "btc";
574+
addressee["asset_id"] = "btc";
575575
} else {
576-
addressee["asset_tag"] = assetid_hex;
576+
addressee["asset_id"] = assetid_hex;
577577
}
578578
}
579579
}
580580

581-
asset_tags.insert(session.asset_id_from_string(addressee.value("asset_tag", "btc")));
582-
if (asset_tags.size() > 1 && net_params.is_main_net()) {
581+
asset_ids.insert(asset_id_from_json(net_params, addressee));
582+
if (asset_ids.size() > 1 && net_params.is_main_net()) {
583583
// Multi-asset send disabled in (liquid) mainnet
584584
GDK_LOG_SEV(log_level::error) << "Multi-asset send not supported";
585585
GDK_RUNTIME_ASSERT(false);
@@ -590,8 +590,8 @@ namespace sdk {
590590

591591
std::vector<nlohmann::json> reordered_addressees;
592592

593-
auto create_tx_outputs = [&](const std::string& asset_tag) {
594-
const bool include_fee = asset_tag == "btc";
593+
auto create_tx_outputs = [&](const std::string& asset_id) {
594+
const bool include_fee = asset_id == "btc";
595595

596596
std::vector<nlohmann::json> current_used_utxos;
597597
amount available_total, total, fee, v;
@@ -612,9 +612,8 @@ namespace sdk {
612612

613613
if (num_addressees) {
614614
for (auto& addressee : *addressees_p) {
615-
const auto addressee_asset_tag
616-
= session.asset_id_from_string(addressee.value("asset_tag", std::string{}));
617-
if (addressee_asset_tag == asset_tag) {
615+
const auto addressee_asset_id = asset_id_from_json(net_params, addressee);
616+
if (addressee_asset_id == asset_id) {
618617
required_total += add_tx_addressee(session, net_params, result, tx, addressee);
619618
reordered_addressees.push_back(addressee);
620619
}
@@ -633,13 +632,13 @@ namespace sdk {
633632
} else {
634633
// Collect utxos in order until we have covered the amount to send
635634
// FIXME: Better coin selection algorithms (esp. minimum size)
636-
const auto asset_utxos_p = utxos.find(asset_tag);
635+
const auto asset_utxos_p = utxos.find(asset_id);
637636
if (asset_utxos_p == utxos.end()) {
638637
if (!is_rbf) {
639638
set_tx_error(result, res::id_insufficient_funds); // Insufficient funds
640639
}
641640
} else {
642-
for (auto& utxo : utxos.at(asset_tag)) {
641+
for (auto& utxo : utxos.at(asset_id)) {
643642
if (send_all || total < required_total) {
644643
v = add_utxo(session, tx, utxo);
645644
total += v;
@@ -685,7 +684,7 @@ namespace sdk {
685684
bool change_address = result.find("change_address") != result.end();
686685
if (change_address) {
687686
const auto asset_change_address
688-
= result.at("change_address").value(asset_tag, nlohmann::json::object());
687+
= result.at("change_address").value(asset_id, nlohmann::json::object());
689688
change_address = !asset_change_address.empty();
690689
}
691690
if (!change_address) {
@@ -708,7 +707,7 @@ namespace sdk {
708707
}
709708

710709
add_paths(session, change_address);
711-
result["change_address"][asset_tag] = change_address;
710+
result["change_address"][asset_id] = change_address;
712711
}
713712

714713
const size_t max_loop_iterations
@@ -723,10 +722,10 @@ namespace sdk {
723722
if (is_liquid) {
724723
constexpr amount::value_type dummy_amount = 1;
725724
if (!have_fee_output) {
726-
if (send_all && addressees_p->at(0).value("asset_tag", "btc") == asset_tag) {
725+
if (send_all && addressees_p->at(0).value("asset_id", "btc") == asset_id) {
727726
// the output commitment will be corrected below. this is a placeholder for the
728727
// blinding.
729-
set_tx_output_commitment(net_params, tx, 0, asset_tag, dummy_amount);
728+
set_tx_output_commitment(net_params, tx, 0, asset_id, dummy_amount);
730729
}
731730
fee_index = add_tx_fee_output(net_params, tx, dummy_amount);
732731
have_fee_output = true;
@@ -745,7 +744,7 @@ namespace sdk {
745744
fee += network_fee;
746745
}
747746

748-
if (send_all && addressees_p->at(0).value("asset_tag", "btc") == asset_tag) {
747+
if (send_all && addressees_p->at(0).value("asset_id", "btc") == asset_id) {
749748
if (available_total < fee + dust_threshold) {
750749
// After paying the fee, we only have dust left, so
751750
// the requested amount isn't payable
@@ -756,7 +755,7 @@ namespace sdk {
756755
// fee) and exit the loop
757756
required_total = available_total - fee;
758757
if (is_liquid) {
759-
set_tx_output_commitment(net_params, tx, 0, asset_tag, required_total.value());
758+
set_tx_output_commitment(net_params, tx, 0, asset_id, required_total.value());
760759
} else {
761760
tx->outputs[0].satoshi = required_total.value();
762761
}
@@ -773,14 +772,14 @@ namespace sdk {
773772
// need to add more to avoid a dusty change output
774773
force_add_utxo = false;
775774
if (manual_selection || utxos.empty()
776-
|| current_used_utxos.size() == utxos.at(asset_tag).size()) {
775+
|| current_used_utxos.size() == utxos.at(asset_id).size()) {
777776
// Used all inputs and do not have enough funds
778777
set_tx_error(result, res::id_insufficient_funds); // Insufficient funds
779778
goto leave_loop;
780779
}
781780

782781
// FIXME: Use our strategy here when non-default implemented
783-
auto& utxo = utxos.at(asset_tag).at(current_used_utxos.size());
782+
auto& utxo = utxos.at(asset_id).at(current_used_utxos.size());
784783
total += add_utxo(session, tx, utxo);
785784
current_used_utxos.emplace_back(utxo);
786785
continue;
@@ -818,16 +817,16 @@ namespace sdk {
818817
// We have more than the dust amount of change. Add a change
819818
// output to collect it, then loop again in case the amount
820819
// this increases the fee by requires more UTXOs.
821-
add_tx_output(net_params, result, tx, result.at("change_address").at(asset_tag).at("address"),
822-
is_liquid ? 1 : 0, asset_tag == "btc" ? std::string{} : asset_tag);
820+
add_tx_output(net_params, result, tx, result.at("change_address").at(asset_id).at("address"),
821+
is_liquid ? 1 : 0, asset_id == "btc" ? std::string{} : asset_id);
823822
have_change_output = true;
824823
change_index = tx->num_outputs - 1;
825824
if (is_liquid && include_fee) {
826825
std::swap(tx->outputs[fee_index], tx->outputs[change_index]);
827826
std::swap(fee_index, change_index);
828827
}
829-
result["have_change"][asset_tag] = have_change_output;
830-
result["change_index"][asset_tag] = change_index;
828+
result["have_change"][asset_id] = have_change_output;
829+
result["change_index"][asset_id] = change_index;
831830
}
832831

833832
used_utxos.insert(used_utxos.end(), std::begin(current_used_utxos), std::end(current_used_utxos));
@@ -843,7 +842,7 @@ namespace sdk {
843842
// Set the change amount
844843
change_amount = (total - required_total - fee).value();
845844
if (is_liquid) {
846-
set_tx_output_commitment(net_params, tx, change_index, asset_tag, change_amount);
845+
set_tx_output_commitment(net_params, tx, change_index, asset_id, change_amount);
847846
} else {
848847
auto& change_output = tx->outputs[change_index];
849848
change_output.satoshi = change_amount;
@@ -856,14 +855,14 @@ namespace sdk {
856855
}
857856
}
858857
// TODO: change amount should be liquid specific (blinded)
859-
result["change_amount"][asset_tag] = change_amount;
860-
result["change_index"][asset_tag] = change_index;
858+
result["change_amount"][asset_id] = change_amount;
859+
result["change_index"][asset_id] = change_index;
861860
};
862861

863862
update_change_output(fee);
864863

865864
if (include_fee && is_liquid) {
866-
set_tx_output_commitment(net_params, tx, fee_index, asset_tag, fee.value());
865+
set_tx_output_commitment(net_params, tx, fee_index, asset_id, fee.value());
867866
}
868867

869868
if (required_total == 0 && (!include_fee || !is_liquid)) {
@@ -874,8 +873,8 @@ namespace sdk {
874873
}
875874

876875
result["used_utxos"] = used_utxos;
877-
result["have_change"][asset_tag] = have_change_output;
878-
result["satoshi"][asset_tag] = required_total.value();
876+
result["have_change"][asset_id] = have_change_output;
877+
result["satoshi"][asset_id] = required_total.value();
879878

880879
update_tx_info(net_params, tx, result);
881880

@@ -895,9 +894,9 @@ namespace sdk {
895894
};
896895

897896
if (is_liquid) {
898-
std::for_each(std::begin(asset_tags), std::end(asset_tags), [&](const auto& asset_tag) {
899-
if (asset_tag != "btc") {
900-
create_tx_outputs(asset_tag);
897+
std::for_each(std::begin(asset_ids), std::end(asset_ids), [&](const auto& id) {
898+
if (id != "btc") {
899+
create_tx_outputs(id);
901900
}
902901
});
903902
}

src/swig_python/contrib/gdk_example_liquid_amp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def get_wallet_transactions(self):
336336
def send_to_address(self, sat_amount, asset_id, destination_address):
337337
details = {
338338
'subaccount': self.subaccount_pointer,
339-
'addressees': [{'satoshi': sat_amount, 'address': destination_address, 'asset_tag': asset_id}]
339+
'addressees': [{'satoshi': sat_amount, 'address': destination_address, 'asset_id': asset_id}]
340340
}
341341

342342
try:

src/transaction_utils.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ namespace sdk {
219219
return script;
220220
}
221221

222+
std::string asset_id_from_json(const network_parameters& net_params, const nlohmann::json& json)
223+
{
224+
std::string id = json_get_value(json, "asset_id");
225+
return id.empty() || id == net_params.policy_asset() ? "btc" : id;
226+
}
227+
222228
std::vector<unsigned char> input_script(
223229
bool low_r, const std::vector<unsigned char>& prevout_script, const ecdsa_sig_t& user_sig)
224230
{
@@ -273,14 +279,14 @@ namespace sdk {
273279
}
274280

275281
amount add_tx_output(const network_parameters& net_params, nlohmann::json& result, wally_tx_ptr& tx,
276-
const std::string& address, amount::value_type satoshi, const std::string& asset_tag)
282+
const std::string& address, amount::value_type satoshi, const std::string& asset_id)
277283
{
278284
std::vector<unsigned char> script = output_script_for_address(net_params, address, result);
279285

280286
if (net_params.is_liquid()) {
281287
const auto ct_value = tx_confidential_value_from_satoshi(satoshi);
282288
const auto asset_bytes
283-
= h2b_rev(!asset_tag.empty() && asset_tag != "btc" ? asset_tag : net_params.policy_asset(), 0x1);
289+
= h2b_rev(!asset_id.empty() && asset_id != "btc" ? asset_id : net_params.policy_asset(), 0x1);
284290
tx_add_elements_raw_output(tx, script, asset_bytes, ct_value, {}, {}, {});
285291
} else {
286292
tx_add_raw_output(tx, satoshi, script);
@@ -298,10 +304,10 @@ namespace sdk {
298304
}
299305

300306
void set_tx_output_commitment(const network_parameters& net_params, wally_tx_ptr& tx, uint32_t index,
301-
const std::string& asset_tag, amount::value_type satoshi)
307+
const std::string& asset_id, amount::value_type satoshi)
302308
{
303309
const auto ct_value = tx_confidential_value_from_satoshi(satoshi);
304-
const auto asset_bytes = h2b_rev(asset_tag == "btc" ? net_params.policy_asset() : asset_tag, 0x1);
310+
const auto asset_bytes = h2b_rev(asset_id == "btc" ? net_params.policy_asset() : asset_id, 0x1);
305311
tx_elements_output_commitment_set(tx, index, asset_bytes, ct_value, {}, {}, {});
306312
}
307313

@@ -362,7 +368,7 @@ namespace sdk {
362368
addressee["satoshi"] = satoshi.value(); // Sets to 0 if not present
363369

364370
return add_tx_output(
365-
net_params, result, tx, address, satoshi.value(), addressee.value("asset_tag", std::string{}));
371+
net_params, result, tx, address, satoshi.value(), asset_id_from_json(net_params, addressee));
366372
}
367373

368374
void update_tx_size_info(const wally_tx_ptr& tx, nlohmann::json& result)

src/transaction_utils.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ namespace sdk {
4848
std::vector<unsigned char> output_script_from_utxo(const network_parameters& net_params, ga_pubkeys& pubkeys,
4949
user_pubkeys& usr_pubkeys, user_pubkeys& recovery_pubkeys, const nlohmann::json& utxo);
5050

51+
// Returns the asset id, or "btc" if it matches the networks policy asset
52+
std::string asset_id_from_json(const network_parameters& net_params, const nlohmann::json& json);
53+
5154
// Make a multisig scriptSig
5255
std::vector<unsigned char> input_script(bool low_r, const std::vector<unsigned char>& prevout_script,
5356
const ecdsa_sig_t& user_sig, const ecdsa_sig_t& ga_sig);
@@ -72,13 +75,13 @@ namespace sdk {
7275

7376
// Add an output to a tx given its address
7477
amount add_tx_output(const network_parameters& net_params, nlohmann::json& result, wally_tx_ptr& tx,
75-
const std::string& address, amount::value_type satoshi = 0, const std::string& asset_tag = {});
78+
const std::string& address, amount::value_type satoshi = 0, const std::string& asset_id = {});
7679

7780
// Add a fee output to a tx, returns the index in tx->outputs
7881
size_t add_tx_fee_output(const network_parameters& net_params, wally_tx_ptr& tx, amount::value_type satoshi);
7982

8083
void set_tx_output_commitment(const network_parameters& net_params, wally_tx_ptr& tx, uint32_t index,
81-
const std::string& asset_tag, amount::value_type satoshi);
84+
const std::string& asset_id, amount::value_type satoshi);
8285

8386
// Add an output from a JSON addressee
8487
amount add_tx_addressee(ga_session& session, const network_parameters& net_params, nlohmann::json& result,

0 commit comments

Comments
 (0)