psbt: add type-to-string that prints b64 string

Re-uses code from what was the bitcoin_tx_to_psbt_b64
This commit is contained in:
niftynei 2020-05-29 12:12:29 -05:00 committed by Christian Decker
parent c100de6d93
commit 5ecacf3dd0
4 changed files with 29 additions and 9 deletions

View File

@ -5,7 +5,9 @@
#include <bitcoin/signature.h>
#include <ccan/cast/cast.h>
#include <ccan/ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <string.h>
#include <wally_psbt.h>
@ -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)
{

View File

@ -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,

View File

@ -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)

View File

@ -4,6 +4,7 @@
#include "utils.h"
#include <ccan/autodata/autodata.h>
#include <secp256k1.h>
#include <wally_psbt.h>
/* 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) \