script interpreter pushing values and bytes on stack

This commit is contained in:
debris 2016-08-23 16:58:06 +02:00
parent e7dadc87dd
commit 8bc58710b2
3 changed files with 54 additions and 4 deletions

View File

@ -14,6 +14,7 @@ pub enum Error {
// BIP62
SignatureHashtype,
SignatureDer,
Minimaldata,
SignatureHighS,
PubkeyType,
}
@ -32,6 +33,7 @@ impl fmt::Display for Error {
// BIP62
Error::SignatureHashtype => "Invalid Signature Hashtype".fmt(f),
Error::SignatureDer => "Invalid Signature".fmt(f),
Error::Minimaldata => "Check minimaldata failed".fmt(f),
Error::SignatureHighS => "Invalid High S in Signature".fmt(f),
Error::PubkeyType => "Invalid Pubkey".fmt(f),
}

View File

@ -206,9 +206,9 @@ fn check_minimal_push(data: &[u8], opcode: Opcode) -> bool {
}
pub fn eval_script(
_stack: &mut Vec<Vec<u8>>,
stack: &mut Vec<Vec<u8>>,
script: &Script,
_flags: &VerificationFlags,
flags: &VerificationFlags,
_checker: &SignatureChecker,
_version: SignatureVersion
) -> Result<bool, Error> {
@ -218,9 +218,15 @@ pub fn eval_script(
for i in script.into_iter() {
match try!(i) {
Instruction::PushValue(_opcode, _num) => {
Instruction::PushValue(_opcode, num) => {
stack.push(num.to_vec());
},
Instruction::PushBytes(_opcode, _bytes) => {
Instruction::PushBytes(opcode, bytes) => {
// TODO: if fExec
if flags.verify_minimaldata && !check_minimal_push(bytes, opcode) {
return Err(Error::Minimaldata);
}
stack.push(bytes.to_vec());
},
Instruction::Normal(_opcode) => {
},

View File

@ -26,3 +26,45 @@ impl From<i64> for Num {
}
}
}
impl Num {
pub fn to_vec(&self) -> Vec<u8> {
if self.value == 0 {
return vec![];
}
let mut result = vec![];
let negative = self.value < 0;
let mut absvalue = match negative {
true => (-self.value) as u64,
false => self.value as u64,
};
while absvalue > 0 {
result.push(absvalue as u8 & 0xff);
absvalue >>= 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.
// - If the most significant byte is >= 0x80 and the value is negative, push a
// new 0x80 byte that will be popped off when converting to an integral.
// - If the most significant byte is < 0x80 and the value is negative, add
// 0x80 to it, since it will be subtracted and interpreted as a negative when
// converting to an integral.
if result[result.len() - 1] & 0x80 != 0 {
match negative {
true => result.push(0x80),
false => result.push(0),
}
} else if negative {
let rlen = result.len();
result[rlen - 1] |= 0x80;
}
result
}
}