def get_sender_address(cls, tx): """ Get sender address from tx. Recover public key from tx and then get address from public key, if tx has single signature type, or get decoded sender address, if tx has multi signature type. Args: tx (dict): transaction dict Returns: Minter address (string) """ # Remember signature data and remove it from tx signature_data = tx.pop('signature_data') # If there is sender address in signature data (multi signature tx), return it if signature_data.get('from_mx'): return signature_data['from_mx'] # Otherwise (single signature tx), recover public key and get address from public key # Unhexlify (convert to bin (ascii)) all non-numeric dict values tx = MinterHelper.hex2bin_recursive(tx) # Encode tx data to RLP tx['data'] = rlp.encode(list(tx['data'].values())) # Message tx_rlp = rlp.encode(list(tx.values())) _keccak = MinterHelper.keccak_hash(tx_rlp) # Recover public key public_key = MinterPrefix.PUBLIC_KEY + ECDSA.recover(_keccak, tuple(signature_data.values())) return MinterWallet.get_address_from_public_key(public_key)
def sign(self, private_key): """ Sign transaction. This method can be called only from instances of inherited classes. Args: private_key (string): private key Return: string """ # Get structure populated with instance data tx = self._structure_from_instance() # Remove sgnature data, it's not needed before getting Keccak tx.pop('signature_data') # Encode tx data to RLP tx['data'] = rlp.encode(list(tx['data'].values())) # Encode all tx to RLP and create Keccak hash tx_rlp = rlp.encode(list(tx.values())) _keccak = MinterHelper.keccak_hash(tx_rlp) # Signature data tx['signature_data'] = rlp.encode(ECDSA.sign(_keccak, private_key)) tx_rlp = rlp.encode(list(tx.values())) self.signed_tx = binascii.hexlify(tx_rlp).decode()
def recover_public_key(cls, tx): """ Recover public key from tx. Args: tx (dict): transaction dict Returns: public_key (string) """ # Remember signature data and remove it from tx signature_data = tx.pop('signature_data') # Unhexlify (convert to bin (ascii)) all non-numeric dict values tx = MinterHelper.hex2bin_recursive(tx) # Encode tx data to RLP tx['data'] = rlp.encode(list(tx['data'].values())) # Message tx_rlp = rlp.encode(list(tx.values())) _keccak = MinterHelper.keccak_hash(tx_rlp) # Recover public key public_key = ECDSA.recover(_keccak, tuple(signature_data.values())) return MinterPrefix.PUBLIC_KEY + public_key
def __hash(data): """ Create service hash by RLP encoding and getting keccak hash from rlp result Args: data (list) Returns: hash (str) """ return MinterHelper.keccak_hash(rlp.encode(data))
def get_address_from_public_key(cls, public_key): """ @param public_key|string @return: string - Wallet address with prefix """ # Create keccak hash _keccak = MinterHelper.keccak_hash( binascii.unhexlify(public_key.replace(MinterPrefix.PUBLIC_KEY, ''))) return MinterPrefix.ADDRESS + _keccak[-40:]
def get_address_from_public_key(cls, public_key): """ Args: public_key (str) Returns: str """ # Create keccak hash _keccak = MinterHelper.keccak_hash( bytes.fromhex(MinterHelper.prefix_remove(public_key))) return MinterHelper.prefix_add(_keccak[-40:], PREFIX_ADDR)
def generate_signature(self, private_key): """ Create signature for transaction Args: private_key (str): private key to sign with Returns: hex_signature (str) """ # Get structure populated with instance data and rlp encoded tx_struct = self.generate_tx_rlp() # Create keccak hash tx_rlp = rlp.encode(list(tx_struct.values())) keccak = MinterHelper.keccak_hash(tx_rlp) # Create signature signature = ECDSA.sign(keccak, private_key) signature = binascii.hexlify(rlp.encode(signature)).decode() return signature