Skip to content

Commit 86a0d6e

Browse files
committed
tx: remove the overloading of tx_to_bytes to also generate signature hashes
Now that signature hashes are generated by tx_io, this logic can be reverted to just the tx serialization.
1 parent 62e9abc commit 86a0d6e

File tree

1 file changed

+23
-71
lines changed

1 file changed

+23
-71
lines changed

src/transaction.c

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,33 +2018,22 @@ int wally_tx_get_hash_prevouts(const struct wally_tx *tx,
20182018
}
20192019

20202020
static int tx_to_bytes(const struct wally_tx *tx,
2021-
const struct tx_serialize_opts *opts,
20222021
uint32_t flags,
20232022
unsigned char *bytes_out, size_t len,
20242023
size_t *written,
20252024
bool is_elements)
20262025
{
20272026
size_t n, i, j, witness_count;
2028-
const unsigned char sighash = opts ? opts->sighash : 0;
2029-
const bool sh_anyonecanpay = sighash & WALLY_SIGHASH_ANYONECANPAY;
2030-
const bool sh_rangeproof = sighash & WALLY_SIGHASH_RANGEPROOF;
2031-
const bool sh_none = (sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_NONE;
2032-
const bool sh_single = (sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_SINGLE;
20332027
unsigned char *p = bytes_out;
20342028

2035-
(void)sh_rangeproof;
2036-
20372029
if (written)
20382030
*written = 0;
20392031

20402032
if (!is_valid_tx(tx) ||
20412033
(flags & ~WALLY_TX_ALL_FLAGS) || !bytes_out || !written ||
2042-
tx_get_length(tx, opts, flags, &n, is_elements) != WALLY_OK)
2034+
tx_get_length(tx, NULL, flags, &n, is_elements) != WALLY_OK)
20432035
return WALLY_EINVAL;
20442036

2045-
if (opts && (flags & WALLY_TX_FLAG_USE_WITNESS))
2046-
return WALLY_ERROR; /* Segwit tx hashing is handled elsewhere */
2047-
20482037
if (!(flags & WALLY_TX_FLAG_ALLOW_PARTIAL)) {
20492038
/* 0-input/output txs can be only be written with this flag */
20502039
if (!tx->num_inputs || !tx->num_outputs)
@@ -2075,44 +2064,28 @@ static int tx_to_bytes(const struct wally_tx *tx,
20752064

20762065
p += uint32_to_le_bytes(tx->version, p);
20772066
if (is_elements) {
2078-
if (!opts)
2079-
*p++ = flags & WALLY_TX_FLAG_USE_WITNESS ? 1 : 0;
2067+
*p++ = flags & WALLY_TX_FLAG_USE_WITNESS ? 1 : 0;
20802068
} else {
20812069
if (flags & WALLY_TX_FLAG_USE_WITNESS) {
20822070
*p++ = 0; /* Write BIP 144 marker */
20832071
*p++ = 1; /* Write BIP 144 flag */
20842072
}
20852073
}
2086-
if (sh_anyonecanpay)
2087-
*p++ = 1;
2088-
else
2089-
p += varint_to_bytes(tx->num_inputs, p);
2074+
2075+
p += varint_to_bytes(tx->num_inputs, p);
20902076

20912077
for (i = 0; i < tx->num_inputs; ++i) {
20922078
const struct wally_tx_input *input = tx->inputs + i;
2093-
if (sh_anyonecanpay && i != opts->index)
2094-
continue; /* sh_anyonecanpay only signs the given index */
2095-
20962079
memcpy(p, input->txhash, sizeof(input->txhash));
20972080
p += sizeof(input->txhash);
2098-
if (!opts && (input->features & WALLY_TX_IS_ISSUANCE))
2081+
if (input->features & WALLY_TX_IS_ISSUANCE)
20992082
p += uint32_to_le_bytes(input->index | WALLY_TX_ISSUANCE_FLAG, p);
2100-
else if (!opts && (input->features & WALLY_TX_IS_PEGIN))
2083+
else if (input->features & WALLY_TX_IS_PEGIN)
21012084
p += uint32_to_le_bytes(input->index | WALLY_TX_PEGIN_FLAG, p);
21022085
else
21032086
p += uint32_to_le_bytes(input->index, p);
2104-
if (opts) {
2105-
if (i == opts->index)
2106-
p += varbuff_to_bytes(opts->script, opts->script_len, p);
2107-
else
2108-
*p++ = 0; /* Blank scripts for non-signing inputs */
2109-
} else
2110-
p += varbuff_to_bytes(input->script, input->script_len, p);
2111-
2112-
if ((sh_none || sh_single) && i != opts->index)
2113-
p += uint32_to_le_bytes(0, p);
2114-
else
2115-
p += uint32_to_le_bytes(input->sequence, p);
2087+
p += varbuff_to_bytes(input->script, input->script_len, p);
2088+
p += uint32_to_le_bytes(input->sequence, p);
21162089
if (input->features & WALLY_TX_IS_ISSUANCE) {
21172090
if (!is_elements)
21182091
return WALLY_EINVAL;
@@ -2127,41 +2100,22 @@ static int tx_to_bytes(const struct wally_tx *tx,
21272100
}
21282101
}
21292102

2130-
if (sh_none)
2131-
*p++ = 0;
2132-
else {
2133-
size_t num_outputs = sh_single ? opts->index + 1 : tx->num_outputs;
2134-
p += varint_to_bytes(num_outputs, p);
2135-
2136-
for (i = 0; i < num_outputs; ++i) {
2137-
const struct wally_tx_output *output = tx->outputs + i;
2138-
if (sh_single && i != opts->index) {
2139-
memcpy(p, EMPTY_OUTPUT, sizeof(EMPTY_OUTPUT));
2140-
p += sizeof(EMPTY_OUTPUT);
2141-
} else {
2142-
if (output->features & WALLY_TX_IS_ELEMENTS) {
2143-
if (!is_elements)
2144-
return WALLY_EINVAL;
2145-
#ifdef BUILD_ELEMENTS
2146-
p += confidential_value_to_bytes(output->asset, output->asset_len, p);
2147-
p += confidential_value_to_bytes(output->value, output->value_len, p);
2148-
p += confidential_value_to_bytes(output->nonce, output->nonce_len, p);
2149-
#endif
2150-
} else {
2151-
p += uint64_to_le_bytes(output->satoshi, p);
2152-
}
2153-
p += varbuff_to_bytes(output->script, output->script_len, p);
2103+
p += varint_to_bytes(tx->num_outputs, p);
21542104

2105+
for (i = 0; i < tx->num_outputs; ++i) {
2106+
const struct wally_tx_output *output = tx->outputs + i;
2107+
if (output->features & WALLY_TX_IS_ELEMENTS) {
2108+
if (!is_elements)
2109+
return WALLY_EINVAL;
21552110
#ifdef BUILD_ELEMENTS
2156-
if (is_elements && sh_rangeproof) {
2157-
p += varbuff_to_bytes(output->rangeproof,
2158-
output->rangeproof_len, p);
2159-
p += varbuff_to_bytes(output->surjectionproof,
2160-
output->surjectionproof_len, p);
2161-
}
2111+
p += confidential_value_to_bytes(output->asset, output->asset_len, p);
2112+
p += confidential_value_to_bytes(output->value, output->value_len, p);
2113+
p += confidential_value_to_bytes(output->nonce, output->nonce_len, p);
21622114
#endif
2163-
}
2115+
} else {
2116+
p += uint64_to_le_bytes(output->satoshi, p);
21642117
}
2118+
p += varbuff_to_bytes(output->script, output->script_len, p);
21652119
}
21662120

21672121
if (!is_elements && (flags & WALLY_TX_FLAG_USE_WITNESS)) {
@@ -2178,8 +2132,6 @@ static int tx_to_bytes(const struct wally_tx *tx,
21782132
}
21792133

21802134
p += uint32_to_le_bytes(tx->locktime, p);
2181-
if (opts)
2182-
uint32_to_le_bytes(opts->tx_sighash, p);
21832135

21842136
#ifdef BUILD_ELEMENTS
21852137
if (is_elements && (flags & WALLY_TX_FLAG_USE_WITNESS)) {
@@ -2224,7 +2176,7 @@ int wally_tx_to_bytes(const struct wally_tx *tx, uint32_t flags,
22242176
if (wally_tx_is_elements(tx, &is_elements) != WALLY_OK)
22252177
return WALLY_EINVAL;
22262178
#endif
2227-
return tx_to_bytes(tx, NULL, flags, bytes_out, len, written, is_elements);
2179+
return tx_to_bytes(tx, flags, bytes_out, len, written, is_elements);
22282180
}
22292181

22302182
/* Common implementation for hex conversion and txid calculation */
@@ -2244,12 +2196,12 @@ static int tx_to_hex_or_txid(const struct wally_tx *tx, uint32_t flags,
22442196
(!output && (!bytes_out || len != WALLY_TXHASH_LEN)))
22452197
return WALLY_EINVAL;
22462198

2247-
ret = tx_to_bytes(tx, NULL, flags, buff_p, sizeof(buff), &n, is_elements);
2199+
ret = tx_to_bytes(tx, flags, buff_p, sizeof(buff), &n, is_elements);
22482200
if (ret == WALLY_OK) {
22492201
if (n > sizeof(buff)) {
22502202
if ((buff_p = wally_malloc(n)) == NULL)
22512203
return WALLY_ENOMEM;
2252-
ret = tx_to_bytes(tx, NULL, flags, buff_p, n, &written, is_elements);
2204+
ret = tx_to_bytes(tx, flags, buff_p, n, &written, is_elements);
22532205
if (n != written)
22542206
ret = WALLY_ERROR; /* Length calculated incorrectly */
22552207
}

0 commit comments

Comments
 (0)