0

For a day there was a task to sign and verify message in Litecoin wallet. A couple of days of searches did not work, so I had to rewrite a few of my functions using the library bitcoin for signing and verifying messages. Special thanks to the guys from the electrum for their achievements that helped to find solutions. Ok, fewer words more code ...

import bitcoin


class SignVerify:

    def sign(self, address, message, private_key):
        if self.check_if_not_message(message):
            set_data = self.sign_message_with_private_key(private_key, address, message)
            return set_data
        else:
            raise AttributeError('Incorrect private key.')

    def verify(self, message, signature, public_key):
        if self.check_if_not_message(signature):

            return self.ecdsa_verify(message, signature, public_key)
        else:
            raise AttributeError('Signature is empty.')

    def sign_message_with_private_key(self, private_key, address, message):
        message_in_byte = message.encode()

        return self.ecdsa_sign(message_in_byte, private_key)

    @staticmethod
    def check_if_not_message(message):
        if message is None or not message or message == ' ':
            return

        return True

    @staticmethod
    def litecoin_sig_hash(message):
        padded = b"\x19Litecoin Signed Message:\n" + bitcoin.num_to_var_int(
            len(message)) + bitcoin.from_string_to_bytes(message)
        return bitcoin.bin_dbl_sha256(padded)

    def ecdsa_sign(self, msg, priv):
        v, r, s = bitcoin.ecdsa_raw_sign(self.litecoin_sig_hash(msg), priv)
        sig = bitcoin.encode_sig(v, r, s)
        assert self.ecdsa_verify(msg, sig,
                                    bitcoin.privtopub(priv)), "Bad Sig!\t %s\nv = %d\n,r = %d\ns = %d" % (sig, v, r, s)
        return sig

    def ecdsa_verify(self, msg, sig, pub):
        return bitcoin.ecdsa_raw_verify(self.litecoin_sig_hash(msg), bitcoin.decode_sig(sig), pub)


if __name__ == '__main__':
    message = 'LNXKMg8NTd5XLAF2n3HNMYYbyZB6KR7E4x'
    address = 'LNXKMg8NTd5XLAF2n3HNMYYbyZB6KR7E4x'
    privkey = 'T8vwiYErLrsWVytCzaFtQkaYgMYRN2qi4XaWYveeEVBfiiepnNjE'
    public_key = '034835d15daca879dd91689e4f2c0650d495bdde0fdb4edb8ceb909830cdf7f26f'
    a = SignVerify()
    signature = a.sign(address=address, message=message, private_key=privkey)
    print('sign => ', signature)
    print('verify => ', a.verify(message=message, signature=signature, public_key=public_key))

I hope someone will help )))

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • I do hope you don't ever receive money with the address you posted here... That aside, what's wrong with your code? If there anything not working? From your post I only see an introduction and a large block of code, but not a question. – user12205 Aug 24 '18 at 08:58
  • This is just a hint to those who, like me, were looking for a solution to their problem. A address purse randomly from the site https://iancoleman.io/bip39/ – Sergey Zaharov Aug 24 '18 at 09:36

0 Answers0