OSDN Git Service

add get-raw-transaction
authoroysheng <oysheng@bytom.io>
Thu, 3 May 2018 11:36:06 +0000 (19:36 +0800)
committeroysheng <oysheng@bytom.io>
Thu, 3 May 2018 11:36:06 +0000 (19:36 +0800)
api/api.go
api/query.go
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/transaction.go

index 2cd64c6..fc20940 100644 (file)
@@ -219,6 +219,7 @@ func (a *API) buildHandler() {
 
        m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx))
        m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconfirmedTxs))
+       m.Handle("/get-raw-transaction", jsonHandler(a.getRawTransaction))
 
        m.Handle("/get-block-hash", jsonHandler(a.getBestBlockHash))
        m.Handle("/get-block-header", jsonHandler(a.getBlockHeader))
index 45d163d..3da897a 100644 (file)
@@ -11,6 +11,7 @@ import (
        "github.com/bytom/consensus"
        chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/protocol/bc"
+       "github.com/bytom/protocol/bc/types"
 )
 
 // POST /list-accounts
@@ -161,6 +162,37 @@ func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
        })
 }
 
+// RawTx is the tx struct for getRawTransaction
+type RawTx struct {
+       Version   uint64                   `json:"version"`
+       Size      uint64                   `json:"size"`
+       TimeRange uint64                   `json:"time_range"`
+       Inputs    []*query.AnnotatedInput  `json:"inputs"`
+       Outputs   []*query.AnnotatedOutput `json:"outputs"`
+}
+
+// POST /get-transaction-template
+func (a *API) getRawTransaction(ctx context.Context, ins struct {
+       Tx types.Tx `json:"raw_transaction"`
+}) Response {
+       tx := &RawTx{
+               Version:   ins.Tx.Version,
+               Size:      ins.Tx.SerializedSize,
+               TimeRange: ins.Tx.TimeRange,
+               Inputs:    []*query.AnnotatedInput{},
+               Outputs:   []*query.AnnotatedOutput{},
+       }
+
+       for i := range ins.Tx.Inputs {
+               tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(&ins.Tx, uint32(i)))
+       }
+       for i := range ins.Tx.Outputs {
+               tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
+       }
+
+       return NewSuccessResponse(tx)
+}
+
 // POST /list-unspent-outputs
 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
        ID string `json:"id"`
index 117af40..5635e00 100644 (file)
@@ -128,6 +128,7 @@ func AddCommands() {
 
        BytomcliCmd.AddCommand(getUnconfirmedTransactionCmd)
        BytomcliCmd.AddCommand(listUnconfirmedTransactionsCmd)
+       BytomcliCmd.AddCommand(getRawTransactionCmd)
 
        BytomcliCmd.AddCommand(listUnspentOutputsCmd)
        BytomcliCmd.AddCommand(listBalancesCmd)
index 93e7ab1..ee8a661 100644 (file)
@@ -275,6 +275,30 @@ var estimateTransactionGasCmd = &cobra.Command{
        },
 }
 
+var getRawTransactionCmd = &cobra.Command{
+       Use:   "get-raw-transaction <raw_transaction>",
+       Short: "get the transaction by raw_transaction",
+       Args:  cobra.ExactArgs(1),
+       Run: func(cmd *cobra.Command, args []string) {
+               var ins = struct {
+                       Tx types.Tx `json:"raw_transaction"`
+               }{}
+
+               err := json.Unmarshal([]byte(args[0]), &ins)
+               if err != nil {
+                       jww.ERROR.Println(err)
+                       os.Exit(util.ErrLocalExe)
+               }
+
+               data, exitCode := util.ClientCall("/get-raw-transaction", &ins)
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               printJSON(data)
+       },
+}
+
 var getTransactionCmd = &cobra.Command{
        Use:   "get-transaction <hash>",
        Short: "get the transaction by matching the given transaction hash",