def __init__(self, address=None, pubkey=None, prefix="SCR"): 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.")
def __init__(self, wif=None, prefix="SCR"): 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)
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, AES.MODE_ECB) 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)
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, AES.MODE_ECB) 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
def __init__(self): self.base58 = Base58() # 2 ^ 256 + 1 # pylint: disable=C0103 self.N = (1 << 256) + 1
def __init__(self, pk, prefix="SCR"): self.prefix = prefix self._pk = Base58(pk, prefix=prefix) self.address = Address(pubkey=pk, prefix=prefix) self.pubkey = self._pk
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'))
def pub2hex(pub): return repr(Base58(pub))
def __init__(self): self.base58 = Base58() # 2 ^ 256 + 1 self.N = (1 << 256) + 1