Skip to content

Instantly share code, notes, and snippets.

@etotheipi
Created November 2, 2011 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etotheipi/1333473 to your computer and use it in GitHub Desktop.
Save etotheipi/1333473 to your computer and use it in GitHub Desktop.
Playing with private-keys in PyBtcEngine
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