Example #1
0
    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))
Example #2
0
    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)
Example #3
0
def TestBuild(script,
              invoke_args,
              wallet,
              plist='05',
              ret='05',
              dynamic=False,
              invoke_attrs=None,
              owners=None,
              snapshot=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,
                                  snapshot=snapshot)
Example #4
0
    def Blockchain_GetBlock(self, engine: ExecutionEngine):
        data = engine.CurrentContext.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.CurrentContext.EvaluationStack.PushT(StackItem.FromInterface(block))
        return True
Example #5
0
    def Blockchain_GetHeader(self, engine: ExecutionEngine):
        data = engine.CurrentContext.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.CurrentContext.EvaluationStack.PushT(StackItem.FromInterface(header))
        return True
Example #6
0
    def DeserializeStackItem(reader):
        stype = reader.ReadUInt8()
        if stype == StackItemType.ByteArray:
            return ByteArray(reader.ReadVarBytes())
        elif stype == StackItemType.Boolean:
            return Boolean(ord(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)
Example #7
0
    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