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

scriptpubkey_to_address() does not give clear error messages

Open Randy808 opened this issue 2 years ago • 2 comments

After attempting to use the library function scriptpubkey_to_address from the Python distribution, I got the error message ValueError: Invalid argument

This was the line I used to invoke the function: wallycore.scriptpubkey_to_address(bytes([0x6a]), wallycore.WALLY_NETWORK_LIQUID)

I tried looking through the repo to see if there's any examples of scriptpubkey_to_address and didn't find much in the code aside from this (which linked the python call to 'wally_scriptpubkey_to_address'): https://github.com/ElementsProject/libwally-core/blob/38b85bf06194ebe0d1d841e11cd8e1b58aa2a9ca/include/wally.hpp#L1360

I did find an example in one of the commits matching the search result: https://github.com/ElementsProject/libwally-core/commit/399906aba358a16f05b233f619fa08aec6bfe116#diff-a1dde0a251f92d169d953a7e3b12d4f97d98a5985abf5b1a0e48075c062e708c

The standalone example file in that commit can be found here (although it's not in master anymore): https://github.com/ElementsProject/libwally-core/blob/399906aba358a16f05b233f619fa08aec6bfe116/src/swig_python/contrib/address.py

When I tried running this line using data from the test case it worked: wallycore.scriptpubkey_to_address(wallycore.hex_to_bytes("76a914bef5a2f9a56a94aab12459f72ad9cf8cf19c7bbe88ac"), wallycore.WALLY_NETWORK_LIQUID)

So then I decided to look into the implementation for 'wally_scriptpubkey_to_address': https://github.com/ElementsProject/libwally-core/blob/e1fc3cd4a3e0947d017ad323126b5413538b4710/src/address.c#L154

There's a line that tries to determine the script 'type': https://github.com/ElementsProject/libwally-core/blob/e1fc3cd4a3e0947d017ad323126b5413538b4710/src/address.c#L160

And then only processes the types WALLY_SCRIPT_TYPE_P2PKH and WALLY_SCRIPT_TYPE_P2SH

The function that gets the types ('wally_scriptpubkey_get_type') can be found here: https://github.com/ElementsProject/libwally-core/blob/38b85bf06194ebe0d1d841e11cd8e1b58aa2a9ca/src/script.c#L369

Although a type is returned for OP_RETURN, it doesn't accept it in 'wally_scriptpubkey_to_address'

The error I get from the call could tell me it wasn't supported instead of the generic 'WALLY_EINVAL' that's returned as the default catch-all: https://github.com/ElementsProject/libwally-core/blob/e1fc3cd4a3e0947d017ad323126b5413538b4710/src/address.c#L209

Randy808 avatar Dec 02 '22 16:12 Randy808