Exemple #1
0
 def test_Base58(self):
     self.assertEqual([
         format(
             Base58(
                 "02b52e04a0acfe611a4b6963462aca94b6ae02b24e321eda865076619"
                 "01adb49"), "wif"),
         format(
             Base58(
                 "5b921f7051be5e13e177a0253229903c40493df410ae04f4a450c8556"
                 "8f19131"), "wif"),
         format(
             Base58(
                 "0e1bfc9024d1f55a7855dc690e45b2e089d2d825a4671a3c3c7e4ea4e"
                 "74ec00e"), "wif"),
         format(
             Base58(
                 "6e5cc4653d46e690c709ed9e0570a2c75a286ad7c1bc69a648aae6855"
                 "d919d3e"), "wif"),
         format(
             Base58(
                 "b84abd64d66ee1dd614230ebbe9d9c6d66d78d93927c395196666762e"
                 "9ad69d8"), "wif")
     ], [
         "5HqUkGuo62BfcJU5vNhTXKJRXuUi9QSE6jp8C3uBJ2BVHtB8WSd",
         "5JWcdkhL3w4RkVPcZMdJsjos22yB5cSkPExerktvKnRNZR5gx1S",
         "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq",
         "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R",
         "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7"
     ])
Exemple #2
0
 def test_B85hexgetb58(self):
     self.assertEqual([
         'BTS2CAbTi1ZcgMJ5otBFZSGZJKJenwGa9NvkLxsrS49Kr8JsiSGc',
         'BTShL45FEyUVSVV1LXABQnh4joS9FsUaffRtsdarB5uZjPsrwMZF',
         'BTS7DQR5GsfVaw4wJXzA3TogDhuQ8tUR2Ggj8pwyNCJXheHehL4Q',
         'BTSqc4QMAJHAkna65i8U4b7nkbWk4VYSWpZebW7JBbD7MN8FB5sc',
         'BTS2QAVTJnJQvLUY4RDrtxzX9jS39gEq8gbqYMWjgMxvsvZTJxDSu'
     ], [
         format(
             Base58(
                 "02b52e04a0acfe611a4b6963462aca94b6ae02b24e321eda86507661901adb49"
             ), "BTS"),
         format(
             Base58(
                 "5b921f7051be5e13e177a0253229903c40493df410ae04f4a450c85568f19131"
             ), "BTS"),
         format(
             Base58(
                 "0e1bfc9024d1f55a7855dc690e45b2e089d2d825a4671a3c3c7e4ea4e74ec00e"
             ), "BTS"),
         format(
             Base58(
                 "6e5cc4653d46e690c709ed9e0570a2c75a286ad7c1bc69a648aae6855d919d3e"
             ), "BTS"),
         format(
             Base58(
                 "b84abd64d66ee1dd614230ebbe9d9c6d66d78d93927c395196666762e9ad69d8"
             ), "BTS")
     ])
Exemple #3
0
 def __init__(self, address=None, pubkey=None, prefix="GLS"):
     self.prefix = prefix
     if pubkey is not None:
         self._pubkey = Base58(pubkey, prefix=prefix)
         self._address = None
     elif address is not None:
         self._pubkey = None
         self._address = Base58(address, prefix=prefix)
     else:
         raise Exception("Address has to be initialized by either the " +
                         "pubkey or the address.")
Exemple #4
0
 def __init__(self, wif=None, prefix="GLS"):
     if wif is None:
         import os
         self._wif = Base58(hexlify(os.urandom(32)).decode('ascii'))
     elif isinstance(wif, Base58):
         self._wif = wif
     else:
         self._wif = Base58(wif)
     # compress pubkeys only
     self._pubkeyhex, self._pubkeyuncompressedhex = self.compressedpubkey()
     self.pubkey = PublicKey(self._pubkeyhex, prefix=prefix)
     self.uncompressed = PublicKey(self._pubkeyuncompressedhex, prefix=prefix)
     self.uncompressed.address = Address(pubkey=self._pubkeyuncompressedhex, prefix=prefix)
     self.address = Address(pubkey=self._pubkeyhex, prefix=prefix)
Exemple #5
0
def encrypt(privkey, passphrase):
    """ BIP0038 non-ec-multiply encryption. Returns BIP0038 encrypted privkey.

    :param privkey: Private key
    :type privkey: Base58
    :param str passphrase: UTF-8 encoded passphrase for encryption
    :return: BIP0038 non-ec-multiply encrypted wif key
    :rtype: Base58

    """
    privkeyhex = repr(privkey)  # hex
    addr = format(privkey.uncompressed.address, "BTC")
    a = bytes(addr, 'ascii')
    salt = hashlib.sha256(hashlib.sha256(a).digest()).digest()[0:4]
    if SCRYPT_MODULE == "scrypt":
        key = scrypt.hash(passphrase, salt, 16384, 8, 8)
    elif SCRYPT_MODULE == "pylibscrypt":
        key = scrypt.scrypt(bytes(passphrase, "utf-8"), salt, 16384, 8, 8)
    else:
        raise ValueError("No scrypt module loaded")
    (derived_half1, derived_half2) = (key[:32], key[32:])
    aes = AES.new(derived_half2)
    encrypted_half1 = _encrypt_xor(privkeyhex[:32], derived_half1[:16], aes)
    encrypted_half2 = _encrypt_xor(privkeyhex[32:], derived_half1[16:], aes)
    " flag byte is forced 0xc0 because Graphene only uses compressed keys "
    payload = (b'\x01' + b'\x42' + b'\xc0' +
               salt + encrypted_half1 + encrypted_half2)
    " Checksum "
    checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
    privatkey = hexlify(payload + checksum).decode('ascii')
    return Base58(privatkey)
Exemple #6
0
def decrypt(encrypted_privkey, passphrase):
    """BIP0038 non-ec-multiply decryption. Returns WIF privkey.

    :param Base58 encrypted_privkey: Private key
    :param str passphrase: UTF-8 encoded passphrase for decryption
    :return: BIP0038 non-ec-multiply decrypted key
    :rtype: Base58
    :raises SaltException: if checksum verification failed (e.g. wrong password)

    """

    d = unhexlify(base58decode(encrypted_privkey))
    d = d[2:]  # remove trailing 0x01 and 0x42
    flagbyte = d[0:1]  # get flag byte
    d = d[1:]  # get payload
    assert flagbyte == b'\xc0', "Flagbyte has to be 0xc0"
    salt = d[0:4]
    d = d[4:-4]
    if SCRYPT_MODULE == "scrypt":
        key = scrypt.hash(passphrase, salt, 16384, 8, 8)
    elif SCRYPT_MODULE == "pylibscrypt":
        key = scrypt.scrypt(bytes(passphrase, "utf-8"), salt, 16384, 8, 8)
    else:
        raise ValueError("No scrypt module loaded")
    derivedhalf1 = key[0:32]
    derivedhalf2 = key[32:64]
    encryptedhalf1 = d[0:16]
    encryptedhalf2 = d[16:32]
    aes = AES.new(derivedhalf2)
    decryptedhalf2 = aes.decrypt(encryptedhalf2)
    decryptedhalf1 = aes.decrypt(encryptedhalf1)
    privraw = decryptedhalf1 + decryptedhalf2
    privraw = ('%064x' %
               (int(hexlify(privraw), 16) ^ int(hexlify(derivedhalf1), 16)))
    wif = Base58(privraw)
    """ Verify Salt """
    privkey = PrivateKey(format(wif, "wif"))
    addr = format(privkey.uncompressed.address, "BTC")
    a = bytes(addr, 'ascii')
    saltverify = hashlib.sha256(hashlib.sha256(a).digest()).digest()[0:4]
    if saltverify != salt:
        raise SaltException('checksum verification failed! Password may be incorrect.')
    return wif
Exemple #7
0
 def __init__(self, pk, prefix="GLS"):
     self.prefix = prefix
     self._pk = Base58(pk, prefix=prefix)
     self.address = Address(pubkey=pk, prefix=prefix)
     self.pubkey = self._pk
Exemple #8
0
 def derivesha512address(self):
     """Derive address using ``RIPEMD160(SHA512(x))``"""
     pkbin = unhexlify(repr(self._pubkey))
     addressbin = ripemd160(hexlify(hashlib.sha512(pkbin).digest()))
     return Base58(hexlify(addressbin).decode("ascii"))