Skip to content

Conversation

@PastaPastaPasta
Copy link
Member

Throws an RPCError RPC_WALLET_ERROR (text = "This wallet is not a HD (Hierarchical Deterministic) wallet.") when the command dumphdinfo is called and the wallet is not HD.

This is in response to #2069

@nmarley
Copy link

nmarley commented Jun 18, 2018

@paulied Thanks for the PR! Not sure if we want to throw an error here, hence the return NullUniValue instead just below.

Nonetheless, can you please ensure the syntax is clean by properly aligning the closing brace for your else branch?

@PastaPastaPasta
Copy link
Member Author

@nmarley Formatting should be fixed
The only problem with relying on return NullUniValue is that it gives no feedback to the user and results in confusion like #2069

@UdjinM6
Copy link

UdjinM6 commented Jun 18, 2018

IMO throwing an error is perfectly fine here and fixes #2069 , though return NullUniValue; will never be reached in this case and should be removed.

This whole thing needs refactoring actually, smth like:

diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp
index 0eebe5c9a..70dd72c07 100644
--- a/src/wallet/rpcdump.cpp
+++ b/src/wallet/rpcdump.cpp
@@ -705,26 +705,23 @@ UniValue dumphdinfo(const JSONRPCRequest& request)
 
     EnsureWalletIsUnlocked();
 
-    // add the base58check encoded extended master if the wallet uses HD
     CHDChain hdChainCurrent;
-    if (pwalletMain->GetHDChain(hdChainCurrent))
-    {
-        if (!pwalletMain->GetDecryptedHDChain(hdChainCurrent))
-            throw JSONRPCError(RPC_INTERNAL_ERROR, "Cannot decrypt HD seed");
+    if (!pwalletMain->GetHDChain(hdChainCurrent))
+        throw JSONRPCError(RPC_WALLET_ERROR, "This wallet is not a HD wallet.");
 
-        SecureString ssMnemonic;
-        SecureString ssMnemonicPassphrase;
-        hdChainCurrent.GetMnemonic(ssMnemonic, ssMnemonicPassphrase);
+    if (!pwalletMain->GetDecryptedHDChain(hdChainCurrent))
+        throw JSONRPCError(RPC_INTERNAL_ERROR, "Cannot decrypt HD seed");
 
-        UniValue obj(UniValue::VOBJ);
-        obj.push_back(Pair("hdseed", HexStr(hdChainCurrent.GetSeed())));
-        obj.push_back(Pair("mnemonic", ssMnemonic.c_str()));
-        obj.push_back(Pair("mnemonicpassphrase", ssMnemonicPassphrase.c_str()));
+    SecureString ssMnemonic;
+    SecureString ssMnemonicPassphrase;
+    hdChainCurrent.GetMnemonic(ssMnemonic, ssMnemonicPassphrase);
 
-        return obj;
-    }
+    UniValue obj(UniValue::VOBJ);
+    obj.push_back(Pair("hdseed", HexStr(hdChainCurrent.GetSeed())));
+    obj.push_back(Pair("mnemonic", ssMnemonic.c_str()));
+    obj.push_back(Pair("mnemonicpassphrase", ssMnemonicPassphrase.c_str()));
 
-    return NullUniValue;
+    return obj;
 }
 
 UniValue dumpwallet(const JSONRPCRequest& request)

@nmarley
Copy link

nmarley commented Jun 18, 2018

I agree that some feedback would be better than none.

Personally I would prefer if we could move to JSONRPC 2.0, avoiding JSONRPCErrors (or refactoring) and returning valid JSON even if an error is encountered, but I guess that's another discussion entirely.

@UdjinM6 UdjinM6 added this to the 12.3 milestone Jun 18, 2018
@PastaPastaPasta
Copy link
Member Author

PastaPastaPasta commented Jun 19, 2018

Based on @UdjinM6's comment, the code has been refactored.
Relevant section:

    CHDChain hdChainCurrent;
    if (!pwalletMain->GetHDChain(hdChainCurrent))
        throw JSONRPCError(RPC_WALLET_ERROR, "This wallet is not a HD wallet.");

    if (!pwalletMain->GetDecryptedHDChain(hdChainCurrent))
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Cannot decrypt HD seed");

    SecureString ssMnemonic;
    SecureString ssMnemonicPassphrase;
    hdChainCurrent.GetMnemonic(ssMnemonic, ssMnemonicPassphrase);

    UniValue obj(UniValue::VOBJ);
    obj.push_back(Pair("hdseed", HexStr(hdChainCurrent.GetSeed())));
    obj.push_back(Pair("mnemonic", ssMnemonic.c_str()));
    obj.push_back(Pair("mnemonicpassphrase", ssMnemonicPassphrase.c_str()));

    return obj;

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@UdjinM6 UdjinM6 changed the title Adds DumpHDInfo Error when wallet isn't HD RPC: dumphdinfo should throw an error when wallet isn't HD Jun 19, 2018
@UdjinM6 UdjinM6 merged commit 700b7ce into dashpay:develop Jun 19, 2018
@PastaPastaPasta PastaPastaPasta deleted the dumphdinfo-patch-adderror branch June 24, 2018 02:28
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
)

* Throws an RPCError `RPC_WALLET_ERROR` when the command `dumphdinfo` is called and the wallet is not HD

* Refactor based on @Udjin's post
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Mar 1, 2019
)

* Throws an RPCError `RPC_WALLET_ERROR` when the command `dumphdinfo` is called and the wallet is not HD

* Refactor based on @Udjin's post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants