Skip to content

Commit 4fe2936

Browse files
darosiorsipa
andcommitted
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 <[email protected]>
1 parent f4e289f commit 4fe2936

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/script/script.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ class CScriptNum
335335
return m_value;
336336
}
337337

338+
int64_t GetInt64() const { return m_value; }
339+
338340
std::vector<unsigned char> getvch() const
339341
{
340342
return serialize(m_value);
@@ -578,4 +580,29 @@ bool IsOpSuccess(const opcodetype& opcode);
578580

579581
bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode);
580582

583+
/** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */
584+
template<typename... Ts>
585+
CScript BuildScript(Ts&&... inputs)
586+
{
587+
CScript ret;
588+
int cnt{0};
589+
590+
([&ret, &cnt] (Ts&& input) {
591+
cnt++;
592+
if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>, CScript>) {
593+
// If it is a CScript, extend ret with it. Move or copy the first element instead.
594+
if (cnt == 0) {
595+
ret = std::forward<Ts>(input);
596+
} else {
597+
ret.insert(ret.end(), input.begin(), input.end());
598+
}
599+
} else {
600+
// Otherwise invoke CScript::operator<<.
601+
ret << input;
602+
}
603+
} (std::forward<Ts>(inputs)), ...);
604+
605+
return ret;
606+
}
607+
581608
#endif // BITCOIN_SCRIPT_SCRIPT_H

0 commit comments

Comments
 (0)