Skip to content

Commit a287516

Browse files
LeoComandiniLawrence Nahum
authored andcommitted
Detect if transaction is elements when possible
Do not allow WALLY_TX_FLAG_USE_ELEMENTS bit to be set for wally_tx_to_* as is it already possible to determine if the transaction is elements.
1 parent 8d091ec commit a287516

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/ctest/test_elements_tx.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ static bool tx_roundtrip(const char *tx_hex, const char *sighash_hex)
4343
size_t i;
4444
size_t is_elements;
4545
int ret;
46-
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS | WALLY_TX_FLAG_USE_ELEMENTS;
46+
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS;
4747

4848
/* Unserialize and serialize the tx and verify they match */
49-
ret = wally_tx_from_hex(tx_hex, flags, &tx);
49+
ret = wally_tx_from_hex(tx_hex, flags | WALLY_TX_FLAG_USE_ELEMENTS, &tx);
5050
check_ret(ret);
5151

5252
ret = wally_tx_to_hex(tx, flags, &new_hex);
@@ -170,12 +170,12 @@ static bool tx_coinbase(const char *tx_hex)
170170
{
171171
struct wally_tx *tx;
172172
char *new_hex;
173-
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS | WALLY_TX_FLAG_USE_ELEMENTS;
173+
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS;
174174
size_t is_elements, is_coinbase;
175175
int ret;
176176

177177
/* Unserialize and serialize the tx and verify they match */
178-
ret = wally_tx_from_hex(tx_hex, flags, &tx);
178+
ret = wally_tx_from_hex(tx_hex, flags | WALLY_TX_FLAG_USE_ELEMENTS, &tx);
179179
check_ret(ret);
180180

181181
ret = wally_tx_to_hex(tx, flags, &new_hex);
@@ -207,12 +207,12 @@ static bool tx_pegin(const char *tx_hex, const char **tx_pegin_wit_hex, size_t n
207207
struct wally_tx_witness_stack *pegin_wit;
208208
struct wally_tx_witness_item *item;
209209
char *new_hex;
210-
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS | WALLY_TX_FLAG_USE_ELEMENTS;
210+
const uint32_t flags = WALLY_TX_FLAG_USE_WITNESS;
211211
size_t is_elements, is_pegin, siz, i;
212212
int ret;
213213

214214
/* Unserialize and serialize the tx and verify they match */
215-
ret = wally_tx_from_hex(tx_hex, flags, &tx);
215+
ret = wally_tx_from_hex(tx_hex, flags | WALLY_TX_FLAG_USE_ELEMENTS, &tx);
216216
check_ret(ret);
217217

218218
ret = wally_tx_to_hex(tx, flags, &new_hex);

src/swig_python/contrib/elements_tx.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ def test_tx(self):
8282
tx_add_elements_raw_output(tx, script, None, ct_value, None, None, None, 0)
8383
size = tx_get_length(tx, 0)
8484
vsize = tx_vsize_from_weight(tx_get_weight(tx))
85-
tx_hex = tx_to_hex(tx, WALLY_TX_FLAG_USE_WITNESS|WALLY_TX_FLAG_USE_ELEMENTS)
85+
tx_hex = tx_to_hex(tx, WALLY_TX_FLAG_USE_WITNESS)
86+
tx_bytes = tx_to_bytes(tx, WALLY_TX_FLAG_USE_WITNESS)
87+
self.assertEqual(tx_hex, hex_from_bytes(tx_bytes))
88+
with self.assertRaises(ValueError):
89+
tx_to_hex(tx, WALLY_TX_FLAG_USE_ELEMENTS)
90+
with self.assertRaises(ValueError):
91+
tx_to_bytes(tx, WALLY_TX_FLAG_USE_ELEMENTS)
8692

8793
def test_coinbase(self):
8894
txhash, seq, script = bytearray(b'\x00'*32), 0xffffffff, b'0000'

src/test/test_transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_serialization(self):
3737
for args in [
3838
(TX_FAKE_HEX[:9]+utf8('0')+TX_FAKE_HEX[92:], 0, pointer(wally_tx())), # No inputs
3939
(TX_FAKE_HEX[:93]+utf8('0')+TX_FAKE_HEX[112:], 0, pointer(wally_tx())), # No outputs
40+
(TX_FAKE_HEX, 2, pointer(wally_tx())), # Elements flag must not be set for serialization
4041
]:
4142
self.assertEqual(WALLY_OK, wally_tx_from_hex(*args))
4243
self.assertEqual(WALLY_EINVAL, wally_tx_to_hex(args[2][0], 0)[0])

src/transaction.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,8 +2146,13 @@ int wally_tx_to_bytes(const struct wally_tx *tx, uint32_t flags,
21462146
unsigned char *bytes_out, size_t len,
21472147
size_t *written)
21482148
{
2149-
return tx_to_bytes(tx, NULL, flags & ~WALLY_TX_FLAG_USE_ELEMENTS, bytes_out, len, written,
2150-
flags & WALLY_TX_FLAG_USE_ELEMENTS);
2149+
size_t is_elements = 0;
2150+
2151+
#ifdef BUILD_ELEMENTS
2152+
if (wally_tx_is_elements(tx, &is_elements) != WALLY_OK)
2153+
return WALLY_EINVAL;
2154+
#endif
2155+
return tx_to_bytes(tx, NULL, flags, bytes_out, len, written, is_elements);
21512156
}
21522157

21532158
static int tx_to_hex(const struct wally_tx *tx, uint32_t flags,
@@ -2182,8 +2187,13 @@ static int tx_to_hex(const struct wally_tx *tx, uint32_t flags,
21822187
int wally_tx_to_hex(const struct wally_tx *tx, uint32_t flags,
21832188
char **output)
21842189
{
2185-
return tx_to_hex(tx, flags & ~WALLY_TX_FLAG_USE_ELEMENTS, output,
2186-
flags & WALLY_TX_FLAG_USE_ELEMENTS);
2190+
size_t is_elements = 0;
2191+
2192+
#ifdef BUILD_ELEMENTS
2193+
if (wally_tx_is_elements(tx, &is_elements) != WALLY_OK)
2194+
return WALLY_EINVAL;
2195+
#endif
2196+
return tx_to_hex(tx, flags, output, is_elements);
21872197
}
21882198

21892199
static int analyze_tx(const unsigned char *bytes, size_t bytes_len,

0 commit comments

Comments
 (0)