#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest

from btclib.base58 import b58encode, b58encode_check, b58decode, \
    b58decode_check, b58encode_int, b58decode_int


class TestBase58CheckEncoding(unittest.TestCase):
    def test_b58_empty(self):
        self.assertEqual(b58encode(b''), b'')
        self.assertEqual(b58decode(b''), b'')
        self.assertEqual(b58decode(b58encode(b'')), b'')
        self.assertEqual(b58encode(b58decode(b'')), b'')

    def test_b58_hello_world(self):
        self.assertEqual(b58encode(b'hello world'), b'StV1DL6CwTryKyV')
        self.assertEqual(b58decode(b'StV1DL6CwTryKyV'), b'hello world')
        self.assertEqual(b58decode(b58encode(b'hello world')), b'hello world')
        self.assertEqual(
            b58encode(b58decode(b'StV1DL6CwTryKyV')), b'StV1DL6CwTryKyV')

    def test_b58_trailing_zeros(self):
        self.assertEqual(b58encode(b'\x00\x00hello world'),
                         b'11StV1DL6CwTryKyV')
        self.assertEqual(b58decode(b'11StV1DL6CwTryKyV'),
                         b'\x00\x00hello world')
        self.assertEqual(
            b58decode(b58encode(b'\0\0hello world')), b'\x00\x00hello world')
        self.assertEqual(
            b58encode(b58decode(b'11StV1DL6CwTryKyV')), b'11StV1DL6CwTryKyV')

    def test_b58_integers(self):
        digits = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
        for i in range(len(digits)):
            char = digits[i:i+1]
            self.assertEqual(b58decode_int(char), i)
            self.assertEqual(b58encode_int(i), char)
        number = 0x111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5  # noqa
        self.assertEqual(b58decode_int(digits), number)
        self.assertEqual(b58encode_int(number), digits[1:])

    def test_b58_exceptions(self):
        # int is not hex-string or bytes
        self.assertRaises(TypeError, b58encode_check, 3)

        encoded = b58encode_check(b"test")

        # unexpected decoded length
        wrong_length = len(encoded)-1
        self.assertRaises(ValueError, b58decode_check, encoded, wrong_length)

        # checksum is invalid
        invalidChecksum = encoded[:-4] + b'1111'
        self.assertRaises(ValueError, b58decode_check, invalidChecksum, 4)

    def test_wif(self):
        # https://en.bitcoin.it/wiki/Wallet_import_format
        prvkey = 0xC28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

        uncompressedKey = b'\x80' + prvkey.to_bytes(32, byteorder='big')
        uncompressedWIF = b'5HueCGU8rMjxEX<KEY>'
        wif = b58encode_check(uncompressedKey)
        self.assertEqual(wif, uncompressedWIF)
        key = b58decode_check(uncompressedWIF)
        self.assertEqual(key, uncompressedKey)

        compressedKey = b'\x80' + prvkey.to_bytes(32, byteorder='big') +b'\x01'
        compressedWIF = b'<KEY>'
        wif = b58encode_check(compressedKey)
        self.assertEqual(wif, compressedWIF)
        key = b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string
        compressedWIF = '<KEY>'
        key = b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string with leading space
        compressedWIF = ' <KEY>'
        b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string with trailing space
        compressedWIF = 'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617 '
        b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

if __name__ == "__main__":
    # execute only if run as a script
    unittest.main()

#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest

from btclib.base58 import b58encode, b58encode_check, b58decode, \
    b58decode_check, b58encode_int, b58decode_int


class TestBase58CheckEncoding(unittest.TestCase):
    def test_b58_empty(self):
        self.assertEqual(b58encode(b''), b'')
        self.assertEqual(b58decode(b''), b'')
        self.assertEqual(b58decode(b58encode(b'')), b'')
        self.assertEqual(b58encode(b58decode(b'')), b'')

    def test_b58_hello_world(self):
        self.assertEqual(b58encode(b'hello world'), b'StV1DL6CwTryKyV')
        self.assertEqual(b58decode(b'StV1DL6CwTryKyV'), b'hello world')
        self.assertEqual(b58decode(b58encode(b'hello world')), b'hello world')
        self.assertEqual(
            b58encode(b58decode(b'StV1DL6CwTryKyV')), b'StV1DL6CwTryKyV')

    def test_b58_trailing_zeros(self):
        self.assertEqual(b58encode(b'\x00\x00hello world'),
                         b'11StV1DL6CwTryKyV')
        self.assertEqual(b58decode(b'11StV1DL6CwTryKyV'),
                         b'\x00\x00hello world')
        self.assertEqual(
            b58decode(b58encode(b'\0\0hello world')), b'\x00\x00hello world')
        self.assertEqual(
            b58encode(b58decode(b'11StV1DL6CwTryKyV')), b'11StV1DL6CwTryKyV')

    def test_b58_integers(self):
        digits = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
        for i in range(len(digits)):
            char = digits[i:i+1]
            self.assertEqual(b58decode_int(char), i)
            self.assertEqual(b58encode_int(i), char)
        number = 0x111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5  # noqa
        self.assertEqual(b58decode_int(digits), number)
        self.assertEqual(b58encode_int(number), digits[1:])

    def test_b58_exceptions(self):
        # int is not hex-string or bytes
        self.assertRaises(TypeError, b58encode_check, 3)

        encoded = b58encode_check(b"test")

        # unexpected decoded length
        wrong_length = len(encoded)-1
        self.assertRaises(ValueError, b58decode_check, encoded, wrong_length)

        # checksum is invalid
        invalidChecksum = encoded[:-4] + b'1111'
        self.assertRaises(ValueError, b58decode_check, invalidChecksum, 4)

    def test_wif(self):
        # https://en.bitcoin.it/wiki/Wallet_import_format
        prvkey = 0xC28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

        uncompressedKey = b'\x80' + prvkey.to_bytes(32, byteorder='big')
        uncompressedWIF = b'5HueCGU8rMjxEX<KEY>'
        wif = b58encode_check(uncompressedKey)
        self.assertEqual(wif, uncompressedWIF)
        key = b58decode_check(uncompressedWIF)
        self.assertEqual(key, uncompressedKey)

        compressedKey = b'\x80' + prvkey.to_bytes(32, byteorder='big') +b'\x01'
        compressedWIF = b'<KEY>'
        wif = b58encode_check(compressedKey)
        self.assertEqual(wif, compressedWIF)
        key = b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string
        compressedWIF = '<KEY>'
        key = b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string with leading space
        compressedWIF = ' <KEY>'
        b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

        # string with trailing space
        compressedWIF = 'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617 '
        b58decode_check(compressedWIF)
        self.assertEqual(key, compressedKey)

if __name__ == "__main__":
    # execute only if run as a script
    unittest.main()

#!/usr/bin/env python3 # Copyright (C) 2017-2019 The btclib developers # # This file is part of btclib. It is subject to the license terms in the # LICENSE file found in the top-level directory of this distribution. # # No part of btclib including this file, may be copied, modified, propagated, # or distributed except according to the terms contained in the LICENSE file. # noqa # int is not hex-string or bytes # unexpected decoded length # checksum is invalid # https://en.bitcoin.it/wiki/Wallet_import_format # string # string with leading space # string with trailing space # execute only if run as a script