14template<
typename M,
typename K,
typename V>
17 auto it =
map.find(key);
18 if (it !=
map.end()) {
85 std::pair<CPubKey, KeyOriginInfo> out;
87 if (
ret) info = std::move(out.second);
183 if (!
GetKey(address, key)) {
201 return mapKeys.contains(address);
217 KeyMap::const_iterator
mi =
mapKeys.find(address);
269 if (
auto id = std::get_if<PKHash>(&dest)) {
272 if (
auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
275 if (
auto script_hash = std::get_if<ScriptHash>(&dest)) {
285 if (
auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
314 if (provider->GetPubKey(
keyid, pubkey))
return true;
323 if (provider->GetKeyOrigin(
keyid, info))
return true;
331 if (provider->GetKey(
keyid, key))
return true;
356 for (
auto&
leaf :
a.leaves) {
357 leaf.merkle_branch.push_back(b.hash);
361 for (
auto&
leaf : b.leaves) {
362 leaf.merkle_branch.push_back(
a.hash);
363 ret.leaves.emplace_back(std::move(
leaf));
390 if ((
size_t)depth + 1 <
m_branch.size()) {
399 if (depth == 0)
m_valid =
false;
413 for (
int depth :
depths) {
419 if ((
size_t)depth + 1 <
branch.size())
return false;
422 if (depth == 0)
return false;
485 if (
leaf.merkle_branch.size()) {
486 std::copy(
leaf.merkle_branch[0].begin(),
500 if (!
tweak ||
tweak->first != output)
return std::nullopt;
502 std::vector<std::tuple<int, std::vector<unsigned char>,
int>>
ret;
510 std::unique_ptr<TreeNode>
sub[2];
513 const std::pair<std::vector<unsigned char>,
int>*
leaf =
nullptr;
538 if (merkle_root !=
spenddata.merkle_root)
continue;
542 for (
size_t depth = 0; depth <
levels; ++depth) {
544 if (
node->explored && !
node->inner)
return std::nullopt;
555 for (
int i = 0; i < 2; ++i) {
556 if (
node->sub[i]->hash == hash || (
node->sub[i]->hash.IsNull() &&
node->sub[1-i]->hash != hash)) {
557 node->sub[i]->hash = hash;
563 if (!desc)
return std::nullopt;
566 node->explored =
true;
568 node->sub[0] = std::make_unique<TreeNode>();
569 node->sub[1] = std::make_unique<TreeNode>();
570 node->sub[1]->hash = hash;
575 if (
node->sub[0])
return std::nullopt;
576 node->explored =
true;
585 std::vector<TreeNode*> stack{&root};
586 while (!stack.empty()) {
588 if (!
node.explored) {
591 }
else if (!
node.inner) {
593 ret.emplace_back(stack.size() - 1,
node.leaf->first,
node.leaf->second);
596 }
else if (
node.sub[0]->done && !
node.sub[1]->done && !
node.sub[1]->explored && !
node.sub[1]->hash.IsNull() &&
611 node.sub[0]->done =
false;
612 node.sub[1]->done =
true;
613 }
else if (
node.sub[0]->done &&
node.sub[1]->done) {
615 node.sub[0]->done =
false;
616 node.sub[1]->done =
false;
619 }
else if (!
node.sub[0]->done) {
621 stack.push_back(&*
node.sub[0]);
622 }
else if (!
node.sub[1]->done) {
624 stack.push_back(&*
node.sub[1]);
634 std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>>
tuples;
636 const auto& leaves =
m_branch[0]->leaves;
637 for (
const auto&
leaf : leaves) {
CScriptID ToScriptID(const ScriptHash &script_hash)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CKeyID ToKeyID(const PKHash &key_hash)
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
An encapsulated private key.
CPubKey GetPubKey() const
Compute the public key from a private key.
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
bool IsCompressed() const
Check whether this is a compressed public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Serialized script, used inside transaction inputs and outputs.
A reference to a CScript: the Hash160 of its serialization.
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
virtual bool AddCScript(const CScript &redeemScript)
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual std::set< CKeyID > GetKeys() const
virtual std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
RecursiveMutex cs_KeyStore
virtual bool HaveKey(const CKeyID &address) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
void DeleteMuSig2Session(const uint256 &session_id) const override
const SigningProvider * m_provider
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
MuSig2SecNonce encapsulates a secret nonce in use in a MuSig2 signing session.
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
std::vector< std::unique_ptr< SigningProvider > > m_providers
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
void AddProvider(std::unique_ptr< SigningProvider > provider)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An interface to be implemented by keystores that support signing.
virtual std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
virtual std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
virtual void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const
virtual void DeleteMuSig2Session(const uint256 &session_id) const
virtual bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
bool GetPubKeyByXOnly(const XOnlyPubKey &pubkey, CPubKey &out) const
virtual std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const
Utility class to construct Taproot outputs from internal key and script tree.
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
TaprootBuilder & Add(int depth, std::span< const unsigned char > script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
XOnlyPubKey m_output_key
The output key, computed when finalizing.
bool IsValid() const
Return true if so far all input was valid.
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
bool m_parity
The tweak parity, computed when finalizing.
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > GetTreeTuples() const
Returns a vector of tuples representing the depth, leaf version, and script.
bool m_valid
Whether the builder is in a valid state so far.
const unsigned char * end() const
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
const unsigned char * begin() const
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
bool IsFullyValid() const
Determine if this pubkey is fully valid.
constexpr bool IsNull() const
static constexpr unsigned int size()
constexpr unsigned char * begin()
uint256 ComputeTapbranchHash(std::span< const unsigned char > a, std::span< const unsigned char > b)
Compute the BIP341 tapbranch hash from two branches.
uint256 ComputeTaprootMerkleRoot(std::span< const unsigned char > control, const uint256 &tapleaf_hash)
Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.
uint256 ComputeTapleafHash(uint8_t leaf_version, std::span< const unsigned char > script)
Compute the BIP341 tapleaf hash from leaf version & script.
static constexpr uint8_t TAPROOT_LEAF_MASK
static constexpr size_t TAPROOT_CONTROL_NODE_SIZE
static constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
static constexpr size_t TAPROOT_CONTROL_MAX_SIZE
static constexpr size_t TAPROOT_CONTROL_BASE_SIZE
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
std::optional< std::vector< std::tuple< int, std::vector< unsigned char >, int > > > InferTaprootTree(const TaprootSpendData &spenddata, const XOnlyPubKey &output)
Given a TaprootSpendData and the output key, reconstruct its script tree.
const SigningProvider & DUMMY_SIGNING_PROVIDER
bool LookupHelper(const M &map, const K &key, V &value)
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
FlatSigningProvider & Merge(FlatSigningProvider &&b) LIFETIMEBOUND
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CPubKey, std::vector< CPubKey > > aggregate_pubkeys
Map from output key to Taproot tree (which can then make the TaprootSpendData.
std::map< uint256, MuSig2SecNonce > * musig2_secnonces
MuSig2 aggregate pubkeys.
std::map< CKeyID, CPubKey > pubkeys
std::map< CKeyID, CKey > keys
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::map< CScriptID, CScript > scripts
std::map< XOnlyPubKey, TaprootBuilder > tr_trees
void DeleteMuSig2Session(const uint256 &session_id) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
bool HaveKey(const CKeyID &keyid) const override
Information about a tracked leaf in the Merkle tree.
Information associated with a node in the Merkle tree.
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
std::map< std::pair< std::vector< unsigned char >, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
XOnlyPubKey internal_key
The BIP341 internal key.
#define AssertLockHeld(cs)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.