예제 #1
0
class TestMinterRedeemCheckTx(unittest.TestCase):
    def setUp(self):
        self.PRIVATE_KEY = '05ddcd4e6f7d248ed1388f0091fe345bf9bf4fc2390384e26005e7675c98b3c1'
        self.SIGNED_TX = 'f9013f0102018a4d4e540000000000000009b8e4f8e2b89df89b01830f423f8a4d4e5400000000000000843b9aca00b8419b3beac2c6ad88a8bd54d24912754bb820e58345731cb1b9bc0885ee74f9e50a58a80aa990a29c98b05541b266af99d3825bb1e5ed4e540c6e2f7c9b40af9ecc011ca00f7ba6d0aa47d74274b960fba02be03158d0374b978dcaa5f56fc7cf1754f821a019a829a3b7bba2fc290f5c96e469851a3876376d6a6a4df937327b3a5e9e8297b841da021d4f84728e0d3d312a18ec84c21768e0caa12a53cb0a1452771f72b0d1a91770ae139fd6c23bcf8cec50f5f2e733eabb8482cf29ee540e56c6639aac469600808001b845f8431ba009493b3296a085a27f2bc015ad5c1cc644ba21bdce1b78a49e987227f24a87a3a07187da48b6ea528d372ed33923f5d74011f56cc2db3cab2cf5b4bbab97990373'
        self.TX = MinterRedeemCheckTx(
            **{
                'nonce':
                1,
                'chain_id':
                MinterTx.TESTNET_CHAIN_ID,
                'gas_coin':
                'MNT',
                'check':
                'Mcf89b01830f423f8a4d4e5400000000000000843b9aca00b8419b3beac2c6ad88a8bd54d24912754bb820e58345731cb1b9bc0885ee74f9e50a58a80aa990a29c98b05541b266af99d3825bb1e5ed4e540c6e2f7c9b40af9ecc011ca00f7ba6d0aa47d74274b960fba02be03158d0374b978dcaa5f56fc7cf1754f821a019a829a3b7bba2fc290f5c96e469851a3876376d6a6a4df937327b3a5e9e8297',
                'proof':
                'da021d4f84728e0d3d312a18ec84c21768e0caa12a53cb0a1452771f72b0d1a91770ae139fd6c23bcf8cec50f5f2e733eabb8482cf29ee540e56c6639aac469600'
            })

    def test_valid_tx(self):
        """
        Is tx instance of needed TX class.
        """

        self.assertIsInstance(self.TX, MinterRedeemCheckTx)

    def test_sign_tx(self):
        """
        Sign transaction and check signed transaction
        """
        self.TX.sign(self.PRIVATE_KEY)

        self.assertEqual(self.TX.signed_tx, self.SIGNED_TX)

    def test_sign_with_signature(self):
        self.TX.signature_type = MinterTx.SIGNATURE_SINGLE_TYPE
        signature = self.TX.generate_signature(self.PRIVATE_KEY)
        self.TX.sign(signature=signature)

        self.assertEqual(self.TX.signed_tx, self.SIGNED_TX)

    def test_from_raw(self):
        tx = MinterTx.from_raw(raw_tx=self.SIGNED_TX)

        self.assertEqual(tx.check, self.TX.check)
        self.assertEqual(tx.proof, self.TX.proof)
예제 #2
0
def my_wallet(client: Client, message: Message):
    user, _ = get_user_model(message.from_user)
    wallet = MinterWallets.objects.get(user=user)
    wallet_obj = MinterWallet.create(mnemonic=wallet.mnemonic)
    private_key = wallet_obj['private_key']

    coin = Tools.objects.get(pk=1).coin
    amount = wallet.balance[coin]
    if not amount:
        coin = 'BIP'
        amount = wallet.balance[coin]
    nonce = API.get_nonce(wallet.address)

    check_obj = MinterCheck(nonce=1,
                            due_block=999999999,
                            coin=coin,
                            value=amount,
                            gas_coin=coin,
                            passphrase=wallet.mnemonic)
    check_str = check_obj.sign(private_key)
    redeem_tx = MinterRedeemCheckTx(check_str,
                                    check_obj.proof(wallet.address, ''),
                                    nonce=1,
                                    gas_coin=coin)
    redeem_tx.sign(private_key)
    redeem_tx_fee = API.estimate_tx_commission(
        redeem_tx.signed_tx, pip2bip=True)['result']['commission']
    logger.info(f'Wallet {wallet.address} balance (check): {wallet.balance}')
    logger.info(f'Redeem check tx fee: {redeem_tx_fee}')
    available_withdraw = amount - redeem_tx_fee

    send_tx = MinterSendCoinTx(coin,
                               wallet.address,
                               amount,
                               nonce=nonce,
                               gas_coin=coin)
    send_tx.sign(wallet_obj['private_key'])
    send_tx_fee = API.estimate_tx_commission(
        send_tx.signed_tx, pip2bip=True)['result']['commission']
    logger.info(f'Send tx fee: {send_tx_fee}')
    available_send = amount - send_tx_fee

    to_wallet_text = None
    timeloop_text = None
    redeem_url = None
    user_address = None
    if available_withdraw > 0:
        to_wallet_text = user.choice_localized(text_name='btn-withdraw-minter')
        passphrase = uuid()
        check_obj = MinterCheck(nonce=1,
                                due_block=999999999,
                                coin=coin,
                                value=available_withdraw,
                                gas_coin=coin,
                                passphrase=passphrase)
        check_str = check_obj.sign(private_key)
        redeem_tx = MinterRedeemCheckTx(check_str,
                                        proof='',
                                        nonce=nonce,
                                        gas_coin=coin)
        redeem_dl = MinterDeeplink(redeem_tx, data_only=True)
        redeem_dl.gas_coin = coin
        redeem_url = redeem_dl.generate(password=passphrase)
        logger.info(redeem_url)

    if available_send > 0:
        user_address = get_user_timeloop_address(message.chat.id)
        logger.info(f'User TL: {user_address}')
        timeloop_text = user.choice_localized(
            text_name='btn-withdraw-timeloop')

    markup = markup_wallet(to_wallet_text=to_wallet_text,
                           redeem_deeplink=redeem_url,
                           timeloop_text=timeloop_text,
                           user_address=user_address)
    text = user.choice_localized(text_name='msg-wallet').format(
        user_wallet_address=wallet.address,
        user_seed_phrase=wallet.mnemonic,
        amount=wallet.balance_formatted)
    client.send_message(user.id, text, reply_markup=markup)