01_ Private key, public key and address

Time:2020-11-25

The private key, public key and address are a string of (almost) impossible to collide.
It can be illustrated by four equations

    1. = random() (0 < k < n; n = 1.158 * 10^77)
    1. =K * g (G is a constant of elliptic curve cryptography)
    1. = Hash.ripemd160(Hash.sha256(K))
    1. = base58(0 + a + checksum)

The process from K (private key) to K (public key) to a (address) is guaranteed to be irreversible by cryptography.

The code is as follows:

const crypto = require('crypto');
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');
var BN = require('bn.js');
var bs58 = require('bs58');

class PrivateKey {
    constructor() {
        this.bn = this.generateKey();
        this.compressed = true;
        this.network = Networks.defaultNetwork;
    }
    
    generateKey() {
        let condition;
        let bn;
        
        do {
            //The numbers between 1 and 2 ^ 256 are randomly generated and displayed in hex format.
            //Hex: an encoding format that encodes each byte into two hexadecimal characters
            // privateHex: "ceea0ada327fc521e9c5ba704a002f56c95de6bffc83901aa2290fc882c4c218"
            const privateHex = crypto.randomBytes(32).toString('hex');
           
            //Privatehex is a string type. The string format can't directly compare the size, so it needs to be converted to a numeric type.
            //But the maximum security number in JS is Number.MAX_ SAFE_ Integer = 9007199254740991, which is not enough to represent a private value.
            //So we use the library BN, compared with private. BN means big number.
            bn = new BN(privateHex, 16)
    
            // max = <BN: fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141>
            const max = new BN(ec.curve.n.toArray())
            
            //In fact, private is smaller than max
            //Max is a constant, n = 1.158 * 10 ^ 77, slightly less than 2 ^ 256
            //The degree of the elliptic curve used by bitcoin
            
            //When BN < max, the private key is successfully generated
            condition = bn.lt(max)
        } while (!condition);
        
        return bn;
    }
}


class PublicKey {
    constructor(privateKey){
        //Elliptic curve multiplication can calculate the public key from the private key
        //It's an irreversible process: k = k * G
        //Where k is the private key, G is a constant point called the generating point, and K is the resulting public key.
        this.point = ec.curve.g.mul(privateKey.bn)
        this.compressed = privateKey.compressed
        this.network = privateKey.network
    }
    
    //No corresponding document was found in this section
    toBuffer () {
        var xbuf = this.point.getX().toBuffer({ size: 32 });
        
        var ybuf = this.point.getY().toBuffer({ size: 32 });
        
        var prefix;
        if (!this.compressed) {
            prefix = new Buffer([0x04]);
            return Buffer.concat([prefix, xbuf, ybuf]);
        } else {
            var odd = ybuf[ybuf.length - 1] % 2;
            if (odd) {
                prefix = new Buffer([0x03]);
            } else {
                prefix = new Buffer([0x02]);
            }
            return Buffer.concat([prefix, xbuf]);
        }
    };
}

class Address {
    constructor(publicKey){
        //Publish key to bitpoint address
        this.hashBuffer =  Hash.ripemd160(Hash.sha256(publicKey.toBuffer()))
        this.network = publicKey.network
        this.type = Address.PayToPublicKeyHash
    }
    
    //Generate bitcoin address seen by users
    // Base58Check Encoding
    toString () {
        //The prefix of bitcoin address is 0 (Hex is 0x00)
        const version = new Buffer([0])
        const payload = this.hashBuffer
        // 1. add version prefix
        const addVersionPrefix =  Buffer.concat([version, payload])
        // 2. hash(version prefix + payload)
        const checksum = Hash.sha256(Hash.sha256(addVersionPrefix)).slice(0, 4)
        // 3. add first 4 bytes as checksum
        const addChecksum = Buffer.concat([addVersionPrefix, checksum])
        // 4. encode in base-58
        return bs58.encode(addChecksum);
    }
}


Address.PayToPublicKeyHash = 'pubkeyhash';
Address.PayToScriptHash = 'scripthash';


class Networks {}

Networks.defaultNetwork = 'livenet';


class Hash {}

Hash.sha256 = function(buf) {
    return crypto.createHash('sha256').update(buf).digest();
};

Hash.ripemd160 = function(buf) {
    return crypto.createHash('ripemd160').update(buf).digest();
};


const  privateKey = new PrivateKey()

console.log(privateKey)

const publicKey = new PublicKey(privateKey)

console.log(publicKey)

const address = new Address(publicKey)

console.log(address)