mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-22 08:08:11 +00:00
commit
cec55847b0
@ -20,29 +20,25 @@ generator = generator_secp256k1
|
|||||||
|
|
||||||
|
|
||||||
def random_secret():
|
def random_secret():
|
||||||
convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
|
|
||||||
|
|
||||||
# Collect 256 bits of random data from the OS's cryptographically secure
|
# Collect 256 bits of random data from the OS's cryptographically secure
|
||||||
# random number generator
|
# random number generator
|
||||||
byte_array = os.urandom(32)
|
byte_array = (os.urandom(32)).hex()
|
||||||
|
|
||||||
return convert_to_int(byte_array)
|
|
||||||
|
|
||||||
|
return int(byte_array,16)
|
||||||
|
|
||||||
def get_point_pubkey(point):
|
def get_point_pubkey(point):
|
||||||
if (point.y() % 2) == 1:
|
if (point.y() % 2) == 1:
|
||||||
key = '03' + '%064x' % point.x()
|
key = '03' + '%064x' % point.x()
|
||||||
else:
|
else:
|
||||||
key = '02' + '%064x' % point.x()
|
key = '02' + '%064x' % point.x()
|
||||||
return key.decode('hex')
|
return key
|
||||||
|
|
||||||
|
|
||||||
def get_point_pubkey_uncompressed(point):
|
def get_point_pubkey_uncompressed(point):
|
||||||
key = ('04' +
|
key = ('04' +
|
||||||
'%064x' % point.x() +
|
'%064x' % point.x() +
|
||||||
'%064x' % point.y())
|
'%064x' % point.y())
|
||||||
return key.decode('hex')
|
return key
|
||||||
|
|
||||||
|
|
||||||
# Generate a new private key.
|
# Generate a new private key.
|
||||||
secret = random_secret()
|
secret = random_secret()
|
||||||
@ -50,9 +46,9 @@ print("Secret: ", secret)
|
|||||||
|
|
||||||
# Get the public key point.
|
# Get the public key point.
|
||||||
point = secret * generator
|
point = secret * generator
|
||||||
print("EC point:", point)
|
print("Elliptic Curve point:", point)
|
||||||
|
|
||||||
print("BTC public key:", get_point_pubkey(point).encode("hex"))
|
print("BTC public key:", get_point_pubkey(point))
|
||||||
|
|
||||||
# Given the point (x, y) we can create the object using:
|
# Given the point (x, y) we can create the object using:
|
||||||
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
|
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
|
||||||
|
Loading…
Reference in New Issue
Block a user