比特币地址生成算法详解

比特币地址生成流程如下图所示:

 

比特币地址生成算法详解

第一步,随机选取一个32字节的数,大小介于1~0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4141之间,作为私钥

18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725

第二步,使用椭圆曲线加密算法(ECDSA-SECP256k1)计算私钥所对应的非压缩公钥(共65字节,1字节0x04,32字节为x坐标,32字节为y坐标)。

0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

第三步,计算公钥的SHA-256哈希值

600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408

第四步,计算上一步哈希值的RIPEMD-160哈希值

010966776006953D5567439E5E39F86A0D273BEE

第五步,在上一步结果之间加入地址版本号(如比特币主网版本号“0x00")

00010966776006953D5567439E5E39F86A0D273BEE

第六步,计算上一步结果的SHA-256哈希值

445C7A8007A93D8733188288BB320A8FE2DEBD2AE1B47F0F50BC10BAE845C094

第七步,再次计算上一步结果的SHA-256哈希值

D61967F63C7DD183914A4AE452C9F6AD5D462CE3D277798075B107615C1A8A30

第八步,取上一步结果的前4个字节(8位十六进制数)D61967F6,把这4个字节加在第五步结果的后面,作为校验(这就是比特币地址的16进制形态)

00010966776006953D5567439E5E39F86A0D273BEED61967F6

第九步,用base58表示法变换一下地址(这就是最常见的比特币地址形态)

16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

下面给出比特币地址生成的python源码

1 import hashlib 2 from ecdsa import SECP256k1, SigningKey 3 import sys 4 import binascii 5 6 # 58 character alphabet used 7 BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' 8 9 def from_bytes (data, big_endian = False): 10 if isinstance(data, str): 11 data = bytearray(data) 12 if big_endian: 13 data = reversed(data) 14 num = 0 15 for offset, byte in enumerate(data): 16 num += byte << (offset * 8) 17 return num 18 19 def base58_encode(version, public_address): 20 """ 21 Gets a Base58Check string 22 See https://en.bitcoin.it/wiki/Base58Check_encoding 23 """ 24 if sys.version_info.major > 2: 25 version = bytes.fromhex(version) 26 else: 27 version = bytearray.fromhex(version) 28 firstSHA256 = hashlib.sha256(version + public_address) 29 print("first sha256: %s"%firstSHA256.hexdigest().upper()) 30 secondSHA256 = hashlib.sha256(firstSHA256.digest()) 31 print("second sha256: %s"%secondSHA256.hexdigest().upper()) 32 checksum = secondSHA256.digest()[:4] 33 payload = version + public_address + checksum 34 print("Hex address: %s"%binascii.hexlify(payload).decode().upper()) 35 if sys.version_info.major > 2: 36 result = int.from_bytes(payload, byteorder="big") 37 else: 38 result = from_bytes(payload, True) 39 # count the leading 0s 40 padding = len(payload) - len(payload.lstrip(b'\0')) 41 encoded = [] 42 43 while result != 0: 44 result, remainder = divmod(result, 58) 45 encoded.append(BASE58_ALPHABET[remainder]) 46 47 return padding*"1" + "".join(encoded)[::-1] 48 49 def get_private_key(hex_string): 50 if sys.version_info.major > 2: 51 return bytes.fromhex(hex_string.zfill(64)) 52 else: 53 return bytearray.fromhex(hex_string.zfill(64)) 54 55 def get_public_key(private_key): 56 # this returns the concatenated x and y coordinates for the supplied private address 57 # the prepended 04 is used to signify that it's uncompressed 58 if sys.version_info.major > 2: 59 return (bytes.fromhex("04") + SigningKey.from_string(private_key, curve=SECP256k1).verifying_key.to_string()) 60 else: 61 return (bytearray.fromhex("04") + SigningKey.from_string(private_key, curve=SECP256k1).verifying_key.to_string()) 62 63 def get_public_address(public_key): 64 address = hashlib.sha256(public_key).digest() 65 print("public key hash256: %s"%hashlib.sha256(public_key).hexdigest().upper()) 66 h = hashlib.new('ripemd160') 67 h.update(address) 68 address = h.digest() 69 print("RIPEMD-160: %s"%h.hexdigest().upper()) 70 return address 71 72 if __name__ == "__main__": 73 #private_key = get_private_key("FEEDB0BDEADBEEF") 74 private_key = get_private_key("18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725") 75 print("private key: %s"%binascii.hexlify(private_key).decode().upper()) 76 public_key = get_public_key(private_key) 77 print("public_key: %s"%binascii.hexlify(public_key).decode().upper()) 78 public_address = get_public_address(public_key) 79 bitcoin_address = base58_encode("00", public_address) 80 print("Final address %s"%bitcoin_address)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjpzp.html