From 1aeeffbe2bd71d07718af4a52c0f62f59021ab5e Mon Sep 17 00:00:00 2001 From: keillera Date: Fri, 24 Apr 2020 16:11:13 +0900 Subject: [PATCH 01/10] Fix bug for get_data_from_raw_transaction. --- src/common/private_chain_util.py | 9 ++++++-- tests/common/test_private_chain_util.py | 21 +++++++++++++++++++ .../token/send/test_me_wallet_token_send.py | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/common/private_chain_util.py b/src/common/private_chain_util.py index 2b03077c..db0fe9d4 100644 --- a/src/common/private_chain_util.py +++ b/src/common/private_chain_util.py @@ -143,7 +143,7 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): # 0:nonce(transaction_count) # 1:gasPrice(0) # 2:gasLimit(0) - # 3:to_address(今回の場合は alis トークンのコントラクトアドレス) + # 3:to_address # 4:value(0) # 5:data # 6:v(検証で利用。但し、内部で chain_id が利用されているため確認対象) @@ -159,7 +159,12 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): raise ValidationError('gasPrice is invalid') if byte_data_list[2].hex() != '': raise ValidationError('gasLimit is invalid') - if byte_data_list[3].hex() != os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS'][2:]: + # relay method の場合は to_address は PRIVATE_CHAIN_BRIDGE_ADDRESS + if byte_data_list[5].hex()[0:8] == 'eeec0e24': + to_address = os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS'] + else: + to_address = os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS'] + if byte_data_list[3].hex() != to_address[2:]: raise ValidationError('private_chain_alis_token_address is invalid') if byte_data_list[4].hex() != '': raise ValidationError('value is invalid') diff --git a/tests/common/test_private_chain_util.py b/tests/common/test_private_chain_util.py index fd2bd9a8..eb8e69e9 100644 --- a/tests/common/test_private_chain_util.py +++ b/tests/common/test_private_chain_util.py @@ -317,6 +317,27 @@ def test_get_data_from_raw_transaction_ok(self): actual = PrivateChainUtil.get_data_from_raw_transaction(signed.rawTransaction.hex(), format(nonce, '#x')) self.assertEqual(test_data[2:], actual) + def test_get_data_from_raw_transaction_ok_with_relay_method(self): + web3 = Web3(HTTPProvider('http://localhost:8584')) + test_account = web3.eth.account.create() + test_data = '0xeeec0e24' + nonce = 10 + transaction = { + 'nonce': nonce, + 'gasPrice': 0, + 'gas': 0, + 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS']), + 'value': 0, + 'data': test_data, + 'chainId': 8995 + } + signed = web3.eth.account.sign_transaction(transaction, test_account.key) + actual = PrivateChainUtil.get_data_from_raw_transaction( + signed.rawTransaction.hex(), + format(nonce, '#x') + ) + self.assertEqual(test_data[2:], actual) + def test_get_data_from_raw_transaction_ng_failure_nonce(self): web3 = Web3(HTTPProvider('http://localhost:8584')) test_account = web3.eth.account.create() diff --git a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py index 854570e5..078ececd 100644 --- a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py +++ b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py @@ -922,7 +922,7 @@ def create_singed_relay_transaction(self, nonce, to_address, value): 'nonce': nonce, 'gasPrice': 0, 'gas': 0, - 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), + 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS']), 'value': 0, 'data': relay_data, 'chainId': 8995 From 364a874e83a87812d6fe33220f5418e96bb0707c Mon Sep 17 00:00:00 2001 From: keillera Date: Fri, 24 Apr 2020 16:29:29 +0900 Subject: [PATCH 02/10] Fix typo --- function02-template.yaml | 10 ++++----- .../users/wallet/address/show/handler.py | 6 +++--- ...s_show.py => users_wallet_address_show.py} | 2 +- swagger/swagger.yaml | 21 ++++++++++++++++--- ...w.py => test_users_wallet_address_show.py} | 8 +++---- 5 files changed, 31 insertions(+), 16 deletions(-) rename src/handlers/users/wallet/address/show/{user_wallet_address_show.py => users_wallet_address_show.py} (94%) rename tests/handlers/users/wallet/address/show/{test_user_wallet_address_show.py => test_users_wallet_address_show.py} (86%) diff --git a/function02-template.yaml b/function02-template.yaml index bf0bdbc6..120d9109 100644 --- a/function02-template.yaml +++ b/function02-template.yaml @@ -249,10 +249,10 @@ Resources: Timeout: 900 TracingConfig: Mode: "Active" - UserWalletAddressShow: + UsersWalletAddressShow: Type: "AWS::Lambda::Function" Properties: - Code: ./deploy/user_wallet_address_show.zip + Code: ./deploy/users_wallet_address_show.zip Environment: Variables: COGNITO_USER_POOL_ID: !Ref CognitoUserPoolId @@ -311,7 +311,7 @@ Outputs: Value: !GetAtt MeWalletNonceShow.Arn Export: Name: !Sub "${AlisAppId}-MeWalletNonceShow" - UserWalletAddressShow: - Value: !GetAtt UserWalletAddressShow.Arn + UsersWalletAddressShow: + Value: !GetAtt UsersWalletAddressShow.Arn Export: - Name: !Sub "${AlisAppId}-UserWalletAddressShow" + Name: !Sub "${AlisAppId}-UsersWalletAddressShow" diff --git a/src/handlers/users/wallet/address/show/handler.py b/src/handlers/users/wallet/address/show/handler.py index c4b0fec9..a7456856 100644 --- a/src/handlers/users/wallet/address/show/handler.py +++ b/src/handlers/users/wallet/address/show/handler.py @@ -1,9 +1,9 @@ import boto3 -from user_wallet_address_show import UserWalletAddressShow +from users_wallet_address_show import UsersWalletAddressShow cognito = boto3.client('cognito-idp') def lambda_handler(event, context): - user_wallet_address_show = UserWalletAddressShow(event, context, cognito=cognito) - return user_wallet_address_show.main() + users_wallet_address_show = UsersWalletAddressShow(event, context, cognito=cognito) + return users_wallet_address_show.main() diff --git a/src/handlers/users/wallet/address/show/user_wallet_address_show.py b/src/handlers/users/wallet/address/show/users_wallet_address_show.py similarity index 94% rename from src/handlers/users/wallet/address/show/user_wallet_address_show.py rename to src/handlers/users/wallet/address/show/users_wallet_address_show.py index 9604f634..59247293 100644 --- a/src/handlers/users/wallet/address/show/user_wallet_address_show.py +++ b/src/handlers/users/wallet/address/show/users_wallet_address_show.py @@ -5,7 +5,7 @@ from jsonschema import validate -class UserWalletAddressShow(LambdaBase): +class UsersWalletAddressShow(LambdaBase): def get_schema(self): return { 'type': 'object', diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index ed3fdd3c..51eace7d 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -2349,7 +2349,12 @@ paths: default: statusCode: '200' uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UserWalletAddressShow.Arn}/invocations + Fn::Join: + - '' + - - Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/" + - Fn::ImportValue: + Fn::Sub: "${AlisAppId}-UsersWalletAddressShow" + - "/invocations" passthroughBehavior: when_no_templates httpMethod: POST type: aws_proxy @@ -2368,7 +2373,12 @@ paths: default: statusCode: "200" uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MeWalletAllowance.Arn}/invocations + Fn::Join: + - '' + - - Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/" + - Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeWalletAllowanceShow" + - "/invocations" passthroughBehavior: when_no_templates httpMethod: POST type: aws_proxy @@ -2387,7 +2397,12 @@ paths: default: statusCode: "200" uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MeWalletNonce.Arn}/invocations + Fn::Join: + - '' + - - Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/" + - Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeWalletNonceShow" + - "/invocations" passthroughBehavior: when_no_templates httpMethod: POST type: aws_proxy diff --git a/tests/handlers/users/wallet/address/show/test_user_wallet_address_show.py b/tests/handlers/users/wallet/address/show/test_users_wallet_address_show.py similarity index 86% rename from tests/handlers/users/wallet/address/show/test_user_wallet_address_show.py rename to tests/handlers/users/wallet/address/show/test_users_wallet_address_show.py index 4d93c3d5..a0309a2c 100644 --- a/tests/handlers/users/wallet/address/show/test_user_wallet_address_show.py +++ b/tests/handlers/users/wallet/address/show/test_users_wallet_address_show.py @@ -3,10 +3,10 @@ import json from unittest import TestCase from unittest.mock import MagicMock -from user_wallet_address_show import UserWalletAddressShow +from users_wallet_address_show import UsersWalletAddressShow -class TestUserWalletAddressShow(TestCase): +class TestUsersWalletAddressShow(TestCase): cognito = boto3.client('cognito-idp') test_user_id = 'test-user' @@ -15,7 +15,7 @@ def setUpClass(cls): os.environ['COGNITO_USER_POOL_ID'] = 'xxxxxxx' def assert_bad_request(self, params): - function = UserWalletAddressShow(params, {}, cognito=self.cognito) + function = UsersWalletAddressShow(params, {}, cognito=self.cognito) response = function.main() self.assertEqual(response['statusCode'], 400) @@ -40,7 +40,7 @@ def test_main_ok(self): } } - response = UserWalletAddressShow(params, {}, cognito=self.cognito).main() + response = UsersWalletAddressShow(params, {}, cognito=self.cognito).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body']), {'wallet_address': test_address}) From 46698a3883a999fb9e8d571fa036c226fa1c5615 Mon Sep 17 00:00:00 2001 From: keillera Date: Fri, 24 Apr 2020 19:09:52 +0900 Subject: [PATCH 03/10] Add persmission. --- permission-template.yaml | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/permission-template.yaml b/permission-template.yaml index f5535b59..da4a2114 100644 --- a/permission-template.yaml +++ b/permission-template.yaml @@ -251,3 +251,48 @@ Resources: Fn::Sub: "${AlisAppId}-MeConfigurationsMuteUsersIndex" Principal: "apigateway.amazonaws.com" SourceArn: !Sub ${RestApiArn}/*/GET/me/configurations/mute_users + MeConfigurationsWalletShowApiGatewayInvoke: + Type: "AWS::Lambda::Permission" + Properties: + Action: "lambda:InvokeFunction" + FunctionName: + Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeConfigurationsWalletShow" + Principal: "apigateway.amazonaws.com" + SourceArn: !Sub ${RestApiArn}/*/GET/me/configurations/wallet + MeConfigurationsWalletAddApiGatewayInvoke: + Type: "AWS::Lambda::Permission" + Properties: + Action: "lambda:InvokeFunction" + FunctionName: + Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeConfigurationsWalletAdd" + Principal: "apigateway.amazonaws.com" + SourceArn: !Sub ${RestApiArn}/*/POST/me/configurations/wallet + UsersWalletAddressShowApiGatewayInvoke: + Type: "AWS::Lambda::Permission" + Properties: + Action: "lambda:InvokeFunction" + FunctionName: + Fn::ImportValue: + Fn::Sub: "${AlisAppId}-UsersWalletAddressShow" + Principal: "apigateway.amazonaws.com" + SourceArn: !Sub ${RestApiArn}/*/GET/users/*/wallet/address + MeWalletAllowanceShowApiGatewayInvoke: + Type: "AWS::Lambda::Permission" + Properties: + Action: "lambda:InvokeFunction" + FunctionName: + Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeWalletAllowanceShow" + Principal: "apigateway.amazonaws.com" + SourceArn: !Sub ${RestApiArn}/*/GET/me/wallet/allowance + MeWalletNonceShowApiGatewayInvoke: + Type: "AWS::Lambda::Permission" + Properties: + Action: "lambda:InvokeFunction" + FunctionName: + Fn::ImportValue: + Fn::Sub: "${AlisAppId}-MeWalletNonceShow" + Principal: "apigateway.amazonaws.com" + SourceArn: !Sub ${RestApiArn}/*/GET/me/wallet/nonce From 6c9a8794463202b89761ce599c13437878e5f87d Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 03:56:41 +0900 Subject: [PATCH 04/10] Add env parameters --- api-template.yaml | 9 +++++++++ function-template.yaml | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/api-template.yaml b/api-template.yaml index ae30f389..c4d09ee0 100644 --- a/api-template.yaml +++ b/api-template.yaml @@ -107,6 +107,12 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' AuthleteApiSecret: Type: 'AWS::SSM::Parameter::Value' + UserConfigurationsTableName: + Type: 'AWS::SSM::Parameter::Value' + PrivateChainAlisTokenAddress: + Type: 'AWS::SSM::Parameter::Value' + PrivateChainBridgeAddress: + Type: 'AWS::SSM::Parameter::Value' BurnAddress: Type: 'AWS::SSM::Parameter::Value' @@ -145,6 +151,7 @@ Globals: TIP_TABLE_NAME: !Ref TipTableName SUCCEEDED_TIP_TABLE_NAME: !Ref SucceededTipTableName EXTERNAL_PROVIDER_USERS_TABLE_NAME: !Ref ExternalProviderUsersTableName + USER_CONFIGURATIONS_TABLE_NAME: !Ref UserConfigurationsTableName DOMAIN: !Ref AlisAppDomain PRIVATE_CHAIN_AWS_ACCESS_KEY: !Ref PrivateChainAwsAccessKey PRIVATE_CHAIN_AWS_SECRET_ACCESS_KEY: !Ref PrivateChainAwsSecretAccessKey @@ -154,6 +161,8 @@ Globals: ELASTIC_SEARCH_ENDPOINT: !Ref ElasticSearchEndpoint PAID_ARTICLES_TABLE_NAME: !Ref PaidArticlesTableName PAID_STATUS_TABLE_NAME: !Ref PaidStatusTableName + PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS: !Ref PrivateChainAlisTokenAddress + PRIVATE_CHAIN_BRIDGE_ADDRESS: !Ref PrivateChainBridgeAddress BURN_ADDRESS: !Ref BurnAddress Resources: diff --git a/function-template.yaml b/function-template.yaml index 1d0e3e95..73175342 100644 --- a/function-template.yaml +++ b/function-template.yaml @@ -26,6 +26,8 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' UsersTableName: Type: 'AWS::SSM::Parameter::Value' + UserConfigurationsTableName: + Type: 'AWS::SSM::Parameter::Value' BetaUsersTableName: Type: 'AWS::SSM::Parameter::Value' ExternalProviderUsersTableName: @@ -64,6 +66,12 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' PrivateChainExecuteApiHost: Type: 'AWS::SSM::Parameter::Value' + PrivateChainAlisTokenAddress: + Type: 'AWS::SSM::Parameter::Value' + PrivateChainBridgeAddress: + Type: 'AWS::SSM::Parameter::Value' + BurnAddress: + Type: 'AWS::SSM::Parameter::Value' BetaModeFlag: Type: 'AWS::SSM::Parameter::Value' DistS3BucketName: @@ -110,8 +118,6 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' FacebookAppToken: Type: 'AWS::SSM::Parameter::Value' - PrivateChainBridgeAddress: - Type: 'AWS::SSM::Parameter::Value' PaidArticlesTableName: Type: 'AWS::SSM::Parameter::Value' PaidStatusTableName: @@ -287,9 +293,13 @@ Resources: PRIVATE_CHAIN_AWS_ACCESS_KEY: !Ref PrivateChainAwsAccessKey PRIVATE_CHAIN_AWS_SECRET_ACCESS_KEY: !Ref PrivateChainAwsSecretAccessKey PRIVATE_CHAIN_EXECUTE_API_HOST: !Ref PrivateChainExecuteApiHost + PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS: !Ref PrivateChainAlisTokenAddress + PRIVATE_CHAIN_BRIDGE_ADDRESS: !Ref PrivateChainBridgeAddress + BURN_ADDRESS: !Ref BurnAddress COGNITO_USER_POOL_ID: !Ref CognitoUserPoolId COGNITO_USER_POOL_APP_ID: !Ref CognitoUserPoolAppId PAID_STATUS_TABLE_NAME: !Ref PaidStatusTableName + USER_CONFIGURATIONS_TABLE_NAME: !Ref UserConfigurationsTableName Handler: handler.lambda_handler MemorySize: 3008 Role: !GetAtt LambdaRole.Arn @@ -322,8 +332,11 @@ Resources: PRIVATE_CHAIN_AWS_ACCESS_KEY: !Ref PrivateChainAwsAccessKey PRIVATE_CHAIN_AWS_SECRET_ACCESS_KEY: !Ref PrivateChainAwsSecretAccessKey PRIVATE_CHAIN_EXECUTE_API_HOST: !Ref PrivateChainExecuteApiHost + PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS: !Ref PrivateChainAlisTokenAddress + BURN_ADDRESS: !Ref BurnAddress TOKEN_SEND_TABLE_NAME: !Ref TokenSendTableName DAILY_LIMIT_TOKEN_SEND_VALUE: !Ref DailyLimitTokenSendValue + USER_CONFIGURATIONS_TABLE_NAME: !Ref UserConfigurationsTableName Handler: handler.lambda_handler MemorySize: 3008 Role: !GetAtt LambdaRole.Arn From 5d172d9da186fb79a2805d130baa3a50d9f08eee Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 05:03:10 +0900 Subject: [PATCH 05/10] Fix a bug that occurs when Nonce is zero. --- src/common/private_chain_util.py | 9 +++++- tests/common/test_private_chain_util.py | 37 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/common/private_chain_util.py b/src/common/private_chain_util.py index db0fe9d4..4023bb00 100644 --- a/src/common/private_chain_util.py +++ b/src/common/private_chain_util.py @@ -153,12 +153,18 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): # 発生しない想定だが念の為個数を確認 if len(byte_data_list) != 9: raise ValidationError('raw_transaction is invalid') - if int(byte_data_list[0].hex(), 16) != int(transaction_count, 16): + # nonce + if byte_data_list[0].hex() != '' and int(byte_data_list[0].hex(), 16) != int(transaction_count, 16): raise ValidationError('nonce is invalid') + if byte_data_list[0].hex() == '' and int(transaction_count, 16) != 0: + raise ValidationError('nonce is invalid') + # gasPrice if byte_data_list[1].hex() != '': raise ValidationError('gasPrice is invalid') + # gasLimit if byte_data_list[2].hex() != '': raise ValidationError('gasLimit is invalid') + # to_address # relay method の場合は to_address は PRIVATE_CHAIN_BRIDGE_ADDRESS if byte_data_list[5].hex()[0:8] == 'eeec0e24': to_address = os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS'] @@ -166,6 +172,7 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): to_address = os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS'] if byte_data_list[3].hex() != to_address[2:]: raise ValidationError('private_chain_alis_token_address is invalid') + # value if byte_data_list[4].hex() != '': raise ValidationError('value is invalid') # v は検証パラメータだが、chain_id を含んでいるため確認する diff --git a/tests/common/test_private_chain_util.py b/tests/common/test_private_chain_util.py index eb8e69e9..8526d8a4 100644 --- a/tests/common/test_private_chain_util.py +++ b/tests/common/test_private_chain_util.py @@ -317,6 +317,24 @@ def test_get_data_from_raw_transaction_ok(self): actual = PrivateChainUtil.get_data_from_raw_transaction(signed.rawTransaction.hex(), format(nonce, '#x')) self.assertEqual(test_data[2:], actual) + def test_get_data_from_raw_transaction_ok_with_nonce_zero(self): + web3 = Web3(HTTPProvider('http://localhost:8584')) + test_account = web3.eth.account.create() + test_data = '0xa9059cbb' + nonce = 0 + transaction = { + 'nonce': nonce, + 'gasPrice': 0, + 'gas': 0, + 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), + 'value': 0, + 'data': test_data, + 'chainId': 8995 + } + signed = web3.eth.account.sign_transaction(transaction, test_account.key) + actual = PrivateChainUtil.get_data_from_raw_transaction(signed.rawTransaction.hex(), format(nonce, '#x')) + self.assertEqual(test_data[2:], actual) + def test_get_data_from_raw_transaction_ok_with_relay_method(self): web3 = Web3(HTTPProvider('http://localhost:8584')) test_account = web3.eth.account.create() @@ -357,6 +375,25 @@ def test_get_data_from_raw_transaction_ng_failure_nonce(self): PrivateChainUtil.get_data_from_raw_transaction(signed.rawTransaction.hex(), format(nonce, '#x')) self.assertEqual(e.exception.args[0], 'nonce is invalid') + def test_get_data_from_raw_transaction_ng_failure_zero_nonce(self): + web3 = Web3(HTTPProvider('http://localhost:8584')) + test_account = web3.eth.account.create() + test_data = '0xa9059cbb' + nonce = 0 + transaction = { + 'nonce': nonce + 1, + 'gasPrice': 0, + 'gas': 0, + 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), + 'value': 0, + 'data': test_data, + 'chainId': 8995 + } + signed = web3.eth.account.sign_transaction(transaction, test_account.key) + with self.assertRaises(ValidationError) as e: + PrivateChainUtil.get_data_from_raw_transaction(signed.rawTransaction.hex(), format(nonce, '#x')) + self.assertEqual(e.exception.args[0], 'nonce is invalid') + def test_get_data_from_raw_transaction_ng_failure_gasPrice(self): web3 = Web3(HTTPProvider('http://localhost:8584')) test_account = web3.eth.account.create() From c5826e3b9156f96313737a72677e8ec2eb3e28c9 Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 14:18:06 +0900 Subject: [PATCH 06/10] Update gasLimit. --- src/common/private_chain_util.py | 2 +- tests/common/test_private_chain_util.py | 18 +++++++++--------- .../create/test_me_articles_purchase_create.py | 4 ++-- .../me/wallet/tip/test_me_wallet_tip.py | 4 ++-- .../token/send/test_me_wallet_token_send.py | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/common/private_chain_util.py b/src/common/private_chain_util.py index 4023bb00..db4ae561 100644 --- a/src/common/private_chain_util.py +++ b/src/common/private_chain_util.py @@ -162,7 +162,7 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): if byte_data_list[1].hex() != '': raise ValidationError('gasPrice is invalid') # gasLimit - if byte_data_list[2].hex() != '': + if byte_data_list[2].hex() != '0186a0': raise ValidationError('gasLimit is invalid') # to_address # relay method の場合は to_address は PRIVATE_CHAIN_BRIDGE_ADDRESS diff --git a/tests/common/test_private_chain_util.py b/tests/common/test_private_chain_util.py index 8526d8a4..f8bbea7f 100644 --- a/tests/common/test_private_chain_util.py +++ b/tests/common/test_private_chain_util.py @@ -307,7 +307,7 @@ def test_get_data_from_raw_transaction_ok(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': test_data, @@ -325,7 +325,7 @@ def test_get_data_from_raw_transaction_ok_with_nonce_zero(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': test_data, @@ -343,7 +343,7 @@ def test_get_data_from_raw_transaction_ok_with_relay_method(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS']), 'value': 0, 'data': test_data, @@ -364,7 +364,7 @@ def test_get_data_from_raw_transaction_ng_failure_nonce(self): transaction = { 'nonce': nonce + 1, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': test_data, @@ -383,7 +383,7 @@ def test_get_data_from_raw_transaction_ng_failure_zero_nonce(self): transaction = { 'nonce': nonce + 1, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': test_data, @@ -401,7 +401,7 @@ def test_get_data_from_raw_transaction_ng_failure_gasPrice(self): nonce = 10 transaction = { 'nonce': nonce, - 'gas': 0, + 'gas': 100000, 'gasPrice': 1, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, @@ -440,7 +440,7 @@ def test_get_data_from_raw_transaction_ng_failure_to(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': test_account.address, 'value': 0, 'data': test_data, @@ -459,7 +459,7 @@ def test_get_data_from_raw_transaction_ng_failure_value(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 1, 'data': test_data, @@ -478,7 +478,7 @@ def test_get_data_from_raw_transaction_ng_failure_v(self): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': test_data, diff --git a/tests/handlers/me/articles/purchase/create/test_me_articles_purchase_create.py b/tests/handlers/me/articles/purchase/create/test_me_articles_purchase_create.py index 8832ffeb..3a6d43db 100644 --- a/tests/handlers/me/articles/purchase/create/test_me_articles_purchase_create.py +++ b/tests/handlers/me/articles/purchase/create/test_me_articles_purchase_create.py @@ -1107,7 +1107,7 @@ def create_singed_transactions(self, to_address, test_purchase_value, burn_value purchase_transaction = { 'nonce': test_nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': purchase_data, @@ -1119,7 +1119,7 @@ def create_singed_transactions(self, to_address, test_purchase_value, burn_value burn_transaction = { 'nonce': test_nonce + 1, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': burn_data, diff --git a/tests/handlers/me/wallet/tip/test_me_wallet_tip.py b/tests/handlers/me/wallet/tip/test_me_wallet_tip.py index a8f7ebf1..48b3f793 100644 --- a/tests/handlers/me/wallet/tip/test_me_wallet_tip.py +++ b/tests/handlers/me/wallet/tip/test_me_wallet_tip.py @@ -700,7 +700,7 @@ def create_singed_transactions(self, to_address, test_tip_value, burn_value): tip_transaction = { 'nonce': test_nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': tip_data, @@ -712,7 +712,7 @@ def create_singed_transactions(self, to_address, test_tip_value, burn_value): burn_transaction = { 'nonce': test_nonce + 1, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': burn_data, diff --git a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py index 078ececd..47bc6706 100644 --- a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py +++ b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py @@ -906,7 +906,7 @@ def create_singed_approve_transaction(self, nonce, value): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS']), 'value': 0, 'data': approve_data, @@ -921,7 +921,7 @@ def create_singed_relay_transaction(self, nonce, to_address, value): transaction = { 'nonce': nonce, 'gasPrice': 0, - 'gas': 0, + 'gas': 100000, 'to': self.web3.toChecksumAddress(os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS']), 'value': 0, 'data': relay_data, From 0739461d2a46cf3e5e4eb03fa801bfd42e43c13e Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 16:01:01 +0900 Subject: [PATCH 07/10] Remove the process checking for the exists of token. --- .../cognito_trigger/custommessage/custom_message.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/handlers/cognito_trigger/custommessage/custom_message.py b/src/handlers/cognito_trigger/custommessage/custom_message.py index 69b06634..9316dfd3 100644 --- a/src/handlers/cognito_trigger/custommessage/custom_message.py +++ b/src/handlers/cognito_trigger/custommessage/custom_message.py @@ -90,10 +90,9 @@ def exec_main_proc(self): # トークンを保持していた場合は例外を出力 def __validate_has_not_token(self, params): address = params.get('custom:private_eth_address') - if address is None: - raise ValidationError('Not exists private_eth_address. user_id: ' + self.event['userName']) - url = 'https://' + os.environ['PRIVATE_CHAIN_EXECUTE_API_HOST'] + '/production/wallet/balance' - payload = {'private_eth_address': address[2:]} - token = PrivateChainUtil.send_transaction(request_url=url, payload_dict=payload) - if token is not None and token != '0x0000000000000000000000000000000000000000000000000000000000000000': - raise ValidationError("Do not allow phone number updates") + if address is not None: + url = 'https://' + os.environ['PRIVATE_CHAIN_EXECUTE_API_HOST'] + '/production/wallet/balance' + payload = {'private_eth_address': address[2:]} + token = PrivateChainUtil.send_transaction(request_url=url, payload_dict=payload) + if token is not None and token != '0x0000000000000000000000000000000000000000000000000000000000000000': + raise ValidationError("Do not allow phone number updates") From f306ec9cae46e71e46fcefc3f41a69314487b377 Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 18:50:26 +0900 Subject: [PATCH 08/10] Remove unused parameters. --- function-template.yaml | 44 ------------------------------------------ 1 file changed, 44 deletions(-) diff --git a/function-template.yaml b/function-template.yaml index 73175342..b8187eeb 100644 --- a/function-template.yaml +++ b/function-template.yaml @@ -10,56 +10,16 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' ArticleHistoryTableName: Type: 'AWS::SSM::Parameter::Value' - ArticleContentEditTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticleEvaluatedManageTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticleAlisTokenTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticleLikedUserTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticleFraudUserTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticlePvUserTableName: - Type: 'AWS::SSM::Parameter::Value' - ArticleScoreTableName: - Type: 'AWS::SSM::Parameter::Value' UsersTableName: Type: 'AWS::SSM::Parameter::Value' UserConfigurationsTableName: Type: 'AWS::SSM::Parameter::Value' - BetaUsersTableName: - Type: 'AWS::SSM::Parameter::Value' ExternalProviderUsersTableName: Type: 'AWS::SSM::Parameter::Value' NotificationTableName: Type: 'AWS::SSM::Parameter::Value' UnreadNotificationManagerTableName: Type: 'AWS::SSM::Parameter::Value' - CommentTableName: - Type: 'AWS::SSM::Parameter::Value' - CommentLikedUserTableName: - Type: 'AWS::SSM::Parameter::Value' - DeletedCommentTableName: - Type: 'AWS::SSM::Parameter::Value' - UserFraudTableName: - Type: 'AWS::SSM::Parameter::Value' - ScreenedArticleTableName: - Type: 'AWS::SSM::Parameter::Value' - TokenDistributionTableName: - Type: 'AWS::SSM::Parameter::Value' - UserFirstExperienceTableName: - Type: 'AWS::SSM::Parameter::Value' - ElasticSearchEndpoint: - Type: 'AWS::SSM::Parameter::Value' - TopicTableName: - Type: 'AWS::SSM::Parameter::Value' - TagTableName: - Type: 'AWS::SSM::Parameter::Value' - TipTableName: - Type: 'AWS::SSM::Parameter::Value' - AlisAppDomain: - Type: 'AWS::SSM::Parameter::Value' PrivateChainAwsAccessKey: Type: 'AWS::SSM::Parameter::Value' PrivateChainAwsSecretAccessKey: @@ -72,10 +32,6 @@ Parameters: Type: 'AWS::SSM::Parameter::Value' BurnAddress: Type: 'AWS::SSM::Parameter::Value' - BetaModeFlag: - Type: 'AWS::SSM::Parameter::Value' - DistS3BucketName: - Type: 'AWS::SSM::Parameter::Value' CognitoUserPoolId: Type: 'AWS::SSM::Parameter::Value' CognitoUserPoolArn: From c0aa80f74ba2cc4014c0fb79d7fd8530418cf46b Mon Sep 17 00:00:00 2001 From: keillera Date: Sat, 25 Apr 2020 20:29:22 +0900 Subject: [PATCH 09/10] Update the process that validate erc20_data. --- src/common/private_chain_util.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/private_chain_util.py b/src/common/private_chain_util.py index db4ae561..a847190e 100644 --- a/src/common/private_chain_util.py +++ b/src/common/private_chain_util.py @@ -170,7 +170,7 @@ def get_data_from_raw_transaction(cls, raw_transaction, transaction_count): to_address = os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS'] else: to_address = os.environ['PRIVATE_CHAIN_ALIS_TOKEN_ADDRESS'] - if byte_data_list[3].hex() != to_address[2:]: + if byte_data_list[3].hex().lower() != to_address[2:].lower(): raise ValidationError('private_chain_alis_token_address is invalid') # value if byte_data_list[4].hex() != '': @@ -193,7 +193,7 @@ def validate_erc20_transfer_data(cls, data, to_address): if data[0:8] != 'a9059cbb': raise ValidationError('method is invalid') # to_address - if data[8:72][24:] != to_address[2:]: + if data[8:72][24:].lower() != to_address[2:].lower(): raise ValidationError('to_address is invalid') # tip_value validate( @@ -216,7 +216,7 @@ def validate_erc20_approve_data(cls, data): if data[0:8] != '095ea7b3': raise ValidationError('method is invalid') # spender_eth_address - if data[8:72][24:] != os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS'][2:]: + if data[8:72][24:].lower() != os.environ['PRIVATE_CHAIN_BRIDGE_ADDRESS'][2:].lower(): raise ValidationError('spender_eth_address is invalid') # value if int(data[72:], 16) != 0: From 10d843fb19ddcb96e5dee78c1b4f3fd0d96184de Mon Sep 17 00:00:00 2001 From: keillera Date: Sun, 26 Apr 2020 14:56:24 +0900 Subject: [PATCH 10/10] Fix a bug in check allownace. --- .../me/wallet/token/send/me_wallet_token_send.py | 6 +++--- .../wallet/token/send/test_me_wallet_token_send.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/handlers/me/wallet/token/send/me_wallet_token_send.py b/src/handlers/me/wallet/token/send/me_wallet_token_send.py index 32c2fb0d..3b6d3825 100644 --- a/src/handlers/me/wallet/token/send/me_wallet_token_send.py +++ b/src/handlers/me/wallet/token/send/me_wallet_token_send.py @@ -71,7 +71,7 @@ def exec_main_proc(self): ################ # validate raw_transaction # init_approve_signed_transaction - if allowance != '0x0': + if int(allowance, 16) != 0: # allowance が設定されている場合は必須 if self.params.get('init_approve_signed_transaction') is None: raise ValidationError('init_approve_signed_transaction is invalid.') @@ -112,8 +112,8 @@ def exec_main_proc(self): ####################### # send_raw_transaction ####################### - # 既に approve されている場合(allowance の戻り値が "0x0" ではない場合)、該当の approve を削除する(0 で更新) - if allowance != '0x0': + # 既に approve されている場合(allowance の戻り値が 0 ではない場合)、該当の approve を削除する(0 で更新) + if int(allowance, 16) != 0: PrivateChainUtil.send_raw_transaction(self.params.get('init_approve_signed_transaction')) # approve 実施 diff --git a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py index 47bc6706..c6754576 100644 --- a/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py +++ b/tests/handlers/me/wallet/token/send/test_me_wallet_token_send.py @@ -63,7 +63,7 @@ def test_main_ok(self): patch('private_chain_util.PrivateChainUtil.is_transaction_completed') as mock_is_transaction_completed: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') mock_is_transaction_completed.return_value = True @@ -288,7 +288,7 @@ def test_main_ok_exists_token_send_info_with_fail_data(self): patch('private_chain_util.PrivateChainUtil.get_transaction_count') as mock_get_transaction_count, \ patch('private_chain_util.PrivateChainUtil.is_transaction_completed') as mock_is_transaction_completed: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') mock_is_transaction_completed.return_value = True @@ -543,7 +543,7 @@ def test_main_ng_over_limit(self): patch('private_chain_util.PrivateChainUtil.get_transaction_count') as mock_get_transaction_count, \ patch('private_chain_util.PrivateChainUtil.is_transaction_completed') as mock_is_transaction_completed: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') mock_is_transaction_completed.return_value = True @@ -688,7 +688,7 @@ def test_main_ng_not_match_approve_value_and_relay_value(self): patch('private_chain_util.PrivateChainUtil.get_transaction_count') as mock_get_transaction_count: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') # テスト実施 @@ -736,7 +736,7 @@ def test_main_ng_with_status_fail_at_SendTransactionError(self): patch('private_chain_util.PrivateChainUtil.get_transaction_count') as mock_get_transaction_count, \ patch('private_chain_util.PrivateChainUtil.is_transaction_completed') as mock_is_transaction_completed: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') mock_is_transaction_completed.side_effect = SendTransactionError() @@ -797,7 +797,7 @@ def test_main_ng_with_status_fail_at_ReceiptError(self): patch('private_chain_util.PrivateChainUtil.get_transaction_count') as mock_get_transaction_count, \ patch('private_chain_util.PrivateChainUtil.is_transaction_completed') as mock_is_transaction_completed: # mock の初期化 - mock_get_allowance.return_value = '0x0' + mock_get_allowance.return_value = '0x' + '0' * 64 mock_get_transaction_count.return_value = format(nonce, '#x') mock_is_transaction_completed.side_effect = ReceiptError()