Example #1
0
 def wif(self, compressed=False) -> str:
     from cryptotools.BTC import base58, sha256
     from cryptotools.BTC.network import network
     extended = network('wif') + self.bytes() + (b'\x01' if compressed else b'')
     hashed = sha256(sha256(extended))
     checksum = hashed[:4]
     return base58.encode(extended + checksum)
Example #2
0
 def from_wif(cls, wif: str) -> 'PrivateKey':
     from cryptotools.BTC import base58, sha256
     from cryptotools.BTC.network import network
     bts = base58.decode(wif)
     network_byte, key, checksum = bts[0:1], bts[1:-4], bts[-4:]
     assert sha256(sha256(network_byte + key))[:4] == checksum, 'Invalid Checksum'
     assert network_byte == network('wif'), 'Invalid Network byte'
     if key.endswith(b'\x01'):
         key = key[:-1]
         compressed = True  # TODO
     else:
         compressed = False  # TODO
     return cls(key)
Example #3
0
def verify_openssl(sig: Signature, sigform: bytes, pub: PublicKey):
    """Validate a signature using OpenSSL"""
    import os
    import tempfile

    with tempfile.TemporaryDirectory() as dirname:
        with open(dirname + '/sig.raw', 'wb') as file:
            file.write(sig.encode())

        with open(dirname + '/hash1.sha256', 'wb') as file:
            file.write(sha256(sigform))

        with open(dirname + '/key.hex', 'w') as file:
            file.write('3056301006072a8648ce3d020106052b8104000a034200\n' + pub.hex())

        os.system(f'xxd -r -p < {dirname}/key.hex | openssl pkey -pubin -inform der > {dirname}/key.pem')
        os.system(f'openssl sha256 < {dirname}/hash1.sha256 -verify {dirname}/key.pem -signature {dirname}/sig.raw')