Exemplo n.º 1
0
    def test_base64_decode(self):
        import base64
        path = self.get_contract_path('Base64Decode.py')
        engine = TestEngine()
        arg = String.from_bytes(base64.b64encode(b'unit test'))
        result = self.run_smart_contract(engine, path, 'Main', arg,
                                         expected_result_type=bytes)
        self.assertEqual(b'unit test', result)

        arg = String.from_bytes(base64.b64encode(b''))
        result = self.run_smart_contract(engine, path, 'Main', arg,
                                         expected_result_type=bytes)
        self.assertEqual(b'', result)

        long_string = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam accumsan magna eu massa '
                       'vulputate bibendum. Aliquam commodo euismod tristique. Sed purus erat, pretium ut interdum '
                       'et, aliquet sed mauris. Curabitur vitae turpis euismod, hendrerit mi a, rhoncus justo. Mauris '
                       'sollicitudin, nisl sit amet feugiat pharetra, odio ligula congue tellus, vel pellentesque '
                       'libero leo id dui. Morbi vel risus vehicula, consectetur mauris eget, gravida ligula. '
                       'Maecenas aliquam velit sit amet nisi ultricies, ac sollicitudin nisi mollis. Lorem ipsum '
                       'dolor sit amet, consectetur adipiscing elit. Ut tincidunt, nisi in ullamcorper ornare, '
                       'est enim dictum massa, id aliquet justo magna in purus.')
        arg = String.from_bytes(base64.b64encode(String(long_string).to_bytes()))
        result = self.run_smart_contract(engine, path, 'Main', arg,
                                         expected_result_type=bytes)
        self.assertEqual(String(long_string).to_bytes(), result)
Exemplo n.º 2
0
    def test_deserialize(self):
        path = self.get_contract_path('Deserialize.py')
        engine = TestEngine()

        expected_result = 42
        value = serialize(expected_result)
        result = self.run_smart_contract(engine,
                                         path,
                                         'deserialize_arg',
                                         value,
                                         expected_result_type=bytes)
        self.assertEqual(expected_result, result)

        expected_result = True
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg',
                                         value)
        self.assertEqual(expected_result, result)

        value = StackItemType.Boolean + value[1:]
        result = self.run_smart_contract(engine, path, 'deserialize_arg',
                                         value)
        self.assertEqual(expected_result, result)

        expected_result = '42'
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg',
                                         value)
        self.assertEqual(expected_result, result)

        expected_result = b'42'
        value = serialize(expected_result)
        result = self.run_smart_contract(engine,
                                         path,
                                         'deserialize_arg',
                                         value,
                                         expected_result_type=bytes)
        self.assertEqual(expected_result, result)

        expected_result = [1, '2', b'3']
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg',
                                         value)
        expected_result[2] = String.from_bytes(expected_result[2])
        self.assertEqual(expected_result, result)

        expected_result = {'int': 1, 'str': '2', 'bytes': b'3'}
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg',
                                         value)
        expected_result['bytes'] = String.from_bytes(expected_result['bytes'])
        self.assertEqual(expected_result, result)
Exemplo n.º 3
0
    def test_deserialize(self):
        path = self.get_contract_path('Deserialize.py')
        self.compile_and_save(path)

        engine = TestEngine()

        expected_result = 42
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value,
                                         expected_result_type=bytes)
        self.assertEqual(expected_result, result)

        expected_result = True
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value)

        # it shouldn't be equal to the convertion, because it converts as an int instead of a boolean
        self.assertEqual(expected_result, result)
        self.assertNotEqual(type(expected_result), type(result))

        value = StackItemType.Boolean + value[1:]
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value,
                                         expected_result_type=bool)
        self.assertEqual(expected_result, result)
        self.assertEqual(type(expected_result), type(result))

        expected_result = '42'
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value)
        self.assertEqual(expected_result, result)

        expected_result = b'42'
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value,
                                         expected_result_type=bytes)
        self.assertEqual(expected_result, result)

        expected_result = [1, '2', b'3']
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value)
        expected_result[2] = String.from_bytes(expected_result[2])
        self.assertEqual(expected_result, result)

        expected_result = {'int': 1, 'str': '2', 'bytes': b'3'}
        value = serialize(expected_result)
        result = self.run_smart_contract(engine, path, 'deserialize_arg', value)
        expected_result['bytes'] = String.from_bytes(expected_result['bytes'])
        self.assertEqual(expected_result, result)
Exemplo n.º 4
0
    def test_concat_with_bytestring(self):
        path = self.get_contract_path('bytestring', 'ConcatWithByteString.py')
        engine = TestEngine()

        prefix = '12345'
        arg = 'a'
        result = self.run_smart_contract(engine, path, 'concat', prefix, arg)
        self.assertEqual(prefix + arg, result)

        arg = b'a'
        result = self.run_smart_contract(engine, path, 'concat', prefix, arg)
        self.assertEqual(prefix + String.from_bytes(arg), result)

        prefix = b'12345'
        arg = b'6789'
        result = self.run_smart_contract(engine,
                                         path,
                                         'concat',
                                         prefix,
                                         arg,
                                         expected_result_type=bytes)
        self.assertEqual(prefix + arg, result)

        arg = '6789'
        result = self.run_smart_contract(engine,
                                         path,
                                         'concat',
                                         prefix,
                                         arg,
                                         expected_result_type=bytes)
        self.assertEqual(prefix + String(arg).to_bytes(), result)
Exemplo n.º 5
0
def contract_parameter_to_json(value: Any) -> Dict[str, Any]:
    if value is None:
        return {'type': AbiType.Any}

    stack_type: AbiType = AbiType.Any
    parameter_value: Any = None

    if isinstance(value, bool):
        stack_type = AbiType.Boolean
        parameter_value = value
    elif isinstance(value, int):
        stack_type = AbiType.Integer
        parameter_value = value
    elif isinstance(value, str):
        stack_type = AbiType.String
        parameter_value = value
    elif isinstance(value, (bytes, bytearray)):
        import base64
        stack_type = AbiType.ByteArray
        parameter_value = String.from_bytes(base64.b64encode(value))
    elif isinstance(value, Sequence):
        stack_type = AbiType.Array
        parameter_value = [contract_parameter_to_json(x) for x in value]
    elif isinstance(value, dict):
        stack_type = AbiType.Map
        parameter_value = [{
            'key': contract_parameter_to_json(key),
            'value': contract_parameter_to_json(value)
        } for key, value in value.items()]

    result = {'type': stack_type.value}
    if parameter_value is not None:
        result['value'] = parameter_value

    return result
Exemplo n.º 6
0
    def storage_get(self, key: Union[str, bytes]) -> Any:
        if isinstance(key, bytes):
            key = String.from_bytes(key)

        if key in self._storage:
            return self._storage[key]
        else:
            return None
Exemplo n.º 7
0
 def to_json(self) -> Dict[str, Any]:
     json = super().to_json()
     json.update({
         'id': self._id,
         'code': self._response_code.value,
         'result': String.from_bytes(base64.b64encode(self._result))
     })
     return json
Exemplo n.º 8
0
 def to_json(self) -> Dict[str, Any]:
     from boa3.neo.vm.type.String import String
     return {
         'signers': [signer.to_json() for signer in self._signers],
         'witnesses': [witness.to_json() for witness in self._witnesses],
         'attributes': [attr.to_json() for attr in self._attributes],
         'script': String.from_bytes(base64.b64encode(self._script))
     }
Exemplo n.º 9
0
def stack_item_from_json(item: Dict[str, Any]) -> Any:
    if 'type' not in item:
        return None

    item_type: StackItemType = StackItemType.get_stack_item_type(item['type'])
    if item_type is StackItemType.InteropInterface:
        return InteropInterface
    if item_type is StackItemType.Any or 'value' not in item:
        return None

    value: Any = None
    item_value: Any = item['value']

    if item_type is StackItemType.Boolean:
        if isinstance(item_value,
                      str) and item_value in (str(True), str(False)):
            item_value = item_value == str(True)
        if not isinstance(item_value, bool):
            raise ValueError
        value = item_value

    elif item_type is StackItemType.Integer:
        if isinstance(item_value, str):
            item_value = int(item_value)
        if not isinstance(item_value, int):
            raise ValueError
        value = item_value

    elif item_type in (StackItemType.ByteString, StackItemType.Buffer):
        if not isinstance(item_value, str):
            raise ValueError

        import base64
        decoded: bytes = base64.b64decode(item_value)

        try:
            value = String.from_bytes(decoded)
        except BaseException:
            value = decoded

    elif item_type is StackItemType.Array:
        if not isinstance(item_value, Sequence) or isinstance(
                item_value, (str, bytes)):
            raise ValueError
        value = [stack_item_from_json(x) for x in item_value]

    elif item_type is StackItemType.Map:
        if not isinstance(item_value, Sequence):
            raise ValueError
        value = {}

        for x in item_value:
            if 'key' not in x or 'value' not in x:
                raise ValueError
            value[stack_item_from_json(x['key'])] = stack_item_from_json(
                x['value'])

    return value
Exemplo n.º 10
0
    def test_concat_with_str(self):
        path = self.get_contract_path('bytestring', 'ConcatWithStr.py')
        engine = TestEngine()

        prefix = '12345'
        arg = 'a'
        result = self.run_smart_contract(engine, path, 'concat', arg)
        self.assertEqual(prefix + arg, result)

        arg = b'6789'
        result = self.run_smart_contract(engine, path, 'concat', arg)
        self.assertEqual(prefix + String.from_bytes(arg), result)
Exemplo n.º 11
0
 def as_str(self) -> str:
     return String.from_bytes(self._key)
Exemplo n.º 12
0
    def storage_delete(self, key: Union[str, bytes]):
        if isinstance(key, bytes):
            key = String.from_bytes(key)

        if key in self._storage:
            self._storage.pop(key)
Exemplo n.º 13
0
    def storage_put(self, key: Union[str, bytes], value: Any):
        if isinstance(key, bytes):
            key = String.from_bytes(key)

        self._storage[key] = value