From 4fe29368c0ded0e62f437cab3a7c904f7fd3ad67 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Thu, 17 Mar 2022 13:36:58 +0100 Subject: script: expose getter for CScriptNum, add a BuildScript helper Some prep work for Miniscript. BuildScript is an efficient way to build Scripts in a generic manner (by concatenating OPs, data, and other Scripts). Co-Authored-By: Pieter Wuille --- src/script/script.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/script/script.h') diff --git a/src/script/script.h b/src/script/script.h index c722374113..3b799ad637 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -335,6 +335,8 @@ public: return m_value; } + int64_t GetInt64() const { return m_value; } + std::vector getvch() const { return serialize(m_value); @@ -578,4 +580,29 @@ bool IsOpSuccess(const opcodetype& opcode); bool CheckMinimalPush(const std::vector& data, opcodetype opcode); +/** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */ +template +CScript BuildScript(Ts&&... inputs) +{ + CScript ret; + int cnt{0}; + + ([&ret, &cnt] (Ts&& input) { + cnt++; + if constexpr (std::is_same_v>, CScript>) { + // If it is a CScript, extend ret with it. Move or copy the first element instead. + if (cnt == 0) { + ret = std::forward(input); + } else { + ret.insert(ret.end(), input.begin(), input.end()); + } + } else { + // Otherwise invoke CScript::operator<<. + ret << input; + } + } (std::forward(inputs)), ...); + + return ret; +} + #endif // BITCOIN_SCRIPT_SCRIPT_H -- cgit v1.2.3