Parsing legacy public keys can fail for three reasons (in this order):
- pubkey is not in hex
- pubkey has an invalid length (not 33 or 65 bytes for compressed/uncompressed, respectively)
- pubkey is crytographically invalid, i.e. is not on curve (
CPubKey.IsFullyValid()
check)
Many RPCs currently perform these checks manually with different error messages, even though we already have a HexToPubKey
helper. This PR puts all three checks in this helper (the length check was done on the call-sites before), adds specific error messages for each case, and consequently uses it for all RPCs that parse legacy pubkeys. This leads to deduplicated code and also to more consistent and detailed error messages for the user.
Affected RPC calls are createmultisig
, addmultisigaddress
, importpubkey
, importmulti
, fundrawtransaction
, walletcreatefundedpsbt
, send
and sendall
.
Note that the error code (-5 a.k.a. RPC_INVALID_ADDRESS_OR_KEY
) doesn’t change in any of the causes, so the changes are not breaking RPC API compatibility. Only the messages are more specific.
The last commits adds test coverage for the cryptographically invalid (not-on-curve) pubkey case which wasn’t exercised before.