Skip to content

Commit c03771a

Browse files
tx: debugging psbt pass through
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent b7878e6 commit c03771a

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

bitcoin/test/run-psbt-from-tx.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ int main(int argc, char *argv[])
112112
assert(final_scriptsig->value_len > 0);
113113
assert(tx2->psbt->inputs[0].final_witness != NULL);
114114

115+
char *hex_tx = "0200000001882d49b841cb341defb60509f5863b163d32d1a819545761d31420cbf9db94d60000000000f8858a80024a01000000000000220020b6d8d2f575683f606ece57e33f59eaa146152f5c7163fb3e917b3cd19b6a558038073ba40b00000022002076f0b18b3c072222fd000432a309620dd43dfb776ab3fd710c9bda5efb0235f49c49c120";
116+
tx = bitcoin_tx_from_hex(tmpctx, hex_tx, strlen(hex_tx));
117+
psbt_input_get_amount(tx->psbt, 0);
118+
119+
msg = tal_arr(tmpctx, u8, 0);
120+
/* convert to wire format */
121+
towire_bitcoin_tx(&msg, tx);
122+
123+
len = tal_bytelen(msg);
124+
assert(len > 0);
125+
126+
tx2 = fromwire_bitcoin_tx(tmpctx,
127+
cast_const2(const u8 **, &msg), &len);
128+
assert(tx2 != NULL);
129+
130+
psbt_input_get_amount(tx2->psbt, 0);
131+
115132
common_shutdown();
116133
return 0;
117134
}

bitcoin/test/run-tx-encode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ int main(int argc, const char *argv[])
163163
"0204096eb817f7efb414ef4d3d8be39dd04374256d3b054a322d4a6ee22736d0"
164164
"3b");
165165

166-
hex_tx = "0200000001fd9915bf664e2b4888b4770101d92d9086144124646b3df49bd6e3156c9d194a000000000086450b80024a010000000000002200207b6d27992d8294099919c1beea2397dbb6d934f8b62c25130a070a608f0e58a2f4d98dff4d000000220020aa8bd5637587bebb499a4f52b13864bc03d7664985da1ba4fc34005d591e55680effaa20";
166+
hex_tx = "020000000180e1806045e74fb8fe0356e6568fe723c2755e54563b2e8df2f7e99202c3d896000000000029635b80024a01000000000000220020ac0192675893119abba0193b6b52dc188d6d1032078903890e72a676ffa237df4294d5e829000000220020ad270f8b9c3719fafeaa3be48aebaeac2a953a1ef658f3d55e0d0439b51df508648d1a20";
167167
tx = bitcoin_tx_from_hex(NULL, hex_tx, strlen(hex_tx));
168168
assert(strcmp(hex_tx, fmt_bitcoin_tx(NULL, tx)) == 0);
169169

bitcoin/tx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ static bool uses_witness(const struct wally_tx *wtx)
430430
{
431431
size_t i;
432432

433+
assert(wtx);
433434
for (i = 0; i < wtx->num_inputs; i++) {
434435
if (wtx->inputs[i].witness)
435436
return true;

common/json_parse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,11 @@ bool json_to_txid(const char *buffer, const jsmntok_t *tok,
582582
}
583583

584584
bool json_to_tx(const char *buffer, const jsmntok_t *tok,
585-
struct bitcoin_tx *tx)
585+
struct bitcoin_tx **tx)
586586
{
587-
tx = bitcoin_tx_from_hex(buffer, buffer + tok->start,
587+
*tx = bitcoin_tx_from_hex(buffer, buffer + tok->start,
588588
tok->end - tok->start);
589-
return tx != NULL;
589+
return *tx != NULL;
590590
}
591591

592592

common/json_parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool json_to_txid(const char *buffer, const jsmntok_t *tok,
105105

106106
/* Extract a bitcoin tx from this */
107107
bool json_to_tx(const char *buffer, const jsmntok_t *tok,
108-
struct bitcoin_tx *tx);
108+
struct bitcoin_tx **tx);
109109

110110
/* Extract a bitcoin outpoint from this */
111111
bool json_to_outpoint(const char *buffer, const jsmntok_t *tok,

common/test/run-json_scan.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "config.h"
22
#include "../json_parse.c"
33
#include "../json_parse_simple.c"
4+
#include <assert.h>
45
#include <common/amount.h>
56
#include <common/setup.h>
7+
#include <stdio.h>
8+
#include <string.h>
69
#include <wire/wire.h>
710

811
/* AUTOGENERATED MOCKS START */
@@ -244,5 +247,21 @@ int main(int argc, char *argv[])
244247
assert(toks);
245248
assert(toks->size == 4);
246249

250+
char *hex_tx = "020000000180e1806045e74fb8fe0356e6568fe723c2755e54563b2e8df2f7e99202c3d896000000000029635b80024a01000000000000220020ac0192675893119abba0193b6b52dc188d6d1032078903890e72a676ffa237df4294d5e829000000220020ad270f8b9c3719fafeaa3be48aebaeac2a953a1ef658f3d55e0d0439b51df508648d1a20";
251+
buf = tal_fmt(tmpctx, "{\"tx\":\"%s\"}", hex_tx);
252+
toks = json_parse_simple(tmpctx, buf, strlen(buf));
253+
toks = json_get_member(buf, toks, "tx");
254+
assert(toks->end - toks->start == strlen(hex_tx));
255+
256+
chainparams = chainparams_for_network("bitcoin");
257+
struct bitcoin_tx *tx = bitcoin_tx_from_hex(NULL, hex_tx, strlen(hex_tx));
258+
assert(tx);
259+
assert(strcmp(hex_tx, fmt_bitcoin_tx(NULL, tx)) == 0);
260+
261+
tx = NULL;
262+
assert(json_to_tx(buf, toks, &tx));
263+
assert(tx);
264+
assert(strcmp(hex_tx, fmt_bitcoin_tx(NULL, tx)) == 0);
265+
247266
common_shutdown();
248267
}

lightningd/opening_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ static bool onfunding_channel_tx_hook_deserialize(struct onfunding_channel_tx_ho
912912
toks[0].end - toks[0].start, buffer + toks[0].start);
913913

914914
payload->tx = NULL;
915-
if (!json_to_tx(buffer, tx_tok, payload->tx))
915+
if (!json_to_tx(buffer, tx_tok, &payload->tx))
916916
fatal("Plugin returned an invalid (json to tx) response to the"
917917
" onfunding_channel_tx hook: %.*s",
918918
tx_tok[0].end - tx_tok[0].start, buffer + tx_tok[0].start);

openingd/openingd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* commit to the database once openingd succeeds.
99
*/
1010
#include "config.h"
11+
#include <assert.h>
1112
#include <bitcoin/script.h>
1213
#include <ccan/array_size/array_size.h>
1314
#include <ccan/breakpoint/breakpoint.h>
@@ -31,6 +32,7 @@
3132
#include <openingd/openingd_wiregen.h>
3233
#include <wire/peer_wire.h>
3334
#include <wire/wire_sync.h>
35+
#include <wally_psbt.h>
3436

3537
/* stdin == lightningd, 3 == peer, 4 = hsmd */
3638
#define REQ_FD STDIN_FILENO
@@ -703,13 +705,15 @@ static bool funder_finalize_channel_setup(struct state *state,
703705
else
704706
*pbase = NULL;
705707

708+
assert((*tx)->psbt->inputs[0].witness_utxo);
706709
msg = towire_openingd_on_funding_tx(tmpctx, *tx, &cid);
707710
wire_sync_write(REQ_FD, msg);
708711
msg = wire_sync_read(tmpctx, REQ_FD);
709712
if (!fromwire_openingd_on_funding_tx_reply(msg, msg, tx))
710713
status_failed(STATUS_FAIL_MASTER_IO, "Bad onfunding_tx %s",
711714
tal_hex(tmpctx, msg));
712715

716+
assert((*tx)->psbt->inputs[0].witness_utxo);
713717
/* We ask the HSM to sign their commitment transaction for us: it knows
714718
* our funding key, it just needs the remote funding key to create the
715719
* witness script. It also needs the amount of the funding output,

0 commit comments

Comments
 (0)