Ejemplo n.º 1
0
 def revoke(self, claim_id: str, issuer: Account, payer: Account,
            gas_price: int, gas_limit: int):
     tx = self.new_revoke_tx(claim_id, issuer.get_address(),
                             payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(issuer)
     if issuer.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = self._sdk.default_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 2
0
 def transfer(self, from_acct: Account, to_address: Union[str, Address],
              amount: int, payer: Account, gas_price: int, gas_limit: int):
     """
     This interface is used to send a transfer transaction that only for ONT or ONG.
     """
     tx = self.new_transfer_tx(from_acct.get_address(), to_address, amount,
                               payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(from_acct)
     if from_acct.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     return self._sdk.default_network.send_raw_transaction(tx)
Ejemplo n.º 3
0
 async def transfer(self, from_acct: Account,
                    to_address: Union[str, Address], amount: int,
                    payer: Account, gas_price: int, gas_limit: int) -> str:
     """
     This interface is used to transfer amount of tokens to to_address asynchronously.
     """
     tx = self.new_transfer_tx(from_acct.get_address(), to_address, amount,
                               payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(from_acct)
     if from_acct.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 4
0
 async def transfer_from(self, spender: Account, owner: Union[str, bytes,
                                                              Address],
                         to_address: Union[str, bytes, Address], value: int,
                         payer: Account, gas_price: int,
                         gas_limit: int) -> str:
     tx = self.new_transfer_from_tx(spender.get_address(),
                                    owner, to_address, value,
                                    payer.get_address(), gas_price,
                                    gas_limit)
     tx.sign_transaction(spender)
     if spender.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 5
0
 async def approve(self, owner: Account,
                   spender: Union[str, bytes, Address], amount: int,
                   payer: Account, gas_price: int, gas_limit: int) -> str:
     """
     This interface is used to allow 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.
     """
     tx = self.new_approve_tx(owner.get_address(), spender, amount,
                              payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(owner)
     if owner.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 6
0
 def transfer_from(self, spender: Account, owner: Union[str, bytes,
                                                        Address],
                   to_address: Union[str, bytes, Address], value: int,
                   payer: Account, gas_price: int, gas_limit: int) -> str:
     """
     Transfers value amount of tokens from address owner to address to_address, and MUST fire the Transfer event.
     """
     tx = self.new_transfer_from_tx(spender.get_address(),
                                    owner, to_address, value,
                                    payer.get_address(), gas_price,
                                    gas_limit)
     tx.sign_transaction(spender)
     if spender.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = self._sdk.default_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 7
0
 async def withdraw(self, claimer: Account, receiver: Union[str, Address],
                    amount: int, payer: Account, gas_price: int,
                    gas_limit: int) -> str:
     """
     This interface is used to withdraw a amount of ong and transfer them to receive address.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     tx = self.new_withdraw_tx(claimer.get_address(), receiver, amount,
                               payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(claimer)
     if claimer.get_address_base58() != payer.get_address_base58():
         tx.add_sign_transaction(payer)
     return await self._sdk.default_aio_network.send_raw_transaction(tx)
Ejemplo n.º 8
0
 async def init(self, founder: Account, payer: Account, gas_price: int,
                gas_limit: int):
     tx = self.new_init_tx(payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(founder)
     if founder.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 9
0
 async def approve(self, approver: Account, spender: Union[str, Address],
                   amount: int, payer: Account, gas_price: int,
                   gas_limit: int) -> str:
     """
     This is an interface used to send an approve transaction
     which allow receiver to spend a amount of ONT or ONG asset in sender's account.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     tx = self.new_approve_tx(approver.get_address(), spender, amount,
                              payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(approver)
     if approver.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     return await self._sdk.default_aio_network.send_raw_transaction(tx)
Ejemplo n.º 10
0
 def init(self, founder: Account, payer: Account, gas_price: int,
          gas_limit: int):
     """
     Contract owner can use this interface to activate oep-4 token.
     """
     tx = self.new_init_tx(payer.get_address(), gas_price, gas_limit)
     tx.sign_transaction(founder)
     if founder.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     tx_hash = self._sdk.default_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 11
0
 def transfer_from(self, spender: Account, from_address: Union[str,
                                                               Address],
                   receiver: Union[str, Address], amount: int,
                   payer: Account, gas_price: int, gas_limit: int) -> str:
     """
     This interface is used to generate a Transaction object for transfer that  allow one account to
     transfer a amount of ONT or ONG Asset to another account, in the condition of the first account had approved.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     tx = self.new_transfer_from_tx(spender.get_address(),
                                    from_address, receiver, amount,
                                    payer.get_address(), gas_price,
                                    gas_limit)
     tx.sign_transaction(spender)
     if spender.get_address_bytes() != payer.get_address_bytes():
         tx.add_sign_transaction(payer)
     return self._sdk.default_network.send_raw_transaction(tx)
Ejemplo n.º 12
0
 async def transfer_multi(self, transfer_list: list, signers: list,
                          payer_acct: Account, gas_price: int,
                          gas_limit: int) -> str:
     """
     This interface is used to transfer amount of token from from-account to to-account multiple times asynchronously.
     """
     tx = self.new_transfer_multi_tx(transfer_list,
                                     payer_acct.get_address(), gas_price,
                                     gas_limit)
     tx.sign_transaction(payer_acct)
     for signer in signers:
         tx.add_sign_transaction(signer)
     tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx)
     return tx_hash
Ejemplo n.º 13
0
    def test_add_recovery(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.did, 0, password)
        tx_hash = sdk.native_vm.did().registry_did(identity.did, ctrl_acct, acct3, self.gas_price,
                                                         self.gas_limit)
        self.check_register_did_case(identity.did, tx_hash)

        rand_private_key = utils.get_random_bytes(32).hex()
        recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        b58_recovery_address = recovery.get_address_base58()
        tx_hash = sdk.native_vm.did().add_recovery(identity.did, ctrl_acct, b58_recovery_address, acct2,
                                                      self.gas_price, self.gas_limit)
        self.check_add_recovery_case(identity.did, recovery.get_address().hex(little_endian=False), tx_hash)

        ddo = sdk.native_vm.did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(), ddo['Owners'][0]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])

        rand_private_key = utils.get_random_bytes(32).hex()
        new_recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        b58_new_recovery_address = new_recovery.get_address_base58()
        try:
            sdk.native_vm.did().add_recovery(identity.did, ctrl_acct, b58_new_recovery_address, acct2,
                                                self.gas_price, self.gas_limit)
        except SDKException as e:
            self.assertIn('already set recovery', e.args[1])

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        tx_hash = sdk.native_vm.did().add_public_key(identity.did, recovery, hex_new_public_key, acct2,
                                                        self.gas_price, self.gas_limit, True)
        self.check_add_public_key_case(identity.did, hex_new_public_key, tx_hash)

        ddo = sdk.native_vm.did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(), ddo['Owners'][0]['Value'])
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][1]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][1]['Type'])
        self.assertEqual('P256', ddo['Owners'][1]['Curve'])
        self.assertEqual(hex_new_public_key, ddo['Owners'][1]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])
        self.assertEqual(b58_recovery_address, ddo['Recovery'])

        tx_hash = sdk.native_vm.did().revoke_public_key(identity.did, recovery, hex_new_public_key, acct3,
                                                           self.gas_price, self.gas_limit, True)
        self.check_remove_public_key_case(identity.did, hex_new_public_key, tx_hash)
        self.check_duplicated_remove_public_key_case(identity.did, hex_new_public_key, ctrl_acct, acct3)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        try:
            sdk.native_vm.did().add_public_key(identity.did, new_recovery, hex_new_public_key, acct2,
                                                  self.gas_price, self.gas_limit, True)
        except SDKException as e:
            self.assertIn('no authorization', e.args[1])