def test_complex_sync(self): for i in range(40): key = pytezos.crypto.Key.generate() private_key = key.secret_key() public_key = key.public_key() user_wallet = Wallet.objects.create( public_key=public_key, currency=self.currency, state=WALLET_STATES.VERIFIED.value) Transaction.objects.create( from_wallet=self.wallet1, to_wallet=user_wallet, amount=10) meta_token_transaction = MetaTransaction( from_wallet=user_wallet, to_wallet=self.wallet1, nonce=1, amount=1) packed_meta_transaction = pack_meta_transaction( meta_token_transaction.to_meta_transaction_dictionary()) signature = key.sign(packed_meta_transaction) MetaTransaction.objects.create( from_wallet=user_wallet, to_wallet=self.wallet1, signature=signature, nonce=1, amount=1) key = pytezos.crypto.Key.generate() public_key = key.public_key() WalletPublicKeyTransferRequest.objects.create( wallet=user_wallet, old_public_key=user_wallet.public_key, new_public_key=public_key) self.assertEquals(81, Transaction.objects.filter( state=TRANSACTION_STATES.OPEN.value).count()) # 50 meta, 50 funding, 1 mint start = time.time() self.assertEquals(True, sync_to_blockchain( is_dry_run=False)) end = time.time() self.assertEquals(81, Transaction.objects.filter( state=TRANSACTION_STATES.DONE.value).count()) print(end - start)
def setUp(self): self.user = get_user_model().objects.create( username="******", password="******") self.user_2 = get_user_model().objects.create( username="******", password="******") self.currency = Currency.objects.create( token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_1_2 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkuSwJiAs2HdRopJwuaoSFKPbSPFAaXLGjT4Hjthc3UeXeign2w6", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2 = Wallet.objects.create(owner=self.user_2, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkutu49fgbHxV6vdVRBLbvCLpuq7CmSR6pnowxZRFcY7c76wUqHT", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.currency.cashout_wallet = self.wallet_2 self.currency.save() self.key = pytezos.Key.from_encoded_key( settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY) self.wallet_pk = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.key.public_key(), currency=self.currency, owner=self.user, state=WALLET_STATES.VERIFIED.value) self.mint_transaction = Transaction.objects.create( to_wallet=self.wallet_pk, amount=100) self.token_transaction = MetaTransaction( from_wallet=self.wallet_pk, to_wallet=self.currency.cashout_wallet, nonce=self.wallet_pk.nonce+1, amount=10) signature = self.key.sign(pack_meta_transaction( self.token_transaction.to_meta_transaction_dictionary())) self.token_transaction.signature = signature self.token_transaction.save()
def test_transaction_create_correct(self): self.client.force_authenticate(user=self.user) self.wallet_pk.state = WALLET_STATES.VERIFIED.value self.wallet_pk.save() # add money to wallet tx1_1 = Transaction.objects.create(to_wallet=self.wallet_pk, amount=150) tx_count = MetaTransaction.objects.all().count() # create signature token_transaction = MetaTransaction(from_wallet=self.wallet_pk, to_wallet=self.wallet_2, nonce=self.wallet_pk.nonce + 1, amount=10) signature = self.key.sign( pack_meta_transaction( token_transaction.to_meta_transaction_dictionary())) response = self.client.post( '/api/wallet/meta_transaction/', { 'from_wallet': self.wallet_pk.wallet_id, 'to_wallet': self.wallet_2.wallet_id, 'amount': 10, 'signature': signature, 'nonce': self.wallet_pk.nonce + 1 }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(tx_count + 1, MetaTransaction.objects.all().count()) self.client.force_authenticate(user=None)
def test_transaction_create_balance_too_small(self): self.wallet_pk.state = WALLET_STATES.VERIFIED.value self.wallet_pk.save() self.client.force_authenticate(user=self.user) # create signature token_transaction = MetaTransaction(from_wallet=self.wallet_pk, to_wallet=self.wallet_2, nonce=self.wallet_pk.nonce + 1, amount=20) packed_meta_transaction = pack_meta_transaction( token_transaction.to_meta_transaction_dictionary()) signature = self.key.sign(packed_meta_transaction) tx_count = MetaTransaction.objects.all().count() response = self.client.post( '/api/wallet/meta_transaction/', { 'from_wallet': self.wallet_pk.wallet_id, 'to_wallet': self.wallet_2.wallet_id, 'amount': 200, 'signature': signature, 'nonce': self.wallet_pk.nonce + 1 }) self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) self.assertEqual(tx_count, MetaTransaction.objects.all().count())
def test_transaction_create_not_verified(self): self.client.force_authenticate(user=self.user) # add money to wallet tx1_1 = Transaction.objects.create(to_wallet=self.wallet_pk, amount=150) # create signature token_transaction = MetaTransaction(from_wallet=self.wallet_pk, to_wallet=self.wallet_2, nonce=1, amount=10) packed_meta_transaction = pack_meta_transaction( token_transaction.to_meta_transaction_dictionary()) signature = self.key.sign(packed_meta_transaction) tx_count = MetaTransaction.objects.all().count() response = self.client.post( '/api/wallet/meta_transaction/', { 'from_wallet': self.wallet_pk.wallet_id, 'to_wallet': self.wallet_2.wallet_id, 'amount': 10, 'signature': signature, 'nonce': self.wallet_pk.nonce + 1 }) self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) self.assertEqual(tx_count, MetaTransaction.objects.all().count())
def test_transaction_create_unauthorized(self): # add money to wallet Transaction.objects.create(to_wallet=self.wallet_pk, amount=20) # create signature token_transaction = MetaTransaction(from_wallet=self.wallet_pk, to_wallet=self.wallet_2, nonce=1, amount=10) packed_meta_transaction = pack_meta_transaction( token_transaction.to_meta_transaction_dictionary()) signature = self.key.sign(packed_meta_transaction) response = self.client.post( '/api/wallet/meta_transaction/', { 'from_wallet': self.wallet_pk.wallet_id, 'to_wallet': self.wallet_2.wallet_id, 'amount': 10, 'signature': signature, 'nonce': 1 }) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
class CashOutRequestApiTest(APITestCase): pubkey_1 = 'edpkuvNy6TuQ2z8o9wnoaTtTXkzQk7nhegCHfxBc4ecsd4qG71KYNG' pubkey_2 = 'edpkuvNy6TuQ2z8o9wnoaTtTXkzQk7nhegCHfxBc4ecsd4qG71KYNg' def setUp(self): self.user = get_user_model().objects.create( username="******", password="******") self.user_2 = get_user_model().objects.create( username="******", password="******") self.currency = Currency.objects.create( token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_1_2 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkuSwJiAs2HdRopJwuaoSFKPbSPFAaXLGjT4Hjthc3UeXeign2w6", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2 = Wallet.objects.create(owner=self.user_2, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkutu49fgbHxV6vdVRBLbvCLpuq7CmSR6pnowxZRFcY7c76wUqHT", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.currency.cashout_wallet = self.wallet_2 self.currency.save() self.key = pytezos.Key.from_encoded_key( settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY) self.wallet_pk = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.key.public_key(), currency=self.currency, owner=self.user, state=WALLET_STATES.VERIFIED.value) self.mint_transaction = Transaction.objects.create( to_wallet=self.wallet_pk, amount=100) self.token_transaction = MetaTransaction( from_wallet=self.wallet_pk, to_wallet=self.currency.cashout_wallet, nonce=self.wallet_pk.nonce+1, amount=10) signature = self.key.sign(pack_meta_transaction( self.token_transaction.to_meta_transaction_dictionary())) self.token_transaction.signature = signature self.token_transaction.save() def test_create_cash_out_request_unauthorized(self): cash_out_request_count = CashOutRequest.objects.all().count() response = self.client.post('/api/wallet/cash_out_request/', { "transaction": self.token_transaction.uuid, "beneficiary_name": "Papers AG", "beneficiary_iban": "CH2509000000619652574" }, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertEqual(cash_out_request_count, CashOutRequest.objects.all().count()) def test_create_cash_out_request_bad_user(self): cash_out_request_count = CashOutRequest.objects.all().count() self.client.force_authenticate(user=self.user_2) response = self.client.post('/api/wallet/cash_out_request/', { "transaction": self.token_transaction.uuid, "beneficiary_name": "Papers AG", "beneficiary_iban": "CH2509000000619652574" }, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(cash_out_request_count, CashOutRequest.objects.all().count()) def test_create_cash_out_request_correct_and_duplicate(self): # correct request cash_out_request_count = CashOutRequest.objects.all().count() self.client.force_authenticate(user=self.user) response = self.client.post('/api/wallet/cash_out_request/', { "transaction": self.token_transaction.uuid, "beneficiary_name": "Papers AG", "beneficiary_iban": "CH2509000000619652574" }, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(cash_out_request_count + 1, CashOutRequest.objects.all().count()) self.client.force_authenticate(user=self.user) response = self.client.post('/api/wallet/cash_out_request/', { "transaction": self.token_transaction.uuid, "beneficiary_name": "Papers AG", "beneficiary_iban": "CH2509000000619652574" }, format='json') self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) def test_cash_out_request_list(self): # correct request self.client.force_authenticate(user=self.user) CashOutRequest.objects.all().delete() response = self.client.post('/api/wallet/cash_out_request/', { "transaction": self.token_transaction.uuid, "beneficiary_name": "Papers AG", "beneficiary_iban": "CH2509000000619652574" }, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.client.force_authenticate(user=self.user) response = self.client.get('/api/wallet/cash_out_request/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['results'], list(map(lambda cash_out_request: CashOutRequestSerializer( cash_out_request).data, CashOutRequest.objects.all()))) self.client.force_authenticate(user=self.user_2) response = self.client.get('/api/wallet/cash_out_request/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['results'], [])
def test_balance_calculation(self): currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') key1 = pytezos.crypto.Key.generate() key2 = pytezos.crypto.Key.generate() wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=key1.public_key(), currency=currency, state=WALLET_STATES.VERIFIED.value) wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=key2.public_key(), currency=currency, state=WALLET_STATES.VERIFIED.value) Transaction.objects.create(to_wallet=wallet1, amount=100) meta_transaction1 = MetaTransaction(from_wallet=wallet1, to_wallet=wallet2, amount=10, nonce=1) signature1 = key1.sign(pack_meta_transaction(meta_transaction1.to_meta_transaction_dictionary())) meta_transaction1.signature = signature1 meta_transaction1.save() self.assertEqual( wallet1.balance, 90) self.assertEqual( wallet2.balance, 10) meta_transaction2 = MetaTransaction(from_wallet=wallet2, to_wallet=wallet1, amount=1, nonce=1) signature2 = key2.sign(pack_meta_transaction(meta_transaction2.to_meta_transaction_dictionary())) meta_transaction2.signature = signature2 meta_transaction2.save() self.assertEqual( wallet1.balance, 91) self.assertEqual( wallet2.balance, 9) meta_transaction1 = MetaTransaction(from_wallet=wallet1, to_wallet=wallet2, amount=1, nonce=2) signature1 = key1.sign(pack_meta_transaction(meta_transaction1.to_meta_transaction_dictionary())) meta_transaction1.signature = signature1 meta_transaction1.save() self.assertEqual( wallet1.balance, 90) self.assertEqual( wallet2.balance, 10)
def test_recovery_flow_with_history(self): Transaction.objects.create(to_wallet=self.wallet1, amount=100) meta_transaction1 = MetaTransaction(from_wallet=self.wallet1, to_wallet=self.wallet2, amount=10, nonce=1) signature1 = self.key1.sign(pack_meta_transaction(meta_transaction1.to_meta_transaction_dictionary())) meta_transaction1.signature = signature1 meta_transaction1.save() self.assertEqual(meta_transaction1.from_public_key, self.wallet1.public_key) self.assertEqual(self.wallet1.balance, 90) self.assertEqual(self.wallet2.balance, 10) self.assertEqual(self.wallet1.nonce, 1) meta_transaction2 = MetaTransaction(from_wallet=self.wallet1, to_wallet=self.wallet2, amount=10, nonce=2) signature2 = self.key1.sign(pack_meta_transaction(meta_transaction2.to_meta_transaction_dictionary())) meta_transaction2.signature = signature2 meta_transaction2.save() self.assertEqual(meta_transaction1.from_public_key, self.wallet1.public_key) self.assertEqual(self.wallet1.balance, 80) self.assertEqual(self.wallet2.balance, 20) self.assertEqual(self.wallet1.nonce, 2) new_key1 = pytezos.crypto.Key.generate() wallet_public_key_transfer_request = WalletPublicKeyTransferRequest.objects.create( wallet=self.wallet1, old_public_key=self.wallet1.public_key, new_public_key=new_key1.public_key()) wallet_public_key_transfer_request.old_public_key = wallet_public_key_transfer_request.wallet.public_key wallet_public_key_transfer_request.wallet.public_key = wallet_public_key_transfer_request.new_public_key wallet_public_key_transfer_request.wallet.save() wallet_public_key_transfer_request.state = TRANSACTION_STATES.DONE.value wallet_public_key_transfer_request.notes = "Has no balance or was recovering to same pubkey, transferred offchain" wallet_public_key_transfer_request.save() self.wallet1.refresh_from_db() self.assertEqual(self.wallet1.nonce, 0) meta_transaction2 = MetaTransaction(from_wallet=self.wallet1, to_wallet=self.wallet2, amount=10, nonce=3) signature2 = new_key1.sign(pack_meta_transaction(meta_transaction2.to_meta_transaction_dictionary())) meta_transaction2.signature = signature2 with self.assertRaises(ValidationError): # the nonce must reset because it's a new pubkey meta_transaction2.save() meta_transaction3 = MetaTransaction(from_wallet=self.wallet1, to_wallet=self.wallet2, amount=10, nonce=1) signature3 = new_key1.sign(pack_meta_transaction(meta_transaction3.to_meta_transaction_dictionary())) meta_transaction3.signature = signature3 meta_transaction3.save() self.assertEqual(meta_transaction3.from_public_key, self.wallet1.public_key) self.assertEqual(self.wallet1.balance, 70) self.assertEqual(self.wallet2.balance, 30)