def convert_private_key_to_wif_format(private_key, secret_prefix_bytes, compressed=True): bin_private_key = private_key.to_bytes(32, byteorder='big') input_data = secret_prefix_bytes + bin_private_key if compressed: input_data += b'\x01' check_sum = double_sha256(input_data).digest()[:4] return bytes_to_base58(input_data + check_sum)
def validate_address(address, coin_symbol): address_bytes = base58_to_bytes(address) coin = coins[coin_symbol] prefix_bytes, _ = decompose_address(address, coin) if not address_bytes.startswith(prefix_bytes): raise ValueError('Address prefix is not correct for this coin') if len(address_bytes) > len(prefix_bytes) + 24: raise ValueError('Too many characters in address') if len(address_bytes) < len(prefix_bytes) + 24: raise ValueError('Too little characters in address') check_sum = address_bytes[-4:] calculated_check_sum = double_sha256(address_bytes[:-4]).digest()[:4] if check_sum != calculated_check_sum: raise ValueError('Check sum is not correct') return validate_pattern(address, coin_symbol, prefix_bytes == coin['script_prefix_bytes'])
def create(self): if self.id: raise Exception('Transaction {} already created'.format(self.id)) for i in range(len(self.inputs)): self.sign_input(i) raw_transaction_data = ( self.version + self.inputs_counter + self.get_encoded_inputs(position=range(len(self.inputs))) + self.outputs_counter + self.get_encoded_outputs() + self.lock_time ) self.id = reverse_byte_hex(double_sha256(raw_transaction_data).hexdigest()) self.raw = raw_transaction_data.hex() logger.info('Created transaction with id: {}\nRaw data: {}'.format( self.id, self.raw)) return self.raw
def calculate_address(digest, address_prefix_bytes): input_data = address_prefix_bytes + digest check_sum = double_sha256(input_data).digest()[:4] address = bytes_to_base58(input_data + check_sum) return address