Repository navigation
wallet: Add deriveHDKey interface - #36070
PraneethGunas wants to merge 5 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
|
concept ACK will review soon :) |
jeanpablojp
left a comment
There was a problem hiding this comment.
Concept ACK
Left some comments.
| return *xprv; | ||
| } | ||
|
|
||
| util::Expected<std::pair<CExtKey, KeyOriginInfo>, WalletError> CWallet::DeriveHDKey(const std::vector<uint32_t>& path, const std::optional<CExtPubKey>& hdkey) const |
There was a problem hiding this comment.
in a1250d2 wallet: Add deriveHDKey interface
All this is duplicated from RPC code. Why not just make both the interface and the RPC rely on the same code? Because of both using different code functions, they both return different error codes for same errors.
Also probably this commit could be split in different commits. First a preparatory commit that creates the DeriveHDKey function + testing, then a commit rebasing the RPC to use it, then a commit creating the interface. You can check #34861 for a commit structure idea :)
There was a problem hiding this comment.
Done. Split into "Add CWallet::DeriveHDKey" and "Add deriveHDKey interface".
On sharing code with the RPC: it already calls SelectHDKey(). What's left is the watch-only, hardened-path and unlock guards, which pin the error codes wallet_derivehdkey.py asserts (-4, -8, -13).
DeriveHDKey() returns GenericError for every failure, so routing the rest through it would turn "Unable to derive HD key at the requested path" from -8 into -5. Keeping -8 would need a new WalletErrorCode just so one caller can pick a different number, which src/wallet/types.h:48-50 warns against.
There was a problem hiding this comment.
Now both the interface and the RPC rely on the same CWallet::DeriveHDKey
a1250d2 to
6131eab
Compare
jeanpablojp
left a comment
There was a problem hiding this comment.
Reviewed again. Left one more comment on the new test.
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
6131eab to
32d7ebf
Compare
|
From https://github.com/bitcoin/bitcoin/actions/runs/33532997440/job/100190403622?pr=36070: --- a/src/interfaces/wallet.h
+++ b/src/interfaces/wallet.h
@@ -34,19 +34,16 @@
#include <vector>
class ArgsManager;
-class CKeyID;
-class CPubKey;
class CScript;
class PartiallySignedTransaction;
class uint256;
enum class FeeReason;
enum class OutputType;
struct bilingual_str;
+
namespace wallet {
-struct CreatedTransactionResult;
class CCoinControl;
class CWallet;
-enum class AddressPurpose;
struct CRecipient;
struct WalletContext;
} // namespace wallet |
32d7ebf to
3c88685
Compare
|
Why is |
It was done so that the the RPC error codes and messages are preserved. Also, looking at the discussions in #35436 (comment) it makes sense to collapse the error codes down based on how the errors are logged/displayed #35436 (comment) |
|
Rebased on master and rebuilt the history |
pseudoramdom
left a comment
There was a problem hiding this comment.
Concept ACK. Left some comments
| master_xpub_2 = wallet.addhdkey()['xpub'] | ||
| assert_raises_rpc_error( | ||
| -5, | ||
| -4, |
There was a problem hiding this comment.
In wallet: Add CWallet::DeriveHDKey
I'm not sure if we're okay with changing the error contract since derivehdkey will ship in v32.
If not, we likely have to duplicate the checks to retain the error codes.
@achow101
There was a problem hiding this comment.
Restored the watch-only and unlock checks in the RPC, so -4 and -13 are unchanged from v32.
-8 and -5 are now collapsed to -4, or we can duplicate the checks in the RPC to preserve the v32 behaviour.
There was a problem hiding this comment.
Haven't reviewed yet, but re changing error codes:
#35690 (comment)
There was a problem hiding this comment.
At this point we should add an invalid parameter error code to at least distinguish caller problems from wallet problems. I'm fine with collapsing INVALID_ADDRESS_OR_KEY with INVALID_PARAMETER, but collapsing those into WALLET_ERROR makes the error code to the user too vague. WALLET_ERROR is really for "something bad happened in the wallet internally", as opposed to "the user gave us a bad argument".
There was a problem hiding this comment.
Agree, after importdescriptors was merged this should be easy.
Those errors already exist now:
Lines 66 to 83 in e7aef7e
There was a problem hiding this comment.
I'll remap them based on invalid param or wallet error
431f0d9 to
462038a
Compare
|
Approach ACK. Will review soon. |
|
What do you think about #36070 (comment) ? |
Move the HD key selection, private key lookup, and derivation out of the derivehdkey RPC into a static DeriveHDKey function in the same file.
Move the watch-only, hardened-path, unlock and xpub-validity checks into DeriveHDKey in their existing order, retaining the v32 RPC codes and messages. The RPC still decodes its inputs and holds the wallet lock. Input parsing now precedes the watch-only check, so a malformed path supplied to a watch-only wallet reports the path error first.
Return WalletError from the helper instead of throwing RPC errors. Keep the checks in the same order. The locked-wallet error now uses the common wallet unlock message. Argument errors use InvalidParameter and map to RPC_INVALID_PARAMETER, including cases previously reported as RPC_INVALID_ADDRESS_OR_KEY. Use HandleWalletErrorCode to translate wallet errors back to RPC codes.
Move DeriveHDKey from the derivehdkey RPC into CWallet so that it is not tied to the RPC.
Only the xpub is returned, since interfaces::Wallet is not authenticated and should not pass private keys.
462038a to
ea18029
Compare
Thanks for the proposal. I’ve reworked the history along those lines, separating the check moves, error changes, and move into CWallet |
| return xpubs; | ||
| } | ||
|
|
||
| util::Expected<std::pair<CExtKey, KeyOriginInfo>, WalletError> CWallet::DeriveHDKey(const std::vector<uint32_t>& path, std::optional<CExtPubKey> hdkey) const |
There was a problem hiding this comment.
Derivation functionality works as expected
On Master
ratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli loadwallet testwallet
{
"name": "testwallet"
}
ratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -named derivehdkey path="m/84h/1h/0h"
{
"origin": "[ce287f45/84h/1h/0h]",
"xpub": "tpubDCGyDHJvhr4r2BMf47yvpTDvP2BM6GhsF1dETWpmXetF4zjMnpettacQYNdgdzYd8FTW6LS3UuPWpqPuZgWmDu1dDSYbXfgwSsKX8upwmE1"
}
ratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$PR Branch
ratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli restorewallet testwallet ~/Desktop/testwallet.dat
{
"name": "testwallet"
}
ratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -named derivehdkey path="m/84h/1h/0h"
{
"origin": "[ce287f45/84h/1h/0h]",
"xpub": "tpubDCGyDHJvhr4r2BMf47yvpTDvP2BM6GhsF1dETWpmXetF4zjMnpettacQYNdgdzYd8FTW6LS3UuPWpqPuZgWmDu1dDSYbXfgwSsKX8upwmE1"
}
ratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ |
|
||
| if (IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | ||
| // Watch-only wallets can't contain unused(KEY) descriptors | ||
| return util::Unexpected{WalletError{WalletErrorCode::GenericError, _("derivehdkey is not available for watch-only wallets")}}; |
There was a problem hiding this comment.
Is there any difference between the error message substring derivehdkey and the current RPC derivehdkey "path" ( {"private":bool,"hdkey":"str",...} )? Wouldn't a universal message work best if the functionality should be exposed to the GUI too?
| HDPubKeyMap wallet_xpubs{GetHDPubKeys(HDKeyFilter::UnusedKey)}; | ||
|
|
||
| if (wallet_xpubs.size() > 1) { | ||
| return util::Unexpected{WalletError{WalletErrorCode::InvalidParameter, _("Unable to determine which HD key to use. Please specify with 'hdkey'")}}; |
There was a problem hiding this comment.
Same case here. Is the hdkey used here different from the named argument of derivehdkey RPC? A universal message would probably work better for the GUI users too
| } | ||
|
|
||
| if (active_xpubs.size() > 1) { | ||
| return util::Unexpected{WalletError{WalletErrorCode::InvalidParameter, _("Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'")}}; |
There was a problem hiding this comment.
Same case here. Is the
hdkeyused here different from the named argument ofderivehdkeyRPC? A universal message would probably work better for the GUI users too

This PR adds a wallet interface for derivehdkey.
The motivation is the same as #35436 and #34861. The derivehdkey RPC exists
(#32784), but the GUI does not go through RPC, so the logic is currently out of
its reach. A dedicated wallet interface makes it available for multisig setup.
Alongside the addhdkey interface, this lets the GUI add an HD key and derive a
shareable xpub from it. Tracked as the deriveHDKey item in #35645.
Key changes: