From c38cdd74c3016fbac0f5858f255690210adbf919 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 23 Oct 2023 13:28:44 +0800 Subject: [PATCH 1/4] rpc: addressmap: Better error messages --- src/wallet/rpcwallet.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2ec3df90fc..2895b1ddde 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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); @@ -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()) { @@ -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()) { @@ -4336,7 +4337,7 @@ UniValue addressmap(const JSONRPCRequest &request) { break; } default: - throwInvalidParam(); + throwInvalidParamType(); } ret.pushKV("format", format); From bbbbbd3c484b898c14f80d11be017dea34985e03 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 23 Oct 2023 13:47:22 +0800 Subject: [PATCH 2/4] rpc: addressmap: add erc55 validity check --- src/wallet/rpcwallet.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2895b1ddde..1c99251e69 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4322,7 +4322,12 @@ UniValue addressmap(const JSONRPCRequest &request) { if (key.IsCompressed()) { key.Decompress(); } - format.pushKV("erc55", EncodeDestination(WitnessV16EthHash(key))); + auto erc55 = EncodeDestination(WitnessV16EthHash(key)); + // Check if it's in the wallet. + // Note: Can be removed if the full wallet is migrated. + // Ref: https://github.com/DeFiCh/ain/issues/2604 + AddrToPubKey(pwallet, erc55); + format.pushKV("erc55", erc55); break; } case AddressConversionType::EVMToDVMAddress: { From 8fc1bfa77c6dda21943f451a4d840ef31fdbe814 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 23 Oct 2023 13:53:53 +0800 Subject: [PATCH 3/4] Add const --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1c99251e69..8063b4b749 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4322,7 +4322,7 @@ UniValue addressmap(const JSONRPCRequest &request) { if (key.IsCompressed()) { key.Decompress(); } - auto erc55 = EncodeDestination(WitnessV16EthHash(key)); + const auto erc55 = EncodeDestination(WitnessV16EthHash(key)); // Check if it's in the wallet. // Note: Can be removed if the full wallet is migrated. // Ref: https://github.com/DeFiCh/ain/issues/2604 From b405684d24dfa7fe9fae2ca69dd6b8eeec0887ed Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 23 Oct 2023 14:17:03 +0800 Subject: [PATCH 4/4] util: Remove obsolete check --- src/rpc/util.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index c1bbd27801..305f242848 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -158,9 +158,6 @@ CPubKey AddrToPubKey(FillableSigningProvider* const keystore, const std::string& if (!keystore->GetPubKey(key, vchPubKey)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in)); } - if (dest.index() == WitV16KeyEthHashType && vchPubKey.IsCompressed()) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no valid public key for address %s", addr_in)); - } if (!vchPubKey.IsFullyValid()) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key"); }