Golang version bitcoin node and bitcoin wallet usage

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

Install BTCD

go get -u github.com/Masterminds/glidegit clone https://github.com/btcsuite/btcd $GOPATH/src/github.com/btcsuite/btcdcd $GOPATH/src/github.com/bitsuit/btcdglide installgo install . ./cmd/...

BTCD projects are managed through glide, so you will need to install the BTCD when installing the glide

Run Btcd (simnet)

btcd -u shenxin -P shenxin --simnet --miningaddr=SUsbWJE1VMREXFUv8KcBmke7cPsSXaFvnE

If the need for automatic mining to add the--generate parameter,--miningaddr= set the address of the mine to receive the bitcoin. If you do not set up automatic mining, you can use the Generate () method in a later client to manually mine the ore (because the simnet is too fast and inconvenient to test, so use manual digging here).

It is important to note that when running simnet, you need to run at least two nodes to mine, or you cannot dig mine (or mine).

Install Btcwallet

git clone https://github.com/btcsuite/btcwallet $GOPATH/src/github.com/btcsuite/btcwalletcd $GOPATH/src/github.com/btcsuite/btcwalletglide installgo install . ./cmd/...

If you use the Btcwallet write client directly, you will need to copy all files under the $GOPATH/src/github.com/btcsuite/btcwallet/vendor folder to the $GOPATH/src/below ($ Files that are already in the gopath/src/folder can not be copied. After copying, delete the vendor folder to ensure that no errors are caused by the reference package path when using the Btcwallet client later.

Create Btcwallet (testnet)

btcwallet -u shenxin -p shenxin --simnet --create

Enter the corresponding operator according to the prompt inside the terminal, and set the wallet password as: "Shenxin". The-U and-p in the command are the account password required for the wallet to connect to the BTCD GRPC service, or use "--btcdusername=", "--btcdpassword=" to set the account and password required for the connection BTCD However, this will not turn on the Btcwallet RPC service.

Run Btcwallet (testnet) server

btcwallet -u shenxin -p shenxin --walletpass=shenxin --simnet

Both BTCD and Btcwallet are running simnet, so Btcwallet is automatically connected to Btcd after startup.

Create Btcwallet Clien

Package Mainimport ("FMT" "Io/ioutil" "Log" "Path/filepath" "Github.com/btcsuite/btcd/chaincfg" "GitHub . Com/btcsuite/btcd/rpcclient "" Github.com/btcsuite/btcutil ")//Default account address: susbwje1vmrexfuv8kcbmke7cpssxafvne//Main account address:siaxfykmf6afkehotyapsygwxbcruddqth//SS account address: Sad47slu1hsgiqb2ew6jf39pfqhjzejerz Sjphlr6mun7fjjbuzk6a3pj9nfgygvpu25func Main () {client: = Initclient () fmt. PRINTLN (client. Listaccounts ()) fmt. Println (Transferfrom ("main", "Susbwje1vmrexfuv8kcbmke7cpssxafvne", 5)) Fmt. Println (Transfer ("Sad47slu1hsgiqb2ew6jf39pfqhjzejerz", 0.5)) generate () FMT. Println (Getbalancebyaccount ("Default")) FMT. Println (Getbalancebyaccount ("main")) FMT. Println (Getbalancebyaccount ("ss"))}func initclient () *rpcclient. Client {ntfnhandlers: = rpcclient. notificationhandlers{Onaccountbalance:func (account string, balance btcutil. Amount, confirmed bool) {log. Printf ("New Balance for account%s:%v", account, Balance)},} Certhomedir: = Btcutil. Appdatadir ("Btcwallet", false) certs, err: = Ioutil. ReadFile (filepath. Join (Certhomedir, "Rpc.cert")) if err! = Nil {log. Fatal (Err)} conncfg: = &rpcclient. connconfig{Host: "localhost:18554", Endpoint: "ws", User: "Shenxin", Pass : "Shenxin", Certificates:certs,} client, err: = Rpcclient. New (conncfg, &ntfnhandlers) if err! = Nil {log.    Fatal (Err)} return Client}func Transferfrom (account, addr string, amount float64) bool {client: = Initclient () ERR: = client. Walletpassphrase ("Shenxin", 5) if err! = Nil {log. Fatal (ERR)} address, _: = Btcutil. Decodeaddress (addr, &chaincfg. Simnetparams) Btcamount, _: = Btcutil. Newamount (amount) hash, err: = client. Sendfrom (account, address, btcamount) if err! = Nil {log. Fatal (ERR) return false} else {log.Println ("Txid:", hash) return true}}func transfer (addr string, amount float64) bool {client: = Initclient () ERR: = client. Walletpassphrase ("Shenxin", 5) if err! = Nil {log. Fatal (ERR)} address, _: = Btcutil. Decodeaddress (addr, &chaincfg. Simnetparams) Btcamount, _: = Btcutil. Newamount (amount) hash, err: = client. Sendtoaddress (address, btcamount) if err! = Nil {log. Fatal (ERR) return false} else {log. Println ("Txid:", hash) return true}}func Generate () bool {client: = Initclient () _, Err: = client. Generate () if err! = Nil {return false} else {return true}}func Getbalancebyaccount (account s Tring) Btcutil. Amount {client: = Initclient () Err: = client. Walletpassphrase ("Shenxin", 5) if err! = Nil {log. Fatal (Err)} amount, Err: = client. GetBalance (account) if err! = Nil {return 0} else {return amount}}

The node has a default account, "Default", and another call to the client. Createnewaccount () Create two accounts "ss" "main". Use the client. Getnewaddress ("AccountName") creates addresses in each account separately.

The Chaincfg.params parameter needs to be set when the address is initialized, the parameter should correspond to the type of node being run, or an error will occur (for example, BTCD is running simnet, so the address is initialized with &chaincfg. Simnetparams).

Client. The sendtoaddress (address, amount) method defaults to sending Bitcoins from the "default" account.

To be resolved:

    • Every time the wallet method is called, the Wallet software log will prompt for an error (does not affect the client running): Rpcs:websocket receive failed from client [:: 1]:59799:websocket:close 1006 Unexpected EOF
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.