Skip to content

Commit

Permalink
rpc: addressmap: Better error messages (#2605)
Browse files Browse the repository at this point in the history
* rpc: addressmap: Better error messages

* Fix failing test

---------

Co-authored-by: Bushstar <bushsolo@gmail.com>
  • Loading branch information
prasannavl and Bushstar authored Oct 23, 2023
1 parent cb99ffd commit d439c39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4283,13 +4283,14 @@ UniValue addressmap(const JSONRPCRequest &request) {
}
.Check(request);

auto throwInvalidParam = []() { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid type parameter")); };
auto throwInvalidParamType = []() { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid type parameter")); };
auto throwInvalidAddressFmt = []() { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid address format")); };

const std::string input = request.params[0].get_str();

int typeInt = request.params[1].get_int();
if (typeInt < 0 || typeInt >= AddressConversionTypeCount) {
throwInvalidParam();
throwInvalidParamType();
}

CTxDestination dest = DecodeDestination(input);
Expand All @@ -4315,7 +4316,7 @@ UniValue addressmap(const JSONRPCRequest &request) {
switch (type) {
case AddressConversionType::DVMToEVMAddress: {
if (dest.index() != WitV0KeyHashType && dest.index() != PKHashType) {
throwInvalidParam();
throwInvalidAddressFmt();
}
CPubKey key = AddrToPubKey(pwallet, input);
if (key.IsCompressed()) {
Expand All @@ -4326,7 +4327,7 @@ UniValue addressmap(const JSONRPCRequest &request) {
}
case AddressConversionType::EVMToDVMAddress: {
if (dest.index() != WitV16KeyEthHashType) {
throwInvalidParam();
throwInvalidAddressFmt();
}
CPubKey key = AddrToPubKey(pwallet, input);
if (!key.IsCompressed()) {
Expand All @@ -4336,7 +4337,7 @@ UniValue addressmap(const JSONRPCRequest &request) {
break;
}
default:
throwInvalidParam();
throwInvalidParamType();
}

ret.pushKV("format", format);
Expand Down
23 changes: 13 additions & 10 deletions test/functional/feature_address_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,18 @@ def addressmap_valid_address_invalid_type_should_fail(self):
address = self.nodes[0].getnewaddress("", "legacy")
p2sh_address = self.nodes[0].getnewaddress("", "p2sh-segwit")
eth_address = self.nodes[0].getnewaddress("", "erc55")
assert_invalid = lambda *args: assert_raises_rpc_error(
assert_invalid_parameter = lambda *args: assert_raises_rpc_error(
-8, "Invalid type parameter", self.nodes[0].addressmap, *args
)
assert_invalid(address, 9)
assert_invalid(address, -1)
assert_invalid(eth_address, AddressConversionType.DVMToEVMAddress)
assert_invalid(address, AddressConversionType.EVMToDVMAddress)
assert_invalid(p2sh_address, AddressConversionType.DVMToEVMAddress)
assert_invalid(p2sh_address, AddressConversionType.DVMToEVMAddress)
assert_invalid_format = lambda *args: assert_raises_rpc_error(
-8, "Invalid address format", self.nodes[0].addressmap, *args
)
assert_invalid_parameter(address, 9)
assert_invalid_parameter(address, -1)
assert_invalid_format(eth_address, AddressConversionType.DVMToEVMAddress)
assert_invalid_format(address, AddressConversionType.EVMToDVMAddress)
assert_invalid_format(p2sh_address, AddressConversionType.DVMToEVMAddress)
assert_invalid_format(p2sh_address, AddressConversionType.DVMToEVMAddress)

def addressmap_invalid_address_should_fail(self):
self.rollback_to(self.start_block_height)
Expand All @@ -222,21 +225,21 @@ def addressmap_invalid_address_should_fail(self):
)
assert_raises_rpc_error(
-8,
"Invalid type parameter",
"Invalid address format",
self.nodes[0].addressmap,
eth_address,
AddressConversionType.DVMToEVMAddress,
)
assert_raises_rpc_error(
-8,
"Invalid type parameter",
"Invalid address format",
self.nodes[0].addressmap,
"test",
AddressConversionType.DVMToEVMAddress,
)
assert_raises_rpc_error(
-8,
"Invalid type parameter",
"Invalid address format",
self.nodes[0].addressmap,
"test",
AddressConversionType.EVMToDVMAddress,
Expand Down

0 comments on commit d439c39

Please sign in to comment.