From 1accef3f7fe5bcaeca56871ae4d694a8d826ce66 Mon Sep 17 00:00:00 2001 From: hyeon Date: Mon, 13 Nov 2023 23:18:07 +0900 Subject: [PATCH 1/3] Set default planet by network --- iap/api/purchase.py | 4 ++++ iap/schemas/receipt.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/iap/api/purchase.py b/iap/api/purchase.py index 9b99a2f1..073b2d18 100644 --- a/iap/api/purchase.py +++ b/iap/api/purchase.py @@ -18,6 +18,7 @@ from common.utils.apple import get_jwt from common.utils.aws import fetch_parameter from common.utils.google import get_google_client +from common.utils.receipt import PlanetID from iap import settings from iap.dependencies import session from iap.main import logger @@ -107,6 +108,9 @@ def request_product(receipt_data: ReceiptSchema, sess=Depends(session)): - `Store` :: str : Store name. Should be `AppleAppStore`. - `TransactionID` :: str : Apple IAP transaction ID formed like `2000000432373050`. """ + if not receipt_data.planetId: + receipt_data.planetId = PlanetID.ODIN if settings.stage == "mainnet" else PlanetID.ODIN_INTERNAL + order_id, product_id, purchased_at = get_order_data(receipt_data) prev_receipt = sess.scalar( select(Receipt).where(Receipt.store == receipt_data.store, Receipt.order_id == order_id) diff --git a/iap/schemas/receipt.py b/iap/schemas/receipt.py index 93c14696..48d4f218 100644 --- a/iap/schemas/receipt.py +++ b/iap/schemas/receipt.py @@ -64,7 +64,7 @@ class ReceiptSchema: data: Union[str, Dict, object] agentAddress: str avatarAddress: str - planetId: Union[str, PlanetID] = PlanetID.ODIN + planetId: Union[str, PlanetID] = None # Google payload: Optional[Dict] = None From 8b38731319ba8ebd86d807892c9edb5a7fd181ea Mon Sep 17 00:00:00 2001 From: hyeon Date: Tue, 14 Nov 2023 01:07:15 +0900 Subject: [PATCH 2/3] Use PLANET_URL to send to bridge --- .github/workflows/deploy.yml | 2 ++ .github/workflows/synth.yml | 1 + common/__init__.py | 1 + iap/iap_cdk_stack.py | 1 + worker/worker/handler.py | 21 ++++++++++++++++----- worker/worker_cdk_stack.py | 1 + 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d8245191..d93c822a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -149,6 +149,7 @@ jobs: GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }} FORM_SHEET: ${{ vars.FORM_SHEET }} CDN_HOST: ${{ vars.CDN_HOST }} + PLANET_URL: ${{ vars.PLANET_URL }} SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }} ODIN_AGENT_ADDRESS: ${{ secrets.ODIN_AGENT_ADDRESS }} ODIN_AVATAR_ADDRESS: ${{ secrets.ODIN_AVATAR_ADDRESS }} @@ -184,6 +185,7 @@ jobs: GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }} FORM_SHEET: ${{ vars.FORM_SHEET }} CDN_HOST: ${{ vars.CDN_HOST }} + PLANET_URL: ${{ vars.PLANET_URL }} SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }} ODIN_AGENT_ADDRESS: ${{ secrets.ODIN_AGENT_ADDRESS }} ODIN_AVATAR_ADDRESS: ${{ secrets.ODIN_AVATAR_ADDRESS }} diff --git a/.github/workflows/synth.yml b/.github/workflows/synth.yml index 460db1ae..720dfbd4 100644 --- a/.github/workflows/synth.yml +++ b/.github/workflows/synth.yml @@ -136,6 +136,7 @@ jobs: GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }} FORM_SHEET: ${{ vars.FORM_SHEET }} CDN_HOST: ${{ vars.CDN_HOST }} + PLANET_URL: ${{ vars.PLANET_URL }} SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }} ODIN_AGENT_ADDRESS: ${{ secrets.ODIN_AGENT_ADDRESS }} ODIN_AVATAR_ADDRESS: ${{ secrets.ODIN_AVATAR_ADDRESS }} diff --git a/common/__init__.py b/common/__init__.py index 8fa637a2..71ccdad5 100644 --- a/common/__init__.py +++ b/common/__init__.py @@ -36,6 +36,7 @@ class Config: account_id: str region_name: str cdn_host: str + planet_url: str google_package_name: str apple_bundle_id: str diff --git a/iap/iap_cdk_stack.py b/iap/iap_cdk_stack.py index 6847cf56..9d9e2bc4 100644 --- a/iap/iap_cdk_stack.py +++ b/iap/iap_cdk_stack.py @@ -98,6 +98,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: "APPLE_ISSUER_ID": config.apple_issuer_id, "HEADLESS": config.headless, "CDN_HOST": config.cdn_host, + "PLANET_URL": config.planet_url, } # Lambda Function diff --git a/worker/worker/handler.py b/worker/worker/handler.py index 754a17ee..26466cee 100644 --- a/worker/worker/handler.py +++ b/worker/worker/handler.py @@ -4,6 +4,7 @@ from dataclasses import dataclass from typing import List, Optional, Tuple, Union +import requests from sqlalchemy import create_engine, select from sqlalchemy.orm import Session, joinedload, scoped_session, sessionmaker @@ -13,7 +14,6 @@ from common.enums import TxStatus from common.models.product import Product from common.models.receipt import Receipt -from common.utils.address import get_vault_agent_address, get_vault_avatar_address from common.utils.aws import fetch_secrets, fetch_kms_key_id from common.utils.receipt import PlanetID @@ -23,6 +23,17 @@ engine = create_engine(DB_URI, pool_size=5, max_overflow=5) +resp = requests.get(os.environ.get("PLANET_URL")) +data = resp.json() +current_planet = PlanetID.ODIN if os.environ.get("STAGE") == "mainnet" else PlanetID.ODIN_INTERNAL +planet_dict = {} +for planet in data: + if PlanetID(bytes(planet["id"], "utf-8")) == current_planet: + planet_dict = { + PlanetID(bytes(k, "utf-8")): {"agent": v["agent"], "avatar": v["avatar"]} + for k, v in planet["bridges"].items() + } + @dataclass class SQSMessageRecord: @@ -66,10 +77,10 @@ def process(sess: Session, message: SQSMessageRecord, nonce: int = None) -> Tupl planet_id: PlanetID = PlanetID(bytes(message.body["planet_id"], 'utf-8')) agent_address = message.body.get("agent_addr") avatar_address = message.body.get("avatar_addr") - # relay - if planet_id != PlanetID.ODIN: - agent_address = get_vault_agent_address(planet_id) - avatar_address = get_vault_avatar_address(planet_id) + # Through bridge + if planet_id != current_planet: + agent_address = planet_dict[planet_id]["agent"] + avatar_address = planet_dict[planet_id]["avatar"] fav_data = [x.to_fav_data(agent_address=agent_address, avatar_address=avatar_address) for x in product.fav_list] item_data = [{ diff --git a/worker/worker_cdk_stack.py b/worker/worker_cdk_stack.py index c7e75981..9480472b 100644 --- a/worker/worker_cdk_stack.py +++ b/worker/worker_cdk_stack.py @@ -87,6 +87,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: f"/iap", "GOOGLE_PACKAGE_NAME": config.google_package_name, "HEADLESS": config.headless, + "PLANET_URL": config.planet_url, } # Worker Lambda Function From 61762f7a0c6e94f66524a2e6dd0b72371ef836f1 Mon Sep 17 00:00:00 2001 From: hyeon Date: Tue, 14 Nov 2023 01:13:04 +0900 Subject: [PATCH 3/3] Use isinstance() instead of type() --- common/_crypto.py | 4 ++-- iap/schemas/receipt.py | 2 +- worker/worker/handler.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/_crypto.py b/common/_crypto.py index e9e3dd81..4e336061 100644 --- a/common/_crypto.py +++ b/common/_crypto.py @@ -158,10 +158,10 @@ def derive_address(address: Union[str, bytes], key: Union[str, bytes], get_byte: if address.startswith("0x"): address = address[2:] - if type(address) == str: + if isinstance(address, str): address = bytes.fromhex(address) - if type(key) == str: + if isinstance(key, str): key = bytes(key, "UTF-8") derived = hmac.new(key, address, sha1).digest() diff --git a/iap/schemas/receipt.py b/iap/schemas/receipt.py index 48d4f218..3748d69a 100644 --- a/iap/schemas/receipt.py +++ b/iap/schemas/receipt.py @@ -74,7 +74,7 @@ class ReceiptSchema: def __post_init__(self): # Parse purchase data to JSON - if type(self.data) == str: + if isinstance(self.data, str): self.data = json.loads(self.data) if self.store in (Store.GOOGLE, Store.GOOGLE_TEST): diff --git a/worker/worker/handler.py b/worker/worker/handler.py index 26466cee..4fe1b8bb 100644 --- a/worker/worker/handler.py +++ b/worker/worker/handler.py @@ -48,7 +48,7 @@ class SQSMessageRecord: awsRegion: str def __post_init__(self): - self.body = json.loads(self.body) if type(self.body) == str else self.body + self.body = json.loads(self.body) if isinstance(self.body, str) else self.body @dataclass