Skip to content

Commit 2c6281f

Browse files
committed
Add key origin support to descriptors
1 parent 5c25409 commit 2c6281f

File tree

3 files changed

+100
-27
lines changed

3 files changed

+100
-27
lines changed

src/script/descriptor.cpp

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct PubkeyProvider
4141
virtual ~PubkeyProvider() = default;
4242

4343
/** Derive a public key. */
44-
virtual bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& out) const = 0;
44+
virtual bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info) const = 0;
4545

4646
/** Whether this represent multiple public keys at different positions. */
4747
virtual bool IsRange() const = 0;
@@ -56,16 +56,50 @@ struct PubkeyProvider
5656
virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
5757
};
5858

59+
class OriginPubkeyProvider final : public PubkeyProvider
60+
{
61+
KeyOriginInfo m_origin;
62+
std::unique_ptr<PubkeyProvider> m_provider;
63+
64+
std::string OriginString() const
65+
{
66+
return HexStr(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint)) + FormatKeyPath(m_origin.path);
67+
}
68+
69+
public:
70+
OriginPubkeyProvider(KeyOriginInfo info, std::unique_ptr<PubkeyProvider> provider) : m_origin(std::move(info)), m_provider(std::move(provider)) {}
71+
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info) const override
72+
{
73+
if (!m_provider->GetPubKey(pos, arg, key, info)) return false;
74+
std::copy(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint), info.fingerprint);
75+
info.path.insert(info.path.begin(), m_origin.path.begin(), m_origin.path.end());
76+
return true;
77+
}
78+
bool IsRange() const override { return m_provider->IsRange(); }
79+
size_t GetSize() const override { return m_provider->GetSize(); }
80+
std::string ToString() const override { return "[" + OriginString() + "]" + m_provider->ToString(); }
81+
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
82+
{
83+
std::string sub;
84+
if (!m_provider->ToPrivateString(arg, sub)) return false;
85+
ret = "[" + OriginString() + "]" + std::move(sub);
86+
return true;
87+
}
88+
};
89+
5990
/** An object representing a parsed constant public key in a descriptor. */
6091
class ConstPubkeyProvider final : public PubkeyProvider
6192
{
6293
CPubKey m_pubkey;
6394

6495
public:
6596
ConstPubkeyProvider(const CPubKey& pubkey) : m_pubkey(pubkey) {}
66-
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& out) const override
97+
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info) const override
6798
{
68-
out = m_pubkey;
99+
key = m_pubkey;
100+
info.path.clear();
101+
CKeyID keyid = m_pubkey.GetID();
102+
std::copy(keyid.begin(), keyid.begin() + sizeof(info.fingerprint), info.fingerprint);
69103
return true;
70104
}
71105
bool IsRange() const override { return false; }
@@ -98,7 +132,7 @@ class BIP32PubkeyProvider final : public PubkeyProvider
98132
CKey key;
99133
if (!arg.GetKey(m_extkey.pubkey.GetID(), key)) return false;
100134
ret.nDepth = m_extkey.nDepth;
101-
std::copy(m_extkey.vchFingerprint, m_extkey.vchFingerprint + 4, ret.vchFingerprint);
135+
std::copy(m_extkey.vchFingerprint, m_extkey.vchFingerprint + sizeof(ret.vchFingerprint), ret.vchFingerprint);
102136
ret.nChild = m_extkey.nChild;
103137
ret.chaincode = m_extkey.chaincode;
104138
ret.key = key;
@@ -118,27 +152,32 @@ class BIP32PubkeyProvider final : public PubkeyProvider
118152
BIP32PubkeyProvider(const CExtPubKey& extkey, KeyPath path, DeriveType derive) : m_extkey(extkey), m_path(std::move(path)), m_derive(derive) {}
119153
bool IsRange() const override { return m_derive != DeriveType::NO; }
120154
size_t GetSize() const override { return 33; }
121-
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& out) const override
155+
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info) const override
122156
{
123157
if (IsHardened()) {
124-
CExtKey key;
125-
if (!GetExtKey(arg, key)) return false;
158+
CExtKey extkey;
159+
if (!GetExtKey(arg, extkey)) return false;
126160
for (auto entry : m_path) {
127-
key.Derive(key, entry);
161+
extkey.Derive(extkey, entry);
128162
}
129-
if (m_derive == DeriveType::UNHARDENED) key.Derive(key, pos);
130-
if (m_derive == DeriveType::HARDENED) key.Derive(key, pos | 0x80000000UL);
131-
out = key.Neuter().pubkey;
163+
if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
164+
if (m_derive == DeriveType::HARDENED) extkey.Derive(extkey, pos | 0x80000000UL);
165+
key = extkey.Neuter().pubkey;
132166
} else {
133167
// TODO: optimize by caching
134-
CExtPubKey key = m_extkey;
168+
CExtPubKey extkey = m_extkey;
135169
for (auto entry : m_path) {
136-
key.Derive(key, entry);
170+
extkey.Derive(extkey, entry);
137171
}
138-
if (m_derive == DeriveType::UNHARDENED) key.Derive(key, pos);
172+
if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
139173
assert(m_derive != DeriveType::HARDENED);
140-
out = key.pubkey;
174+
key = extkey.pubkey;
141175
}
176+
CKeyID keyid = m_extkey.pubkey.GetID();
177+
std::copy(keyid.begin(), keyid.begin() + sizeof(info.fingerprint), info.fingerprint);
178+
info.path = m_path;
179+
if (m_derive == DeriveType::UNHARDENED) info.path.push_back((uint32_t)pos);
180+
if (m_derive == DeriveType::HARDENED) info.path.push_back(((uint32_t)pos) | 0x80000000L);
142181
return true;
143182
}
144183
std::string ToString() const override
@@ -221,9 +260,11 @@ class SingleKeyDescriptor final : public Descriptor
221260
bool Expand(int pos, const SigningProvider& arg, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override
222261
{
223262
CPubKey key;
224-
if (!m_provider->GetPubKey(pos, arg, key)) return false;
263+
KeyOriginInfo info;
264+
if (!m_provider->GetPubKey(pos, arg, key, info)) return false;
225265
output_scripts = std::vector<CScript>{m_script_fn(key)};
226-
out.pubkeys.emplace(key.GetID(), std::move(key));
266+
out.origins.emplace(key.GetID(), std::move(info));
267+
out.pubkeys.emplace(key.GetID(), key);
227268
return true;
228269
}
229270
};
@@ -272,15 +313,19 @@ class MultisigDescriptor : public Descriptor
272313

273314
bool Expand(int pos, const SigningProvider& arg, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override
274315
{
275-
std::vector<CPubKey> pubkeys;
276-
pubkeys.reserve(m_providers.size());
316+
std::vector<std::pair<CPubKey, KeyOriginInfo>> entries;
317+
entries.reserve(m_providers.size());
318+
// Construct temporary data in `entries`, to avoid producing output in case of failure.
277319
for (const auto& p : m_providers) {
278-
CPubKey key;
279-
if (!p->GetPubKey(pos, arg, key)) return false;
280-
pubkeys.push_back(key);
320+
entries.emplace_back();
321+
if (!p->GetPubKey(pos, arg, entries.back().first, entries.back().second)) return false;
281322
}
282-
for (const CPubKey& key : pubkeys) {
283-
out.pubkeys.emplace(key.GetID(), std::move(key));
323+
std::vector<CPubKey> pubkeys;
324+
pubkeys.reserve(entries.size());
325+
for (auto& entry : entries) {
326+
pubkeys.push_back(entry.first);
327+
out.origins.emplace(entry.first.GetID(), std::move(entry.second));
328+
out.pubkeys.emplace(entry.first.GetID(), entry.first);
284329
}
285330
output_scripts = std::vector<CScript>{GetScriptForMultisig(m_threshold, pubkeys)};
286331
return true;
@@ -343,13 +388,15 @@ class ComboDescriptor final : public Descriptor
343388
bool Expand(int pos, const SigningProvider& arg, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override
344389
{
345390
CPubKey key;
346-
if (!m_provider->GetPubKey(pos, arg, key)) return false;
391+
KeyOriginInfo info;
392+
if (!m_provider->GetPubKey(pos, arg, key, info)) return false;
347393
CKeyID keyid = key.GetID();
348394
{
349395
CScript p2pk = GetScriptForRawPubKey(key);
350396
CScript p2pkh = GetScriptForDestination(keyid);
351397
output_scripts = std::vector<CScript>{std::move(p2pk), std::move(p2pkh)};
352398
out.pubkeys.emplace(keyid, key);
399+
out.origins.emplace(keyid, std::move(info));
353400
}
354401
if (key.IsCompressed()) {
355402
CScript p2wpkh = GetScriptForDestination(WitnessV0KeyHash(keyid));
@@ -447,7 +494,8 @@ bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out)
447494
return true;
448495
}
449496

450-
std::unique_ptr<PubkeyProvider> ParsePubkey(const Span<const char>& sp, bool permit_uncompressed, FlatSigningProvider& out)
497+
/** Parse a public key that excludes origin information. */
498+
std::unique_ptr<PubkeyProvider> ParsePubkeyInner(const Span<const char>& sp, bool permit_uncompressed, FlatSigningProvider& out)
451499
{
452500
auto split = Split(sp, '/');
453501
std::string str(split[0].begin(), split[0].end());
@@ -484,6 +532,28 @@ std::unique_ptr<PubkeyProvider> ParsePubkey(const Span<const char>& sp, bool per
484532
return MakeUnique<BIP32PubkeyProvider>(extpubkey, std::move(path), type);
485533
}
486534

535+
/** Parse a public key including origin information (if enabled). */
536+
std::unique_ptr<PubkeyProvider> ParsePubkey(const Span<const char>& sp, bool permit_uncompressed, FlatSigningProvider& out)
537+
{
538+
auto origin_split = Split(sp, ']');
539+
if (origin_split.size() > 2) return nullptr;
540+
if (origin_split.size() == 1) return ParsePubkeyInner(origin_split[0], permit_uncompressed, out);
541+
if (origin_split[0].size() < 1 || origin_split[0][0] != '[') return nullptr;
542+
auto slash_split = Split(origin_split[0].subspan(1), '/');
543+
if (slash_split[0].size() != 8) return nullptr;
544+
std::string fpr_hex = std::string(slash_split[0].begin(), slash_split[0].end());
545+
if (!IsHex(fpr_hex)) return nullptr;
546+
auto fpr_bytes = ParseHex(fpr_hex);
547+
KeyOriginInfo info;
548+
static_assert(sizeof(info.fingerprint) == 4, "Fingerprint must be 4 bytes");
549+
assert(fpr_bytes.size() == 4);
550+
std::copy(fpr_bytes.begin(), fpr_bytes.end(), info.fingerprint);
551+
if (!ParseKeyPath(slash_split, info.path)) return nullptr;
552+
auto provider = ParsePubkeyInner(origin_split[1], permit_uncompressed, out);
553+
if (!provider) return nullptr;
554+
return MakeUnique<OriginPubkeyProvider>(std::move(info), std::move(provider));
555+
}
556+
487557
/** Parse a script in a particular context. */
488558
std::unique_ptr<Descriptor> ParseScript(Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out)
489559
{

src/script/sign.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ bool HidingSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& inf
686686

687687
bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
688688
bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
689+
bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return LookupHelper(origins, keyid, info); }
689690
bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
690691

691692
FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b)

src/script/sign.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SigningProvider
3434
virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
3535
virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
3636
virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
37-
virtual bool GetKeyOrigin(const CKeyID& id, KeyOriginInfo& info) const { return false; }
37+
virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
3838
};
3939

4040
extern const SigningProvider& DUMMY_SIGNING_PROVIDER;
@@ -58,10 +58,12 @@ struct FlatSigningProvider final : public SigningProvider
5858
{
5959
std::map<CScriptID, CScript> scripts;
6060
std::map<CKeyID, CPubKey> pubkeys;
61+
std::map<CKeyID, KeyOriginInfo> origins;
6162
std::map<CKeyID, CKey> keys;
6263

6364
bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
6465
bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
66+
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
6567
bool GetKey(const CKeyID& keyid, CKey& key) const override;
6668
};
6769

0 commit comments

Comments
 (0)