extend script_builder

This commit is contained in:
gladcow 2020-04-14 18:08:27 +03:00
parent 30717b73ae
commit 9842a11846
2 changed files with 21 additions and 6 deletions

View file

@ -12,14 +12,23 @@ script_builder& script_builder::operator<<( op opcode )
return *this;
}
script_builder& script_builder::operator<<( uint8_t number )
script_builder& script_builder::operator<<( uint32_t number )
{
FC_ASSERT( 0 <= number && number <= 16 );
if ( number == 0 )
if ( number == 0 ) {
script.push_back( static_cast<uint8_t>( op::_0 ) );
else
} else if ( number <= 16) {
script.push_back( static_cast<uint8_t>( op::_1 ) + number - 1 );
} else {
while (number) {
script.push_back(number & 0xff);
number >>= 8;
}
// - If the most significant byte is >= 0x80 and the value is positive, push a
// new zero-byte to make the significant byte < 0x80 again. So, the result can
// be 5 bytes max.
if (script.back() & 0x80)
script.push_back(0);
}
return *this;
}

View file

@ -25,14 +25,20 @@ enum class op {
_16 = 0x60,
// control
IF = 0x63,
ENDIF = 0x68,
RETURN = 0x6a,
// stack ops
DUP = 0x76,
SWAP = 0x7c,
// bit logic
EQUAL = 0x87,
EQUALVERIFY = 0x88,
ADD = 0x93,
GREATERTHAN = 0xa0,
GREATERTHANOREQUAL = 0xa2,
// crypto
HASH160 = 0xa9,
@ -46,7 +52,7 @@ public:
script_builder& operator<<( op opcode );
script_builder& operator<<( uint8_t number );
script_builder& operator<<( uint32_t number );
script_builder& operator<<( size_t size );