Skip to content

Commit

Permalink
Explicitly support conversion between equivalent hash types
Browse files Browse the repository at this point in the history
ScriptHash <-> CScriptID
CKeyID -> PKHash
PKHash -> WitnessV0KeyHash
  • Loading branch information
Empact committed Jun 19, 2020
1 parent f32c1e0 commit 966a22d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;

CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
CScriptID::CScriptID(const ScriptHash& in) : uint160(static_cast<uint160>(in)) {}

ScriptHash::ScriptHash(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
ScriptHash::ScriptHash(const CScriptID& in) : uint160(static_cast<uint160>(in)) {}

PKHash::PKHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {}
PKHash::PKHash(const CKeyID& pubkey_id) : uint160(pubkey_id) {}

WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {}
WitnessV0KeyHash::WitnessV0KeyHash(const PKHash& pubkey_hash) : uint160(static_cast<uint160>(pubkey_hash)) {}

CKeyID ToKeyID(const PKHash& key_hash)
{
Expand Down
5 changes: 5 additions & 0 deletions src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static const bool DEFAULT_ACCEPT_DATACARRIER = true;

class CKeyID;
class CScript;
struct ScriptHash;

/** A reference to a CScript: the Hash160 of its serialization (see script.h) */
class CScriptID : public uint160
Expand All @@ -26,6 +27,7 @@ class CScriptID : public uint160
CScriptID() : uint160() {}
explicit CScriptID(const CScript& in);
explicit CScriptID(const uint160& in) : uint160(in) {}
explicit CScriptID(const ScriptHash& in);
};

/**
Expand Down Expand Up @@ -78,6 +80,7 @@ struct PKHash : public uint160
PKHash() : uint160() {}
explicit PKHash(const uint160& hash) : uint160(hash) {}
explicit PKHash(const CPubKey& pubkey);
explicit PKHash(const CKeyID& pubkey_id);
};
CKeyID ToKeyID(const PKHash& key_hash);

Expand All @@ -91,6 +94,7 @@ struct ScriptHash : public uint160
explicit ScriptHash(const PKHash& hash) = delete;
explicit ScriptHash(const uint160& hash) : uint160(hash) {}
explicit ScriptHash(const CScript& script);
explicit ScriptHash(const CScriptID& script);
};

struct WitnessV0ScriptHash : public uint256
Expand All @@ -105,6 +109,7 @@ struct WitnessV0KeyHash : public uint160
WitnessV0KeyHash() : uint160() {}
explicit WitnessV0KeyHash(const uint160& hash) : uint160(hash) {}
explicit WitnessV0KeyHash(const CPubKey& pubkey);
explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
};
CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);

Expand Down

0 comments on commit 966a22d

Please sign in to comment.