diff options
author | Antoine Poinsot <darosior@protonmail.com> | 2022-03-17 13:36:58 +0100 |
---|---|---|
committer | Antoine Poinsot <darosior@protonmail.com> | 2022-03-17 14:09:07 +0100 |
commit | 4fe29368c0ded0e62f437cab3a7c904f7fd3ad67 (patch) | |
tree | 0b5dcaac594ada37c009d9ed211f34c284241b99 /src/script/script.h | |
parent | f4e289f384efdda6c3f56e1e1c30820a91ac2612 (diff) |
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 <pieter@wuille.net>
Diffstat (limited to 'src/script/script.h')
-rw-r--r-- | src/script/script.h | 27 |
1 files changed, 27 insertions, 0 deletions
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<unsigned char> getvch() const { return serialize(m_value); @@ -578,4 +580,29 @@ bool IsOpSuccess(const opcodetype& opcode); bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode); +/** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */ +template<typename... Ts> +CScript BuildScript(Ts&&... inputs) +{ + CScript ret; + int cnt{0}; + + ([&ret, &cnt] (Ts&& input) { + cnt++; + if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>, CScript>) { + // If it is a CScript, extend ret with it. Move or copy the first element instead. + if (cnt == 0) { + ret = std::forward<Ts>(input); + } else { + ret.insert(ret.end(), input.begin(), input.end()); + } + } else { + // Otherwise invoke CScript::operator<<. + ret << input; + } + } (std::forward<Ts>(inputs)), ...); + + return ret; +} + #endif // BITCOIN_SCRIPT_SCRIPT_H |