Skip to content

Commit 17198ee

Browse files
committed
tx: remove the last signature hash vestiges from the serialization code
1 parent aa00afe commit 17198ee

File tree

1 file changed

+16
-66
lines changed

1 file changed

+16
-66
lines changed

src/transaction.c

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ static const unsigned char DUMMY_SIG[EC_SIGNATURE_DER_MAX_LEN + 1]; /* +1 for si
3434

3535
#define MAX_INVALID_SATOSHI ((uint64_t) -1)
3636

37-
/* Extra options when serializing for hashing */
38-
struct tx_serialize_opts
39-
{
40-
uint32_t sighash; /* 8 bit sighash value for sig */
41-
uint32_t tx_sighash; /* 32 bit sighash value for tx */
42-
size_t index; /* index of input we are signing */
43-
const unsigned char *script; /* scriptPubkey spent by the input we are signing */
44-
size_t script_len; /* length of 'script' in bytes */
45-
uint64_t satoshi; /* Amount of the input we are signing */
46-
const unsigned char *value; /* Confidential value of the input we are signing */
47-
size_t value_len; /* length of 'value' in bytes */
48-
};
49-
50-
static const unsigned char EMPTY_OUTPUT[9] = {
51-
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
52-
};
53-
5437
#define WALLY_SATOSHI_MAX ((uint64_t)WALLY_BTC_MAX * WALLY_SATOSHI_PER_BTC)
5538

5639
/* LCOV_EXCL_START */
@@ -1699,16 +1682,11 @@ static int get_txin_issuance_size(const struct wally_tx_input *input,
16991682
* without iterating the transaction twice with different flags.
17001683
*/
17011684
static int tx_get_lengths(const struct wally_tx *tx,
1702-
const struct tx_serialize_opts *opts, uint32_t flags,
1685+
uint32_t flags,
17031686
size_t *base_size, size_t *witness_size,
17041687
size_t *witness_count, bool is_elements)
17051688
{
17061689
size_t n, i, j;
1707-
const unsigned char sighash = opts ? opts->sighash : 0;
1708-
const bool sh_anyonecanpay = sighash & WALLY_SIGHASH_ANYONECANPAY;
1709-
const bool sh_rangeproof = is_elements && (sighash & WALLY_SIGHASH_RANGEPROOF);
1710-
const bool sh_none = (sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_NONE;
1711-
const bool sh_single = (sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_SINGLE;
17121690

17131691
*base_size = 0;
17141692
*witness_size = 0;
@@ -1717,11 +1695,6 @@ static int tx_get_lengths(const struct wally_tx *tx,
17171695
if (!is_valid_tx(tx))
17181696
return WALLY_EINVAL;
17191697

1720-
if (opts) {
1721-
if (flags & WALLY_TX_FLAG_USE_WITNESS)
1722-
return WALLY_ERROR; /* Segwit tx hashing done elsewhere */
1723-
}
1724-
17251698
if ((flags & ~WALLY_TX_ALL_FLAGS) ||
17261699
((flags & WALLY_TX_FLAG_USE_WITNESS) &&
17271700
wally_tx_get_witness_count(tx, witness_count) != WALLY_OK))
@@ -1731,53 +1704,31 @@ static int tx_get_lengths(const struct wally_tx *tx,
17311704
flags &= ~WALLY_TX_FLAG_USE_WITNESS;
17321705

17331706
n = sizeof(tx->version) +
1734-
varint_get_length(sh_anyonecanpay ? 1 : tx->num_inputs) +
1735-
(sh_none ? 1 : varint_get_length(sh_single ? opts->index + 1 : tx->num_outputs)) +
1736-
sizeof(tx->locktime) +
1737-
(opts ? sizeof(leint32_t) : 0); /* Include trailing tx_sighash */
1707+
varint_get_length(tx->num_inputs) +
1708+
varint_get_length(tx->num_outputs) + sizeof(tx->locktime);
17381709

1739-
if (!opts && is_elements)
1710+
if (is_elements)
17401711
n += sizeof(uint8_t); /* witness flag */
17411712
for (i = 0; i < tx->num_inputs; ++i) {
17421713
const struct wally_tx_input *input = tx->inputs + i;
17431714
size_t issuance_size = 0;
17441715

1745-
if (sh_anyonecanpay && i != opts->index)
1746-
continue; /* sh_anyonecanpay only signs the given index */
1747-
17481716
n += sizeof(input->txhash) +
17491717
sizeof(input->index) +
17501718
sizeof(input->sequence);
17511719

17521720
if (get_txin_issuance_size(input, &issuance_size, NULL) != WALLY_OK)
17531721
return WALLY_EINVAL;
17541722
n += issuance_size;
1755-
1756-
if (opts) {
1757-
if (i == opts->index)
1758-
n += varbuff_get_length(opts->script_len);
1759-
else
1760-
++n;
1761-
} else
1762-
n += varbuff_get_length(input->script_len);
1763-
1723+
n += varbuff_get_length(input->script_len);
17641724
}
17651725

1766-
if (!sh_none) {
1767-
size_t num_outputs = sh_single ? opts->index + 1 : tx->num_outputs;
1768-
1769-
for (i = 0; i < num_outputs; ++i) {
1770-
const struct wally_tx_output *output = tx->outputs + i;
1771-
if (sh_single && i != opts->index)
1772-
n += sizeof(EMPTY_OUTPUT);
1773-
else {
1774-
size_t wit_size = 0, *wit_p = sh_rangeproof ? &wit_size : NULL;
1775-
size_t txout_len = txout_get_serialized_len(output, is_elements, wit_p);
1776-
if (!txout_len)
1777-
return WALLY_EINVAL; /* Error getting txout length */
1778-
n += txout_len + wit_size;
1779-
}
1780-
}
1726+
for (i = 0; i < tx->num_outputs; ++i) {
1727+
const struct wally_tx_output *output = tx->outputs + i;
1728+
size_t txout_len = txout_get_serialized_len(output, is_elements, NULL);
1729+
if (!txout_len)
1730+
return WALLY_EINVAL; /* Error getting txout length */
1731+
n += txout_len;
17811732
}
17821733

17831734
*base_size = n;
@@ -1834,8 +1785,7 @@ static int tx_get_lengths(const struct wally_tx *tx,
18341785
}
18351786

18361787
static int tx_get_length(const struct wally_tx *tx,
1837-
const struct tx_serialize_opts *opts, uint32_t flags,
1838-
size_t *written, bool is_elements)
1788+
uint32_t flags, size_t *written, bool is_elements)
18391789
{
18401790
size_t base_size, witness_size, witness_count;
18411791
int ret;
@@ -1844,7 +1794,7 @@ static int tx_get_length(const struct wally_tx *tx,
18441794
*written = 0;
18451795
if (!written)
18461796
return WALLY_EINVAL;
1847-
ret = tx_get_lengths(tx, opts, flags, &base_size, &witness_size,
1797+
ret = tx_get_lengths(tx, flags, &base_size, &witness_size,
18481798
&witness_count, is_elements);
18491799
if (ret == WALLY_OK) {
18501800
if (witness_count && (flags & WALLY_TX_FLAG_USE_WITNESS))
@@ -1864,7 +1814,7 @@ int wally_tx_get_length(const struct wally_tx *tx, uint32_t flags,
18641814
return WALLY_EINVAL;
18651815
#endif
18661816

1867-
return tx_get_length(tx, NULL, flags, written, is_elements != 0);
1817+
return tx_get_length(tx, flags, written, is_elements != 0);
18681818
}
18691819

18701820
int wally_tx_get_weight(const struct wally_tx *tx, size_t *written)
@@ -1881,7 +1831,7 @@ int wally_tx_get_weight(const struct wally_tx *tx, size_t *written)
18811831
#endif
18821832

18831833
if (!written ||
1884-
tx_get_lengths(tx, NULL, WALLY_TX_FLAG_USE_WITNESS, &base_size,
1834+
tx_get_lengths(tx, WALLY_TX_FLAG_USE_WITNESS, &base_size,
18851835
&witness_size, &witness_count, is_elements != 0) != WALLY_OK)
18861836
return WALLY_EINVAL;
18871837

@@ -2031,7 +1981,7 @@ static int tx_to_bytes(const struct wally_tx *tx,
20311981

20321982
if (!is_valid_tx(tx) ||
20331983
(flags & ~WALLY_TX_ALL_FLAGS) || !bytes_out || !written ||
2034-
tx_get_length(tx, NULL, flags, &n, is_elements) != WALLY_OK)
1984+
tx_get_length(tx, flags, &n, is_elements) != WALLY_OK)
20351985
return WALLY_EINVAL;
20361986

20371987
if (!(flags & WALLY_TX_FLAG_ALLOW_PARTIAL)) {

0 commit comments

Comments
 (0)