Skip to content

Commit

Permalink
wallet: move GetID() to Descriptor, add test vectors
Browse files Browse the repository at this point in the history
This ensures that ToString(COMPAT) can't be changed accidentally.
  • Loading branch information
Sjors committed Jun 22, 2023
1 parent 0f294a7 commit 5d61a9a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 62 deletions.
15 changes: 15 additions & 0 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ struct PubkeyProvider

/** Derive a private key, if private data is available in arg. */
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;

virtual uint256 GetID() const = 0;

This comment has been minimized.

Copy link
@furszy

furszy Jun 22, 2023

Why this if it's not being used anywhere and all the implementations return an empty uint256?

Only the GetID() in the descriptor struct is enough.

This comment has been minimized.

Copy link
@Sjors

Sjors Jun 26, 2023

Author Owner

The compiler was complaining, but maybe I added it in one spot too many.

};

class OriginPubkeyProvider final : public PubkeyProvider
Expand Down Expand Up @@ -260,6 +262,7 @@ class OriginPubkeyProvider final : public PubkeyProvider
{
return m_provider->GetPrivKey(pos, arg, key);
}
uint256 GetID() const override { return uint256(); }
};

/** An object representing a parsed constant public key in a descriptor. */
Expand Down Expand Up @@ -305,6 +308,7 @@ class ConstPubkeyProvider final : public PubkeyProvider
{
return arg.GetKey(m_pubkey.GetID(), key);
}
uint256 GetID() const override { return uint256(); }
};

enum class DeriveType {
Expand Down Expand Up @@ -520,6 +524,8 @@ class BIP32PubkeyProvider final : public PubkeyProvider
key = extkey.key;
return true;
}

uint256 GetID() const override { return uint256(); }
};

/** Base class for all Descriptor implementations. */
Expand Down Expand Up @@ -702,6 +708,15 @@ class DescriptorImpl : public Descriptor
}

std::optional<OutputType> GetOutputType() const override { return std::nullopt; }

virtual uint256 GetID() const override {
// Always use the apostrophe for spkm ID
std::string desc_str = ToString(/*compat_format=*/true);
uint256 id;
CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin());
return id;
}

};

/** A parsed addr(A) descriptor. */
Expand Down
7 changes: 7 additions & 0 deletions src/script/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ struct Descriptor {

/** @return The OutputType of the scriptPubKey(s) produced by this descriptor. Or nullopt if indeterminate (multiple or none) */
virtual std::optional<OutputType> GetOutputType() const = 0;


/* Unique identifier that may not change over time, unless explictly marked as not backwards compatible.
This is not part of BIP 380, not guaranteed to be interoperable and should not be exposed to the user.
*/
virtual uint256 GetID() const = 0;

};

/** Parse a `descriptor` string. Included private keys are put in `out`.
Expand Down
Loading

0 comments on commit 5d61a9a

Please sign in to comment.