def sign_transaction(self, transaction, private_key, public_key=None): """Sign a transaction A transaction signed with the `current_owner` corresponding private key. Args: transaction (dict): transaction to sign. private_key (str): base58 encoded private key to create a signature of the transaction. public_key (str): (optional) base58 encoded public key to identify each signature of a multisig transaction. Returns: dict: transaction with the `signature` field included. """ private_key = PrivateKey(private_key) if len(transaction['transaction']['current_owners']) == 1: signatures_updated = private_key.sign(self.serialize(transaction)) else: # multisig, sign for each input and store {pub_key: signature_for_priv_key} if public_key is None: raise ValueError( 'public_key must be provided for signing multisig transactions' ) transaction_without_signatures = transaction.copy() signatures = transaction_without_signatures.pop('signatures') \ if 'signatures' in transaction_without_signatures else [] signatures_updated = signatures.copy() signatures_updated = [ s for s in signatures_updated if not s['public_key'] == public_key ] signatures_updated.append({ 'public_key': public_key, 'signature': private_key.sign( self.serialize(transaction_without_signatures)) }) signed_transaction = transaction.copy() signed_transaction.update({'signatures': signatures_updated}) return signed_transaction
def sign_transaction(self, transaction, private_key): """Sign a transaction A transaction signed with the `current_owner` corresponding private key. Args: transaction (dict): transaction to sign. private_key (str): base58 encoded private key to create a signature of the transaction. Returns: dict: transaction with the `signature` field included. """ private_key = PrivateKey(private_key) signature = private_key.sign(self.serialize(transaction)) signed_transaction = transaction.copy() signed_transaction.update({'signature': signature}) return signed_transaction
def test_sign_verify(self): message = 'Hello World!' public_key = PublicKey(self.PUBLIC_VALUE_COMPRESSED_B58) private_key = PrivateKey(self.PRIVATE_VALUE_B58) assert public_key.verify(message, private_key.sign(message)) is True
def test_sign_verify(self): message = 'Hello World!' public_key = PublicKey(self.PUBLIC_VALUE_COMPRESSED_B58) private_key = PrivateKey(self.PRIVATE_VALUE_B58) assert public_key.verify(message, private_key.sign(message)) == True