-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
blockchain_integration/pi_network/decentralized_identity/identity_management.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
from web3 import Web3 | ||
|
||
class IdentityManagement: | ||
def __init__(self, contract_address, abi, provider_url): | ||
self.web3 = Web3(Web3.HTTPProvider(provider_url)) | ||
self.contract = self.web3.eth.contract(address=contract_address, abi=abi) | ||
|
||
def register_identity(self, account, private_key, name, email): | ||
nonce = self.web3.eth.getTransactionCount(account) | ||
tx = self.contract.functions.registerIdentity(name, email).buildTransaction({ | ||
'chainId': 1, # Mainnet ID, change as needed | ||
'gas': 2000000, | ||
'gasPrice': self.web3.toWei('50', 'gwei'), | ||
'nonce': nonce, | ||
}) | ||
signed_tx = self.web3.eth.account.signTransaction(tx, private_key) | ||
tx_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction) | ||
return self.web3.toHex(tx_hash) | ||
|
||
def update_identity(self, account, private_key, name, email): | ||
nonce = self.web3.eth.getTransactionCount(account) | ||
tx = self.contract.functions.updateIdentity(name, email).buildTransaction({ | ||
'chainId': 1, | ||
'gas': 2000000, | ||
'gasPrice': self.web3.toWei('50', 'gwei'), | ||
'nonce': nonce, | ||
}) | ||
signed_tx = self.web3.eth.account.signTransaction(tx, private_key) | ||
tx_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction) | ||
return self.web3.toHex(tx_hash) | ||
|
||
def get_identity(self, user_address): | ||
return self.contract.functions.getIdentity(user_address).call() | ||
|
||
def identity_exists(self, user_address): | ||
return self.contract.functions.identityExists(user_address).call() | ||
|
||
if __name__ == "__main__": | ||
# Example usage | ||
provider_url = "https://your.ethereum.node" # Replace with your Ethereum node URL | ||
contract_address = "0xYourContractAddress" # Replace with your deployed contract address | ||
abi = json.loads('[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_email","type":"string"}],"name":"registerIdentity","outputs":[],"stateMutability":"nonpayable","type":"function"}, ...]') # Replace with your contract ABI | ||
|
||
identity_manager = IdentityManagement(contract_address, abi, provider_url) | ||
|
||
# Example: Register an identity | ||
account = "0xYourAccountAddress" # Replace with your Ethereum account address | ||
private_key = "YourPrivateKey" # Replace with your private key | ||
name = "John Doe" | ||
email = "[email protected]" | ||
|
||
tx_hash = identity_manager.register_identity(account, private_key, name, email) | ||
print(f "Identity registered! Transaction hash: {tx_hash}") | ||
|
||
# Example: Update an identity | ||
new_name = "Johnathan Doe" | ||
new_email = "[email protected]" | ||
tx_hash = identity_manager.update_identity(account, private_key, new_name, new_email) | ||
print(f"Identity updated! Transaction hash: {tx_hash}") | ||
|
||
# Example: Retrieve an identity | ||
identity = identity_manager.get_identity(account) | ||
print(f"Identity: Name: {identity[0]}, Email: {identity[1]}") | ||
|
||
# Example: Check if identity exists | ||
exists = identity_manager.identity_exists(account) | ||
print(f"Identity exists: {exists}") |