mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-12 05:34:57 +01:00
Merge #15024: Allow specific private keys to be derived from descriptor
53b7de629dAdd test for dumping the private key imported from descriptor (MeshCollider)2857bc4a64Extend importmulti descriptor tests (MeshCollider)81a884bbd0Import private keys from descriptor with importmulti if provided (MeshCollider)a4d1bd1a29Add private key derivation functions to descriptors (MeshCollider) Pull request description: ~This is based on #14491, review the last 3 commits only.~ Currently, descriptors have an Expand() function which returns public keys and scripts for a specific index of a ranged descriptor. But the private key for a specific index is not given. This allows private keys for specific indices to be derived. This also allows those keys to be imported through the `importmulti` RPC rather than having to provide them separately. ACKs for commit 53b7de: achow101: ACK53b7de629dTree-SHA512: c060bc01358a1adc76d3d470fefc2bdd39c837027f452e9bc4bd2e726097e1ece4af9d5627efd942a5f8819271e15ba54f010b169b50a9435a1f0f40fd1cebf3
This commit is contained in:
@@ -164,6 +164,9 @@ struct PubkeyProvider
|
||||
|
||||
/** Get the descriptor string form including private data (if available in arg). */
|
||||
virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
|
||||
|
||||
/** Derive a private key, if private data is available in arg. */
|
||||
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
|
||||
};
|
||||
|
||||
class OriginPubkeyProvider final : public PubkeyProvider
|
||||
@@ -195,6 +198,10 @@ public:
|
||||
ret = "[" + OriginString() + "]" + std::move(sub);
|
||||
return true;
|
||||
}
|
||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
||||
{
|
||||
return m_provider->GetPrivKey(pos, arg, key);
|
||||
}
|
||||
};
|
||||
|
||||
/** An object representing a parsed constant public key in a descriptor. */
|
||||
@@ -222,6 +229,10 @@ public:
|
||||
ret = EncodeSecret(key);
|
||||
return true;
|
||||
}
|
||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
||||
{
|
||||
return arg.GetKey(m_pubkey.GetID(), key);
|
||||
}
|
||||
};
|
||||
|
||||
enum class DeriveType {
|
||||
@@ -266,14 +277,9 @@ public:
|
||||
{
|
||||
if (key) {
|
||||
if (IsHardened()) {
|
||||
CExtKey extkey;
|
||||
if (!GetExtKey(arg, extkey)) return false;
|
||||
for (auto entry : m_path) {
|
||||
extkey.Derive(extkey, entry);
|
||||
}
|
||||
if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
|
||||
if (m_derive == DeriveType::HARDENED) extkey.Derive(extkey, pos | 0x80000000UL);
|
||||
*key = extkey.Neuter().pubkey;
|
||||
CKey priv_key;
|
||||
if (!GetPrivKey(pos, arg, priv_key)) return false;
|
||||
*key = priv_key.GetPubKey();
|
||||
} else {
|
||||
// TODO: optimize by caching
|
||||
CExtPubKey extkey = m_extkey;
|
||||
@@ -312,6 +318,18 @@ public:
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
||||
{
|
||||
CExtKey extkey;
|
||||
if (!GetExtKey(arg, extkey)) return false;
|
||||
for (auto entry : m_path) {
|
||||
extkey.Derive(extkey, entry);
|
||||
}
|
||||
if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
|
||||
if (m_derive == DeriveType::HARDENED) extkey.Derive(extkey, pos | 0x80000000UL);
|
||||
key = extkey.key;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** Base class for all Descriptor implementations. */
|
||||
@@ -462,6 +480,20 @@ public:
|
||||
Span<const unsigned char> span = MakeSpan(cache);
|
||||
return ExpandHelper(pos, DUMMY_SIGNING_PROVIDER, &span, output_scripts, out, nullptr) && span.size() == 0;
|
||||
}
|
||||
|
||||
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
|
||||
{
|
||||
for (const auto& p : m_pubkey_args) {
|
||||
CKey key;
|
||||
if (!p->GetPrivKey(pos, provider, key)) continue;
|
||||
out.keys.emplace(key.GetPubKey().GetID(), key);
|
||||
}
|
||||
if (m_script_arg) {
|
||||
FlatSigningProvider subprovider;
|
||||
m_script_arg->ExpandPrivate(pos, provider, subprovider);
|
||||
out = Merge(out, subprovider);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** Construct a vector with one element, which is moved into it. */
|
||||
|
||||
@@ -60,6 +60,14 @@ struct Descriptor {
|
||||
* out: scripts and public keys necessary for solving the expanded scriptPubKeys will be put here (may be equal to provider).
|
||||
*/
|
||||
virtual bool ExpandFromCache(int pos, const std::vector<unsigned char>& cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
|
||||
|
||||
/** Expand the private key for a descriptor at a specified position, if possible.
|
||||
*
|
||||
* pos: the position at which to expand the descriptor. If IsRange() is false, this is ignored.
|
||||
* provider: the provider to query for the private keys.
|
||||
* out: any private keys available for the specified pos will be placed here.
|
||||
*/
|
||||
virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
|
||||
};
|
||||
|
||||
/** Parse a descriptor string. Included private keys are put in out.
|
||||
|
||||
Reference in New Issue
Block a user