OSDN Git Service

fix get-unconfirmed-transaction mux_id 0 bug (#1358)
[bytom/bytom.git] / api / query.go
index bbb66f9..721ebfb 100644 (file)
@@ -13,15 +13,26 @@ import (
        "github.com/bytom/consensus"
        "github.com/bytom/crypto/ed25519/chainkd"
        chainjson "github.com/bytom/encoding/json"
+       "github.com/bytom/errors"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
 )
 
 // POST /list-accounts
 func (a *API) listAccounts(ctx context.Context, filter struct {
-       ID string `json:"id"`
+       ID    string `json:"id"`
+       Alias string `json:"alias"`
 }) Response {
-       accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
+       accountID := filter.ID
+       if filter.Alias != "" {
+               acc, err := a.wallet.AccountMgr.FindByAlias(filter.Alias)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               accountID = acc.ID
+       }
+
+       accounts, err := a.wallet.AccountMgr.ListAccounts(accountID)
        if err != nil {
                log.Errorf("listAccounts: %v", err)
                return NewErrorResponse(err)
@@ -95,6 +106,8 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
        AccountID   string `json:"account_id"`
        Detail      bool   `json:"detail"`
        Unconfirmed bool   `json:"unconfirmed"`
+       From        uint   `json:"from"`
+       Count       uint   `json:"count"`
 }) Response {
        transactions := []*query.AnnotatedTx{}
        var err error
@@ -126,9 +139,11 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
 
        if filter.Detail == false {
                txSummary := a.wallet.GetTransactionsSummary(transactions)
-               return NewSuccessResponse(txSummary)
+               start, end := getPageRange(len(txSummary), filter.From, filter.Count)
+               return NewSuccessResponse(txSummary[start:end])
        }
-       return NewSuccessResponse(transactions)
+       start, end := getPageRange(len(transactions), filter.From, filter.Count)
+       return NewSuccessResponse(transactions[start:end])
 }
 
 // POST /get-unconfirmed-transaction
@@ -152,7 +167,16 @@ func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
                TimeRange:  txDesc.Tx.TimeRange,
                Inputs:     []*query.AnnotatedInput{},
                Outputs:    []*query.AnnotatedOutput{},
-               StatusFail: false,
+               StatusFail: txDesc.StatusFail,
+       }
+
+       resOutID := txDesc.Tx.ResultIds[0]
+       resOut := txDesc.Tx.Entries[*resOutID]
+       switch out := resOut.(type) {
+       case *bc.Output:
+               tx.MuxID = *out.Source.Ref
+       case *bc.Retirement:
+               tx.MuxID = *out.Source.Ref
        }
 
        for i := range txDesc.Tx.Inputs {
@@ -188,6 +212,7 @@ func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
 
 // RawTx is the tx struct for getRawTransaction
 type RawTx struct {
+       ID        bc.Hash                  `json:"tx_id"`
        Version   uint64                   `json:"version"`
        Size      uint64                   `json:"size"`
        TimeRange uint64                   `json:"time_range"`
@@ -201,6 +226,7 @@ func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
        Tx types.Tx `json:"raw_transaction"`
 }) Response {
        tx := &RawTx{
+               ID:        ins.Tx.ID,
                Version:   ins.Tx.Version,
                Size:      ins.Tx.SerializedSize,
                TimeRange: ins.Tx.TimeRange,
@@ -236,9 +262,12 @@ func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
 // POST /list-unspent-outputs
 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
        ID            string `json:"id"`
+       Unconfirmed   bool   `json:"unconfirmed"`
        SmartContract bool   `json:"smart_contract"`
+       From          uint   `json:"from"`
+       Count         uint   `json:"count"`
 }) Response {
-       accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID, filter.SmartContract)
+       accountUTXOs := a.wallet.GetAccountUtxos(filter.ID, filter.Unconfirmed, filter.SmartContract)
 
        UTXOs := []query.AnnotatedUTXO{}
        for _, utxo := range accountUTXOs {
@@ -258,8 +287,8 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
                        Change:              utxo.Change,
                }}, UTXOs...)
        }
-
-       return NewSuccessResponse(UTXOs)
+       start, end := getPageRange(len(UTXOs), filter.From, filter.Count)
+       return NewSuccessResponse(UTXOs[start:end])
 }
 
 // return gasRate
@@ -282,29 +311,46 @@ type AccountPubkey struct {
 
 // POST /list-pubkeys
 func (a *API) listPubKeys(ctx context.Context, ins struct {
-       AccountID string `json:"account_id"`
+       AccountID    string `json:"account_id"`
+       AccountAlias string `json:"account_alias"`
+       PublicKey    string `json:"public_key"`
 }) Response {
-       account, err := a.wallet.AccountMgr.FindByID(ctx, ins.AccountID)
+       var err error
+       account := &account.Account{}
+       if ins.AccountAlias != "" {
+               account, err = a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
+       } else {
+               account, err = a.wallet.AccountMgr.FindByID(ins.AccountID)
+       }
+
        if err != nil {
                return NewErrorResponse(err)
        }
 
        pubKeyInfos := []PubKeyInfo{}
-       idx := a.wallet.AccountMgr.GetContractIndex(ins.AccountID)
+       idx := a.wallet.AccountMgr.GetContractIndex(account.ID)
        for i := uint64(1); i <= idx; i++ {
                rawPath := signers.Path(account.Signer, signers.AccountKeySpace, i)
                derivedXPub := account.XPubs[0].Derive(rawPath)
                pubkey := derivedXPub.PublicKey()
 
+               if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(pubkey) {
+                       continue
+               }
+
                var path []chainjson.HexBytes
                for _, p := range rawPath {
                        path = append(path, chainjson.HexBytes(p))
                }
 
-               pubKeyInfos = append([]PubKeyInfo{{
+               pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
                        Pubkey: hex.EncodeToString(pubkey),
                        Path:   path,
-               }}, pubKeyInfos...)
+               })
+       }
+
+       if len(pubKeyInfos) == 0 {
+               return NewErrorResponse(errors.New("Not found publickey for the account"))
        }
 
        return NewSuccessResponse(&AccountPubkey{