def test_sort_public_key(self):
     pub_keys = [acct1.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes()]
     builder = ProgramBuilder()
     sort_pub_keys = builder.sort_public_keys(pub_keys)
     self.assertEqual("023cab3b268c4f268456a972c672c276d23a9c3ca3dfcfc0004d786adbf1fb9282", sort_pub_keys[0].hex())
     self.assertEqual("03d0fdb54acba3f81db3a6e16fa02e7ea3678bd205eb4ed2f1cfa8ab5e5d45633e", sort_pub_keys[1].hex())
     self.assertEqual("02e8e84be09b87985e7f9dfa74298f6bb7f70f85515afca7e041fe964334e4b6c1", sort_pub_keys[2].hex())
    def test_add_multi_sign_transaction(self):
        asset = sdk.native_vm.asset()
        pub_keys = [acct1.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes()]
        m = 2
        b58_multi_address = Address.b58_address_from_multi_pub_keys(m, pub_keys)
        amount = 1
        gas_price = 500
        gas_limit = 20000
        tx_hash = asset.send_transfer('ont', acct2, b58_multi_address, amount, acct2, gas_limit, gas_price)
        self.assertEqual(64, len(tx_hash))
        tx_hash = asset.send_transfer('ong', acct2, b58_multi_address, amount, acct2, gas_limit, gas_price)
        self.assertEqual(64, len(tx_hash))
        b58_acct1_address = acct1.get_address_base58()
        b58_acct2_address = acct2.get_address_base58()
        self.assertEqual('ATyGGJBnANKFbf2tQMp4muUEZK7KuZ52k4', b58_multi_address)
        tx = sdk.native_vm.asset().new_transfer_transaction('ong', b58_acct1_address, b58_multi_address, amount, b58_acct1_address,
                                            gas_limit, gas_price)
        tx.add_sign_transaction(acct1)

        tx = sdk.native_vm.asset().new_transfer_transaction('ont', b58_multi_address, b58_acct2_address, amount, b58_acct1_address,
                                            gas_limit, gas_price)
        tx.sign_transaction(acct1)
        tx.add_multi_sign_transaction(m, pub_keys, acct1)
        tx.add_multi_sign_transaction(m, pub_keys, acct2)
        tx_hash = sdk.rpc.send_raw_transaction(tx)
        self.assertEqual(64, len(tx_hash))
Esempio n. 3
0
    def test_get_balance(self):
        private_key1 = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f'
        private_key2 = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
        private_key3 = '1383ed1fe570b6673351f1a30a66b21204918ef8f673e864769fa2a653401114'
        acct = Account(private_key1, SignatureScheme.SHA256withECDSA)
        acct2 = Account(private_key2, SignatureScheme.SHA256withECDSA)
        acct3 = Account(private_key3, SignatureScheme.SHA256withECDSA)
        pub_keys = [
            acct.get_public_key_bytes(),
            acct2.get_public_key_bytes(),
            acct3.get_public_key_bytes()
        ]
        multi_addr = Address.address_from_multi_pub_keys(2, pub_keys)
        base58_address = 'ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6'
        address_balance = restful_client.get_balance(base58_address)
        try:
            address_balance['ont']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')
        try:
            address_balance['ong']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')
        address_balance = restful_client.get_balance(acct.get_address_base58())
        try:
            address_balance['ont']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')
        try:
            address_balance['ong']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')

        multi_address_balance = restful_client.get_balance(
            multi_addr.b58encode())
        try:
            multi_address_balance['ont']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')
        try:
            multi_address_balance['ong']
        except KeyError:
            raised = True
            self.assertFalse(raised, 'Exception raised')
Esempio n. 4
0
#!/usr/bin/env python3

import time
import unittest

from test import sdk, acct1, acct2, acct3

from ontology.common.address import Address
from ontology.account.account import Account
from ontology.utils.utils import get_random_hex_str
from ontology.exception.exception import SDKException
from ontology.utils.contract_data import ContractDataParser

pub_keys = [acct1.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes()]
multi_address = Address.address_from_multi_pub_keys(2, pub_keys)


class TestRpcClient(unittest.TestCase):
    def test_get_version(self):
        try:
            version = sdk.rpc.get_version()
            self.assertTrue(isinstance(version, str))
            if version != '':
                self.assertIn('v', version)
        except SDKException as e:
            self.assertTrue('ConnectTimeout' in e.args[1])

    def test_get_connection_count(self):
        try:
            count = sdk.rpc.get_connection_count()
            self.assertGreaterEqual(count, 0)
Esempio n. 5
0
    def test_multi_serialize(self):
        pub_keys = [
            acct1.get_public_key_bytes(),
            acct2.get_public_key_bytes(),
            acct3.get_public_key_bytes()
        ]
        m = 2
        multi_address = Address.address_from_multi_pub_keys(m, pub_keys)
        b58_multi_address = multi_address.b58encode()
        b58_acct1_address = acct1.get_address_base58()
        b58_acct2_address = acct2.get_address_base58()
        gas_price = 500
        gas_limit = 20000
        tx1 = sdk.native_vm.asset().new_transfer_transaction(
            'ong', b58_multi_address, b58_acct2_address, 1000000000,
            b58_acct1_address, gas_limit, gas_price)
        tx_bytes = tx1.serialize()
        tx2 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx1), dict(tx2))

        tx2.sign_transaction(acct1)
        tx_bytes = tx2.serialize()
        tx3 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx2), dict(tx3))
        tx3.add_multi_sign_transaction(m, pub_keys, acct1)
        tx_bytes = tx3.serialize()
        tx4 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx3), dict(tx4))
        tx4.add_multi_sign_transaction(m, pub_keys, acct2)
        tx_bytes = tx4.serialize()
        tx5 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx4), dict(tx5))
        tx_hash = sdk.rpc.send_raw_transaction(tx5)
        self.assertEqual(64, len(tx_hash))
        sleep(6)
        event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)
        contract_address = '0200000000000000000000000000000000000000'
        notify = ContractEventParser.get_notify_list_by_contract_address(
            event, contract_address)
        for event in notify:
            self.assertEqual(event['States'][0], 'transfer')
        multi_address = Address.address_from_multi_pub_keys(m, pub_keys[::-1])
        b58_multi_address = multi_address.b58encode()
        b58_acct1_address = acct1.get_address_base58()
        b58_acct2_address = acct2.get_address_base58()
        tx1 = sdk.native_vm.asset().new_transfer_transaction(
            'ong', b58_multi_address, b58_acct2_address, 100000,
            b58_acct1_address, gas_limit, gas_price)
        tx_bytes = tx1.serialize()
        tx2 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx1), dict(tx2))

        tx2.sign_transaction(acct1)
        tx_bytes = tx2.serialize()
        tx3 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx2), dict(tx3))
        tx3.add_multi_sign_transaction(m, pub_keys, acct1)
        tx_bytes = tx3.serialize()
        tx4 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx3), dict(tx4))
        tx4.add_multi_sign_transaction(m, pub_keys, acct2)
        tx_bytes = tx4.serialize()
        tx5 = Transaction.deserialize_from(tx_bytes)
        self.assertEqual(dict(tx4), dict(tx5))
        tx_hash = sdk.rpc.send_raw_transaction(tx5)
        self.assertEqual(64, len(tx_hash))
        sleep(6)
        event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)
        contract_address = '0200000000000000000000000000000000000000'
        notify = ContractEventParser.get_notify_list_by_contract_address(
            event, contract_address)
        for event in notify:
            self.assertEqual(event['States'][0], 'transfer')