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>
This commit is contained in:
Antoine Poinsot 2022-03-17 13:36:58 +01:00
parent f4e289f384
commit 4fe29368c0
No known key found for this signature in database
GPG key ID: E13FC145CD3F4304

View file

@ -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