mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-22 16:18:11 +00:00
Trailing L not needed in Py2 & Syntax Error in Py3
Also print() function, ternary if, avoid backslashes.
This commit is contained in:
parent
b80cd6dbc5
commit
14138a859f
@ -1,56 +1,56 @@
|
|||||||
import ecdsa
|
import ecdsa
|
||||||
import os
|
import os
|
||||||
from ecdsa.util import string_to_number, number_to_string
|
|
||||||
|
|
||||||
# secp256k1, http://www.oid-info.com/get/1.3.132.0.10
|
# secp256k1, http://www.oid-info.com/get/1.3.132.0.10
|
||||||
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
|
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
|
||||||
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
|
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
||||||
_b = 0x0000000000000000000000000000000000000000000000000000000000000007L
|
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
|
||||||
_a = 0x0000000000000000000000000000000000000000000000000000000000000000L
|
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
|
||||||
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
|
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
|
||||||
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
|
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
|
||||||
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
|
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
|
||||||
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
|
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
|
||||||
oid_secp256k1 = (1, 3, 132, 0, 10)
|
oid_secp256k1 = (1, 3, 132, 0, 10)
|
||||||
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1)
|
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1,
|
||||||
|
generator_secp256k1, oid_secp256k1)
|
||||||
ec_order = _r
|
ec_order = _r
|
||||||
|
|
||||||
curve = curve_secp256k1
|
curve = curve_secp256k1
|
||||||
generator = generator_secp256k1
|
generator = generator_secp256k1
|
||||||
|
|
||||||
|
|
||||||
def random_secret():
|
def random_secret():
|
||||||
convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
|
convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
|
||||||
|
|
||||||
# Collect 256 bits of random data from the OS's cryptographically secure random generator
|
# Collect 256 bits of random data from the OS's cryptographically secure
|
||||||
|
# random generator
|
||||||
byte_array = os.urandom(32)
|
byte_array = os.urandom(32)
|
||||||
|
|
||||||
return convert_to_int(byte_array)
|
return convert_to_int(byte_array)
|
||||||
|
|
||||||
|
|
||||||
def get_point_pubkey(point):
|
def get_point_pubkey(point):
|
||||||
if point.y() & 1:
|
key = ('03' if point.y() & 1 else '02') + '%064x' % point.x()
|
||||||
key = '03' + '%064x' % point.x()
|
|
||||||
else:
|
|
||||||
key = '02' + '%064x' % point.x()
|
|
||||||
return key.decode('hex')
|
return key.decode('hex')
|
||||||
|
|
||||||
|
|
||||||
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.decode('hex')
|
||||||
|
|
||||||
|
|
||||||
# Generate a new private key.
|
# Generate a new private key.
|
||||||
secret = random_secret()
|
secret = random_secret()
|
||||||
print "Secret: ", secret
|
print("Secret: ", secret)
|
||||||
|
|
||||||
# Get the public key point.
|
# Get the public key point.
|
||||||
point = secret * generator
|
point = secret * generator
|
||||||
print "EC point:", point
|
print("EC point:", point)
|
||||||
|
|
||||||
print "BTC public key:", get_point_pubkey(point).encode("hex")
|
print("BTC public key:", get_point_pubkey(point).encode("hex"))
|
||||||
|
|
||||||
# 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)
|
||||||
assert point1 == point
|
assert(point1 == point)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user