libwally-core icon indicating copy to clipboard operation
libwally-core copied to clipboard

wally_psbt_to_bytes() difficult to use

Open rustyrussell opened this issue 5 years ago • 1 comments

Without any way of knowing the length to allocate, how do we call wally_psbt_to_bytes?

For the moment, we allocate 1k and loop, doubling each time. But worse, it returns WALLY_EINVAL for reasons OTHER than "you didn't give enough room".

There are two ways of fixing this:

  1. Expose psbt_get_length(). This is inefficient.
  2. Change the semantics of bytes_written to "bytes_needed", and set it to the number of bytes you want. Then return WALLY_ENOMEM to distinguish from unrelated errors.

The second allows a more optimal implementation in future.

rustyrussell avatar May 15 '20 00:05 rustyrussell

Isn't psbt_get_length() already exposed?

Change the semantics of bytes_written to "bytes_needed", and set it to the number of bytes you want. Then return WALLY_ENOMEM to distinguish from unrelated errors.

I don't quite understand this. Do you mean that wally_psbt_to_bytes should compute how many bytes are needed and return that if the buffer is big enough (i.e. output the result of psbt_get_length)?

achow101 avatar May 15 '20 07:05 achow101