def sign(self, private_key): """ Sign check Args: private_key (str) """ if not self.passphrase: raise ValueError('Passphrase should be not empty string') # Prepare structure # It contains nonce, chain_id, due_block, coin, value, lock, v, r, s # lock, v, r, s appended later in code structure = [ int(binascii.hexlify(str(self.nonce).encode()), 16), self.chain_id, self.due_block, MinterConvertor.encode_coin_name(self.coin), MinterConvertor.convert_value(value=self.value, to='pip') ] # Create msg hash msg_hash = self.__hash(structure) # SHA256 from passphrase sha = hashlib.sha256() sha.update(self.passphrase.encode()) passphrase = sha.hexdigest() # Create lock from signature self.lock = self.__lockfromsignature( signature=ECDSA.sign(message=msg_hash, private_key=passphrase) ) # Re-create msg hash with adding lock to structure structure.append(self.lock) msg_hash = self.__hash(structure) # Re-create signature, add it to check attrs and to structure signature = ECDSA.sign(message=msg_hash, private_key=private_key) self.signature = { 'v': signature[0], 'r': format(signature[1], 'x'), 's': format(signature[2], 'x') } structure += signature # Get RLP, which will be the check check = binascii.hexlify(rlp.encode(structure)) return MinterPrefix.CHECK + check.decode()
def proof(cls, address, passphrase): """ Create proof Args: address (str) passphrase (str) Returns: str """ # Get address hash address = MinterPrefix.remove_prefix( string=address, prefix=MinterPrefix.ADDRESS ) address = MinterHelper.hex2bin(address) address_hash = cls.__hash(data=[address]) # Create SHA256 from passphrase sha = hashlib.sha256() sha.update(passphrase.encode()) passphrase = sha.hexdigest() # Get signature signature = ECDSA.sign(message=address_hash, private_key=passphrase) return binascii.hexlify(cls.__lockfromsignature(signature)).decode()
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 sign(self, private_key): """ Sign check Args: private_key (str) """ # Prepare structure # It contains nonce, chain_id, due_block, coin, value, gas_coin, # lock, v, r, s. # lock, v, r, s appended later in code structure = [ int(str(self.nonce).encode().hex(), 16), self.chain_id, self.due_block, MinterHelper.encode_coin_name(self.coin), MinterHelper.to_pip(self.value), MinterHelper.encode_coin_name(self.gas_coin) ] # Create msg hash msg_hash = self.__hash(structure) # SHA256 from passphrase sha = hashlib.sha256() sha.update(self.passphrase.encode()) passphrase = sha.hexdigest() # Create lock from signature self.lock = self.__lockfromsignature( signature=ECDSA.sign(message=msg_hash, private_key=passphrase)) # Re-create msg hash with adding lock to structure structure.append(self.lock) msg_hash = self.__hash(structure) # Re-create signature, add it to check attrs and to structure signature = ECDSA.sign(message=msg_hash, private_key=private_key) self.signature = { 'v': signature[0], 'r': format(signature[1], 'x'), 's': format(signature[2], 'x') } structure += signature # Get RLP, which will be the check check = rlp.encode(structure).hex() return MinterHelper.prefix_add(check, PREFIX_CHECK)
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
def proof(cls, address, passphrase=''): """ Create proof Args: address (str) passphrase (str) Returns: str """ # Get address hash address = MinterHelper.prefix_remove(address) address = bytes.fromhex(address) address_hash = cls.__hash(data=[address]) # Create SHA256 from passphrase sha = hashlib.sha256() sha.update(passphrase.encode()) passphrase = sha.hexdigest() # Get signature signature = ECDSA.sign(message=address_hash, private_key=passphrase) return cls.__lockfromsignature(signature).hex()