53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import asyncio
|
|
from os import getenv, urandom
|
|
import os
|
|
|
|
from nacl.bindings import crypto_sign_seed_keypair
|
|
from tonsdk.utils import Address
|
|
|
|
from app.core._blockchain.ton.wallet_v3cr3 import WalletV3CR3
|
|
from app.core.models._config import ServiceConfig
|
|
from app.core.storage import db_session
|
|
from app.core.logger import make_log
|
|
|
|
|
|
async def load_hot_pair_async():
|
|
async with db_session() as session:
|
|
service_config = ServiceConfig(session)
|
|
hot_seed = await service_config.get('private_key')
|
|
if hot_seed is None:
|
|
make_log("HotWallet", "No seed found, generating new one", level='info')
|
|
hot_seed_env = os.getenv("TON_INIT_HOT_SEED")
|
|
if not hot_seed_env:
|
|
hot_seed_bytes = urandom(32)
|
|
make_log("HotWallet", f"Generated random seed")
|
|
else:
|
|
hot_seed_bytes = bytes.fromhex(hot_seed_env)
|
|
make_log("HotWallet", f"Loaded seed from env")
|
|
|
|
await service_config.set('private_key', hot_seed_bytes.hex())
|
|
hot_seed = hot_seed_bytes.hex()
|
|
|
|
hot_seed_bytes = bytes.fromhex(hot_seed)
|
|
public_key, private_key = crypto_sign_seed_keypair(hot_seed_bytes)
|
|
return hot_seed_bytes, public_key, private_key
|
|
|
|
|
|
_extra_ton_wallet_options = {}
|
|
if getenv('TON_CUSTOM_WALLET_ADDRESS'):
|
|
_extra_ton_wallet_options['address'] = Address(getenv('TON_CUSTOM_WALLET_ADDRESS'))
|
|
|
|
|
|
def _init_wallet():
|
|
# Safe to call at import time; Sanic event loop not running yet
|
|
hot_seed, hot_pubkey, hot_privkey = asyncio.run(load_hot_pair_async())
|
|
wallet = WalletV3CR3(
|
|
private_key=hot_privkey,
|
|
public_key=hot_pubkey,
|
|
**_extra_ton_wallet_options
|
|
)
|
|
return hot_seed, hot_pubkey, hot_privkey, wallet
|
|
|
|
|
|
hot_seed, hot_pubkey, hot_privkey, service_wallet = _init_wallet()
|