func getTransactionByID(gate *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } txid := r.FormValue("txid") if txid == "" { wh.Error400(w, "txid is empty") return } h, err := cipher.SHA256FromHex(txid) if err != nil { wh.Error400(w, err.Error()) return } tx, err := gate.V.GetTransaction(h) if err != nil { wh.Error400(w, err.Error()) return } if tx == nil { wh.Error404(w, "not found") return } resTx := visor.TransactionResult{ Transaction: visor.NewReadableTransaction(tx), Status: tx.Status, } wh.SendOr404(w, &resTx) } }
func getRawTx(gate *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } txid := r.FormValue("txid") if txid == "" { wh.Error400(w, "txid is empty") return } h, err := cipher.SHA256FromHex(txid) if err != nil { wh.Error400(w, err.Error()) return } tx, err := gate.V.GetTransaction(h) if err != nil { wh.Error400(w, err.Error()) return } d := tx.Txn.Serialize() wh.SendOr404(w, hex.EncodeToString(d)) return } }
func getUxOutByID(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } uxid := r.FormValue("uxid") if uxid == "" { wh.Error400(w, "uxid is empty") return } id, err := cipher.SHA256FromHex(uxid) if err != nil { wh.Error400(w, err.Error()) return } uxout, err := gateway.GetUxOutByID(id) if err != nil { wh.Error400(w, err.Error()) return } if uxout == nil { wh.Error404(w, "not found") return } wh.SendOr404(w, uxout) } }
func transactionCMD() gcli.Command { name := "transaction" return gcli.Command{ Name: name, Usage: "Show detail info of specific transaction", ArgsUsage: "[transaction id]", OnUsageError: onCommandUsageError(name), Action: func(c *gcli.Context) error { txid := c.Args().First() if txid == "" { return errors.New("txid is empty") } // validate the txid _, err := cipher.SHA256FromHex(txid) if err != nil { return errors.New("error txid") } tx, err := getTransactionByID(txid) if err != nil { return err } v, err := json.MarshalIndent(tx, "", " ") if err != nil { return errors.New("invalid tx result") } fmt.Println(string(v)) return nil }, } // Commands = append(Commands, cmd) }
// get block by hash or seq // method: GET // url: /block?hash=[:hash] or /block?seq[:seq] // params: hash or seq, should only specify one filter. func getBlock(gate *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } hash := r.FormValue("hash") seq := r.FormValue("seq") var b *coin.Block switch { case hash == "" && seq == "": wh.Error400(w, "should specify one filter, hash or seq") return case hash != "" && seq != "": wh.Error400(w, "should only specify one filter, hash or seq") return case hash != "": h, err := cipher.SHA256FromHex(hash) if err != nil { wh.Error400(w, err.Error()) return } b = gate.V.GetBlockByHash(h) case seq != "": uSeq, err := strconv.ParseUint(seq, 10, 64) if err != nil { wh.Error400(w, err.Error()) return } b = gate.V.GetBlockBySeq(uSeq) } if b == nil { wh.SendOr404(w, nil) return } wh.SendOr404(w, visor.NewReadableBlock(b)) } }
func getTransactionHandler(req Request, gateway Gatewayer) Response { var txid []string if err := req.DecodeParams(&txid); err != nil { logger.Critical("decode params failed:%v", err) return makeErrorResponse(errCodeInvalidParams, errMsgInvalidParams) } if len(txid) != 1 { return makeErrorResponse(errCodeInvalidParams, errMsgInvalidParams) } t, err := cipher.SHA256FromHex(txid[0]) if err != nil { logger.Critical("decode txid err: %v", err) return makeErrorResponse(errCodeInvalidParams, "invalid transaction hash") } txn, err := gateway.GetTransaction(t) if err != nil { logger.Debugf("%v", err) return makeErrorResponse(errCodeInternalError, errMsgInternalError) } return makeSuccessResponse(req.ID, TxnResult{txn}) }
func TransactionFromJSON(str string) (coin.Transaction, error) { var TxIn TransactionJSON err := json.Unmarshal([]byte(str), TxIn) if err != nil { errors.New("cannot deserialize") } var tx coin.Transaction tx.Sigs = make([]cipher.Sig, len(TxIn.Sigs)) tx.In = make([]cipher.SHA256, len(TxIn.In)) tx.Out = make([]coin.TransactionOutput, len(TxIn.Out)) for i, _ := range tx.Sigs { sig2, err := cipher.SigFromHex(TxIn.Sigs[i]) if err != nil { return coin.Transaction{}, errors.New("invalid signature") } tx.Sigs[i] = sig2 } for i, _ := range tx.In { hash, err := cipher.SHA256FromHex(TxIn.In[i]) if err != nil { return coin.Transaction{}, errors.New("invalid signature") } tx.In[i] = hash } for i, _ := range tx.Out { out, err := TransactionOutputFromJSON(TxIn.Out[i]) if err != nil { return coin.Transaction{}, errors.New("invalid output") } tx.Out[i] = out } tx.Length = uint32(tx.Size()) tx.Type = 0 hash, err := cipher.SHA256FromHex(TxIn.Hash) if err != nil { return coin.Transaction{}, errors.New("invalid hash") } if hash != tx.Hash() { } InnerHash, err := cipher.SHA256FromHex(TxIn.Hash) if InnerHash != tx.InnerHash { return coin.Transaction{}, errors.New("inner hash") } err = tx.Verify() if err != nil { return coin.Transaction{}, errors.New("transaction failed verification") } return tx, nil }