def test_interop_map1(self): map = Map() self.assertEqual(map.Keys, []) self.assertEqual(map.Values, []) map.SetItem(Integer(BigInteger(3)), ByteArray(b'abc')) self.assertEqual(map.Keys, [Integer(BigInteger(3))]) self.assertEqual(map.Values, [ByteArray(b'abc')])
def test_to_parameter(self): stack_item = Integer(BigInteger(14)) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 14}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': True}) cp1 = ContractParameter.AsParameterType(ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': '0e'}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType(ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': '14'}) cp1 = ContractParameter.AsParameterType(ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'}) stack_item = Boolean(False) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 0}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': False}) cp1 = ContractParameter.AsParameterType(ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': ''}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType(ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': 'False'}) cp1 = ContractParameter.AsParameterType(ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'}) stack_item = ByteArray(bytearray(b'\xe0\x02')) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 736}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': True}) cp1 = ContractParameter.AsParameterType(ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': 'e002'}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType(ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': 'e002'}) cp1 = ContractParameter.AsParameterType(ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'})
def Runtime_Serialize(self, engine): stack_item = engine.EvaluationStack.Pop() ms = StreamManager.GetStream() writer = BinaryWriter(ms) try: stack_item.Serialize(writer) except Exception as e: logger.error("Cannot serialize item %s: %s " % (stack_item, e)) return False ms.flush() retVal = ByteArray(ms.getvalue()) StreamManager.ReleaseStream(ms) engine.EvaluationStack.PushT(retVal) return True
def test_verify_sig(self): stackItemMessage = ByteArray('abcdef') self.econtext.EvaluationStack.PushT(stackItemMessage) # sig sig = binascii.unhexlify( b'cd0ca967d11cea78e25ad16f15dbe77672258bfec59ff3617c95e317acff063a48d35f71aa5ce7d735977412186e1572507d0f4d204c5bcb6c90e03b8b857fbd' ) self.econtext.EvaluationStack.PushT(StackItem.New(sig)) # pubkey pubkey = binascii.unhexlify( b'036fbcb5e138c1ce5360e861674c03228af735a9114a5b7fb4121b8350129f3ffe' ) self.econtext.EvaluationStack.PushT(pubkey) self.engine.ExecuteOp(OpCode.VERIFY, self.econtext) res = self.econtext.EvaluationStack.Pop() self.assertEqual(res, StackItem.New(True))
def Runtime_Serialize(self, engine: ExecutionEngine): stack_item = engine.CurrentContext.EvaluationStack.Pop() ms = StreamManager.GetStream() writer = BinaryWriter(ms) try: stack_item.Serialize(writer) except Exception as e: logger.error("Cannot serialize item %s: %s " % (stack_item, e)) return False ms.flush() if ms.tell() > ApplicationEngine.maxItemSize: return False retVal = ByteArray(ms.getvalue()) StreamManager.ReleaseStream(ms) engine.CurrentContext.EvaluationStack.PushT(retVal) return True