The private key, public key, and address of the BTCD code

Source: Internet
Author: User

The first experience of bitcoin Btcd code mentions bitcoin in addition to the main network, as well as testnet and regtest networks.
Testnet is a public test network, all development can access the network, in order to avoid malicious hoarding above the testnet bitcoin, this testnet will be emptied every other time and start again with the new founding block. This should be the reason why you can sometimes see Testnet3 in your code.
Regtest is a local test network that is not publicly available and is used only as a local development test.
In addition, there is a test network that segnet is the quarantine witness. And the BTCD code also see simnet this test network option, visual and Regtest is the same as the local test network, but the specific difference is not in-depth study.

This article is mainly from the BTCD related code, the study of Bitcoin's private key, public key and address how to generate, details see the second edition of the Master Bitcoin (Chinese version), simply say:
The first and most important step is to generate the private key from a random number. The Bitcoin software uses the underlying random number generator of the operating system to generate 256-bit entropy, the private key.
In the second step, the public key can be computed from the private key by the elliptic curve multiplication.
The third step is to calculate the Bitcoin address from the public key.

The following code is a copy of the code from "vindicate on Bitcoin-using Golang to place the pledge on Bitcoin's blockchain".

Package main import ("GITHUB.COM/BTCSUITE/BTCD/BTCEC" "Github.com/btcsuite/btcutil" "Github.com/btcsuite/btcd/chain CFG "" FMT ") func GENERATEBTC () (String, string, error) {Privkey, err: = Btcec. Newprivatekey (BTCEC. S256 ()) if err! = Nil {return "", "", err} privkeywif, err: = Btcutil. Newwif (Privkey, &chaincfg. Mainnetparams, FALSE) if err! = Nil {return "", "", err} pubkeyserial: = Privkey.pubkey (). Serializeuncompressed () pubkeyaddress, err: = Btcutil. Newaddresspubkey (Pubkeyserial, &chaincfg.  MAINNETPARAMS) If err! = Nil {return "", "", Err} return privkeywif.string (), pubkeyaddress.encodeaddress (), Nil} func Generatebtctest (String, string, error) {Privkey, err: = Btcec. Newprivatekey (BTCEC. S256 ()) if err! = Nil {return "", "", err} privkeywif, err: = Btcutil. Newwif (Privkey, &chaincfg. Testnet3params, FALSE) if err! = Nil {return "", "", err} pubkeyserial: = Privkey.pubkey (). SerializeuncompreSsed () pubkeyaddress, err: = Btcutil. Newaddresspubkey (Pubkeyserial, &chaincfg. TESTNET3PARAMS) If err! = Nil {return "", "", Err} return privkeywif.string (), pubkeyaddress.encodeaddress ()   , nil} func main () {Wifkey, address, _: = Generatebtctest ()//test address//Wifkey, Address, _: = GENERATEBTC ()//official address Fmt. Println (Address, Wifkey)}

The analysis is as follows:

Random number and private key

privKey, err := btcec.NewPrivateKey(btcec.S256())

First, in the BTCD code of the previous clone, there is a module called BTCEC, which implements the elliptic curve cipher algorithm required for bitcoin. S256 returns a special elliptic curve that is defined by the SECP256K1 standard. Looking at the Newprivatekey implementation details, we found that an elliptic curve and a random number generator were passed into the crypto package.
This code directly returns the private key and the public key, which is the first and second steps above. Note that the key in this code is an object of type Privatekey, which has variables that hold the public key.

// github.com/btcsuite/btcd/btcec/privkey.go// NewPrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey// instead of the normal ecdsa.PrivateKey.func NewPrivateKey(curve elliptic.Curve) (*PrivateKey, error) {    key, err := ecdsa.GenerateKey(curve, rand.Reader)    if err != nil {        return nil, err    }    return (*PrivateKey)(key), nil}

Here just can be compared with the bitcoin source reading (1)-The private key article, you can find that in the C + + version, its random number generation should be stronger, because it uses several different random sources, and the Golang version only uses the Rand method provided by the software. This may have potential pitfalls, if the random number distribution of software in an environment is unbalanced, the random number algorithm chooses a high probability random number in a certain area. So the original theory 2^256 a range will be greatly reduced, hackers thus increased the likelihood of random collisions.

WIF

privKeyWif, err := btcutil.NewWIF(privKey, &chaincfg.MainNetParams, false)

Continue to look at the code, which is the 256-digit private key in the Wallet import format (WIF) to represent. The same 256-digit private key, with different encodings, can have different representations, and wif is one of the formats.


Same key, different format

Be aware that this is the Btcutil code that is associated with the BTCD project, which can be seen as a generic class package.

Address

pubKeyAddress, err := btcutil.NewAddressPubKey(pubKeySerial, &chaincfg.MainNetParams)

Similar to wif for private keys, the public key can be used to calculate its address by some algorithms, and the details need to refer to the "Mastering Bitcoin" explanation.


How to generate a Bitcoin address from a public key

In this way, a rule-compliant Bitcoin address and the private key are generated so that you can check the address format correctly on the https://btc.com.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.