Exemple #1
0
 def to_bytes_address(hex_address: str) -> bytes:
     try:
         bytes_address = bytes.fromhex(hex_address)
     except ValueError as e:
         raise SDKException(ErrorCode.other_error(e.args[0]))
     address = Address(bytes_address)
     return address.to_bytes()
Exemple #2
0
 def new_transfer_multi_tx(self, transfer_list: list,
                           payer: Union[str, bytes,
                                        Address], gas_price: int,
                           gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which can
     transfer amount of token from from-account to to-account multiple times.
     """
     func = NeoInvokeFunction('transferMulti')
     for index, item in enumerate(transfer_list):
         if not isinstance(item[2], int):
             raise SDKException(
                 ErrorCode.param_err(
                     'the data type of value should be int.'))
         if item[2] < 0:
             raise SDKException(
                 ErrorCode.param_err(
                     'the value should be equal or great than 0.'))
         transfer_list[index] = [
             Address.b58decode(item[0]),
             Address.b58decode(item[1]), item[2]
         ]
     for item in transfer_list:
         func.add_params_value(item)
     params = InvokeTransaction.generate_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
 def new_revoke_tx(self, claim_id: str, issuer: Union[str, bytes, Address],
                   payer: Union[str, bytes,
                                Address], gas_price: int, gas_limit: int):
     func = NeoInvokeFunction('Revoke')
     func.set_params_value(claim_id, Address.b58decode(issuer))
     tx = InvokeTransaction(Address.b58decode(payer), gas_price, gas_limit)
     tx.add_invoke_code(self.__hex_contract_address, func)
     return tx
Exemple #4
0
 def new_allowance_tx(self, from_address: Union[str, Address],
                      to_address: Union[str, Address]) -> InvokeTransaction:
     args = dict(from_address=Address.b58decode(from_address),
                 to_address=Address.b58decode(to_address))
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'allowance',
                                            args)
     return InvokeTransaction(payload=invoke_code)
 def test_b58decode(self):
     length = 20
     rand_code = utils.get_random_bytes(length)
     address = Address(rand_code)
     b58_address = address.b58encode()
     zero = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, zero)
     decode_address = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, decode_address)
Exemple #6
0
 def new_allowance_tx(
         self, owner: Union[str, bytes, Address],
         spender: Union[str, bytes, Address]) -> InvokeTransaction:
     func = NeoInvokeFunction('allowance')
     func.set_params_value(Address.b58decode(owner),
                           Address.b58decode(spender))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
 def new_commit_tx(self, claim_id: str, issuer_address: Union[str, bytes,
                                                              Address],
                   owner_ont_id: str, payer_address: Union[str, bytes,
                                                           Address],
                   gas_price: int, gas_limit: int) -> InvokeTransaction:
     func = NeoInvokeFunction('Commit')
     func.set_params_value(claim_id, Address.b58decode(issuer_address),
                           owner_ont_id)
     tx = InvokeTransaction(Address.b58decode(payer_address), gas_price,
                            gas_limit)
     tx.add_invoke_code(self.__hex_contract_address, func)
     return tx
Exemple #8
0
 def new_transfer_tx(self, from_address: Union[str, Address],
                     to_address: Union[str, Address], amount: int,
                     payer: Union[str, Address], gas_price: int,
                     gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which can transfer amount of tokens to to_address.
     """
     func = NeoInvokeFunction('transfer')
     func.set_params_value(Address.b58decode(from_address),
                           Address.b58decode(to_address), amount)
     params = InvokeTransaction.generate_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
Exemple #9
0
 def new_change_recovery_tx(self, did: str, b58_new_recovery_address: str,
                            b58_recovery_address: str,
                            payer: Union[str, bytes,
                                         Address], gas_price: int,
                            gas_limit: int) -> InvokeTransaction:
     bytes_new_recovery = Address.b58decode(
         b58_new_recovery_address).to_bytes()
     bytes_recovery = Address.b58decode(b58_recovery_address).to_bytes()
     args = dict(did=did.encode('utf-8'),
                 new_recovery=bytes_new_recovery,
                 recovery=bytes_recovery)
     tx = self._generate_transaction('changeRecovery', args, payer,
                                     gas_price, gas_limit)
     return tx
Exemple #10
0
 def new_transfer_from_tx(self, spender: Union[str, bytes, Address],
                          owner: Union[str, bytes, Address],
                          to_address: Union[str, bytes, Address],
                          value: int, payer: Union[str, bytes,
                                                   Address], gas_price: int,
                          gas_limit: int) -> InvokeTransaction:
     func = NeoInvokeFunction('transferFrom')
     if not isinstance(value, int):
         raise SDKException(
             ErrorCode.param_err('the data type of value should be int.'))
     func.set_params_value(Address.b58decode(spender),
                           Address.b58decode(owner),
                           Address.b58decode(to_address), value)
     tx = InvokeTransaction(payer, gas_price, gas_limit)
     tx.add_invoke_code(self._contract_address, func)
     return tx
Exemple #11
0
 def new_balance_of_tx(self, owner: Union[str,
                                          Address]) -> InvokeTransaction:
     owner = Address.b58decode(owner)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'balanceOf',
                                            owner)
     return InvokeTransaction(payload=invoke_code)
 def create_account_info(self, label: str, pwd: str, salt: str, private_key: str) -> AccountInfo:
     acct = self.__create_account(label, pwd, salt, private_key, True)
     info = AccountInfo()
     info.address_base58 = Address.from_public_key(acct.get_public_key_bytes()).b58encode()
     info.public_key = acct.get_public_key_bytes().hex()
     info.encrypted_pri_key = acct.export_gcm_encrypted_private_key(pwd, salt)
     info.address_u160 = acct.get_address().to_bytes().hex()
     info.salt = salt
     return info
Exemple #13
0
 def new_transfer_from_tx(self, spender: Union[str, Address],
                          from_address: Union[str, Address],
                          receiver: Union[str, Address], amount: int,
                          payer: Union[str, Address], gas_price: int,
                          gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a Transaction object that allow one account to transfer
     a amount of ONT or ONG Asset to another account, in the condition of the first account had been approved.
     """
     args = dict(spender=Address.b58decode(spender),
                 from_address=Address.b58decode(from_address),
                 to_address=Address.b58decode(receiver),
                 amount=amount)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'transferFrom',
                                            args)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
 async def test_get_balance(self):
     pub_keys = [acct1.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes()]
     multi_address = Address.from_multi_pub_keys(2, pub_keys)
     address_list = [acct1.get_address_base58(), acct2.get_address_base58(), acct3.get_address_base58(),
                     acct4.get_address_base58(), multi_address.b58encode()]
     for address in address_list:
         balance = await sdk.websocket.get_balance(address)
         self.assertTrue(isinstance(balance, dict))
         self.assertGreaterEqual(balance['ONT'], 0)
         self.assertGreaterEqual(balance['ONG'], 0)
 async def unbound(self, address: Union[str, Address]) -> int:
     """
     This interface is used to query the amount of account's unbound ong.
     """
     ont_contract = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
     if isinstance(address, Address):
         address = address.b58encode()
     return int(await self._sdk.default_aio_network.get_allowance(
         'ong',
         Address(ont_contract).b58encode(), address))
Exemple #16
0
 def new_approve_tx(self, approver: Union[str,
                                          Address], spender: Union[str,
                                                                   Address],
                    amount: int, payer: Union[str, Address], gas_price: int,
                    gas_limit: int) -> Transaction:
     """
     This interface is used to generate a Transaction object for approve.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     args = dict(sender=Address.b58decode(approver),
                 receiver=Address.b58decode(spender),
                 amount=amount)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'approve', args)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
Exemple #17
0
 def new_balance_of_tx(
         self, owner: Union[str, bytes, Address]) -> InvokeTransaction:
     """
     This interface is used to generate transaction which can get the account balance of another account with owner address.
     """
     func = NeoInvokeFunction('balanceOf')
     func.set_params_value(Address.b58decode(owner))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
 def test_balance_of_transaction(self):
     func = WasmInvokeFunction('balanceOf')
     func.set_params_value(Address.b58decode('ANDfjwrUroaVtvBguDtrWKRMyxFwvVwnZD'))
     tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct4.get_address(), self.gas_price,
                                              self.gas_limit)
     target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c31e0962616c6' \
                      '16e63654f6646b1a18af6b7c9f8a4602f9f73eeb3030f0c29b7'
     self.assertEqual(target_payload, tx.payload.hex())
     tx.sign_transaction(acct4)
     result = sdk.rpc.send_raw_transaction_pre_exec(tx)
     self.assertGreaterEqual(WasmData.to_int(result.get('Result')), 0)
 def test_from_hd_pubkey(self):
     hd_pub_key = HDPublicKey.b58decode(self.bip32_pubkey)
     address = Address.from_hd_public_key(hd_pub_key)
     self.assertEqual('AW8Tf5R4kyURy6LQ8Th181Z5GpTovWGLg6',
                      address.b58encode())
     child_address_lst = [
         'ARXRQog4iZazp5YfXRyDZvU6ahrt3c2bb7',
         'APXh8MqcARUgafqvUNnpECzwKDtipkf3Zr',
         'ASpmd1MpFSpQ5rhicjRDqBpE1inP3Z7tus',
         'APA3M4BRqjBsHXRkeTFiFVb4X1u8FiEgAr',
         'AKq5SBTCzHBaqWWDUTGvekbsNJKKtf4ff5',
         'APzMnHqrGF1cGZyAdFCwEL29TnAgCuBrY6',
         'AJ8LEkLGeNsWvVrP7SgK9szoZ1MGgUTq1s',
         'AJo49LSK6rQEwc6qTYMsAZmvLPRrb6qcWa',
         'AYzCcNY3PwV432iXVREpDHvpX166KN45xP',
         'AbL1wvGCnbzywHBuX1VwQev8xuhJxbPE4P'
     ]
     for index, child_address in enumerate(child_address_lst):
         child_pks = HDPublicKey.from_path(hd_pub_key, f'0/{index}')
         address = Address.from_hd_public_key(child_pks[-1])
         self.assertEqual(child_address, address.b58encode())
Exemple #20
0
 def new_transfer_tx(self, from_address: Union[str, Address],
                     to_address: Union[str, Address], amount: int,
                     payer: Union[str, Address], gas_price: int,
                     gas_limit: int) -> Transaction:
     """
     This interface is used to generate a Transaction object for transfer.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     state = [{
         'from': Address.b58decode(from_address),
         'to': Address.b58decode(to_address),
         'amount': amount
     }]
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'transfer',
                                            state)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
def ensure_bytes_address(*objs: str or bytes or Account):
    result = list()
    for obj in objs:
        if isinstance(obj, bytes):
            result.append(obj)
        elif isinstance(obj, str):
            result.append(Address.b58decode(obj).to_bytes())
        elif isinstance(obj, Account):
            result.append(obj.get_address_bytes())
        else:
            raise SDKException(ErrorCode.param_error)
    return tuple(result)
 def setUp(self):
     pub_keys = [
         acct1.get_public_key_bytes(),
         acct2.get_public_key_bytes(),
         acct3.get_public_key_bytes()
     ]
     multi_address = Address.from_multi_pub_keys(2, pub_keys)
     self.address_list = [
         acct1.get_address_base58(),
         acct2.get_address_base58(),
         acct3.get_address_base58(),
         acct4.get_address_base58(),
         multi_address.b58encode()
     ]
Exemple #23
0
 def __iter__(self):
     data = dict()
     data['version'] = self.version
     data['txType'] = self.tx_type
     data['nonce'] = self.nonce
     data['gasPrice'] = self.gas_price
     data['gasLimit'] = self.gas_limit
     data['payer'] = Address(self.payer).b58encode()
     data['payload'] = binascii.b2a_hex(self.payload)
     data['attributes'] = binascii.b2a_hex(self.attributes)
     data['sigs'] = list()
     for sig in self.sig_list:
         data['sigs'].append(dict(sig))
     for key, value in data.items():
         yield (key, value)
Exemple #24
0
 def new_approve_tx(self, owner: Union[str, bytes,
                                       Address], spender: Union[str, bytes,
                                                                Address],
                    amount: int, payer: Union[str, bytes, Address],
                    gas_price: int, gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which allows spender to
     withdraw from owner account multiple times, up to the _value amount.
     If this function is called again it overwrites the current allowance with amount value.
     """
     if not isinstance(amount, int):
         raise SDKException(
             ErrorCode.param_err('the data type of amount should be int.'))
     if amount < 0:
         raise SDKException(
             ErrorCode.param_err(
                 'the amount should be equal or great than 0.'))
     func = NeoInvokeFunction('approve')
     func.set_params_value(Address.b58decode(owner),
                           Address.b58decode(spender), amount)
     params = InvokeTransaction.generate_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
Exemple #25
0
    def parse_ddo(did: str, serialized_ddo: str or bytes) -> dict:
        """
        This interface is used to deserialize a hexadecimal string into a DDO object in the from of dict.

        :param did: the unique ID for identity.
        :param serialized_ddo: an serialized description object of ONT ID in form of str or bytes.
        :return: a description object of ONT ID in the from of dict.
        """
        if len(serialized_ddo) == 0:
            return dict()
        if isinstance(serialized_ddo, str):
            stream = StreamManager.get_stream(
                bytearray.fromhex(serialized_ddo))
        elif isinstance(serialized_ddo, bytes):
            stream = StreamManager.get_stream(serialized_ddo)
        else:
            raise SDKException(
                ErrorCode.params_type_error(
                    'bytes or str parameter is required.'))
        reader = BinaryReader(stream)
        try:
            public_key_bytes = reader.read_var_bytes()
        except SDKException:
            public_key_bytes = b''
        try:
            attribute_bytes = reader.read_var_bytes()
        except SDKException:
            attribute_bytes = b''
        try:
            recovery_bytes = reader.read_var_bytes()
        except SDKException:
            recovery_bytes = b''
        if len(recovery_bytes) != 0:
            b58_recovery = Address(recovery_bytes).b58encode()
        else:
            b58_recovery = ''
        pub_keys = DID.parse_pub_keys(did, public_key_bytes)
        attribute_list = DID.parse_attributes(attribute_bytes)
        ddo = dict(Owners=pub_keys,
                   Attributes=attribute_list,
                   Recovery=b58_recovery,
                   DID=did)
        return ddo
 def test_transfer_tx(self):
     amount = 100
     func = WasmInvokeFunction('transfer')
     func.set_params_value(acct1.get_address(), Address.b58decode('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve'), amount)
     tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct2.get_address(), self.gas_price,
                                              self.gas_limit)
     target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c341087472616e7366657246b1a18af6b7c9f8a4602f9f73' \
                      'eeb3030f0c29b7d2c124dd088190f709b684e0bc676d70c41b377664000000000000000000000000000000'
     self.assertEqual(target_payload, tx.payload.hex())
     tx.sign_transaction(acct1, acct2)
     result = sdk.rpc.send_raw_transaction_pre_exec(tx)
     self.assertEqual('01', result.get('Result'))
     self.assertEqual(1, result.get('State'))
     notify_list = Event.get_event_from_event_list_by_contract_address(result.get('Notify'),
                                                                       self.oep4_contract_address)
     self.assertEqual(self.oep4_contract_address, notify_list[0].get('ContractAddress'))
     states = notify_list[1].get('States')
     self.assertEqual('transfer', WasmData.to_utf8(states[0]))
     self.assertEqual(acct1.get_address().b58encode(), WasmData.to_b58_address(states[1]))
     self.assertEqual('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve', WasmData.to_b58_address(states[2]))
     self.assertEqual(amount, WasmData.to_int(states[3]))
Exemple #27
0
 def __init__(self,
              version=0,
              tx_type: TxType or int = None,
              gas_price: int = 0,
              gas_limit: int = 0,
              payer: Union[str, bytes, Address, None] = b'',
              payload: bytearray = bytearray(),
              nonce: int = None,
              attributes: bytearray = bytearray(),
              sig_list: List[Sig] = None):
     if gas_price < 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the gas price should be equal or greater than zero.'))
     if gas_limit < 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the gas limit should be equal or greater than zero.'))
     self.version = version
     if isinstance(tx_type, int):
         tx_type = TxType(tx_type)
     if tx_type is not None:
         self.tx_type = tx_type.value
     if not nonce:
         nonce = randint(0, 0xFFFFFFFF)
     self.nonce = nonce
     self.gas_price = gas_price
     self.gas_limit = gas_limit
     if not payer:
         payer = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     if isinstance(payer, str):
         payer = Address.b58decode(payer).to_bytes()
     if isinstance(payer, Address):
         payer = payer.to_bytes()
     self.payer = payer
     self.payload = payload
     self.attributes = attributes
     if not sig_list:
         sig_list = list()
     self.sig_list = sig_list
    def test_add_multi_sign_transaction(self):
        pub_keys = [
            acct1.get_public_key_bytes(),
            acct2.get_public_key_bytes(),
            acct3.get_public_key_bytes()
        ]
        m = 2
        b58_multi_address = Address.from_multi_pub_keys(m,
                                                        pub_keys).b58encode()
        amount = 1
        gas_price = 500
        gas_limit = 20000
        tx_hash = sdk.native_vm.ont().transfer(acct2, b58_multi_address,
                                               amount, acct2, gas_price,
                                               gas_limit)
        self.assertEqual(64, len(tx_hash))
        tx_hash = sdk.native_vm.ong().transfer(acct2, b58_multi_address,
                                               amount, acct2, gas_price,
                                               gas_limit)
        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.ong().new_transfer_tx(b58_acct1_address,
                                                 b58_multi_address, amount,
                                                 b58_acct1_address, gas_price,
                                                 gas_limit)
        tx.add_sign_transaction(acct1)

        tx = sdk.native_vm.ont().new_transfer_tx(b58_multi_address,
                                                 b58_acct2_address, amount,
                                                 b58_acct1_address, gas_price,
                                                 gas_limit)
        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))
 def test_push_address(self):
     b58_address_list = [
         'AS7MjVEicEsJ4zjEfm2LoKoYoFsmapD7rT',
         'AFmseVrdL9f9oyCzZefL9tG6UbviEH9ugK',
         'AS3SCXw8GKTEeXpdwVw7EcC4rqSebFYpfb',
         'AJkkLbouowk6teTaxz1F2DYKfJh24PVk3r',
         'Ad4pjz2bqep4RhQrUAzMuZJkBC3qJ1tZuT',
         'AK98G45DhmPXg4TFPG1KjftvkEaHbU8SHM'
     ]
     wasm_address_list = [
         '71609b2c2f7b9447b089ad1da31586f42ca9eb10',
         '0000000000000000000000000000000000000007',
         '70a2ababdae0a9d1f9fc7296df3c6d343b772cf7',
         '20b1dc499cdf56ba70a574a1e17ac986d1f06ec2',
         'e98f4998d837fcdd44a50561f7f32140c7c6c260',
         '24ed4f965d3a5a76f5d0e87633c0b76941fc8827'
     ]
     for index, b58_address in enumerate(b58_address_list):
         self.builder.push_address(Address.b58decode(b58_address))
         self.assertEqual(wasm_address_list[index],
                          self.builder.to_bytes().hex())
         self.builder.clear_up()
 def __init__(self, private_key: str or bytes, scheme=SignatureScheme.SHA256withECDSA):
     self.__signature_scheme = scheme
     if scheme == SignatureScheme.SHA256withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_384withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_384withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA512withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_224withECDSA:
         self.__key_type = KeyType.ECDSA
     else:
         raise TypeError
     if isinstance(private_key, bytes) and len(private_key) == 32:
         self.__private_key = private_key
     elif isinstance(private_key, str) and len(private_key) == 64:
         self.__private_key = bytes.fromhex(private_key)
     else:
         raise SDKException(ErrorCode.invalid_private_key)
     self.__curve_name = Curve.P256
     self.__public_key = Signature.ec_get_public_key_by_private_key(self.__private_key, self.__curve_name)
     self.__address = Address.from_public_key(self.__public_key)