def test_from_private_key_bytes(self):
     byte_data = f'{"0"*32}'.encode()
     private_key_object = PrivateKey(byte_data)
     from_private_key = IconJsonrpc.from_private_key(
         private_key_object.private_key)
     self.assertTrue(from_private_key.signer)
     self.assertTrue(from_private_key.address)
    def test_sendTransaction_v2(self):
        # IconJsonrpc object from string
        addr = f'hx{"0"*40}'
        from_str = IconJsonrpc.from_string(addr)

        # IconJsonrpc object from keystore file
        key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore')
        password = '******'
        from_keystore = IconJsonrpc.from_key_store(keystore=key_store,
                                                   password=password)

        # IconJsonrpc object from private key
        from_private_key = IconJsonrpc.from_private_key()

        icon_jsonrpc_objs = [from_str, from_keystore, from_private_key]

        request_template = {
            "to": f'hx{"a"*40}',
            "value": hex(int(1e10)),
            "fee": hex(int(1e16)),
            "nonce": hex(1),
            "timestamp": hex(int(time.time() * 10**6))
        }
        # test transfer
        for obj in icon_jsonrpc_objs:
            self.check_sendTransaction_v2(obj, request_template)
    def test_call(self):
        # IconJsonrpc object from string
        addr = f'hx{"0"*40}'
        from_str = IconJsonrpc.from_string(addr)

        # IconJsonrpc object from keystore file
        key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore')
        password = '******'
        from_keystore = IconJsonrpc.from_key_store(keystore=key_store,
                                                   password=password)

        # IconJsonrpc object from private key
        from_private_key = IconJsonrpc.from_private_key()

        to_addr = f'hx{"a"*40}'
        call_data = {
            "method": "transfer",
            "params": {
                "to": "hxto",
                "value": "0x10"
            }
        }
        # with IconJsonrpc object from string
        request = from_str.call(to=to_addr, data=call_data)
        self.check_jsonschema_validation(request=request)
        self.assertEqual(from_str.address, request['params']['from'])
        self.assertEqual(to_addr, request['params']['to'])
        self.assertEqual('call', request['params']['dataType'])
        self.assertEqual(call_data, request['params']['data'])

        # with IconJsonrpc object from keystore
        request = from_keystore.call(to=to_addr, data=call_data)
        self.check_jsonschema_validation(request=request)
        self.assertEqual(from_keystore.address, request['params']['from'])
        self.assertEqual(to_addr, request['params']['to'])
        self.assertEqual('call', request['params']['dataType'])
        self.assertEqual(call_data, request['params']['data'])

        # with IconJsonrpc object from private_key
        request = from_private_key.call(to=to_addr, data=call_data)
        self.check_jsonschema_validation(request=request)
        self.assertEqual(from_private_key.address, request['params']['from'])
        self.assertEqual(to_addr, request['params']['to'])
        self.assertEqual('call', request['params']['dataType'])
        self.assertEqual(call_data, request['params']['data'])
 def test_from_private_key(self):
     from_private_key = IconJsonrpc.from_private_key()
     self.assertTrue(from_private_key.signer)
     self.assertTrue(from_private_key.address)
    def test_sendTransaction(self):
        # IconJsonrpc object from string
        addr = f'hx{"0"*40}'
        from_str = IconJsonrpc.from_string(addr)

        # IconJsonrpc object from keystore file
        key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore')
        password = '******'
        from_keystore = IconJsonrpc.from_key_store(keystore=key_store,
                                                   password=password)

        # IconJsonrpc object from private key
        from_private_key = IconJsonrpc.from_private_key()

        icon_jsonrpc_objs = [from_str, from_keystore, from_private_key]

        request_template = {
            "to": f'hx{"a"*40}',
            "version": hex(3),
            "value": hex(int(1e10)),
            "stepLimit": hex(0x2000),
            "nid": hex(3),
            "nonce": hex(1),
            "timestamp": hex(int(time.time() * 10**6))
        }
        """
        test transfer
        """
        for obj in icon_jsonrpc_objs:
            self.check_sendTransaction(obj, request_template)
        """
        test dataType 'call'
        """
        request_template["dataType"] = 'call'
        request_template["data"] = {
            "method": "transfer",
            "params": {
                "to": "hxto",
                "value": hex(0x10)
            }
        }
        for obj in icon_jsonrpc_objs:
            self.check_sendTransaction(obj, request_template)
        """
        test dataType 'deploy'
        """
        request_template["dataType"] = 'deploy'
        request_template["data"] = {
            "contentType": "application/zip",
            "content": "0xcontent",
            "params": {
                "on_install": "need_param?",
                "on_update": "need_param?",
            }
        }
        for obj in icon_jsonrpc_objs:
            self.check_sendTransaction(obj, request_template)
        """
        test dataType 'message'
        """
        request_template["dataType"] = 'message'
        request_template["data"] = "0xmessage"

        for obj in icon_jsonrpc_objs:
            self.check_sendTransaction(obj, request_template)