Created
November 2, 2011 12:08
-
-
Save etotheipi/1333473 to your computer and use it in GitHub Desktop.
Playing with private-keys in PyBtcEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pybtcengine import * | |
# Create new private key and get its address information | |
print 'Creating new address:' | |
newAddr = PyBtcAddress().generateNew() | |
newAddr.pprint() | |
print ' Private key (integer):', newAddr.privKeyInt | |
print ' Private key (hex, BE):', int_to_hex(newAddr.privKeyInt, widthBytes=32, endOut=BIGENDIAN) | |
privKeyBinary33 = '\x80' + int_to_binary(newAddr.privKeyInt, widthBytes=32, endOut=BIGENDIAN) | |
chksum = hash256(privKeyBinary33) | |
privKeyEnc37 = privKeyBinary33 + chksum[:4] | |
privKeyBase58 = binary_to_addrStr(privKeyEnc37) | |
print ' Encoded privkey:', binary_to_hex(privKeyEnc37) | |
print ' Base58 privkey: ', binary_to_addrStr(privKeyEnc37) | |
# Get back to the raw private key from encoded format | |
print '\nRecovering private key from base58 encoding:' | |
pk37 = addrStr_to_binary(privKeyBase58) | |
leadByte, privKeyBin, chk = pk37[:1], pk37[1:33], pk37[33:] | |
verifyKey = hash256(leadByte+privKeyBin).startswith(chk) | |
if verifyKey: | |
print ' Valid checksum!' | |
else: | |
print ' Checksum does not match!' | |
privKeyInt = binary_to_int(privKeyBin, BIGENDIAN) | |
addr = PyBtcAddress().createFromPrivateKey(privKeyInt) | |
print ' Recovered address information:' | |
addr.pprint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment