Private-Key
ecdsa - 創建私鑰和比特幣地址
我有以下程式碼:
import binascii import hashlib from fastecdsa import keys, curve # generate a private key for curve P256 priv_key = keys.gen_private_key(curve.secp256k1) print (priv_key) print ("______") # get the public key corresponding to the private key we just generated pub_key = keys.get_public_key(priv_key, curve.secp256k1) print (pub_key)
這會返回:
68553277193328358088381091283586955911631072878513122367770244549235879948867 ______ (defdba7ac050b698bc63134bbc495064b5a99125b023d8ae92d21ca43be77961, 68ed2abc9d36bae64947b94dd15864e3b2f7601b7009c5b9bae8a8775553fea0)
第一個是私鑰,第二個是公鑰。如何將這些數字轉換為比特幣格式?
我試過這個腳本:https ://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/ec-math.py
但在 Python 3.5 中不起作用
我解決了這個問題。這是程式碼:
import os import hashlib from hashlib import sha256 def ripemd160(x): d = hashlib.new("ripemd160") d.update(x) return d P = 2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1 G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8) B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" def point_add(p, q): xp, yp = p xq, yq = q if p == q: l = pow(2 * yp % P, P - 2, P) * (3 * xp * xp) % P else: l = pow(xq - xp, P - 2, P) * (yq - yp) % P xr = (l ** 2 - xp - xq) % P yr = (l * xp - l * xr - yp) % P return xr, yr def point_mul(p, d): n = p q = None for i in range(256): if d & (1 << i): if q is None: q = n else: q = point_add(q, n) n = point_add(n, n) return q def point_bytes(p): x, y = p return b"\x04" + x.to_bytes(32, "big") + y.to_bytes(32, "big") def b58_encode(d): out = "" p = 0 x = 0 while d[0] == 0: out += "1" d = d[1:] for i, v in enumerate(d[::-1]): x += v * (256 ** i) while x > 58 ** (p + 1): p += 1 while p >= 0: a, x = divmod(x, 58 ** p) out += B58[a] p -= 1 return out def make_address(privkey): q = point_mul(G, int.from_bytes(privkey, "big")) hash160 = ripemd160(sha256(point_bytes(q)).digest()).digest() addr = b"\x00" + hash160 checksum = sha256(sha256(addr).digest()).digest()[:4] addr += checksum wif = b"\x80" + privkey checksum = sha256(sha256(wif).digest()).digest()[:4] wif += checksum addr = b58_encode(addr) wif = b58_encode(wif) return addr, wif print("=========================") from ecdsa import SigningKey, SECP256k1 sk = SigningKey.generate(curve=SECP256k1) vk = sk.get_verifying_key() addr, wif = make_address(sk.to_string()) print("Address: " + addr) print("Privkey: " + wif)