Skip to content

Commit 5d61a9a

Browse files
committed
wallet: move GetID() to Descriptor, add test vectors
This ensures that ToString(COMPAT) can't be changed accidentally.
1 parent 0f294a7 commit 5d61a9a

File tree

5 files changed

+89
-62
lines changed

5 files changed

+89
-62
lines changed

src/script/descriptor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ struct PubkeyProvider
209209

210210
/** Derive a private key, if private data is available in arg. */
211211
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
212+
213+
virtual uint256 GetID() const = 0;
212214
};
213215

214216
class OriginPubkeyProvider final : public PubkeyProvider
@@ -260,6 +262,7 @@ class OriginPubkeyProvider final : public PubkeyProvider
260262
{
261263
return m_provider->GetPrivKey(pos, arg, key);
262264
}
265+
uint256 GetID() const override { return uint256(); }
263266
};
264267

265268
/** An object representing a parsed constant public key in a descriptor. */
@@ -305,6 +308,7 @@ class ConstPubkeyProvider final : public PubkeyProvider
305308
{
306309
return arg.GetKey(m_pubkey.GetID(), key);
307310
}
311+
uint256 GetID() const override { return uint256(); }
308312
};
309313

310314
enum class DeriveType {
@@ -520,6 +524,8 @@ class BIP32PubkeyProvider final : public PubkeyProvider
520524
key = extkey.key;
521525
return true;
522526
}
527+
528+
uint256 GetID() const override { return uint256(); }
523529
};
524530

525531
/** Base class for all Descriptor implementations. */
@@ -702,6 +708,15 @@ class DescriptorImpl : public Descriptor
702708
}
703709

704710
std::optional<OutputType> GetOutputType() const override { return std::nullopt; }
711+
712+
virtual uint256 GetID() const override {
713+
// Always use the apostrophe for spkm ID
714+
std::string desc_str = ToString(/*compat_format=*/true);
715+
uint256 id;
716+
CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin());
717+
return id;
718+
}
719+
705720
};
706721

707722
/** A parsed addr(A) descriptor. */

src/script/descriptor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ struct Descriptor {
146146

147147
/** @return The OutputType of the scriptPubKey(s) produced by this descriptor. Or nullopt if indeterminate (multiple or none) */
148148
virtual std::optional<OutputType> GetOutputType() const = 0;
149+
150+
151+
/* Unique identifier that may not change over time, unless explictly marked as not backwards compatible.
152+
This is not part of BIP 380, not guaranteed to be interoperable and should not be exposed to the user.
153+
*/
154+
virtual uint256 GetID() const = 0;
155+
149156
};
150157

151158
/** Parse a `descriptor` string. Included private keys are put in `out`.

0 commit comments

Comments
 (0)