def __init__(self, event_type, event_payload, contract_hash, block_number, tx_hash, execution_success=False, test_mode=False): super(NotifyEvent, self).__init__(event_type, event_payload, contract_hash, block_number, tx_hash, execution_success, test_mode) self.is_standard_notify = False plen = len(self.event_payload) if plen > 0: self.notify_type = self.event_payload[0] empty = UInt160(data=bytearray(20)) try: if plen == 4 and self.notify_type in [NotifyType.TRANSFER, NotifyType.APPROVE]: if self.event_payload[1] is None: self.addr_from = empty else: self.addr_from = UInt160(data=self.event_payload[1]) if len(self.event_payload[1]) == 20 else empty self.addr_to = UInt160(data=self.event_payload[2]) if len(self.event_payload[2]) == 20 else empty self.amount = int(BigInteger.FromBytes(event_payload[3])) if isinstance(event_payload[3], bytes) else int(event_payload[3]) self.is_standard_notify = True elif plen == 3 and self.notify_type == NotifyType.REFUND: self.addr_to = UInt160(data=self.event_payload[1]) if len(self.event_payload[1]) == 20 else empty self.amount = int(BigInteger.FromBytes(event_payload[2])) if isinstance(event_payload[2], bytes) else int(event_payload[2]) self.addr_from = self.contract_hash self.is_standard_notify = True except Exception as e: logger.info("Could not determine notify event: %s %s" % (e, self.event_payload))
def Blockchain_GetHeader(self, engine): data = engine.EvaluationStack.Pop().GetByteArray() header = None if len(data) <= 5: height = BigInteger.FromBytes(data) if Blockchain.Default() is not None: header = Blockchain.Default().GetHeaderBy( height_or_hash=height) elif height == 0: header = Blockchain.GenesisBlock().Header elif len(data) == 32: hash = UInt256(data=data) if Blockchain.Default() is not None: header = Blockchain.Default().GetHeaderBy(height_or_hash=hash) elif hash == Blockchain.GenesisBlock().Hash: header = Blockchain.GenesisBlock().Header engine.EvaluationStack.PushT(StackItem.FromInterface(header)) return True
def TestBuild(script, invoke_args, wallet, plist='05', ret='05', dynamic=False, invoke_attrs=None, owners=None): properties = ContractPropertyState.HasStorage if dynamic: properties += ContractPropertyState.HasDynamicInvoke if not isinstance(ret, bytearray): ret = bytearray(binascii.unhexlify(str(ret).encode('utf-8'))) script = generate_deploy_script(script, contract_properties=int(properties), parameter_list=plist, return_type=BigInteger.FromBytes(ret)) return test_deploy_and_invoke(script, invoke_args, wallet, invoke_attrs=invoke_attrs, owners=owners)
def test_big_integer_sign(self): b1 = BigInteger(3) b2 = BigInteger(0) b3 = BigInteger(-4) self.assertEqual(b1.Sign, 1) self.assertEqual(b2.Sign, 0) self.assertEqual(b3.Sign, -1) c1 = BigInteger(-100) c1_bytes = c1.ToByteArray() c2 = BigInteger.FromBytes(c1_bytes, signed=True) self.assertEqual(c2.Sign, -1) c2_unsigned = BigInteger.FromBytes(c1_bytes, signed=False) self.assertEqual(c2_unsigned.Sign, 1)
def test_big_integer_frombytes(self): b1 = BigInteger(8972340892734890723) ba = b1.ToByteArray() b2 = BigInteger.FromBytes(ba) self.assertEqual(b1, b2) self.assertTrue(b1.Equals(b2))
def DeserializeStackItem(reader): stype = reader.ReadUInt8() if stype == StackItemType.ByteArray: return ByteArray(reader.ReadVarBytes()) elif stype == StackItemType.Boolean: return Boolean(reader.ReadByte()) elif stype == StackItemType.Integer: return Integer( BigInteger.FromBytes(reader.ReadVarBytes(), signed=True)) elif stype == StackItemType.Array: stack_item = Array() count = reader.ReadVarInt() while count > 0: count -= 1 stack_item.Add(StackItem.DeserializeStackItem(reader)) return stack_item elif stype == StackItemType.Struct: stack_item = Struct(value=None) count = reader.ReadVarInt() while count > 0: count -= 1 stack_item.Add(StackItem.DeserializeStackItem(reader)) return stack_item elif stype == StackItemType.Map: logger.warn("Map deserialize not implemented in c# core") return None else: logger.error("Could not deserialize stack item with type: %s " % stype) return None
def __init__(self, event_type, event_payload, contract_hash, block_number, tx_hash, execution_success=False, test_mode=False): super(NotifyEvent, self).__init__(event_type, event_payload, contract_hash, block_number, tx_hash, execution_success, test_mode) self.is_standard_notify = False if self.event_payload.Type == ContractParameterType.Array and len(self.event_payload.Value) > 0: payload = self.event_payload.Value plen = len(payload) self.notify_type = payload[0].Value empty = UInt160(data=bytearray(20)) try: if plen == 4 and self.notify_type in [NotifyType.TRANSFER, NotifyType.APPROVE]: if payload[1].Value is None: self.addr_from = empty logger.debug("Using contract addr from address %s " % self.event_payload) elif payload[1].Value is False: logger.debug("Using contract addr from address %s " % self.event_payload) self.addr_from = empty else: self.addr_from = UInt160(data=payload[1].Value) if len(payload[1].Value) == 20 else empty self.addr_to = UInt160(data=payload[2].Value) if len(payload[2].Value) == 20 else empty self.amount = int(BigInteger.FromBytes(payload[3].Value)) if isinstance(payload[3].Value, (bytes, bytearray)) else int(payload[3].Value) self.is_standard_notify = True elif self.notify_type == NotifyType.REFUND and plen >= 3: # Might have more arguments self.addr_to = UInt160(data=payload[1].Value) if len(payload[1].Value) == 20 else empty self.amount = int(BigInteger.FromBytes(payload[2].Value)) if isinstance(payload[2].Value, (bytes, bytearray)) else int(payload[2].Value) self.addr_from = self.contract_hash self.is_standard_notify = True elif self.notify_type == NotifyType.MINT and plen == 3: self.addr_to = UInt160(data=payload[1].Value) if len(payload[1].Value) == 20 else empty self.amount = int(BigInteger.FromBytes(payload[2].Value)) if isinstance(payload[2].Value, (bytes, bytearray)) else int(payload[2].Value) self.addr_from = self.contract_hash self.is_standard_notify = True except Exception as e: logger.debug("Could not determine notify event: %s %s" % (e, self.event_payload)) elif self.event_payload.Type == ContractParameterType.String: self.notify_type = self.event_payload.Value
def sc_notify(event): logger.info("SmartContract Runtime.Notify event: %s", event) # Make sure that the event payload list has at least one element. if not len(event.event_payload): return # The event payload list has at least one element. As developer of the smart contract # you should know what data-type is in the bytes, and how to decode it. In this example, # it's just a string, so we decode it with utf-8: byte_array = event.event_payload[0] tuple = bytes(byte_array).split(b'SEPARATOR') ts = BigInteger.FromBytes(tuple[0]) n_correct = BigInteger.FromBytes(tuple[1]) prediction = BigInteger.FromBytes(tuple[2]) logger.info("TS: {}".format(ts)) logger.info("n_correct: {}".format(n_correct)) logger.info("prediction: {}".format(prediction)) with open("../../webapp/CMC_Blockchain.txt", "w+") as f: f.write("{},{},{}".format(ts, n_correct, prediction))
def sc_log(event): logger.info("SmartContract Runtime.Notify event: %s", event) # Make sure that the event payload list has at least one element. if not len(event.event_payload): return # Make sure not test mode if event.test_mode: return # The event payload list has at least one element. As developer of the smart contract # you should know what data-type is in the bytes, and how to decode it. In this example, # it's just a string, so we decode it with utf-8: logger.info("- payload part 1: %s", event.event_payload[0]) price = BigInteger.FromBytes(event.event_payload[0]) USD_price = float(price) / 1000.0 print(USD_price)
def Blockchain_GetBlock(self, engine): data = engine.EvaluationStack.Pop() if data: data = data.GetByteArray() else: return False block = None if len(data) <= 5: height = BigInteger.FromBytes(data) if Blockchain.Default() is not None: block = Blockchain.Default().GetBlockByHeight(height) elif height == 0: block = Blockchain.GenesisBlock() elif len(data) == 32: hash = UInt256(data=data).ToBytes() if Blockchain.Default() is not None: block = Blockchain.Default().GetBlockByHash(hash=hash) elif hash == Blockchain.GenesisBlock().Hash: block = Blockchain.GenesisBlock().Header engine.EvaluationStack.PushT(StackItem.FromInterface(block)) return True
def DeserializeStackItem(reader): stype = reader.ReadUInt8() if stype == StackItemType.ByteArray: return ByteArray(reader.ReadVarBytes()) elif stype == StackItemType.Boolean: return Boolean(reader.ReadByte()) elif stype == StackItemType.Integer: return Integer( BigInteger.FromBytes(reader.ReadVarBytes(), signed=True)) elif stype == StackItemType.Array: stack_item = Array() count = reader.ReadVarInt() while count > 0: count -= 1 stack_item.Add(StackItem.DeserializeStackItem(reader)) return stack_item elif stype == StackItemType.Struct: stack_item = Struct(value=None) count = reader.ReadVarInt() while count > 0: count -= 1 stack_item.Add(StackItem.DeserializeStackItem(reader)) return stack_item elif stype == StackItemType.Map: stack_item = Map() count = reader.ReadVarInt() while count > 0: count -= 1 key = StackItem.DeserializeStackItem(reader) val = StackItem.DeserializeStackItem(reader) stack_item.SetItem(key, val) return stack_item else: raise ValueError( "Could not deserialize stack item with type: %s " % stype)
from binascii import hexlify, unhexlify from datetime import datetime import pytz from BinaryReader import BinaryReader import io from neocore.BigInteger import BigInteger #from neo.Settings import settings from neocore.Cryptography.Crypto import * from neocore import UIntBase #f = io.BytesIO(b'\x97afZ') f = io.BytesIO(b'p\\x15\\x02') #f = io.BytesIO(b'\xa1\x16\x02') reader = BinaryReader(f) print(BigInteger.FromBytes(b'$pkZ')) print(BigInteger.FromBytes(b'\xd8\x9dkZ')) print(BigInteger.FromBytes(b'\xd8\x9dkZ')) print(BigInteger.FromBytes(b'\x04\x9fkZ')) b = BigInteger.FromBytes(b'\x04\x9fkZ') print(BigInteger(136854).ToByteArray()) print("Yo") print(b) print(int(datetime.now().timestamp())) #print(datetime.fromtimestamp(reader.ReadUInt32())) print(datetime.fromtimestamp(1516658660)) print(datetime.fromtimestamp(1516658961))