比特币地址生成Nodejs实现实例

比特币地址生成——Nodejs实现实例
参照:

https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses

地址生成流程:

0 - 获取一个随机的32字节私钥

9a9a6539856be209b8ea2adbd155c0919646d108515b60b7b13d6a79f1ae5174

1 - 计算上述私钥的(压缩)公钥

0340A609475AFA1F9A784CAD0DB5D5BA7DBAAB2147A5D7B9BBDE4D1334A0E40A5E

2 - 对(压缩)公钥进行SHA256哈希计算

2b0995c0703c96d694f03a8987f89d387459fc359694737547a75764989c5e16

3 - 对步骤2的哈希值进行RIPEMD-160哈希计算

154de7cabbb5822075e92c57a27ca3ef3e8be50c

4 - 在步骤3的哈希值前添加地址版本号(主网为0x00,测试网为0xef)

00154de7cabbb5822075e92c57a27ca3ef3e8be50c

5 - 对步骤4的扩展RIPEMD-160哈希值进行SHA256哈希计算

ab7d579d497d75ab7e337212345635a4c071c249c6e8ec07532d2ea4d82290e6

6 - 对步骤5的哈希值再次进行SHA256哈希计算

fc897c2001ef5e99b2e37853e84dd041bebe6f831f462729de2af27e4ab9ea7e

7 - 取步骤6结果值的前4个字节作为校验码

fc897c20

8 - 将校验码添加到步骤4的扩展RIPEMD-160哈希值末尾

00154de7cabbb5822075e92c57a27ca3ef3e8be50cfc897c20

9 - 将结果转为位Base58编码格式

12weWzbq5jT7c3MHbHD2WP2uLXEUtaGLXZ

Node(v10.5.0)实现源码:

/** Create Bitcoin Address */
const crypto = require(`crypto`);
const ecdh = crypto.createECDH('secp256k1');
const bs58 = require(`bs58`);
// 0 - Having a private ECDSA key
var privateKey = crypto.randomBytes(32);
console.log(`Private key:[${privateKey.toString(`hex`)}]`);
// 1 - Take the corresponding public key generated with it (33 bytes, 1 byte 0x02 (y-coord is even), 
// and 32 bytes corresponding to X coordinate)
ecdh.setPrivateKey(privateKey);
var cpublicKey = Buffer.from(ecdh.getPublicKey('hex', 'compressed'), 'hex');
console.log(`Public key:[${cpublicKey.toString(`hex`).toUpperCase()}]`);
// 2 - Perform SHA-256 hashing on the public key
var sha1 = crypto.createHash(`sha256`).update(cpublicKey).digest();
console.log(`SHA-256:[${sha1.toString(`hex`)}]`);
// 3 - Perform RIPEMD-160 hashing on the result of SHA-256
var ripemd160 = crypto.createHash(`rmd160`).update(sha1).digest();
console.log(`RIPEMD-160:[${ripemd160.toString(`hex`)}]`);
// 4 - Add version byte in front of RIPEMD-160 hash (0x00 for Main Network, 0x6f for Testnet)
const version = Buffer.from([0x00]);
var extendedPriKey = Buffer.alloc(ripemd160.length + version.length);
extendedPriKey = Buffer.concat([version, ripemd160], extendedPriKey.length);
console.log(`Extended RIPEMD-160:[${extendedPriKey.toString(`hex`)}]`);
// 5 - Perform SHA-256 hash on the extended RIPEMD-160 result
var sha2 = crypto.createHash(`sha256`).update(extendedPriKey).digest();
console.log(`SHA-256:[${sha2.toString(`hex`)}]`);
// 6 - Perform SHA-256 hash on the result of the previous SHA-256 hash
var sha3 = crypto.createHash(`sha256`).update(sha2).digest();
console.log(`SHA-256:[${sha3.toString(`hex`)}]`);
// 7 - Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
var checksum = Buffer.alloc(4);
sha3.copy(checksum, 0, 0, checksum.length);
console.log(`Checksum:[${checksum.toString(`hex`)}]`);
// 8 - Add the 4 checksum bytes from stage 7 at the end of extended RIPEMD-160 hash from stage 4. 
// This is the 25-byte binary Bitcoin Address.
var btcAddress = Buffer.alloc(extendedPriKey.length + checksum.length);
btcAddress = Buffer.concat([extendedPriKey, checksum], btcAddress.length);
console.log(`25-byte binary bitcoin address:[${btcAddress.toString(`hex`)}]`);
// 9 - Convert the result from a byte string into a base58 string using Base58Check encoding. 
// This is the most commonly used Bitcoin Address format
var address = bs58.encode(btcAddress);
console.log(`Address:[${address}]`);

需要安装包 bs58

你可能感兴趣的:(Bitcoin)