diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index b84d5d3537..496941db7b 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -10,6 +10,9 @@ import ( "github.com/ethereum/go-ethereum/shyftdb" "github.com/gorilla/mux" + "bytes" + "io/ioutil" + "encoding/json" ) // GetTransaction gets txs @@ -244,3 +247,39 @@ func GetInternalTransactionsHash(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Get Internal Transaction Hash", transactionHash) } + +func BroadcastTx(w http.ResponseWriter, r *http.Request) { + // Example return result (returns tx hash): + // {"jsonrpc":"2.0","id":1,"result":"0xafa4c62f29dbf16bbfac4eea7cbd001a9aa95c59974043a17f863172f8208029"} + + // http params + vars := mux.Vars(r) + transactionHash := vars["transaction_hash"] + + // format the transactionHash into a proper sendRawTransaction jsonrpc request + formatted_json := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["%s"],"id":0}`, transactionHash)) + + // send json rpc request + resp, _ := http.Post("http://localhost:8545", "application/json", bytes.NewBuffer(formatted_json)) + body, _ := ioutil.ReadAll(resp.Body) + byt := []byte(string(body)) + + // read json and return result as http response, be it an error or tx hash + var dat map[string]interface{} + if err := json.Unmarshal(byt, &dat); err != nil { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "ERROR parsing json") + } + tx_hash := dat["result"] + if(tx_hash == nil) { + errMap := dat["error"].(map[string]interface{}) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "ERROR:", errMap["message"]) + } else { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "Transaction Hash:", tx_hash) + } +} \ No newline at end of file diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index 4ce0dc80c8..fd6517eb5e 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -87,4 +87,10 @@ var routes = Routes{ "/api/get_internal_transactions_hash/{transactions_hash}", GetInternalTransactionsHash, }, + Route{ + "BroadcastTx", + "GET", + "/api/broadcast_tx/{transaction_hash}", + BroadcastTx, + }, }