Add phone number resolution to address for cli arg identifier

This commit is contained in:
nolash 2021-11-05 20:01:51 +01:00
parent 14e0c1fdb4
commit e8b6173ec5
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 65 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import logging
from cic_eth_registry import CICRegistry
from cic_eth_registry.lookup.tokenindex import TokenIndexLookup
from cic_types.ext.metadata import MetadataRequestsHandler
from chainlib.eth.address import to_checksum_address
# local imports
from clicada.tx import TxGetter
@ -21,24 +22,34 @@ ctrl = CmdCtrl(argv=sys.argv[1:], logger=logg)
tx_getter = TxGetter(ctrl.get('TX_CACHE_URL'))
MetadataRequestsHandler.base_url = ctrl.get('META_URL')
store_path = '.clicada'
user_phone_file_label = 'phone'
user_phone_store = FileUserStore(ctrl.chain(), user_phone_file_label, store_path)
try:
user_phone_store.put('25413241324', '5c4f9EeE1a6375d30f50ab547cD4EE21B37ac8Ab')
except FileExistsError:
logg.debug('already have record')
#try:
# user_phone_store.put('25413241324', '5c4f9EeE1a6375d30f50ab547cD4EE21B37ac8Ab')
#except FileExistsError:
# logg.debug('already have record')
k = '25413241324'
user_address = user_phone_store.get(k)
logg.debug('loaded user address {} for {}'.format(user_address, k))
#k = '25413241324'
user_address = user_phone_store.by_phone(ctrl.get('_ARG_USER_IDENTIFIER'))
if user_address == None:
sys.stderr.write('unknown identifier: {}\n'.format(ctrl.get('_ARG_USER_IDENTIFIER')))
sys.exit(1)
try:
user_address = to_checksum_address(user_address)
except ValueError:
sys.stderr.write('invalid response "{}" for {}\n'.format(user_address, ctrl.get('_ARG_USER_IDENTIFIER')))
sys.exit(1)
logg.debug('loaded user address {} for {}'.format(user_address, ctrl.get('_ARG_USER_IDENTIFIER')))
txs = tx_getter.get(user_address)
token_store = FileTokenStore(ctrl.chain(), ctrl.conn(), 'token', store_path)
MetadataRequestsHandler.base_url = ctrl.get('META_URL')
user_address_file_label = 'address'
user_address_store = FileUserStore(ctrl.chain(), user_address_file_label, store_path)

View File

@ -64,14 +64,14 @@ class ResolvedTokenTx(TokenTx):
return v
if self.tx_type == TokenTxType.faucet_giveto.value:
return 'FAUCET'
return user_store.get_metadata(self.sender)
return user_store.get_label(self.sender)
def resolve_recipient_entity(self, user_store):
v = self.resolve_stored_entity(user_store, self.recipient)
if v != None:
return v
return user_store.get_metadata(self.recipient)
return user_store.get_label(self.recipient)
def resolve_entities(self, user_store):

View File

@ -2,13 +2,18 @@
import stat
import os
import logging
import json
import urllib.parse
# external imports
from hexathon import strip_0x
from cic_types.condiments import MetadataPointer
from cic_types.ext.metadata import MetadataRequestsHandler
from cic_types.models.person import Person
from cic_types.ext.requests import make_request
from cic_types.processor import generate_metadata_pointer
import requests.exceptions
import phonenumbers
# local imports
from clicada.encode import tx_normalize
@ -57,7 +62,7 @@ class FileUserStore:
logg.info('added user store {} record {} -> {}'.format(self.label, k, v))
def get(self, k):
p = os.path.join(self.store_path, k)
f = open(p, 'r')
@ -66,7 +71,40 @@ class FileUserStore:
return r.strip()
def get_metadata(self, address):
def by_phone(self, phone):
phone = phonenumbers.parse(phone, None)
phone = phonenumbers.format_number(phone, phonenumbers.PhoneNumberFormat.E164)
phone_file = phone.replace('+', '')
try:
v = self.get(phone_file)
return v
except FileNotFoundError:
pass
#getter = MetadataRequestsHandler(MetadataPointer.PHONE, phone.encode('utf-8'))
r = None
try:
ptr = generate_metadata_pointer(phone.encode('utf-8'), MetadataPointer.PHONE)
url = urllib.parse.urljoin(MetadataRequestsHandler.base_url, ptr)
r = make_request('GET', url)
return r.json()
except requests.exceptions.HTTPError as e:
logg.debug('no address found for phone {}: {}'.format(phone, e))
return None
def get_label(self, address):
add = tx_normalize.wallet_address(address)
try:
v = self.get(address)
v = json.loads(v)
person = Person()
person_data = person.deserialize(person_data=v)
return str(person_data)
except FileNotFoundError:
pass
address = strip_0x(address)
getter = MetadataRequestsHandler(MetadataPointer.PERSON, bytes.fromhex(address))
r = None
@ -75,7 +113,9 @@ class FileUserStore:
except requests.exceptions.HTTPError as e:
logg.debug('no metadata found for {}: {}'.format(address, e))
return address
data = r.json()
person = Person()
person_data = person.deserialize(person_data=data)
self.put(address, json.dumps(person_data.serialize()))
return str(person_data)

View File

@ -2,3 +2,4 @@ usumbufu~=0.3.2
confini~=0.5.1
cic-eth-registry~=0.6.1
cic-types~=0.2.1a3
phonenumbers==8.12.12