// broadcastTx tries to send the transaction using an api that will broadcast // a submitted transaction on behalf of the user. func broadcastTx(tx *btcwire.MsgTx) { buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) tx.Serialize(buf) hexstr := hex.EncodeToString(buf.Bytes()) url := "https://insight.bitpay.com/api/tx/send" contentType := "application/json" sendTxJson := &sendTxJson{RawTx: hexstr} j, err := json.Marshal(sendTxJson) if err != nil { log.Fatal(fmt.Errorf("Broadcasting the tx failed: %v", err)) } buf = bytes.NewBuffer(j) resp, err := http.Post(url, contentType, buf) if err != nil { log.Fatal(fmt.Errorf("Broadcasting the tx failed: %v", err)) } b, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Printf("The sending api responded with:\n%s\n", b) }
// dumpHex dumps the raw bytes of a Bitcoin transaction to stdout. This is the // format that Bitcoin wire's protocol accepts, so you could connect to a node, // send them these bytes, and if the tx was valid, the node would forward the // tx through the network. func dumpHex(tx *btcwire.MsgTx) { buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) tx.Serialize(buf) hexstr := hex.EncodeToString(buf.Bytes()) fmt.Println("Here is your raw bitcoin transaction:") fmt.Println(hexstr) }