diff --git a/src/script/script.cpp b/src/script/script.cpp index 9bdf3ed808..fba7745983 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -194,6 +194,49 @@ unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const return subscript.GetSigOpCount(true); } +// +// ELEMENTS: + +bool CScript::IsPegoutScript(uint256& genesis_hash, CScript& pegout_scriptpubkey) const +{ + const_iterator pc = begin(); + std::vector data; + opcodetype opcode; + + // OP_RETURN + if (!GetOp(pc, opcode, data) || opcode != OP_RETURN) { + return false; + } + + if (!GetOp(pc, opcode, data) || data.size() != 32 ) { + return false; + } + genesis_hash = uint256(data); + + // Read in parent chain destination scriptpubkey + if (!GetOp(pc, opcode, data) || data.size() == 0 ) { + return false; + } + pegout_scriptpubkey = CScript(data.begin(), data.end()); + + return true; +} + +bool CScript::IsPegoutScript(const uint256& genesis_hash_check) const +{ + uint256 genesis_hash; + CScript pegout_scriptpubkey; + // Ensure hash matches parent chain genesis block + if (this->IsPegoutScript(genesis_hash, pegout_scriptpubkey) && + genesis_hash_check == genesis_hash) { + return true; + } + return false; +} + +// END ELEMENTS +// + bool CScript::IsPayToScriptHash() const { // Extra-fast test for pay-to-script-hash CScripts: diff --git a/src/script/script.h b/src/script/script.h index 9c88b7e498..a66dbc9353 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -17,6 +17,7 @@ #include #include #include +#include #include // Maximum number of bytes pushable to the stack @@ -540,6 +541,15 @@ public: bool IsPayToWitnessScriptHash() const; bool IsWitnessProgram(int& version, std::vector& program) const; + // ELEMENTS: + // Returns true if script follows OP_RETURN + // and if correct it returns both things in the output parameters. + bool IsPegoutScript(uint256& genesis_hash, CScript& pegout_scriptpubkey) const; + // Returns true if script follows OP_RETURN + // it may also have additional pushes at the end. It checks + // the genesis hash matches the specified one. + bool IsPegoutScript(const uint256& genesis_hash) const; + /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */ bool IsPushOnly(const_iterator pc) const; bool IsPushOnly() const;