diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index 56706fb54..d14686916 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -310,6 +312,28 @@ struct amount_sat psbt_input_get_amount(struct wally_psbt *psbt, return val; } +bool psbt_from_b64(const char *b64str, struct wally_psbt **psbt) +{ + int wally_err; + wally_err = wally_psbt_from_base64(b64str, psbt); + return wally_err == WALLY_OK; +} + +char *psbt_to_b64(const tal_t *ctx, const struct wally_psbt *psbt) +{ + char *serialized_psbt, *ret_val; + int ret; + + ret = wally_psbt_to_base64(cast_const(struct wally_psbt *, psbt), + &serialized_psbt); + assert(ret == WALLY_OK); + + ret_val = tal_strdup(ctx, serialized_psbt); + wally_free_string(serialized_psbt); + return ret_val; +} +REGISTER_TYPE_TO_STRING(wally_psbt, psbt_to_b64); + const u8 *psbt_get_bytes(const tal_t *ctx, const struct wally_psbt *psbt, size_t *bytes_written) { diff --git a/bitcoin/psbt.h b/bitcoin/psbt.h index 218125c24..29b160672 100644 --- a/bitcoin/psbt.h +++ b/bitcoin/psbt.h @@ -60,6 +60,8 @@ void psbt_input_set_prev_utxo_wscript(struct wally_psbt *psbt, size_t in, struct amount_sat psbt_input_get_amount(struct wally_psbt *psbt, size_t in); +bool psbt_from_b64(const char *b64str, struct wally_psbt **psbt); +char *psbt_to_b64(const tal_t *ctx, const struct wally_psbt *psbt); const u8 *psbt_get_bytes(const tal_t *ctx, const struct wally_psbt *psbt, size_t *bytes_written); struct wally_psbt *psbt_from_bytes(const tal_t *ctx, const u8 *bytes, diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 2dc66c6c7..10f3f3a28 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -486,15 +486,7 @@ void bitcoin_tx_finalize(struct bitcoin_tx *tx) char *bitcoin_tx_to_psbt_base64(const tal_t *ctx, struct bitcoin_tx *tx) { - char *serialized_psbt, *ret_val; - int ret; - - ret = wally_psbt_to_base64(tx->psbt, &serialized_psbt); - assert(ret == WALLY_OK); - - ret_val = tal_strdup(ctx, serialized_psbt); - wally_free_string(serialized_psbt); - return ret_val; + return psbt_to_b64(ctx, tx->psbt); } struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psbt STEALS) diff --git a/common/type_to_string.h b/common/type_to_string.h index 209c26e6a..ce134b946 100644 --- a/common/type_to_string.h +++ b/common/type_to_string.h @@ -4,6 +4,7 @@ #include "utils.h" #include #include +#include /* This must match the type_to_string_ cases. */ union printable_types { @@ -35,6 +36,7 @@ union printable_types { const struct amount_sat *amount_sat; const struct fee_states *fee_states; const char *charp_; + const struct wally_psbt *wally_psbt; }; #define type_to_string(ctx, type, ptr) \