Crashing with assert for invalid user input is problematic for a library, e.g. this is impossible for FFI users to recover from, forcing them to re-implement the error handling logic client-side. See e.g. the go and .net bindings, or the discussion in https://github.com/stringintech/kernel-bindings-tests/issues/5.
This PR changes behaviour by:
- updating
btck_script_pubkey_verifyto return-1for invalid/malformed requests,0for invalid scripts, and1for valid scripts. Previously, the program would crash for the-1cases. - updating 5 getters to return
nullptrwhen the request index is out of bounds. Previously, the program would crash in these cases.
The wrapper is updated to throw std::runtime_error or std::out_of_bounds errors, and the tests are also updated.
By not crashing, we can properly test for these edge cases in kernel-bindings-tests to ensure that wrappers are handling them properly, as opposed to e.g. not differentiating between 0 and -1, which a naive (incorrect) implementation could do.
Note: I’m not sure why btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION and btck_ScriptVerifyStatus_ERROR_SPENT_OUTPUTS_REQUIRED are currently handled as INVALID_SCRIPT, in my view they should probably be INVALID_REQUEST (which means btck_ScriptVerifyStatus becomes meaningless in its current form), but I’ve not implemented that yet as I’m not 100% sure. Happy to update that here, or can also be done in a separate pull later.