diff --git a/.coveragerc b/.coveragerc index 476fc43..eac75d0 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,6 +1,6 @@ [run] branch = true -omit = */tests/* +omit = */tests/*,*/examples/* source = . [report] diff --git a/CHANGELOG.md b/CHANGELOG.md index 591afa6..01c8ecd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## v0.5.0 + +- Removed all the hardcoded services to make the code cleaner. This is a backwards incompatible change since + the current methods to access smart contract functions weren't PEP8 standard, didn't comply with SRP nor DRY either. **IMPORTANT**: This makes the new library incompatible backwards, check the examples to see how to implement it. + ## v0.4.0 - Fix presentations registry operations to use the correct smart contract address when delegated diff --git a/README.md b/README.md index 301b00f..f590214 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Python version of the Alastria Identity lib -# Installing +## Installing ```bash pip install alastria-identity @@ -14,14 +14,16 @@ or you could use Poetry poetry add alastria-identity ``` -# Testing +## Testing Execute tests + ```bash docker-compose run --rm identity poetry run python -m coverage run -m pytest alastria_identity -v . ``` Create and check test coverage + ```bash docker-compose run --rm identity poetry run coverage html python -m http.server 8000 @@ -29,10 +31,36 @@ python -m http.server 8000 Open `http://localhost:8000` in your browser -# TODO +## How to use the library + +You can check [the examples in this folder, we'll continue updating the documentation](https://github.com/Wealize/alastria-identity-lib-py/tree/main/alastria_identity/examples). + +## Some gooodies + +You can check in different files utilities that might be useful to you. + +- **Types**: We use these dataclasses so it's easier to interact with the library, feel free to [use them to make it easier on yourself](https://github.com/Wealize/alastria-identity-lib-py/tree/main/alastria_identity/types). :-) +- **JWT tokens handling**: [This service](https://github.com/Wealize/alastria-identity-lib-py/blob/main/alastria_identity/services/tokens.py) makes encoding decoding jwt tokens easier. +- **Config builder**: [The config builder](https://github.com/Wealize/alastria-identity-lib-py/blob/main/alastria_identity/services/config_builder.py) receives an url and a parser class and parses the configuration so It can be read by the transaction service. You can check [the test to see how this structure ends up](https://github.com/Wealize/alastria-identity-lib-py/blob/main/alastria_identity/tests/test_parsers.py). +- **Transaction service**: [The service in charge of building a transaction](https://github.com/Wealize/alastria-identity-lib-py/tree/main/alastria_identity/services/transaction_service.py) and "talking" to the smart contract, it receives a configuration to know which parameters can use and what's available. -- This README -- Add more code examples +## Glossary + +- **Provider node url**: It's the url (with `/rpc` endpoint) of the node you want to connect in which the library can interact with the Alastria identity + smart contracts. +- **Contracts info**: It's the file Alastria members created to be loaded to the identity libraries. You can't change it using the javascript library but you can set it in this one. We generate the configuration the Transaction service uses on the fly. +- **Contract names**: The contract names you need to use the library, in this case you can check the Alastria examples in the [ContractsInfo file]( 'https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md'). You can check it better formatted pasting it to [codebeautify.org](https://codebeautify.org/python-formatter-beautifier). +- **Contract addresses**: As the one above It's self-explanatory, the address of the smart contract deployed you want to use. + +## TODO + +- ~This README~ +- ~Add more code examples~ [You can check them here](https://github.com/Wealize/alastria-identity-lib-py/tree/main/alastria_identity/examples) - ~Create the PyPI package and push it to pypi.org~ - Test the connection with the identity Alastria network node -- Delegate calls is still a WIP, we need to finish that +- ~Delegate calls is still a WIP, we need to finish that~ +- Get address from the ContractInfo data + +## Contributors + +Started with :heart: by the [Wealize Team](https://github.com/Wealize/alastria-identity-lib-py/graphs/contributors). diff --git a/alastria_identity/examples/alastria_contract_config_generation.py b/alastria_identity/examples/alastria_contract_config_generation.py deleted file mode 100644 index 6c086af..0000000 --- a/alastria_identity/examples/alastria_contract_config_generation.py +++ /dev/null @@ -1,15 +0,0 @@ -from alastria_identity.services import IdentityConfigBuilder -from alastria_identity.services import ContractParser - - -def main(): - builder = IdentityConfigBuilder( - 'https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md', - ContractParser - ) - config = builder.generate() - print(config) - - -if __name__ == '__main__': - main() diff --git a/alastria_identity/examples/config_generation_example.py b/alastria_identity/examples/config_generation_example.py new file mode 100644 index 0000000..d2d820c --- /dev/null +++ b/alastria_identity/examples/config_generation_example.py @@ -0,0 +1,19 @@ +from alastria_identity.services import IdentityConfigBuilder, ContractParser + + +def main(): + # We generate the config based on the markdown url + 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() + + # This is the format of the config + # We'll use config['functions'] for the TransactionService + print(config) + + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/contract_service_example.py b/alastria_identity/examples/contract_service_example.py new file mode 100644 index 0000000..c5c96de --- /dev/null +++ b/alastria_identity/examples/contract_service_example.py @@ -0,0 +1,31 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + ContractsService, IdentityConfigBuilder, ContractParser) + + +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) + + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/create_alastria_identity_example.py b/alastria_identity/examples/create_alastria_identity_example.py new file mode 100644 index 0000000..0d4a3f2 --- /dev/null +++ b/alastria_identity/examples/create_alastria_identity_example.py @@ -0,0 +1,46 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + IdentityConfigBuilder, ContractParser, TransactionService) +from alastria_identity.types import Transaction + + +def main(): + # We generate the config based on the markdown url + 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() + + PROVIDER_NODE_URL = os.environ.get( + 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') + web3_instance = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) + + # Non delegated call + PUBLIC_KEY = os.environ.get('PUBLIC_KEY', 'mykey') + + transaction_service = TransactionService( + config, + 'AlastriaPublicKeyRegistry', + web3_instance) + transaction_response: Transaction = transaction_service.generate_transaction( + 'addKey', + [PUBLIC_KEY] + ) + + transaction_service = TransactionService( + config, + 'AlastriaIdentityManager', + web3_instance) + transaction_response: Transaction = transaction_service.generate_transaction( + 'createAlastriaIdentity', + [transaction_response.data] + ) + + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/credential_contract_example.py b/alastria_identity/examples/credential_contract_example.py new file mode 100644 index 0000000..7b5603a --- /dev/null +++ b/alastria_identity/examples/credential_contract_example.py @@ -0,0 +1,38 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + IdentityConfigBuilder, ContractParser, TransactionService) + + +def main(): + # We generate the config based on the markdown url + CONTRACTS_INFO_URL = os.environ.get( + '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() + + # Non delegated call + PROVIDER_NODE_URL = os.environ.get( + 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') + web3_endpoint = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) + + transaction_service = TransactionService( + config, 'AlastriaCredentialRegistry', web3_endpoint) + + subject_status, issuer_status = 1, 1 + transaction_service.generate_transaction( + 'getCredentialStatus', [subject_status, issuer_status]) + + issuer_credential_hash, status = b'dummy', 1 + transaction_service.enable_delegated_call().generate_transaction( + 'updateCredentialStatus', [issuer_credential_hash, status]) + + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/identity_manager_example.py b/alastria_identity/examples/identity_manager_example.py new file mode 100644 index 0000000..d0c7cea --- /dev/null +++ b/alastria_identity/examples/identity_manager_example.py @@ -0,0 +1,42 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + IdentityConfigBuilder, ContractParser, TransactionService) +from alastria_identity.types import NetworkDid + + +def main(): + # We generate the config based on the markdown url + 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() + + # Non delegated call + PROVIDER_NODE_URL = os.environ.get( + 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') + web3_endpoint = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) + + transaction_service = TransactionService( + config, + 'AlastriaIdentityManager', + web3_endpoint) + + SIGN_DID = os.environ.get( + 'SIGN_DID', + 'did:ala:quor:redT:ee2d1fe7b0d4571155c93497a7a9bde56fb87b40') + + # We can use NetworkDid to get the proxy_address out of a did + sign_address = NetworkDid.from_did(SIGN_DID).proxy_address + checksum = Web3.toChecksumAddress(sign_address) + + transaction_service.enable_delegated_call().generate_transaction( + 'prepareAlastriaID', [checksum]) + + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/presentation_registry_example.py b/alastria_identity/examples/presentation_registry_example.py new file mode 100644 index 0000000..ddeb359 --- /dev/null +++ b/alastria_identity/examples/presentation_registry_example.py @@ -0,0 +1,33 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + IdentityConfigBuilder, ContractParser, TransactionService) + + +def main(): + # We generate the config based on the markdown url + 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() + + # Non delegated call + PROVIDER_NODE_URL = os.environ.get( + 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') + web3_endpoint = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) + + transaction_service = TransactionService( + config, + 'AlastriaPresentationRegistry', + web3_endpoint) + + receiver_presentation_hash, status = b'myhash', 1 + transaction_service.enable_delegated_call().generate_transaction( + 'updateReceiverPresentation', [receiver_presentation_hash, status]) + +if __name__ == '__main__': + main() diff --git a/alastria_identity/examples/public_key_example.py b/alastria_identity/examples/public_key_example.py new file mode 100644 index 0000000..3b41960 --- /dev/null +++ b/alastria_identity/examples/public_key_example.py @@ -0,0 +1,37 @@ +import os + +from web3 import Web3 + +from alastria_identity.services import ( + IdentityConfigBuilder, ContractParser, TransactionService) + + +def main(): + # We generate the config based on the markdown url + 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() + + # Non delegated call + PROVIDER_NODE_URL = os.environ.get( + 'PROVIDER_NODE_URL', 'https://127.0.0.1/rpc') + web3_endpoint = Web3(Web3.HTTPProvider(PROVIDER_NODE_URL)) + + transaction_service = TransactionService( + config, + 'AlastriaPublicKeyRegistry', + web3_endpoint) + + subject_address = Web3.toChecksumAddress('0xee2d1fe7b0d4571155c93497a7a9bde56fb87b40') + public_key = b'12345' + transaction_service.generate_transaction( + 'getPublicKeyStatus', [subject_address, public_key]) + + transaction_service.enable_delegated_call().generate_transaction( + 'deletePublicKey', [subject_address]) + +if __name__ == '__main__': + main() diff --git a/alastria_identity/exceptions.py b/alastria_identity/exceptions.py new file mode 100644 index 0000000..ced22e5 --- /dev/null +++ b/alastria_identity/exceptions.py @@ -0,0 +1 @@ +class ContractNameError(Exception): pass diff --git a/alastria_identity/services/__init__.py b/alastria_identity/services/__init__.py index cfb23ea..ec2d146 100644 --- a/alastria_identity/services/__init__.py +++ b/alastria_identity/services/__init__.py @@ -1,15 +1,6 @@ from .config_builder import IdentityConfigBuilder -from .contracts import ( - IDENTITY_MANAGER_ADDRESS, - ContractsService, - PUBLIC_KEY_REGISTRY_ADDRESS, - PRESENTATION_REGISTRY_ADDRESS, - CREDENTIAL_REGISTRY_ADDRESS -) -from .credential_registry import CredentialRegistryService +from .contracts import ContractsService from .identity import UserIdentityService -from .identity_manager import IdentityManagerService from .parsers import ContractParser -from .presentation_registry import PresentationRegistryService from .tokens import TokenService -from .public_key import PublicKeyService +from .transaction_service import TransactionService diff --git a/alastria_identity/services/config_builder.py b/alastria_identity/services/config_builder.py index 6a60713..e64bcf5 100644 --- a/alastria_identity/services/config_builder.py +++ b/alastria_identity/services/config_builder.py @@ -1,41 +1,55 @@ from typing import List import re +from collections import defaultdict import requests from .parsers import ContractParser +from alastria_identity.types import ConfigParser class IdentityConfigBuilder: CONTRACT_NAME_REGEX = r'sol_(?P.*)\.abi' CONTRACT_URL_POSITION = -2 + ADDRESS_POSITION = -3 - def __init__(self, contracts_info_url: str, parser_class: ContractParser): + def __init__(self, contracts_info_url: str, parser_class: ConfigParser): self.parser_class = parser_class self.contracts_info_url = contracts_info_url - def get_contracts(self) -> List[str]: - contracts_raw_response = requests.get(self.contracts_info_url) - return list(self.extract_urls_from_response( - contracts_raw_response.content.decode('utf-8'))) - - def extract_urls_from_response( - self, contracts_content: List[dict] - ) -> List[str]: - return map( - lambda contract_line: contract_line.split('|')[self.CONTRACT_URL_POSITION].strip(), - contracts_content.split('\n')[2:-1] - ) - def generate(self): - config = {} + ''' + We will output a config with two keys: + - Functions are the ones interesting for generating transactions + - Addresses will be used to point to the right contract also using + its name + ''' + config = defaultdict(dict) contracts = self.get_contracts() - for contract_url in contracts: + for contract_item in contracts: name = re.search( self.CONTRACT_NAME_REGEX, - contract_url + contract_item['url'] ).group('name') - config[name] = self.parser_class(contract_url).parse() + + config[name]['functions'] = self.parser_class(contract_item['url']).parse() + config[name]['address'] = contract_item['address'] return config + + def get_contracts(self) -> List[str]: + contracts_raw_response = requests.get(self.contracts_info_url) + return list(self.extract_contract_item_from_response( + contracts_raw_response.content.decode('utf-8'))) + + def extract_contract_item_from_response( + self, contracts_content: str + ) -> List[dict]: + return map( + lambda contract_line: { + 'url': contract_line.split('|')[self.CONTRACT_URL_POSITION].strip(), + 'address': contract_line.split('|')[self.ADDRESS_POSITION].strip() + }, + contracts_content.split('\n')[2:-1] + ) diff --git a/alastria_identity/services/contracts.py b/alastria_identity/services/contracts.py index e02aa8e..3b76eff 100644 --- a/alastria_identity/services/contracts.py +++ b/alastria_identity/services/contracts.py @@ -1,43 +1,23 @@ from web3 import Web3 from web3.contract import Contract -IDENTITY_MANAGER_ADDRESS = '0xbd4a2c84edb97be5beff7cd341bd63567e73f8c9' -PUBLIC_KEY_REGISTRY_ADDRESS = '0x4958091227bbfbe1fdfc0fd79fc44844dc014ca0' -PRESENTATION_REGISTRY_ADDRESS = '0x54d1dbfacada17ff39f2bac08e05fbdb4659f671' -CREDENTIAL_REGISTRY_ADDRESS = '0x7bbca11cbd86b562136d5708eba40f4bc0aa1ddc' +from alastria_identity.exceptions import ContractNameError -class ContractsService: - @staticmethod - def AlastriaIdentityEntity(endpoint: Web3) -> Contract: - # TODO get the ABI from autogenerated config - return endpoint.eth.contract( - abi=[{"inputs":[],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_cif","type":"string"},{"internalType":"string","name":"_url_logo","type":"string"},{"internalType":"string","name":"_url_createAID","type":"string"},{"internalType":"string","name":"_url_AOA","type":"string"},{"internalType":"bool","name":"_active","type":"bool"}],"name":"addEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_name","type":"string"}],"name":"setNameEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_cif","type":"string"}],"name":"setCifEntity","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_url_logo","type":"string"}],"name":"setUrlLogo","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_url_createAID","type":"string"}],"name":"setUrlCreateAID","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"},{"internalType":"string","name":"_url_AOA","type":"string"}],"name":"setUrlAOA","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"internalType":"address","name":"_addressEntity","type":"address"}],"name":"getEntity","outputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_cif","type":"string"},{"internalType":"string","name":"_url_logo","type":"string"},{"internalType":"string","name":"_url_createAID","type":"string"},{"internalType":"string","name":"_url_AOA","type":"string"},{"internalType":"bool","name":"_active","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"entitiesList","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"payable":False,"stateMutability":"view","type":"function"}] - ) - @staticmethod - def AlastriaIdentityManager(endpoint: Web3) -> Contract: - # TODO get the ABI from autogenerated config - return endpoint.eth.contract( - 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"}] - ) +class ContractsService: + def __init__(self, config): + self.config = config - @staticmethod - def AlastriaPublicKeyRegistry(endpoint: Web3) -> Contract: - # TODO get the ABI from autogenerated config + def get_contract_handler( + self, contract_name: str, endpoint: Web3 + ) -> Contract: return endpoint.eth.contract( - abi=[{"constant":True,"inputs":[{"name":"subject","type":"address"},{"name":"publicKey","type":"bytes32"}],"name":"getPublicKeyStatus","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"},{"name":"startDate","type":"uint256"},{"name":"endDate","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"subject","type":"address"}],"name":"getCurrentPublicKey","outputs":[{"name":"","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"publicKey","type":"string"}],"name":"deletePublicKey","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"publicKey","type":"string"}],"name":"addKey","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"version","outputs":[{"name":"","type":"int256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"previousPublishedVersion","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"publicKeyList","outputs":[{"name":"","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"publicKey","type":"string"}],"name":"revokePublicKey","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_previousPublishedVersion","type":"address"}],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"name":"publicKey","type":"string"}],"name":"PublicKeyDeleted","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"publicKey","type":"string"}],"name":"PublicKeyRevoked","type":"event"}] - ) + abi=self.get_abi_by_contract_name(contract_name)) - @staticmethod - def AlastriaCredentialRegistry(endpoint: Web3) -> Contract: - # TODO get the ABI from autogenerated config - return endpoint.eth.contract( - abi=[{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"issuerCredentialList","outputs":[{"name":"","type":"bytes32"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"subject","type":"address"}],"name":"getSubjectCredentialList","outputs":[{"name":"","type":"uint256"},{"name":"","type":"bytes32[]"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"version","outputs":[{"name":"","type":"int256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"subject","type":"address"},{"name":"subjectCredentialHash","type":"bytes32"}],"name":"getSubjectCredentialStatus","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"subjectStatus","type":"uint8"},{"name":"issuerStatus","type":"uint8"}],"name":"getCredentialStatus","outputs":[{"name":"","type":"uint8"}],"payable":False,"stateMutability":"pure","type":"function"},{"constant":True,"inputs":[],"name":"previousPublishedVersion","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"bytes32"}],"name":"subjectCredentialRegistry","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"},{"name":"URI","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"issuer","type":"address"},{"name":"issuerCredentialHash","type":"bytes32"}],"name":"getIssuerCredentialStatus","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"subjectCredentialList","outputs":[{"name":"","type":"bytes32"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"issuerCredentialHash","type":"bytes32"}],"name":"addIssuerCredential","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"issuerCredentialHash","type":"bytes32"},{"name":"status","type":"uint8"}],"name":"updateCredentialStatus","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"subjectCredentialHash","type":"bytes32"}],"name":"deleteSubjectCredential","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"subjectCredentialHash","type":"bytes32"},{"name":"URI","type":"string"}],"name":"addSubjectCredential","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_previousPublishedVersion","type":"address"}],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"name":"subjectCredentialHash","type":"bytes32"}],"name":"SubjectCredentialDeleted","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"issuerCredentialHash","type":"bytes32"},{"indexed":False,"name":"status","type":"uint8"}],"name":"IssuerCredentialRevoked","type":"event"}] - ) + def get_abi_by_contract_name(self, contract_name: str) -> list: + try: + contract_config = self.config[contract_name]['functions'] + except KeyError: + raise ContractNameError(f'The contract {contract_name} does not exist') - @staticmethod - def AlastriaPresentationRegistry(endpoint: Web3) -> Contract: - # TODO get the ABI from autogenerated config - return endpoint.eth.contract( - abi=[{"constant":True,"inputs":[{"name":"subjectStatus","type":"uint8"},{"name":"receiverStatus","type":"uint8"}],"name":"getPresentationStatus","outputs":[{"name":"","type":"uint8"}],"payable":False,"stateMutability":"pure","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"bytes32"}],"name":"subjectPresentationRegistry","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"},{"name":"URI","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"receiverPresentationHash","type":"bytes32"},{"name":"status","type":"uint8"}],"name":"updateReceiverPresentation","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"subjectPresentationHash","type":"bytes32"},{"name":"URI","type":"string"}],"name":"addSubjectPresentation","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"subject","type":"address"},{"name":"subjectPresentationHash","type":"bytes32"}],"name":"getSubjectPresentationStatus","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"version","outputs":[{"name":"","type":"int256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"previousPublishedVersion","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"receiver","type":"address"},{"name":"receiverPresentationHash","type":"bytes32"}],"name":"getReceiverPresentationStatus","outputs":[{"name":"exists","type":"bool"},{"name":"status","type":"uint8"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"subject","type":"address"}],"name":"getSubjectPresentationList","outputs":[{"name":"","type":"uint256"},{"name":"","type":"bytes32[]"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"subjectPresentationListRegistry","outputs":[{"name":"","type":"bytes32"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"subjectPresentationHash","type":"bytes32"},{"name":"status","type":"uint8"}],"name":"updateSubjectPresentation","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_previousPublishedVersion","type":"address"}],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"name":"hash","type":"bytes32"},{"indexed":False,"name":"status","type":"uint8"}],"name":"PresentationUpdated","type":"event"}] - ) + return [action for action in contract_config.values()] diff --git a/alastria_identity/services/credential_registry.py b/alastria_identity/services/credential_registry.py deleted file mode 100644 index 2fb26e7..0000000 --- a/alastria_identity/services/credential_registry.py +++ /dev/null @@ -1,83 +0,0 @@ -from web3 import Web3 - -from alastria_identity.types import (Transaction, NetworkDid) -from alastria_identity.services import ( - ContractsService, - CREDENTIAL_REGISTRY_ADDRESS, - IDENTITY_MANAGER_ADDRESS) - - -class CredentialRegistryService: - def __init__(self, endpoint: Web3): - self.endpoint = endpoint - - def add_subject_credential(self, subject_credential_hash: str, uri: str) -> Transaction: - return self._build_transaction( - "addSubjectCredential", - [subject_credential_hash, uri], - delegated=True) - - def delete_subject_credential(self, subject_credential_hash: str) -> Transaction: - return self._build_transaction( - "deleteSubjectCredential", - [subject_credential_hash], - delegated=True) - - def get_subject_credential_status(self, subject_did: str, subject_credential_hash: str) -> Transaction: - subject_address = NetworkDid.from_did(subject_did).proxy_address - return self._build_transaction( - "getSubjectCredentialStatus", - [subject_address, subject_credential_hash], - delegated=False) - - def get_subject_credential_list(self, subject_did: str) -> Transaction: - subject_address = NetworkDid.from_did(subject_did).proxy_address - return self._build_transaction( - "getSubjectCredentialList", - [subject_address], - delegated=False) - - def add_issuer_credential(self, issuer_credential_hash: str) -> Transaction: - return self._build_transaction( - "addIssuerCredential", - [issuer_credential_hash], - delegated=True) - - def get_issuer_credential_status(self, issuer_did: str, issuer_credential_hash: str) -> Transaction: - issuer_address = NetworkDid.from_did(issuer_did).proxy_address - return self._build_transaction( - "getIssuerCredentialStatus", - [issuer_address, issuer_credential_hash], - delegated=False) - - def update_credential_status(self, issuer_credential_hash: str, status: int) -> Transaction: - return self._build_transaction( - "updateCredentialStatus", - [issuer_credential_hash, status], - delegated=True) - - def get_credential_status(self, subject_status: int, issuer_status: int) -> Transaction: - return self._build_transaction( - "getCredentialStatus", - [subject_status, issuer_status], - delegated=False) - - def _build_transaction(self, function_name: str, args: list, delegated: bool) -> Transaction: - encoded_abi = ContractsService.AlastriaCredentialRegistry(self.endpoint).encodeABI( - fn_name=function_name, - args=args - ) - - data = self.delegated(encoded_abi) if delegated else encoded_abi - contract_address = IDENTITY_MANAGER_ADDRESS if delegated else CREDENTIAL_REGISTRY_ADDRESS - - return Transaction( - to=Web3.toChecksumAddress(contract_address), - data=data) - - def delegated(self, delegated_data) -> str: - return ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='delegateCall', - args=[Web3.toChecksumAddress( - CREDENTIAL_REGISTRY_ADDRESS), 0, delegated_data] - ) diff --git a/alastria_identity/services/identity_manager.py b/alastria_identity/services/identity_manager.py deleted file mode 100644 index 94261ee..0000000 --- a/alastria_identity/services/identity_manager.py +++ /dev/null @@ -1,218 +0,0 @@ -from web3 import Web3 - -from alastria_identity.types import ( - Transaction, - NetworkDid, - Entity) -from alastria_identity.services import IdentityConfigBuilder, ContractsService, IDENTITY_MANAGER_ADDRESS - - -class IdentityManagerService: - def __init__(self, endpoint: Web3): - self.endpoint = endpoint - - 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 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 add_idendity_issuer(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='addIdentityIssuer', - args=[issuer_address, level])) - 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 delete_identity_issuer(self, did_issuer: str) -> Transaction: - issuer_address = NetworkDid.from_did(did_issuer).proxy_address - data = self.delegated(ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='deleteIdentityIssuer', - args=[issuer_address])) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=data - ) - - def get_eidas_level(self, did_issuer: str) -> Transaction: - issuer_address = NetworkDid.from_did(did_issuer).proxy_address - data = ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='getEidasLevel', - args=[issuer_address]) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=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 delete_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='deleteIdentityServiceProvider', - args=[provider_address])) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=data - ) - - def is_identity_service_provider(self, did_service_provider: str) -> Transaction: - provider_address = NetworkDid.from_did( - did_service_provider).proxy_address - data = ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='isIdentityServiceProvider', - args=[provider_address]) - 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 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 - ) - - def set_entity_name(self, entity: Entity) -> Transaction: - entity_address = NetworkDid.from_did(entity.did_entity).proxy_address - data = self.delegated(ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='setNameEntity', - args=[entity.name])) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=data - ) - - def set_entity_cif(self, entity: Entity) -> Transaction: - entity_address = NetworkDid.from_did(entity.did_entity).proxy_address - data = self.delegated(ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='setCifEntity', - args=[entity.cif])) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=data - ) - - def set_entity_url_logo(self, entity: Entity) -> Transaction: - entity_address = NetworkDid.from_did(entity.did_entity).proxy_address - data = self.delegated(ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='setUrlLogo', - args=[entity.url_logo])) - return Transaction( - to=Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - data=data - ) - - def set_entity_url_create_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='setUrlCreateAID', - args=[entity.url_create_aid])) - 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 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 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 - ) - - def delegated(self, delegated_data) -> str: - return ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='delegateCall', - args=[Web3.toChecksumAddress( - IDENTITY_MANAGER_ADDRESS), 0, delegated_data] - ) diff --git a/alastria_identity/services/parsers.py b/alastria_identity/services/parsers.py index d4a270c..cac8462 100644 --- a/alastria_identity/services/parsers.py +++ b/alastria_identity/services/parsers.py @@ -2,8 +2,13 @@ import requests +from alastria_identity.types import ConfigParser + + +class ContractParser(ConfigParser): + DEFAULT_GITHUB_URL = 'https://github.com/' + DEFAULT_GITHUB_RAW_URL = 'https://raw.githubusercontent.com/' -class ContractParser: def __init__(self, contract_url): self.contract_url = contract_url @@ -31,8 +36,8 @@ def get_name_in_content(self, content): def get_json_data_from_url(self): contract_url = self.contract_url.replace( - 'https://github.com/', - 'https://raw.githubusercontent.com/' + self.DEFAULT_GITHUB_URL, + self.DEFAULT_GITHUB_RAW_URL, ).replace( 'blob/', '' diff --git a/alastria_identity/services/presentation_registry.py b/alastria_identity/services/presentation_registry.py deleted file mode 100644 index 9d86011..0000000 --- a/alastria_identity/services/presentation_registry.py +++ /dev/null @@ -1,83 +0,0 @@ -from web3 import Web3 - -from alastria_identity.types import (Transaction, NetworkDid) -from alastria_identity.services import ( - ContractsService, - PRESENTATION_REGISTRY_ADDRESS, - IDENTITY_MANAGER_ADDRESS) - - -class PresentationRegistryService: - def __init__(self, endpoint: Web3): - self.endpoint = endpoint - - def add_subject_presentation(self, subject_presentation_hash: str, uri: str) -> Transaction: - return self._build_transaction( - "addSubjectPresentation", - [subject_presentation_hash, uri], - delegated=True) - - def update_subject_presentation(self, subject_presentation_hash: str, status: int) -> Transaction: - return self._build_transaction( - "updateSubjectPresentation", - [subject_presentation_hash, status], - delegated=True - ) - - def get_subject_presentation_status(self, subject_did: str, subject_presentation_hash: str) -> Transaction: - subject_address = NetworkDid.from_did(subject_did).proxy_address - return self._build_transaction( - "getSubjectPresentationStatus", - [subject_address, subject_presentation_hash], - delegated=False - ) - - def get_subject_presentation_list(self, subject_did: str) -> Transaction: - subject_address = NetworkDid.from_did(subject_did).proxy_address - return self._build_transaction( - "getSubjectPresentationList", - [subject_address], - delegated=False - ) - - def update_receiver_presentation(self, receiver_presentation_hash: str, status: int) -> Transaction: - return self._build_transaction( - "updateReceiverPresentation", - [receiver_presentation_hash, status], - delegated=True - ) - - def get_receiver_presentation_status(self, receiver_did: str, receiver_presentation_hash: str) -> Transaction: - receiver_address = NetworkDid.from_did(receiver_did).proxy_address - return self._build_transaction( - "getReceiverPresentationStatus", - [receiver_address, receiver_presentation_hash], - delegated=False - ) - - def get_presentation_status(self, subject_status: int, receiver_status: int) -> Transaction: - return self._build_transaction( - "getPresentationStatus", - [subject_status, receiver_status], - delegated=False - ) - - def _build_transaction(self, function_name: str, args: list, delegated: bool) -> Transaction: - encoded_abi = ContractsService.AlastriaPresentationRegistry(self.endpoint).encodeABI( - fn_name=function_name, - args=args - ) - - data = self.delegated(encoded_abi) if delegated else encoded_abi - contract_address = IDENTITY_MANAGER_ADDRESS if delegated else PRESENTATION_REGISTRY_ADDRESS - - return Transaction( - to=Web3.toChecksumAddress(contract_address), - data=data) - - def delegated(self, delegated_data) -> str: - return ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='delegateCall', - args=[Web3.toChecksumAddress( - PRESENTATION_REGISTRY_ADDRESS), 0, delegated_data] - ) diff --git a/alastria_identity/services/public_key.py b/alastria_identity/services/public_key.py deleted file mode 100644 index 9076327..0000000 --- a/alastria_identity/services/public_key.py +++ /dev/null @@ -1,63 +0,0 @@ -from web3 import Web3 - -from alastria_identity.types import (Transaction, NetworkDid) -from alastria_identity.services import ( - ContractsService, - PUBLIC_KEY_REGISTRY_ADDRESS -) - - -class PublicKeyService: - def __init__(self, endpoint: Web3): - self.endpoint = endpoint - - def add_key(self, public_key: str) -> Transaction: - return self._build_transaction( - "addKey", - [public_key], - delegated=True) - - def revoke_public_key(self, public_key: str) -> Transaction: - return self._build_transaction( - "revokePublicKey", - [public_key], - delegated=True) - - def delete_public_key(self, public_key: str) -> Transaction: - return self._build_transaction( - "deletePublicKey", - [public_key], - delegated=True) - - def get_current_public_key(self, did: str) -> Transaction: - subject_address = NetworkDid.from_did(did).proxy_address - return self._build_transaction( - "getCurrentPublicKey", - [subject_address], - delegated=False) - - def get_public_key_status(self, did: str, public_key: str) -> Transaction: - subject_address = NetworkDid.from_did(did).proxy_address - return self._build_transaction( - "getPublicKeyStatus", - [subject_address, public_key], - delegated=False) - - 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 delegated(self, delegated_data) -> str: - return ContractsService.AlastriaIdentityManager(self.endpoint).encodeABI( - fn_name='delegateCall', - args=[Web3.toChecksumAddress( - PUBLIC_KEY_REGISTRY_ADDRESS), 0, delegated_data] - ) diff --git a/alastria_identity/services/tokens.py b/alastria_identity/services/tokens.py index b5a80d5..959e439 100644 --- a/alastria_identity/services/tokens.py +++ b/alastria_identity/services/tokens.py @@ -31,16 +31,16 @@ def remove_starting_hex_prefix(self, hex_data: str): return hex_data @staticmethod - def create_did(network_did: NetworkDid): + def create_did(network_did: NetworkDid) -> str: return f'did:ala:{network_did.network}:{network_did.network_id}:{network_did.proxy_address}' - def sign_jwt(self, jwt_data: JwtToken): + def sign_jwt(self, jwt_data: JwtToken) -> str: token = jwt.JWT(header=jwt_data.header, claims=jwt_data.payload, algs=[self.algorithm]) token.make_signed_token(self.signing_key) return token.serialize() - def verify_jwt(self, jwt_data: str, raw_public_key: str): + def verify_jwt(self, jwt_data: str, raw_public_key: str) -> bool: try: pem = VerifyingKey.from_string(decode_hex( raw_public_key), curve=SECP256k1).to_pem() @@ -54,7 +54,7 @@ def verify_jwt(self, jwt_data: str, raw_public_key: str): return False @staticmethod - def decode_jwt(jwt_data: str): + def decode_jwt(jwt_data: str) -> dict: jws_token = jws.JWS(jwt_data) jws_token.deserialize(jwt_data) return { diff --git a/alastria_identity/services/transaction_service.py b/alastria_identity/services/transaction_service.py new file mode 100644 index 0000000..11c94f8 --- /dev/null +++ b/alastria_identity/services/transaction_service.py @@ -0,0 +1,58 @@ +from web3 import Web3 + +from alastria_identity.types import Transaction, NetworkDid +from alastria_identity.services import ContractsService + + +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) diff --git a/alastria_identity/tests/test_config_builder.py b/alastria_identity/tests/test_config_builder.py new file mode 100644 index 0000000..b4905db --- /dev/null +++ b/alastria_identity/tests/test_config_builder.py @@ -0,0 +1,57 @@ +from mock import Mock, patch +from unittest.mock import patch + +from web3 import Web3 + +from alastria_identity.services import IdentityConfigBuilder, ContractParser + + +def test_extract_contract_item_from_response(): + contracts_content = '''| Contract Name | Address | ABI | +| :------------ | :-------| :--- | +| Eidas | 0x57a9604784f82e5637624ca9c87015aaa31e300d | https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_libs_Eidas_sol_Eidas.abi | +| AlastriaIdentityManager | 0xbd4a2c84edb97be5beff7cd341bd63567e73f8c9 | https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_identityManager_AlastriaIdentityManager_sol_AlastriaIdentityManager.abi | +''' + expected_output = [ + {'url': 'https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_libs_Eidas_sol_Eidas.abi', + 'address': '0x57a9604784f82e5637624ca9c87015aaa31e300d'}, + {'url': 'https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_identityManager_AlastriaIdentityManager_sol_AlastriaIdentityManager.abi', + 'address': '0xbd4a2c84edb97be5beff7cd341bd63567e73f8c9'} + ] + + identity = IdentityConfigBuilder( + contracts_info_url= 'https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md', + parser_class=ContractParser) + + output = identity.extract_contract_item_from_response(contracts_content) + + assert list(output) == expected_output + + +@patch.object(IdentityConfigBuilder, 'get_contracts') +def test_generate_return_valid_contracts(get_contracts): + # We should mock the request response with responses in the future + expected_output = { + 'Eidas': { + 'functions': [], + 'address': '0x57a9604784f82e5637624ca9c87015aaa31e300d' + }, + 'AlastriaIdentityManager': { + 'functions': [], + 'address': '0xbd4a2c84edb97be5beff7cd341bd63567e73f8c9' + }, + } + get_contracts.return_value = [ + {'url': 'https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_libs_Eidas_sol_Eidas.abi', + 'address': '0x57a9604784f82e5637624ca9c87015aaa31e300d'}, + {'url': 'https://github.com/alastria/alastria-identity/blob/develop/contracts/abi/__contracts_identityManager_AlastriaIdentityManager_sol_AlastriaIdentityManager.abi', + 'address': '0xbd4a2c84edb97be5beff7cd341bd63567e73f8c9'} + ] + + identity = IdentityConfigBuilder( + contracts_info_url= 'https://raw.githubusercontent.com/alastria/alastria-identity/master/contracts/ContractInfo.md', + parser_class=ContractParser) + + config_output = identity.generate() + + assert config_output.keys() == expected_output.keys() diff --git a/alastria_identity/tests/test_contracts.py b/alastria_identity/tests/test_contracts.py new file mode 100644 index 0000000..0c431a8 --- /dev/null +++ b/alastria_identity/tests/test_contracts.py @@ -0,0 +1,21 @@ +from mock import Mock, patch +from unittest.mock import patch + +from web3 import Web3 + +from alastria_identity.services import ( + ContractsService, IdentityConfigBuilder, ContractParser) + + +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 diff --git a/alastria_identity/tests/test_credential_registry.py b/alastria_identity/tests/test_credential_registry.py deleted file mode 100644 index ce737a8..0000000 --- a/alastria_identity/tests/test_credential_registry.py +++ /dev/null @@ -1,248 +0,0 @@ -from mock import Mock, patch -from dataclasses import asdict -from unittest.mock import patch - -from web3 import Web3 - -from alastria_identity.services import ( - CredentialRegistryService, - CREDENTIAL_REGISTRY_ADDRESS, - IDENTITY_MANAGER_ADDRESS) -from alastria_identity.types import Entity - - -@patch('alastria_identity.services.CredentialRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_add_subject_credential( - mock_alastria_credential_registry, - mock_delegated): - web3_mock = Mock() - subject_credential_hash = '0x1234' - uri = '0x9876' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'addSubjectCredentialReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.add_subject_credential(subject_credential_hash, uri) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='addSubjectCredential', args=[subject_credential_hash, uri]) - mock_delegated.assert_called_with('addSubjectCredentialReturnValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.CredentialRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_delete_subject_credential( - mock_alastria_credential_registry, - mock_delegated): - web3_mock = Mock() - subject_credential_hash = '0x1234' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'deleteSubjectCredentialReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.delete_subject_credential(subject_credential_hash) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='deleteSubjectCredential', args=[subject_credential_hash]) - mock_delegated.assert_called_with('deleteSubjectCredentialReturnValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_get_subject_credential_status( - mock_alastria_credential_registry): - web3_mock = Mock() - subject_credential_hash = '0x1234' - subject_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - subject_did = f'did:ala:quor:redT:{subject_address}' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'getSubjectCredentialStatusReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(CREDENTIAL_REGISTRY_ADDRESS), - 'data': 'getSubjectCredentialStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.get_subject_credential_status( - subject_did, subject_credential_hash) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='getSubjectCredentialStatus', args=[subject_address, subject_credential_hash]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_get_subject_credential_list( - mock_alastria_credential_registry): - web3_mock = Mock() - subject_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - subject_did = f'did:ala:quor:redT:{subject_address}' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'getSubjectCredentialListReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(CREDENTIAL_REGISTRY_ADDRESS), - 'data': 'getSubjectCredentialListReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.get_subject_credential_list(subject_did) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='getSubjectCredentialList', args=[subject_address]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.CredentialRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_add_issuer_credential( - mock_alastria_credential_registry, - mock_delegated): - web3_mock = Mock() - issuer_credential_hash = '0x1234' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'addIssuerCredentialReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.add_issuer_credential(issuer_credential_hash) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='addIssuerCredential', args=[issuer_credential_hash]) - mock_delegated.assert_called_with('addIssuerCredentialReturnValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_get_issuer_credential_status( - mock_alastria_credential_registry): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - issuer_did = f'did:ala:quor:redT:{issuer_address}' - issuer_credential_hash = '0x1234' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'getIssuerCredentialStatusReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(CREDENTIAL_REGISTRY_ADDRESS), - 'data': 'getIssuerCredentialStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.get_issuer_credential_status( - issuer_did, issuer_credential_hash) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='getIssuerCredentialStatus', args=[issuer_address, issuer_credential_hash]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.CredentialRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_update_credential_status( - mock_alastria_credential_registry, - mock_delegated): - web3_mock = Mock() - status = 2 - issuer_credential_hash = '0x1234' - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'updateCredentialStatusReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.update_credential_status( - issuer_credential_hash, status) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='updateCredentialStatus', args=[issuer_credential_hash, status]) - mock_delegated.assert_called_with('updateCredentialStatusReturnValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaCredentialRegistry') -def test_get_credential_status( - mock_alastria_credential_registry): - web3_mock = Mock() - subject_status = 2 - issuer_status = 2 - mock_alastria_credential_registry( - web3_mock).encodeABI.return_value = 'getCredentialStatusReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(CREDENTIAL_REGISTRY_ADDRESS), - 'data': 'getCredentialStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = CredentialRegistryService(web3_mock) - - transaction = service.get_credential_status(subject_status, issuer_status) - - mock_alastria_credential_registry.assert_called_with(web3_mock) - mock_alastria_credential_registry(web3_mock).encodeABI.assert_called_with( - fn_name='getCredentialStatus', args=[subject_status, issuer_status]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_delegated_call( - mock_alastria_identity_manager): - web3_mock = Mock() - data = 'exampleData' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'delegatedValue' - service = CredentialRegistryService(web3_mock) - - transaction = service.delegated(data) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='delegateCall', - args=[Web3.toChecksumAddress(CREDENTIAL_REGISTRY_ADDRESS), 0, data]) - assert transaction == 'delegatedValue' diff --git a/alastria_identity/tests/test_identity_manager.py b/alastria_identity/tests/test_identity_manager.py deleted file mode 100644 index ced25b9..0000000 --- a/alastria_identity/tests/test_identity_manager.py +++ /dev/null @@ -1,600 +0,0 @@ -from mock import * -from dataclasses import asdict -from unittest.mock import patch -from web3 import Web3 - -from alastria_identity.services import IdentityManagerService, IDENTITY_MANAGER_ADDRESS -from alastria_identity.types import Entity - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_prepare_alastria_id( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - sign_addres = '0x1234' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'prepareAlastriaIDReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.prepare_alastria_id(sign_addres) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='prepareAlastriaID', args=[sign_addres]) - mock_delegated.assert_called_with('prepareAlastriaIDReturnValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_create_alastria_identity( - mock_public_key_registry, - mock_identity_manager): - web3_mock = Mock() - public_key = '0x1234' - mock_public_key_registry(web3_mock).encodeABI.return_value = 'addKeyValue' - mock_identity_manager( - web3_mock).encodeABI.return_value = 'createAlastriaIdentityValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'createAlastriaIdentityValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.create_alastria_identity(public_key) - - mock_public_key_registry.assert_called_with(web3_mock) - mock_public_key_registry(web3_mock).encodeABI.assert_called_with( - fn_name='addKey', args=[public_key]) - mock_identity_manager.assert_called_with(web3_mock) - mock_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='createAlastriaIdentity', args=['addKeyValue']) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_add_identity_issuer( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_issuer = f'did:ala:quor:redT:{issuer_address}' - level = '1' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'addIdentityIssuerValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.add_idendity_issuer(did_issuer, level) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='addIdentityIssuer', args=[issuer_address, level]) - mock_delegated.assert_called_with('addIdentityIssuerValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_update_identity_issuer_eidas_level( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_issuer = f'did:ala:quor:redT:{issuer_address}' - level = '3' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'updateIdentityIssuerEidasLevelValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.update_identity_issuer_eidas_level(did_issuer, level) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='updateIdentityIssuerEidasLevel', args=[issuer_address, level]) - mock_delegated.assert_called_with('updateIdentityIssuerEidasLevelValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_delete_identity_issuer( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_issuer = f'did:ala:quor:redT:{issuer_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'deleteIdentityIssuerEidasLevelValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.delete_identity_issuer(did_issuer) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='deleteIdentityIssuer', args=[issuer_address]) - mock_delegated.assert_called_with('deleteIdentityIssuerEidasLevelValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_get_eidas_level( - mock_alastria_identity_manager): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_issuer = f'did:ala:quor:redT:{issuer_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'getEidasLevelValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'getEidasLevelValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.get_eidas_level(did_issuer) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='getEidasLevel', args=[issuer_address]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_add_identity_service_provider( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - provider_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_service_provider = f'did:ala:quor:redT:{provider_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'addIdentityServiceProviderValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.add_identity_service_provider(did_service_provider) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='addIdentityServiceProvider', args=[provider_address]) - mock_delegated.assert_called_with('addIdentityServiceProviderValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_delete_identity_service_provider( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - provider_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_service_provider = f'did:ala:quor:redT:{provider_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'deleteIdentityServiceProviderValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.delete_identity_service_provider( - did_service_provider) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='deleteIdentityServiceProvider', args=[provider_address]) - mock_delegated.assert_called_with('deleteIdentityServiceProviderValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_is_identity_service_provider( - mock_alastria_identity_manager): - web3_mock = Mock() - provider_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_service_provider = f'did:ala:quor:redT:{provider_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'isIdentityServiceProviderValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'isIdentityServiceProviderValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.is_identity_service_provider(did_service_provider) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='isIdentityServiceProvider', args=[provider_address]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_is_identity_issuer( - mock_alastria_identity_manager): - web3_mock = Mock() - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_issuer = f'did:ala:quor:redT:{issuer_address}' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'isIdentityIssuerValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'isIdentityIssuerValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.is_identity_issuer(did_issuer) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='isIdentityIssuer', args=[issuer_address]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_add_entity( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'addEntityValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.add_entity(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='addEntity', - args=[ - entity_address, - entity.name, - entity.cif, - entity.url_logo, - entity.url_create_aid, - entity.url_aoa, - entity.status - ]) - mock_delegated.assert_called_with('addEntityValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_set_entity_name( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'setEntityNameValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.set_entity_name(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='setNameEntity', - args=[entity.name]) - mock_delegated.assert_called_with('setEntityNameValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_set_entity_cif( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'setEntityCifValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.set_entity_cif(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='setCifEntity', - args=[entity.cif]) - mock_delegated.assert_called_with('setEntityCifValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_set_entity_url_logo( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'setEntityUrlLogoValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.set_entity_url_logo(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='setUrlLogo', - args=[entity.url_logo]) - mock_delegated.assert_called_with('setEntityUrlLogoValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_set_entity_url_create_aid( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'setEntityUrlCreateAIDValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.set_entity_url_create_aid(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='setUrlCreateAID', - args=[entity.url_create_aid]) - mock_delegated.assert_called_with('setEntityUrlCreateAIDValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.IdentityManagerService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_set_entity_url_aoa( - mock_alastria_identity_manager, - mock_delegated): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'setEntityUrlAoaAIDValue' - mock_delegated.return_value = 'delegatedReturnValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.set_entity_url_aoa_aid(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='setUrlAOA', - args=[entity.url_aoa]) - mock_delegated.assert_called_with('setEntityUrlAoaAIDValue') - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_get_entity( - mock_alastria_identity_manager): - web3_mock = Mock() - entity_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - did_entity = f'did:ala:quor:redT:{entity_address}' - entity = Entity( - did_entity, - 'EntityName', - 'EntityCIF', - 'EntityUrlLogo', - 'EntityCreateAID', - 'EntityAOA', - 0) - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'getEntityValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'getEntityValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.get_entity(entity) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='getEntity', - args=[entity_address]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_get_entities_list( - mock_alastria_identity_manager): - web3_mock = Mock() - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'getEntitiesListValue' - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'getEntitiesListValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = IdentityManagerService(web3_mock) - - transaction = service.get_entities_list() - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='entitiesList', - args=[]) - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_delegated_call( - mock_alastria_identity_manager): - web3_mock = Mock() - data = 'exampleData' - mock_alastria_identity_manager( - web3_mock).encodeABI.return_value = 'delegatedValue' - service = IdentityManagerService(web3_mock) - - transaction = service.delegated(data) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='delegateCall', - args=[Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), 0, data]) - assert transaction == 'delegatedValue' diff --git a/alastria_identity/tests/test_identity_service.py b/alastria_identity/tests/test_identity_service.py new file mode 100644 index 0000000..8cf3e38 --- /dev/null +++ b/alastria_identity/tests/test_identity_service.py @@ -0,0 +1,27 @@ +from mock import Mock, patch, MagicMock +from unittest.mock import patch + +from web3 import Web3 + +from alastria_identity.services import UserIdentityService +from alastria_identity.types import UserIdentity, Transaction + + +def test_update_transaction_nonce(): + endpoint = MagicMock() + endpoint.eth.getTransactionCount.return_value = 3 + default_nonce = 1 + user_identity = UserIdentity( + endpoint=endpoint, + address='0x123', + private_key='1234', + nonce=default_nonce, + transactions=[] + ) + transaction = Transaction() + + identity = UserIdentityService(user_identity) + + updated_transaction = identity.update_transaction_nonce(transaction) + + assert updated_transaction.nonce != default_nonce diff --git a/alastria_identity/tests/test_presentation_registry.py b/alastria_identity/tests/test_presentation_registry.py deleted file mode 100644 index 022cbab..0000000 --- a/alastria_identity/tests/test_presentation_registry.py +++ /dev/null @@ -1,303 +0,0 @@ -from mock import Mock, patch -from dataclasses import asdict -from unittest.mock import patch -from web3 import Web3 - -from alastria_identity.services import ( - PresentationRegistryService, - PRESENTATION_REGISTRY_ADDRESS, - IDENTITY_MANAGER_ADDRESS) - - -@patch('alastria_identity.services.PresentationRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_add_subject_presentation( - mock_alastria_presentation_registry, - mock_delegated -): - - web3_mock = Mock() - subject_presentation_hash = '0x1234' - uri = '0x9876' - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'addSubjectPresentationReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.add_subject_presentation(subject_presentation_hash, uri) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='addSubjectPresentation', - args=[subject_presentation_hash, uri] - ) - mock_delegated.assert_called_with('addSubjectPresentationReturnValue') - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.PresentationRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_update_subject_presentation( - mock_alastria_presentation_registry, - mock_delegated -): - - web3_mock = Mock() - subject_presentation_hash = '0x1234' - status = 1 - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'updateSubjectPresentationReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.update_subject_presentation( - subject_presentation_hash, status - ) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='updateSubjectPresentation', - args=[subject_presentation_hash, status] - ) - mock_delegated.assert_called_with('updateSubjectPresentationReturnValue') - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_get_subject_presentation_status( - mock_alastria_presentation_registry -): - - web3_mock = Mock() - subject_presentation_hash = '0x1234' - subject_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - subject_did = f'did:ala:quor:redT:{subject_address}' - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'getSubjectPresentationStatusReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PRESENTATION_REGISTRY_ADDRESS), - 'data': 'getSubjectPresentationStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.get_subject_presentation_status( - subject_did, subject_presentation_hash - ) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getSubjectPresentationStatus', - args=[subject_address, subject_presentation_hash] - ) - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_get_subject_presentation_list( - mock_alastria_presentation_registry -): - - web3_mock = Mock() - subject_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - subject_did = f'did:ala:quor:redT:{subject_address}' - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'getSubjectPresentationListReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PRESENTATION_REGISTRY_ADDRESS), - 'data': 'getSubjectPresentationListReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.get_subject_presentation_list(subject_did) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getSubjectPresentationList', - args=[subject_address] - ) - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.PresentationRegistryService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_update_receiver_presentation( - mock_alastria_presentation_registry, - mock_delegated -): - - web3_mock = Mock() - receiver_presentation_hash = '0x1234' - status = 1 - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'updateReceiverPresentationReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(IDENTITY_MANAGER_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.update_receiver_presentation(receiver_presentation_hash, status) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='updateReceiverPresentation', - args=[receiver_presentation_hash, status] - ) - mock_delegated.assert_called_with('updateReceiverPresentationReturnValue') - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_get_receiver_presentation_status( - mock_alastria_presentation_registry -): - - web3_mock = Mock() - receiver_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - receiver_did = f'did:ala:quor:redT:{receiver_address}' - receiver_presentation_hash = '0x1234' - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'getReceiverPresentationStatusReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PRESENTATION_REGISTRY_ADDRESS), - 'data': 'getReceiverPresentationStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PresentationRegistryService(web3_mock) - - transaction = service.get_receiver_presentation_status( - receiver_did, receiver_presentation_hash - ) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getReceiverPresentationStatus', - args=[receiver_address, receiver_presentation_hash] - ) - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPresentationRegistry') -def test_get_presentation_status( - mock_alastria_presentation_registry -): - - web3_mock = Mock() - subject_status = 2 - receiver_status = 2 - - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.return_value = 'getPresentationStatusReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PRESENTATION_REGISTRY_ADDRESS), - 'data': 'getPresentationStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - service = PresentationRegistryService(web3_mock) - - transaction = service.get_presentation_status(subject_status, receiver_status) - - mock_alastria_presentation_registry.assert_called_with(web3_mock) - mock_alastria_presentation_registry( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getPresentationStatus', - args=[subject_status, receiver_status] - ) - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaIdentityManager') -def test_delegated_call( - mock_alastria_identity_manager -): - - web3_mock = Mock() - data = 'exampleData' - - mock_alastria_identity_manager( - web3_mock - ).encodeABI.return_value = 'delegatedValue' - service = PresentationRegistryService(web3_mock) - - transaction = service.delegated(data) - - mock_alastria_identity_manager.assert_called_with(web3_mock) - mock_alastria_identity_manager(web3_mock).encodeABI.assert_called_with( - fn_name='delegateCall', - args=[Web3.toChecksumAddress(PRESENTATION_REGISTRY_ADDRESS), 0, data] - ) - - assert transaction == 'delegatedValue' diff --git a/alastria_identity/tests/test_public_key.py b/alastria_identity/tests/test_public_key.py deleted file mode 100644 index 542a429..0000000 --- a/alastria_identity/tests/test_public_key.py +++ /dev/null @@ -1,190 +0,0 @@ -from mock import Mock, patch -from dataclasses import asdict -from unittest.mock import patch -from web3 import Web3 - -from alastria_identity.services import PUBLIC_KEY_REGISTRY_ADDRESS, PublicKeyService - - -@patch('alastria_identity.services.PublicKeyService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_add_key( - mock_alastria_add_public_key, - mock_delegated -): - web3_mock = Mock() - public_key = '0x3' - - mock_alastria_add_public_key( - web3_mock - ).encodeABI.return_value = 'addKeyReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PublicKeyService(web3_mock) - - transaction = service.add_key(public_key) - - mock_alastria_add_public_key.assert_called_with(web3_mock) - mock_alastria_add_public_key( - web3_mock - ).encodeABI.assert_called_with( - fn_name='addKey', - args=[public_key] - ) - mock_delegated.assert_called_with('addKeyReturnValue') - - assert asdict(transaction) == expected_transaction - - -@patch('alastria_identity.services.PublicKeyService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_revoke_public_key( - mock_alastria_public_key, - mock_delegated -): - web3_mock = Mock() - public_key = '0x3' - - mock_alastria_public_key( - web3_mock - ).encodeABI.return_value = 'revokePublicKeyReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PublicKeyService(web3_mock) - - transaction = service.revoke_public_key(public_key) - - mock_alastria_public_key.assert_called_with(web3_mock) - mock_alastria_public_key( - web3_mock - ).encodeABI.assert_called_with( - fn_name='revokePublicKey', - args=[public_key] - ) - mock_delegated.assert_called_with('revokePublicKeyReturnValue') - - assert asdict(transaction) == expected_transaction - -@patch('alastria_identity.services.PublicKeyService.delegated') -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_delete_public_key( - mock_alastria_public_key, - mock_delegated -): - web3_mock = Mock() - public_key = '0x3' - - mock_alastria_public_key( - web3_mock - ).encodeABI.return_value = 'deletePublicKeyReturnValue' - mock_delegated.return_value = 'delegatedReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), - 'data': 'delegatedReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PublicKeyService(web3_mock) - - transaction = service.delete_public_key(public_key) - - mock_alastria_public_key.assert_called_with(web3_mock) - mock_alastria_public_key( - web3_mock - ).encodeABI.assert_called_with( - fn_name='deletePublicKey', - args=[public_key] - ) - mock_delegated.assert_called_with('deletePublicKeyReturnValue') - - assert asdict(transaction) == expected_transaction - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_get_current_public_key( - mock_alastria_public_key, -): - web3_mock = Mock() - public_key = '0x3' - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - issuer_did = f'did:ala:quor:redT:{issuer_address}' - - mock_alastria_public_key( - web3_mock - ).encodeABI.return_value = 'getCurrentPublicKeyReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), - 'data': 'getCurrentPublicKeyReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PublicKeyService(web3_mock) - - transaction = service.get_current_public_key(issuer_did) - - mock_alastria_public_key.assert_called_with(web3_mock) - mock_alastria_public_key( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getCurrentPublicKey', - args=[issuer_address] - ) - - assert asdict(transaction) == expected_transaction - -@patch('alastria_identity.services.identity_manager.ContractsService.AlastriaPublicKeyRegistry') -def test_get_public_key_status( - mock_alastria_public_key, -): - web3_mock = Mock() - public_key = '0x3' - issuer_address = 'e53d78c1c6fc694a0f29b3f24bee439338acbe3e' - issuer_did = f'did:ala:quor:redT:{issuer_address}' - - mock_alastria_public_key( - web3_mock - ).encodeABI.return_value = 'getPublicKeyStatusReturnValue' - - expected_transaction = { - 'to': Web3.toChecksumAddress(PUBLIC_KEY_REGISTRY_ADDRESS), - 'data': 'getPublicKeyStatusReturnValue', - 'gasPrice': 0, - 'gas': 600000, - 'nonce': '0x0' - } - - service = PublicKeyService(web3_mock) - - transaction = service.get_public_key_status(issuer_did, public_key) - - mock_alastria_public_key.assert_called_with(web3_mock) - mock_alastria_public_key( - web3_mock - ).encodeABI.assert_called_with( - fn_name='getPublicKeyStatus', - args=[issuer_address, public_key] - ) - - assert asdict(transaction) == expected_transaction - diff --git a/alastria_identity/tests/test_transaction_service.py b/alastria_identity/tests/test_transaction_service.py new file mode 100644 index 0000000..98eff3e --- /dev/null +++ b/alastria_identity/tests/test_transaction_service.py @@ -0,0 +1,37 @@ +from mock import Mock, patch +from unittest.mock import patch + +from web3 import Web3 + +from alastria_identity.services import TransactionService +from alastria_identity.types import Transaction + + +def test_is_delegated_call_return_false(): + contract_config = {'ContractName': {'address': '0x123', 'functions': {}}} + transaction = TransactionService( + config=contract_config, + contract_name='ContractName', + endpoint=Web3(Web3.HTTPProvider('https://127.0.0.1/rpc')) + ) + + is_delegated = transaction.is_delegated_call() + + assert not is_delegated + + +def test_is_delegated_call_return_true(): + contract_config = { + 'AlastriaIdentityManager': { + 'address': '0x123', + 'functions': {'myfunction': {'constant':True,'inputs':[{'name':'','type':'address'}],'name':'identityKeys','outputs':[{'name':'','type':'address'}],'payable':False,'stateMutability':'view','type':'function'}}}} + transaction = TransactionService( + config=contract_config, + contract_name='AlastriaIdentityManager', + endpoint=Web3(Web3.HTTPProvider('https://127.0.0.1/rpc')) + ) + transaction.enable_delegated_call() + + is_delegated = transaction.is_delegated_call() + + assert is_delegated diff --git a/alastria_identity/types/__init__.py b/alastria_identity/types/__init__.py index f20870a..93215e2 100644 --- a/alastria_identity/types/__init__.py +++ b/alastria_identity/types/__init__.py @@ -11,6 +11,7 @@ from .entity import Entity from .credential import Credential from .presentation_request import PresentationRequest, PresentationRequestData +from .config_parser import ConfigParser DEFAULT_GAS_LIMIT = 600000 DEFAULT_NONCE = '0x0' diff --git a/alastria_identity/types/config_parser.py b/alastria_identity/types/config_parser.py new file mode 100644 index 0000000..6ab4271 --- /dev/null +++ b/alastria_identity/types/config_parser.py @@ -0,0 +1,6 @@ +from abc import ABC, abstractmethod + + +class ConfigParser(ABC): + @abstractmethod + def parse(self): pass diff --git a/alastria_identity/types/presentation_request.py b/alastria_identity/types/presentation_request.py index 142491e..9d0294b 100644 --- a/alastria_identity/types/presentation_request.py +++ b/alastria_identity/types/presentation_request.py @@ -21,7 +21,7 @@ class PresentationRequestData: context: List[str] = field(default_factory=list) type: List[str] = field(default_factory=list) level_of_assurance: int = 0 - + def build_data(self) -> dict: full_context = [ 'https://www.w3.org/2018/credentials/v1', diff --git a/config_generation_example.py b/config_generation_example.py new file mode 100644 index 0000000..d2d820c --- /dev/null +++ b/config_generation_example.py @@ -0,0 +1,19 @@ +from alastria_identity.services import IdentityConfigBuilder, ContractParser + + +def main(): + # We generate the config based on the markdown url + 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() + + # This is the format of the config + # We'll use config['functions'] for the TransactionService + print(config) + + +if __name__ == '__main__': + main()