Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.