def __init__(self, config, contract_name, endpoint): self.config = config self.contract_name = contract_name self.contract_address = self.config[contract_name]['address'] self.endpoint = endpoint self.contract_handler = ContractsService( self.config).get_contract_handler(contract_name, self.endpoint) self.delegated_call_address = None
def create_alastria_identity(self, public_key: str) -> Transaction: public_key_data = ContractsService.AlastriaPublicKeyRegistry( self.endpoint).encodeABI(fn_name="addKey", args=[public_key]) data = ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI(fn_name="createAlastriaIdentity", args=[public_key_data]) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def delegated(self, delegated_data) -> str: identity_manager_contract = ContractsService( self.config).get_contract_handler( self.DEFAULT_CONTRACT_DELEGATED_NAME, self.endpoint) return identity_manager_contract.encodeABI( fn_name=self.DEFAULT_DELEGATED_FUNCTION_NAME, args=[ Web3.toChecksumAddress(self.contract_address), 0, delegated_data ])
def test_get_identity_manager_abi(): expected_abi = [{"constant":True,"inputs":[{"name":"","type":"address"}],"name":"identityKeys","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"_identityIssuer","type":"address"}],"name":"getEidasLevel","outputs":[{"name":"","type":"uint8"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_identityServiceProvider","type":"address"}],"name":"addIdentityServiceProvider","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_url_logo","type":"string"}],"name":"setUrlLogo","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"newOwner","type":"address"}],"name":"transfer","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_cif","type":"string"}],"name":"setCifEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"}],"name":"pendingIDs","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"addr","type":"address"}],"name":"isOwner","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_identityServiceProvider","type":"address"}],"name":"deleteIdentityServiceProvider","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"alastriaPresentationRegistry","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_identityIssuer","type":"address"},{"name":"_level","type":"uint8"}],"name":"updateIdentityIssuerEidasLevel","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_signAddress","type":"address"}],"name":"prepareAlastriaID","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_url_createAID","type":"string"}],"name":"setUrlCreateAID","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"version","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_destination","type":"address"},{"name":"_value","type":"uint256"},{"name":"_data","type":"bytes"}],"name":"delegateCall","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"_identityIssuer","type":"address"}],"name":"isIdentityIssuer","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"addPublicKeyCallData","type":"bytes"}],"name":"createAlastriaIdentity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"entitiesList","outputs":[{"name":"","type":"address[]"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"_addressEntity","type":"address"}],"name":"getEntity","outputs":[{"name":"_name","type":"string"},{"name":"_cif","type":"string"},{"name":"_url_logo","type":"string"},{"name":"_url_createAID","type":"string"},{"name":"_url_AOA","type":"string"},{"name":"_active","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_identityIssuer","type":"address"},{"name":"_level","type":"uint8"}],"name":"addIdentityIssuer","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_url_AOA","type":"string"}],"name":"setUrlAOA","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_name","type":"string"},{"name":"_cif","type":"string"},{"name":"_url_logo","type":"string"},{"name":"_url_createAID","type":"string"},{"name":"_url_AOA","type":"string"},{"name":"_active","type":"bool"}],"name":"addEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"accountLost","type":"address"},{"name":"newAccount","type":"address"}],"name":"recoverAccount","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_identityIssuer","type":"address"}],"name":"deleteIdentityIssuer","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"_identityServiceProvider","type":"address"}],"name":"isIdentityServiceProvider","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"alastriaCredentialRegistry","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_addressEntity","type":"address"},{"name":"_name","type":"string"}],"name":"setNameEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"alastriaPublicKeyRegistry","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_version","type":"uint256"}],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":True,"name":"signAddress","type":"address"}],"name":"PreparedAlastriaID","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"name":"method","type":"string"}],"name":"OperationWasNotSupported","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"name":"identity","type":"address"},{"indexed":True,"name":"creator","type":"address"},{"indexed":False,"name":"owner","type":"address"}],"name":"IdentityCreated","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"name":"oldAccount","type":"address"},{"indexed":False,"name":"newAccount","type":"address"},{"indexed":True,"name":"serviceProvider","type":"address"}],"name":"IdentityRecovered","type":"event"}] builder = IdentityConfigBuilder( contracts_info_url='https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md', parser_class=ContractParser ) config = builder.generate() service = ContractsService(config) abi = service.get_abi_by_contract_name('AlastriaIdentityManager') assert abi == expected_abi
def get_entity(self, entity: Entity) -> Transaction: entity_address = NetworkDid.from_did(entity.did_entity).proxy_address data = ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI(fn_name='getEntity', args=[entity_address]) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def set_entity_url_aoa_aid(self, entity: Entity) -> Transaction: entity_address = NetworkDid.from_did(entity.did_entity).proxy_address data = self.delegated( ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( fn_name='setUrlAOA', args=[entity.url_aoa])) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def is_identity_issuer(self, did_issuer: str) -> Transaction: issuer_address = NetworkDid.from_did(did_issuer).proxy_address data = ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI(fn_name='isIdentityIssuer', args=[issuer_address]) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def prepare_alastria_id(self, sign_address: str) -> Transaction: data = self.delegated( ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( fn_name="prepareAlastriaID", args=[sign_address])) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def delegated(self, delegated_data) -> str: return ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI( fn_name='delegateCall', args=[ Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), 0, delegated_data ])
def add_identity_service_provider( self, did_service_provider: str) -> Transaction: provider_address = NetworkDid.from_did( did_service_provider).proxy_address data = self.delegated( ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( fn_name='addIdentityServiceProvider', args=[provider_address])) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def update_identity_issuer_eidas_level(self, did_issuer: str, level: int) -> Transaction: issuer_address = NetworkDid.from_did(did_issuer).proxy_address data = self.delegated( ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( fn_name='updateIdentityIssuerEidasLevel', args=[issuer_address, level])) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def _build_transaction(self, function_name: str, args: list, delegated: bool) -> Transaction: encoded_abi = ContractsService.AlastriaPublicKeyRegistry( self.endpoint).encodeABI(fn_name=function_name, args=args) data = self.delegated(encoded_abi) if delegated else encoded_abi return Transaction( to=Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), data=data)
def main(): # We generate the config based on the markdown CONTRACTS_INFO_URL = 'https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md' builder = IdentityConfigBuilder( contracts_info_url=CONTRACTS_INFO_URL, parser_class=ContractParser ) config = builder.generate() # We instantiate the contract service ALASTRIA_IDENTITY_MANAGER_CONTRACT_NAME = 'AlastriaIdentityManager' PROVIDER_NODE_URL = os.environ.get( 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') endpoint = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) contract_service = ContractsService(config) alastria_identity_manager_contract = contract_service.get_contract_handler( ALASTRIA_IDENTITY_MANAGER_CONTRACT_NAME, endpoint)
def add_entity(self, entity: Entity) -> Transaction: entity_address = NetworkDid.from_did(entity.did_entity).proxy_address data = self.delegated( ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( fn_name='addEntity', args=[ entity_address, entity.name, entity.cif, entity.url_logo, entity.url_create_aid, entity.url_aoa, entity.status ])) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
class TransactionService: DEFAULT_DELEGATED_FUNCTION_NAME = 'delegateCall' DEFAULT_CONTRACT_DELEGATED_NAME = 'AlastriaIdentityManager' def __init__(self, config, contract_name, endpoint): self.config = config self.contract_name = contract_name self.contract_address = self.config[contract_name]['address'] self.endpoint = endpoint self.contract_handler = ContractsService( self.config).get_contract_handler(contract_name, self.endpoint) self.delegated_call_address = None def enable_delegated_call(self): self.delegated_call_address = self.config[ self.DEFAULT_CONTRACT_DELEGATED_NAME]['address'] return self def generate_transaction(self, function_name: str, args: list) -> Transaction: encoded_abi = self.contract_handler.encodeABI(fn_name=function_name, args=args) payload = encoded_abi if self.is_delegated_call(): payload = self.delegated(encoded_abi) contract_address = self.delegated_call_address or self.contract_address return Transaction(to=Web3.toChecksumAddress(contract_address), data=payload) def delegated(self, delegated_data) -> str: identity_manager_contract = ContractsService( self.config).get_contract_handler( self.DEFAULT_CONTRACT_DELEGATED_NAME, self.endpoint) return identity_manager_contract.encodeABI( fn_name=self.DEFAULT_DELEGATED_FUNCTION_NAME, args=[ Web3.toChecksumAddress(self.contract_address), 0, delegated_data ]) def is_delegated_call(self) -> bool: return bool(self.delegated_call_address)
def get_entities_list(self) -> Transaction: data = ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI(fn_name='entitiesList', args=[]) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)
def get_identity_key(self, address: str) -> Transaction: data = ContractsService.AlastriaIdentityManager( self.endpoint).encodeABI(fn_name='identityKeys', args=[address]) return Transaction(to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), data=data)