From 9b1b05617fc3619fb3b5b8e91c611b707de8287b Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 13 Aug 2020 12:14:17 -0500 Subject: [PATCH] psbt: use platform-independent serialization for psbt fields portability for the win Suggested-By: @jgriffiths --- common/psbt_open.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/common/psbt_open.c b/common/psbt_open.c index 70a027b8f..e168ada82 100644 --- a/common/psbt_open.c +++ b/common/psbt_open.c @@ -4,20 +4,23 @@ #include #include #include +#include #include #include bool psbt_get_serial_id(const struct wally_map *map, u16 *serial_id) { size_t value_len; + beint16_t bev; void *result = psbt_get_lightning(map, PSBT_TYPE_SERIAL_ID, &value_len); if (!result) return false; - if (value_len != sizeof(*serial_id)) + if (value_len != sizeof(bev)) return false; - memcpy(serial_id, result, value_len); + memcpy(&bev, result, value_len); + *serial_id = be16_to_cpu(bev); return true; } @@ -297,7 +300,9 @@ void psbt_input_add_serial_id(struct wally_psbt_input *input, u16 serial_id) { u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL); - psbt_input_add_unknown(input, key, &serial_id, sizeof(serial_id)); + beint16_t bev = cpu_to_be16(serial_id); + + psbt_input_add_unknown(input, key, &bev, sizeof(bev)); } @@ -305,7 +310,8 @@ void psbt_output_add_serial_id(struct wally_psbt_output *output, u16 serial_id) { u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL); - psbt_output_add_unknown(output, key, &serial_id, sizeof(serial_id)); + beint16_t bev = cpu_to_be16(serial_id); + psbt_output_add_unknown(output, key, &bev, sizeof(bev)); } bool psbt_has_serial_input(struct wally_psbt *psbt, u16 serial_id) @@ -345,16 +351,18 @@ bool psbt_input_get_max_witness_len(struct wally_psbt_input *input, u16 *max_witness_len) { size_t value_len; + beint16_t bev; void *result = psbt_get_lightning(&input->unknowns, PSBT_TYPE_MAX_WITNESS_LEN, &value_len); if (!result) return false; - if (value_len != sizeof(*max_witness_len)) + if (value_len != sizeof(bev)) return false; - memcpy(max_witness_len, result, value_len); + memcpy(&bev, result, value_len); + *max_witness_len = be16_to_cpu(bev); return true; }