Skip to content

Commit

Permalink
Merge pull request #106 from planetarium/bugfix/multiplanetary
Browse files Browse the repository at this point in the history
Update multiplanetary appicances
  • Loading branch information
U-lis authored Nov 13, 2023
2 parents dcfecbc + 61762f7 commit 34afd16
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/synth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions common/_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions iap/iap_cdk_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions iap/schemas/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
23 changes: 17 additions & 6 deletions worker/worker/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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:
Expand All @@ -37,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
Expand Down Expand Up @@ -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 = [{
Expand Down
1 change: 1 addition & 0 deletions worker/worker_cdk_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34afd16

Please sign in to comment.