In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to use Golang to store vows on the block chain of Bitcoin. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First of all, let's take a look at the transaction link of an area chain browser:
Https://www.blocktrail.com/tBCC/tx/a63edbbfa17e45b0890520ca30fce6d8eacd41635d1c447418fcfedffa14d914 opens the link, slides to the end, and reads the text shown in the picture.
How do you do that? This is a bitcoin transaction, how can Chinese be attached? This article will teach you step by step how to add text to bitcoin transactions. Because the transaction of Bitcoin is unusurable and permanently on the regional chain, then the accompanying text has this attribute, which is like an oath! How meaningful it is to keep vows!
Step 1: buy a certain amount of bitcoin
Bitcoin transactions require fees, but the current fees are not cheap, at least hundreds of yuan, which is too expensive. So I recommend that you first go to the Bitcoin test network to get some coins to test. How to get free bitcoin in the bitcoin test network, you can Google, some websites will give it away for free. For example, you can go to the https://testnet.coinfaucet.eu/en/ site to get it, the current premise is that you have a bitcoin address of the test network, otherwise you don't know who to send coins to. Here's how to use Golang to generate bitcoin test addresses:
Https://github.com/btcsuite/btcd is a golang implementation of bitcoin. Install it according to its documentation, assuming that you have successfully installed it locally.
The following code contains the generated test and the official address, which you can run to get the test address.
Package main import ("github.com/btcsuite/btcd/btcec"github.com/btcsuite/btcutil"github.com/btcsuite/btcd/chaincfg"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:
Address: mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd
Private key: cV4HmdzGF3gG7NdEtVV7sjq22yoBmZBe5MEGKUqvQTXXXXX (note that the key is not correct, please do not use it)
Then, you go to https://testnet.coinfaucet.eu/en/ to get a free test bitcoin, and you can view the unspent transaction information of this address through the interface:
Https://api.blockcypher.com/v1/btc/test3/addrs/mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd/full
A total of two payments were made, the first 0.65 and the second 1.3 the total 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": [/ / second {"block_hash": "00000000000004149feebc41cfeb5a66df052f989aec60faec711caee4f93b3c", "block_height": 1255326 "block_index": 53, "hash": "2c56134c99b24e17f5c3852d910e2e090848652c4e7b08ee8aa7450b2e14d7c4", "addresses": ["2N48GnaEkd8eZgQ5MLTb6EGBvqfuQ94sVTv", "2NAwfhDByKfV5ZPr4cnLVg9hMHcx31CZbEp", "mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd"], "total": 197064067190, "fees": 100000, "size": 140,140, "preference": "high", "relayed_by": "94.130.106.25418 333" "confirmed": "2017-12-19T02:32:36Z", "received": "2017-12-19T02: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": 197064167190, "sequence": 4294967295, "addresses": ["2NAwfhDByKfV5ZPr4cnLVg9hMHcx31CZbEp"], "script_type": "pay-to-script-hash", "age": 1255313, "witness": ["3044022034bb850d1efab224a14b7cd7565a9fce58fb89794f50471419115f1b893f626d022027a849a46f3a902944e3f01a66eb0fc489bfe8e5a7815b8644128b7e6f89ace101" "030916ad60b499268f909e20867b49d22e55b3864aafc896120c6daec1011ceecb"]}], "outputs": [{"value": 130000000, "script": "76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac", "addresses": ["mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd"], "script_type": "pay-to-pubkey-hash"} {"value": 196934067190, "script": "a9147758cec0a445f9908186f5cfeeb52bc0077c7e1487", "spent_by": "5c3c00896db0ba1a7526c6e8c3495c29264df99d4a494afbd909af4f0d4df605", "addresses": ["2N48GnaEkd8eZgQ5MLTb6EGBvqfuQ94sVTv"], "script_type": "pay-to-script-hash"}}, / / first {"block_hash": "00000000000004149feebc41cfeb5a66df052f989aec60faec711caee4f93b3c" "block_height": 1255326, "block_index": 40, "hash": "48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067", "addresses": ["2N4Mrw2XRMEfAuf51JiZsaQCSxr9UowxSbJ", "2N5CNRLZXXZt2JFxPLX9z6HF9gdgLqG3ycH", "mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd"], "total": 196092936523, "fees": 100000, "size": 140,140, "preference": "high", "relayed_by": "88.196.208.1818333" "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, "sequence": 4294967295, "addresses": ["2N5CNRLZXXZt2JFxPLX9z6HF9gdgLqG3ycH"], "script_type": "pay-to-script-hash", "age": 1255313, "witness": ["30440220741aa14828e97e0fd9668b9070e2ab886f3646456c33b143c291f6c47e1ec985022005f624843908a745dbc5d703361065a5fbca9aa0b1fb4aca105cd09d8e6fd09e01" "02fe3a6a5cfb075b84d5a0e6daa2220dc49c8a36b61f49f86111dd08141166b7fc"]}], / / the following values are more important "outputs": [{"value": 65000000, "script": "76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac", "addresses": ["mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd"] "script_type": "pay-to-pubkey-hash"}, {"value": 196027936523, "script": "a91479eab7f3bf5054cc47da2761f0b61d6cb622622a87", "spent_by": "dc32b80ba7f863572c8dd97508d8c7a04af35d10671c9ca3c60f0d28c552c8d1", "addresses": ["2N4Mrw2XRMEfAuf51JiZsaQCSxr9UowxSbJ"] "script_type": "pay-to-script-hash"}]}]}
Remember these values of the first stroke:
Tx_hash 48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067
Tx_output_n 0
Script: 76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac
Value 65000000
We're going to use this free transaction next.
Step 2: construct the transaction with text attached
Next, construct a transaction. This transaction sends yourself 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 leftToMe = balance-fee / / balance-transaction fee is the rest of the / / 1. Construct output outputs: = [] * wire.TxOut {} / / 1.1 output 1, transfer the remaining money to yourself addr, _: = btcutil.DecodeAddress (address, & chaincfg.SimNetParams) pkScript, _: = txscript.PayToAddrScript (addr) outputs = append (outputs, wire.NewTxOut (leftToMe, pkScript)) / / 1.2 output 2, add the text comment: = "this is a message, " pkScript _ = txscript.NullDataScript ([] byte (comment)) outputs = append (outputs, wire.NewTxOut (int64 (0), pkScript)) step 3: construct the input / / 2. Construction input prevTxHash: = "48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067" prevPkScriptHex: = "76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac" prevTxOutputN: = uint32 (0) hash, _: = chainhash.NewHashFromStr (prevTxHash) / / tx hashoutPoint: = wire.NewOutPoint (hash, prevTxOutputN) / / several 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,} step 4: signature transaction (signature input) / / 3. Signature privKey: = "cV4HmdzGF3gG7NdEtVV7sjq22yoBmZBe5MEGKUqvQTXXXXX" / / Private key sign (tx, privKey, prevPkScripts) / / signature method 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} step 5: output the original transaction information and broadcast it 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:
Package tximport ("github.com/btcsuite/btcd/wire"github.com/btcsuite/btcutil"github.com/btcsuite/btcd/chaincfg"github.com/btcsuite/btcd/txscript"github.com/btcsuite/btcd/chaincfg/chainhash"encoding/hex"fmt"bytes") func main () {address: = "mt4p3rZpJE5fXEqvGzNBk9HxYXcWKpPJSd" Var balance int64 = 65000000 / / balance var fee int64 = 0.001 * 1e8 / / transaction fee var leftToMe = balance-fee / / balance-transaction fee is the rest of my / / 1. Construct output outputs: = [] * wire.TxOut {} / / 1.1Exp 1, transfer the remaining money addr, _: = btcutil.DecodeAddress (address, & chaincfg.SimNetParams) pkScript, _: = txscript.PayToAddrScript (addr) outputs = append (outputs, wire.NewTxOut (leftToMe, pkScript)) / / 1.2Exp 2 Add the text comment: = "this is a message, " pkScript, _ = txscript.NullDataScript ([] byte (comment)) outputs = append (outputs, wire.NewTxOut (int64 (0), pkScript)) / / 2. Construction input prevTxHash: = "48eea09764713f3dadcfed29490ab5e288299e01e571e1f7a1396a75ce38e067" prevPkScriptHex: = "76a91489a7f0117eaf47d8b4af740c66116e35ffe1bea988ac" prevTxOutputN: = uint32 (0) hash, _: = chainhash.NewHashFromStr (prevTxHash) / / tx hash outPoint: = wire.NewOutPoint (hash, prevTxOutputN) / / several 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)} / / signature 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}} on "how to use Golang to store vows on the block chain of bitcoin" is here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.