Skip to content

Commit 2f88ec9

Browse files
committed
ga_tx: do not rely on script_type
script_type is Green specific and cannot be set by Electrum sessions.
1 parent 78f5a31 commit 2f88ec9

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/ga_tx.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ namespace sdk {
6767
const std::string txhash = utxo.at("txhash");
6868
const auto txid = h2b_rev(txhash);
6969
const uint32_t index = utxo.at("pt_idx");
70-
const auto type = script_type(utxo.at("script_type"));
70+
const auto is_segwit = utxo.value(
71+
"is_segwit", is_segwit_script_type(utxo.value("script_type", script_type::ga_pubkey_hash_out)));
7172
const bool low_r = session.get_nonnull_signer()->supports_low_r();
7273
const uint32_t dummy_sig_type = low_r ? WALLY_TX_DUMMY_SIG_LOW_R : WALLY_TX_DUMMY_SIG;
7374
const bool external = !json_get_value(utxo, "private_key").empty();
@@ -90,7 +91,7 @@ namespace sdk {
9091

9192
wally_tx_witness_stack_ptr wit;
9293

93-
if (is_segwit_script_type(type)) {
94+
if (is_segwit) {
9495
// TODO: If the UTXO is CSV and expired, spend it using the users key only (smaller)
9596
wit = tx_witness_stack_init(4);
9697
tx_witness_stack_add_dummy(wit, WALLY_TX_DUMMY_NULL);
@@ -128,7 +129,8 @@ namespace sdk {
128129
// - 2of3 p2wsh, backup key signing
129130
// - 2of2 csv, csv path
130131
const auto type = script_type(utxo.at("script_type"));
131-
if (!is_segwit_script_type(type)) {
132+
const auto is_segwit = utxo.value("is_segwit", is_segwit_script_type(type));
133+
if (!is_segwit) {
132134
// 2of2 p2sh: script sig: OP_0 <ga_sig> <user_sig>
133135
// 2of3 p2sh: script sig: OP_0 <ga_sig> <user_sig>
134136
const auto& input = tx->inputs[index];
@@ -887,8 +889,9 @@ namespace sdk {
887889
const auto txhash = u.at("txhash");
888890
const uint32_t subaccount = json_get_value(u, "subaccount", 0u);
889891
const uint32_t pointer = json_get_value(u, "pointer", 0u);
890-
const auto type = script_type(u.at("script_type"));
891892
const bool is_internal = json_get_value(u, "is_internal", false);
893+
const auto is_segwit
894+
= u.value("is_segwit", is_segwit_script_type(u.value("script_type", script_type::ga_pubkey_hash_out)));
892895
const auto script = h2b(u.at("prevout_script"));
893896
const std::string private_key = json_get_value(u, "private_key");
894897
auto signer = session.get_nonnull_signer();
@@ -909,7 +912,7 @@ namespace sdk {
909912
const auto user_sig = signer->sign_hash(path, tx_hash);
910913
const auto der = ec_sig_to_der(user_sig, true);
911914

912-
if (is_segwit_script_type(type)) {
915+
if (is_segwit) {
913916
// TODO: If the UTXO is CSV and expired, spend it using the users key only (smaller)
914917
// Note that this requires setting the inputs sequence number to the CSV time too
915918
auto wit = tx_witness_stack_init(1);
@@ -929,10 +932,11 @@ namespace sdk {
929932
const network_parameters& net_params, const nlohmann::json& utxo, const wally_tx_ptr& tx, size_t index)
930933
{
931934
const amount::value_type v = utxo.at("satoshi");
932-
const auto type = script_type(utxo.at("script_type"));
935+
const auto is_segwit = utxo.value(
936+
"is_segwit", is_segwit_script_type(utxo.value("script_type", script_type::ga_pubkey_hash_out)));
933937
const auto script = h2b(utxo.at("prevout_script"));
934938

935-
const uint32_t flags = is_segwit_script_type(type) ? WALLY_TX_FLAG_USE_WITNESS : 0;
939+
const uint32_t flags = is_segwit ? WALLY_TX_FLAG_USE_WITNESS : 0;
936940

937941
if (!net_params.is_liquid()) {
938942
const amount satoshi{ v };
@@ -980,11 +984,12 @@ namespace sdk {
980984
{
981985
GDK_RUNTIME_ASSERT(json_get_value(u, "private_key").empty());
982986

983-
const auto type = script_type(u.at("script_type"));
987+
const auto is_segwit
988+
= u.value("is_segwit", is_segwit_script_type(u.value("script_type", script_type::ga_pubkey_hash_out)));
984989
const auto script = h2b(u.at("prevout_script"));
985990
auto der = h2b(der_hex);
986991

987-
if (is_segwit_script_type(type)) {
992+
if (is_segwit) {
988993
// See above re: spending using the users key only
989994
auto wit = tx_witness_stack_init(1);
990995
tx_witness_stack_add(wit, der);

0 commit comments

Comments
 (0)