def __message(self, public_key: PublicKey) -> str: body = { "name": f'{self.__class__.__module__}:{self.__class__.__name__}', "from": public_key.to_checksum_address(), "data": self.data } return json.dumps(body)
def __validate(self, public_key: PublicKey): transaction_address = json.loads(self.raw_transaction)['from'] recovered_address = public_key.to_checksum_address() if transaction_address != recovered_address: raise exceptions.WrongSignatureError( f'{recovered_address} did not match expected {transaction_address}' )
def verify_message(self, address: str, pubkey: bytes, message: bytes, signature_bytes: bytes): """ Verifies that the message was signed by the keypair. """ # Check that address and pubkey match eth_pubkey = PublicKey(pubkey) signature = EthSignature(signature_bytes=signature_bytes) if not eth_pubkey.to_checksum_address() == address: raise ValueError("Pubkey address ({}) doesn't match the provided address ({})".format(eth_pubkey.to_checksum_address, address)) hashed_message = keccak(message) if not self.blockchain.interface.call_backend_verify( eth_pubkey, signature, hashed_message): raise PowerUpError("Signature is not valid for this message or pubkey.") else: return True
def create_identity_subnode( did: str, domain_name: str, website: str, commercial_name: str, new_privatekey: PrivatekeyJWK, parent_privatekey: PrivatekeyJWK, ) -> Tuple[str, DIDDocument]: """Create a new identity in the Trust Framework, as a subnode depending on a parent node. The data needed is: - did: the DID of the new entity, which has to be created before. - domain_name: The full domain name for the new entity. The new subdomain has to depend from an existing domain - website: The URL of the website of the new entity - commercial_name: The name of the new entity, as it appears in official records - new_privatekey: The private key of the new entity, in JWK format. - parent_privatekey: The private key of the parent domain, in JWK format The call is intended to be called by the entity owning the parent node. After the new identity is created, the subnode ownership is assigned to the new entity (that is, the new node can be controlled by the new_privatekey) """ # Check that node has at least two components: subnode.parent s = domain_name.partition(".") if len(s[1]) == 0: return "Domain name has only one component", None this_node = s[0] parent_node = s[2] # Obtain subnode's private and public key and Ethereum address subnode_account = Account.from_key(base64url_decode(new_privatekey.d)) subnode_publicKey = base64url_decode(new_privatekey.x) + base64url_decode( new_privatekey.y) pb = PublicKey(subnode_publicKey) subnode_address = pb.to_checksum_address() # The caller account from its private key Manager_account = Account.from_key(base64url_decode(parent_privatekey.d)) # Initialize the DIDDocument didDoc = DIDDocument(DID=did, node_name=parent_node, label=this_node, address=subnode_address, publicKey=subnode_publicKey, manager_account=Manager_account) # Add the entity info service = { "id": did + "#info", "type": "EntityCommercialInfo", "serviceEndpoint": website, "name": commercial_name } didDoc.addService(service) # Add the Secure Messaging Server info service = { "id": did + "#sms", "type": "SecureMessagingService", "serviceEndpoint": "https://safeisland.hesusruiz.org/api" } didDoc.addService(service) # Store the info in the blockchain trust framework success, tx_receipt, tx_hash = didDoc.createIdentity(ens, resolver) if not success: return "Failed to create identity in blockchain", None success, tx_receipt, tx_hash = ens.setApprovalForAll( resolver.address(), True, subnode_account.key) if not success: return "Failed in setApprovalForAll", None return None, didDoc