Vindicate on Bitcoin-use Golang to place vows on Bitcoin blockchain

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Let's look at a link to a regional chain browser:

https://www.blocktrail.com/tBCC/tx/a63edbbfa17e45b0890520ca30fce6d8eacd41635d1c447418fcfedffa14d914 Open this link and slide to the end , will look at the text

How did this happen? This is a bitcoin transaction, how can I attach Chinese? This article is a step-by-step way to teach you how to add text to a bitcoin transaction. Because the bitcoin transaction is non-tamper, and the permanent existence of the area chain, then its accompanying text has this attribute, it is like an oath! Oath remember, more meaningful!!

The first step: buy certain Bitcoins

Bitcoin trading is a fee, and the current handling fee is not cheap, moving at least hundred yuan, which is too expensive. So I recommend that you first go to the bitcoin testing network to get some coins to test. How to get the bitcoin test network free Bitcoin, you can Google, some sites will be sent for free. For example, you can go to https://testnet.coinfaucet.eu/en/this site to obtain, the current access to the premise is that you have a test network of Bitcoin address, or do not know who to send coins ah. Here's how you can use Golang to generate a Bitcoin test address:

HTTPS://GITHUB.COM/BTCSUITE/BTCD is the bitcoin version of the Golang implementation, first follow its documentation installation, here is assumed that you have successfully installed on the local.

The following code contains the build test and the official address that you can run to get the test address.

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)}

Suppose you get a test address:

    1. Address: MT4P3RZPJE5FXEQVGZNBK9HXYXCWKPPJSD
    2. Private key: Cv4hmdzgf3gg7ndetvv7sjq22yobmzbe5megkuqvqtxxxxx (note that the key is not correct, please do not use)

Then, you go to https://testnet.coinfaucet.eu/en/to receive the free test Bitcoin, which can be viewed through the interface to view the unused transaction information for that address:

Https://api.blockcypher.com/v1/btc/test3/addrs/mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd/full

A total of two, the first 0.65 second 1.3 Total the balance of this address is 1.95

{"Address": "Mt4p3rzpje5fxeqvgznbk9hxyxcwkppjsd", "total_received": 195000000, "total_sent": 0, "balance": 195000000,     "Unconfirmed_balance": 0, "final_balance": 195000000, "N_tx": 2, "Unconfirmed_n_tx": 0, "Final_n_tx": 2, "TXs": [  The second pen {"Block_hash": "00000000000004149feebc41cfeb5a66df052f989aec60faec711caee4f93b3c", "Block_height": 1255326, "Block_index": +, "hash": "2c56134c99b24e17f5c3852d910e2e090848652c4e7b08ee8aa7450b2e14d7c4", "a Ddresses ": [" 2N48GNAEKD8EZGQ5MLTB6EGBVQFUQ94SVTV "," 2nawfhdbykfv5zpr4cnlvg9hmhcx31czbep "," Mt4p3rZpJ E5FXEQVGZNBK9HXYXCWKPPJSD "]," total ": 197064067190," fees ": 100000," size ": $," preference ":" High "," relayed_by ":" 94.130.106.254:18333 "," Confirmed ":" 2017-12-19t02:32:36z "," Received ":" 2017-12-19t0       2:17:26.601z "," ver ": 1," Double_spend ": false," VIN_SZ ": 1," VOUT_SZ ": 2," confirmations ": 5672,  "Confidence": 1,    "Inputs": [{"Prev_hash": "7265ffdf8310fc2ecd6277759f39de9c801149ca602c6b2236667d2af2d5dd29", "Output_index": 1, "script": "1600149EB46621CCEAC0E393B5CD5FFB481FAFA48A16FC", "Output_value": 1970641          67190, "sequence": 4294967295, "Addresses": ["2NAWFHDBYKFV5ZPR4CNLVG9HMHCX31CZBEP" ], "Script_type": "Pay-to-script-hash", "Age": 1255313, "Witness": ["3044022034bb85 0d1efab224a14b7cd7565a9fce58fb89794f50471419115f1b893f626d022027a849a46f3a902944e3f01a66eb0fc489bfe8e5a7815b8644128b7e6f8      9ace101 "," 030916AD60B499268F909E20867B49D22E55B3864AAFC896120C6DAEC1011CEECB "]}], "Outputs": [{"Value": 130000000, "script": "76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac "," Addresses ": [" MT4P3RZPJE5FXEQVGZNBK9HXYXCWKPPJSD "]," Script_type ":" Pay-to-pub      Key-hash "},  {"Value": 196934067190, "script": "a9147758cec0a445f9908186f5cfeeb52bc0077c7e1487", "Spent_b Y ":" 5c3c00896db0ba1a7526c6e8c3495c29264df99d4a494afbd909af4f0d4df605 "," Addresses ": [" 2n48gnaekd8ezg      Q5MLTB6EGBVQFUQ94SVTV "]," Script_type ":" Pay-to-script-hash "}]},//First pen { "Block_hash": "00000000000004149feebc41cfeb5a66df052f989aec60faec711caee4f93b3c", "block_height": 1255326, "bloc        K_index ": +," hash ":" 48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067 "," Addresses ": [ "2N4MRW2XRMEFAUF51JIZSAQCSXR9UOWXSBJ", "2n5cnrlzxxzt2jfxplx9z6hf9gdglqg3ych", "Mt4p3rzpje5fxeqvgznbk9hxyxcwk PPJSD "]," total ": 196092936523," fees ": 100000," size ": $," preference ":" High "," relayed _by ":" 88.196.208.18:18333 "," Confirmed ":" 2017-12-19t02:32:36z "," Received ":" 2017-12-19t02:17:33.267z "," Ver ": 1," double_spEnd ": false," VIN_SZ ": 1," VOUT_SZ ": 2," confirmations ": 5672," confidence ": 1," inputs ": [          {"Prev_hash": "2880f6c768afa728fa9374af0617d535a960243f2820de53992279a59b84d8a3", "Output_index": 1, "Script": "1600147ce118c5a9faa2fdf5bc1c7feb41ddac7084a481", "Output_value": 196093036523, "sequ ence ": 4294967295," Addresses ": [" 2n5cnrlzxxzt2jfxplx9z6hf9gdglqg3ych "]," script_t Ype ":" Pay-to-script-hash "," Age ": 1255313," Witness ": [" 30440220741aa14828e97e0fd9668b9070e 2ab886f3646456c33b143c291f6c47e1ec985022005f624843908a745dbc5d703361065a5fbca9aa0b1fb4aca105cd09d8e6fd09e01 "," 02FE3A6A5CFB075B84D5A0E6DAA2220DC49C8A36B61F49F86111DD08141166B7FC "]}],//The following several values are more important" OU          Tputs ": [{" Value ": 65000000," script ":" 76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac ",   "Addresses": [         "MT4P3RZPJE5FXEQVGZNBK9HXYXCWKPPJSD"], "Script_type": "Pay-to-pubkey-hash"}, { "Value": 196027936523, "script": "A91479eab7f3bf5054cc47da2761f0b61d6cb622622a87", "spent_by": "Dc32b80ba7f863572c8dd97508d8c7a04af35d10671c9ca3c60f0d28c552c8d1", "Addresses": ["2n4mrw2xrmefauf51j IZSAQCSXR9UOWXSBJ "]," Script_type ":" Pay-to-script-hash "}]}]}

Remember these values for the first stroke:

    • Tx_hash 48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067
    • Tx_output_n 0
    • Script:76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac
    • Value 65000000

We're going to use this unpaid deal for the next time.

Step Two: Structuring transactions, attaching text

The following constructs a transaction, which sends itself a sum of money, the transaction fee is 0.001

Address: = "MT4P3RZPJE5FXEQVGZNBK9HXYXCWKPPJSD" var balance int64 = 65000000//balance var fee int64 = 0.001 * 1e8//transaction fee var Lef Ttome = balance-fee//Balance-transaction fee is left to me//1. Construct output outputs: = []*wire. txout{}//1.1 Output 1, give yourself the rest of the money addr, _: = Btcutil. Decodeaddress (address, &chaincfg. Simnetparams) Pkscript, _: = Txscript. Paytoaddrscript (addr) outputs = append (outputs, wire. Newtxout (Lefttome, Pkscript))//1.2 Output 2, add text Comment: = "This is a message, haha" pkscript, _ = Txscript. Nulldatascript ([]byte (comment)) outputs = append (outputs, wire. Newtxout (Int64 (0), pkscript))

Step three: Construct the input

2. Construct input Prevtxhash: = "48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067" Prevpkscripthex: = " 76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac "PREVTXOUTPUTN: = UInt32 (0) Hash, _: = Chainhash. NEWHASHFROMSTR (Prevtxhash)//tx Hashoutpoint: = Wire. Newoutpoint (hash, PREVTXOUTPUTN)//number of output txin: = Wire. Newtxin (outpoint, nil, nil) inputs: = []*wire. Txin{txin} prevpkscript, _: = Hex. Decodestring (prevpkscripthex) Prevpkscripts: = Make ([][]byte, 1) prevpkscripts[0] = Prevpkscript tx: = &wire. msgtx{   Version: Wire  . Txversion,   txin:     inputs,   txout:    outputs,   locktime:0,}

Fourth Step: Signature Transaction (Signature input)

3. Signature Privkey: = "cv4hmdzgf3gg7ndetvv7sjq22yobmzbe5megkuqvqtxxxxx"//Private key sign (TX, Privkey, prevpkscripts)//Signature method Func signs ( Tx *wire. MSGTX, privkeystr string, prevpkscripts [][]byte)  {   Inputs: = TX. Txin   wif, err: = Btcutil. Decodewif (PRIVKEYSTR)    FMT. Println ("Wif err", err)   privkey: = wif. Privkey    for I: = range inputs {      Pkscript: = prevpkscripts[i]      var script []byte      script, err = Txscript. Signaturescript (TX, I, Pkscript, Txscript. Sighashall,            Privkey, False)      Inputs[i]. signaturescript = Script   }}

Fifth step: Output trading raw information, broadcast to the network

4. Output hexbuf: = bytes. Newbuffer (Make ([]byte, 0, TX. Serializesize ())) If err: = Tx. Serialize (BUF); Err! = Nil {}txhex: = hex. Encodetostring (BUF. Bytes ()) fmt. PRINTLN ("hex", Txhex)

Broadcast the output hex to the network, Https://tbtc.blockdozer.com/insight/tx/send

The complete source code is given below:

Func Main () {address: = "MT4P3RZPJE5FXEQVGZNBK9HXYXCWKPPJSD" var balance int64 = 65000000//balance var fee int64 = 0.0 * 1e8//transaction fee var lefttome = balance-fee//Balance-transaction fee is left to me//1. Construct output outputs: = []*wire. txout{}//1.1 Output 1, give yourself the rest of the money addr, _: = Btcutil. Decodeaddress (address, &chaincfg. Simnetparams) Pkscript, _: = Txscript. Paytoaddrscript (addr) outputs = append (outputs, wire. Newtxout (Lefttome, Pkscript))//1.2 Output 2, add text Comment: = "This is a message, haha" pkscript, _ = Txscript. Nulldatascript ([]byte (comment)) outputs = append (outputs, wire. Newtxout (Int64 (0), pkscript))//2. Construct input Prevtxhash: = "48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067" Prevpkscripthex: = " 76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac "PREVTXOUTPUTN: = UInt32 (0) Hash, _: = Chainhash. NEWHASHFROMSTR (Prevtxhash)//TX hash Outpoint: = wire. Newoutpoint (hash, PREVTXOUTPUTN)//number of output txin: = Wire. Newtxin (outpoint, nil, nil) inputs: = []*wire. txin{Txin} prevpkscript, _: = Hex. Decodestring (prevpkscripthex) Prevpkscripts: = Make ([][]byte, 1) prevpkscripts[0] = Prevpkscript tx: = &wire. msgtx{Version:wire. Txversion, Txin:inputs, Txout:outputs, locktime:0,}//3. Signature Privkey: = "cv4hmdzgf3gg7ndetvv7sjq22yobmzbe5megkuqvqtxxxxx"//Private key sign (TX, Privkey, prevpkscripts)//4. Output hex buf: = bytes. Newbuffer (Make ([]byte, 0, TX. Serializesize ())) If err: = Tx. Serialize (BUF); Err! = Nil {} Txhex: = Hex. Encodetostring (BUF. Bytes ()) fmt. PRINTLN ("hex", Txhex)}//signed Func sign (TX *wire. MSGTX, privkeystr string, prevpkscripts [][]byte) {Inputs: = tx. Txin wif, err: = Btcutil. Decodewif (PRIVKEYSTR) fmt. Println ("Wif err", err) Privkey: = wif. Privkey for I: = range inputs {pkscript: = prevpkscripts[i] var script []byte script, err = Txscript. Signaturescript (TX, I, Pkscript, Txscript. Sighashall, Privkey, False) Inputs[i]. Signaturescript = ScripT}} 

Through the above method, the Bitcoin lettering I have achieved in  www.ibitlin.com , welcome to use!!

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.