def test_get_private_eth_address_ng_not_exists_private_eth_address(self):
     with self.assertRaises(RecordNotFoundError) as e:
         self.cognito.admin_get_user = MagicMock(return_value={
             'UserAttributes': [{
                 'Name': 'hoge',
                 'Value': 'piyo'
             }]
         })
         UserUtil.get_private_eth_address(self.cognito, 'test')
     self.assertEqual(e.exception.args[0],
                      'Record Not Found: private_eth_address')
Example #2
0
    def exec_main_proc(self):
        # get private_eth_address
        private_eth_address = UserUtil.get_private_eth_address(
            self.cognito, self.params['user_id'])

        return {
            'statusCode': 200,
            'body': json.dumps({'wallet_address': private_eth_address})
        }
 def test_get_private_eth_address_ok(self):
     test_address = '0x401BA17D89D795B3C6e373c5062F1C3F8979e73B'
     self.cognito.admin_get_user = MagicMock(
         return_value={
             'UserAttributes': [{
                 'Name': 'hoge',
                 'Value': 'piyo'
             }, {
                 'Name': 'custom:private_eth_address',
                 'Value': test_address
             }]
         })
     result = UserUtil.get_private_eth_address(self.cognito, 'test')
     self.assertEqual(test_address, result)
    def exec_main_proc(self):
        ################
        # get parameter
        ################
        # article info
        article_info_table = self.dynamodb.Table(
            os.environ['ARTICLE_INFO_TABLE_NAME'])
        article_info = article_info_table.get_item(
            Key={
                'article_id': self.params['article_id']
            }).get('Item')
        # eth_address
        from_user_eth_address = self.event['requestContext']['authorizer'][
            'claims']['custom:private_eth_address']
        to_user_eth_address = UserUtil.get_private_eth_address(
            self.cognito, article_info['user_id'])
        # transaction_count
        transaction_count = PrivateChainUtil.get_transaction_count(
            from_user_eth_address)

        ################
        # validation
        ################
        # does not tip same user
        if article_info['user_id'] == self.event['requestContext'][
                'authorizer']['claims']['cognito:username']:
            raise ValidationError('Can not tip to myself')

        # validate raw_transaction
        # tip
        tip_data = PrivateChainUtil.get_data_from_raw_transaction(
            self.params['tip_signed_transaction'], transaction_count)
        PrivateChainUtil.validate_erc20_transfer_data(tip_data,
                                                      to_user_eth_address)
        # burn
        transaction_count = PrivateChainUtil.increment_transaction_count(
            transaction_count)
        burn_data = PrivateChainUtil.get_data_from_raw_transaction(
            self.params['burn_signed_transaction'], transaction_count)
        PrivateChainUtil.validate_erc20_transfer_data(
            burn_data, '0x' + os.environ['BURN_ADDRESS'])

        # burn 量が正しいこと
        tip_value = int(tip_data[72:], 16)
        burn_value = int(burn_data[72:], 16)
        calc_burn_value = int(Decimal(tip_value) / Decimal(10))
        if burn_value != calc_burn_value:
            raise ValidationError('burn_value is invalid.')

        # 残高が足りていること
        if not self.__is_burnable_user(from_user_eth_address, tip_value,
                                       burn_value):
            raise ValidationError('Required at least {token} token'.format(
                token=tip_value + burn_value))

        #######################
        # send_raw_transaction
        #######################
        transaction_hash = PrivateChainUtil.send_raw_transaction(
            self.params['tip_signed_transaction'])
        burn_transaction = None
        try:
            # 投げ銭が成功した時のみバーン処理を行う
            if PrivateChainUtil.is_transaction_completed(transaction_hash):
                # バーンのトランザクション処理
                burn_transaction = PrivateChainUtil.send_raw_transaction(
                    self.params['burn_signed_transaction'])
            else:
                logging.info(
                    'Burn was not executed because tip transaction was uncompleted.'
                )
        except Exception as err:
            logging.fatal(err)
            traceback.print_exc()
        finally:
            # create tip info
            self.__create_tip_info(transaction_hash, tip_value,
                                   burn_transaction, article_info)

        return {'statusCode': 200}
    def exec_main_proc(self):
        ################
        # get parameter
        ################
        # get article info
        article_info_table = self.dynamodb.Table(
            os.environ['ARTICLE_INFO_TABLE_NAME'])
        article_info = article_info_table.get_item(
            Key={
                'article_id': self.params['article_id']
            }).get('Item')
        # purchase article
        paid_articles_table = self.dynamodb.Table(
            os.environ['PAID_ARTICLES_TABLE_NAME'])
        paid_status_table = self.dynamodb.Table(
            os.environ['PAID_STATUS_TABLE_NAME'])
        # eth_address
        article_user_eth_address = UserUtil.get_private_eth_address(
            self.cognito, article_info['user_id'])
        user_eth_address = self.event['requestContext']['authorizer'][
            'claims']['custom:private_eth_address']
        # transaction_count
        transaction_count = PrivateChainUtil.get_transaction_count(
            user_eth_address)

        ################
        # validation
        ################
        # does not purchase same user's article
        user_id = self.event['requestContext']['authorizer']['claims'][
            'cognito:username']
        if article_info['user_id'] == user_id:
            raise ValidationError('Can not purchase own article')
        # validate raw_transaction
        # purchase
        purchase_data = PrivateChainUtil.get_data_from_raw_transaction(
            self.params['purchase_signed_transaction'], transaction_count)
        PrivateChainUtil.validate_erc20_transfer_data(
            purchase_data, article_user_eth_address)
        # burn
        transaction_count = PrivateChainUtil.increment_transaction_count(
            transaction_count)
        burn_data = PrivateChainUtil.get_data_from_raw_transaction(
            self.params['burn_signed_transaction'], transaction_count)
        PrivateChainUtil.validate_erc20_transfer_data(
            burn_data, '0x' + os.environ['BURN_ADDRESS'])

        # burn 量が正しいこと
        purchase_value = int(purchase_data[72:], 16)
        burn_value = int(burn_data[72:], 16)
        calc_burn_value = int(Decimal(purchase_value) / Decimal(9))
        if burn_value != calc_burn_value:
            raise ValidationError('burn_value is invalid.')

        # purchase_value が記事で指定されている金額に基づいた量が設定されていること
        DBUtil.validate_latest_price(self.dynamodb, self.params['article_id'],
                                     purchase_value)

        ################
        # purchase
        ################
        sort_key = TimeUtil.generate_sort_key()
        # 多重リクエストによる不必要なレコード生成を防ぐためにpaid_statusレコードを生成
        self.__create_paid_status(paid_status_table, user_id)
        # 購入のトランザクション処理
        purchase_transaction = PrivateChainUtil.send_raw_transaction(
            self.params['purchase_signed_transaction'])
        # 購入記事データを作成
        self.__create_paid_article(paid_articles_table, article_info,
                                   purchase_transaction, sort_key)
        # プライベートチェーンへのポーリングを行いトランザクションの承認状態を取得
        transaction_status = self.__polling_to_private_chain(
            purchase_transaction)
        # トランザクションの承認状態をpaid_articleとpaid_statusに格納
        self.__update_transaction_status(article_info, paid_articles_table,
                                         transaction_status, sort_key,
                                         paid_status_table, user_id)

        # 購入のトランザクションが成功した時のみバーンのトランザクションを発行する
        if transaction_status == 'done':
            try:
                # 購入に成功した場合、著者の未読通知フラグをTrueにする
                self.__update_unread_notification_manager(
                    article_info['user_id'])
                # 著者へ通知を作成
                self.__notify_author(article_info, user_id)
                # バーンのトランザクション処理
                burn_transaction = PrivateChainUtil.send_raw_transaction(
                    self.params['burn_signed_transaction'])
                # バーンのトランザクションを購入テーブルに格納
                self.__add_burn_transaction_to_paid_article(
                    burn_transaction, paid_articles_table, article_info,
                    sort_key)
            except Exception as err:
                logging.fatal(err)
                traceback.print_exc()
        # 記事購入者へは購入処理中の場合以外で通知を作成
        if transaction_status == 'done' or transaction_status == 'fail':
            self.__update_unread_notification_manager(user_id)
            self.__notify_purchaser(article_info, user_id, transaction_status)

        return {
            'statusCode': 200,
            'body': json.dumps({'status': transaction_status})
        }