gasgasgas/gas3/register.py
2023-09-01 03:53:23 +01:00

64 lines
1.8 KiB
Python

# standard imports
import logging
# external imports
from chainlib.eth.address import to_checksum_address
from hexathon import strip_0x
from eth_accounts_index import AccountsIndex
from eth_faucet import EthFaucet
# local imports
from gas3.error import InvalidInput
from gas3.error import Used
from gas3.base import check
logg = logging.getLogger(__name__)
def register_address(settings, address):
c = AccountsIndex(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('FEE_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
(tx_hash, o) = c.add(settings.get('ACCOUNTS_CONTRACT'), settings.get('SENDER_ADDRESS'), address)
r = settings.get('CONN').do(o)
logg.debug('registry tx for {}: {}'.format(address, tx_hash))
c = EthFaucet(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('FEE_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
(tx_hash, o) = c.give_to(settings.get('FAUCET_CONTRACT'), settings.get('SENDER_ADDRESS'), address)
r = settings.get('CONN').do(o)
logg.debug('faucet tx for {}: {}'.format(address, tx_hash))
return tx_hash
def do_register(req, env, data_dir, settings):
i = req.path.find('/', 1)
if i == -1:
raise InvalidInput(env.path)
p = req.path[i+1:]
r = None
r = check(p, data_dir)
if not r:
raise Used('already used: ' + p)
v = env['wsgi.input'].read(42)
if type(v) == bytes:
v = v.decode('utf-8')
try:
v = strip_0x(v)
v = to_checksum_address(v)
except:
raise InvalidInput('invalid address: ' + str(v))
data_dir.add(p, bytes.fromhex(v))
tx_hash = register_address(settings, v)
return tx_hash