From f15d2d4425c45415e37f4563b8838e6cda3ede92 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Fri, 21 Aug 2015 23:49:07 +0300 Subject: [PATCH] Stack machine helpers --- Novacoin/ScriptCode.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 files changed, 37 insertions(+), 0 deletions(-) diff --git a/Novacoin/ScriptCode.cs b/Novacoin/ScriptCode.cs index 5970794..45f8098 100644 --- a/Novacoin/ScriptCode.cs +++ b/Novacoin/ScriptCode.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Numerics; namespace Novacoin { @@ -994,7 +995,43 @@ namespace Novacoin return stack[nStackElement]; } + /// + /// Cast argument to boolean value + /// + /// Some byte sequence + /// + private static bool CastToBool(IEnumerable arg) + { + byte[] value = arg.ToArray(); + + for (var i = 0; i < value.Length; i++) + { + if (value[i] != 0) + { + // Can be negative zero + if (i == value.Length - 1 && value[i] == 0x80) + return false; + return true; + } + } + return false; + } + + /// + /// Cast argument to integer value + /// + /// + /// + private static BigInteger CastToBigInteger(IEnumerable value) + { + if (value.Count() > 4) + { + throw new StackMachineException("CastToBigNum() : overflow"); + } + + return new BigInteger(value.ToArray()); + } }; } -- 1.7.1