OSDN Git Service

list-addresses sort addressResp by create index
[bytom/bytom.git] / api / accounts.go
index cca6be5..61ec5bc 100644 (file)
@@ -2,7 +2,7 @@ package api
 
 import (
        "context"
-       "strings"
+       "sort"
 
        log "github.com/sirupsen/logrus"
 
@@ -15,9 +15,9 @@ import (
 
 // POST /create-account
 func (a *API) createAccount(ctx context.Context, ins struct {
-       RootXPubs []chainkd.XPub         `json:"root_xpubs"`
-       Quorum    int                    `json:"quorum"`
-       Alias     string                 `json:"alias"`
+       RootXPubs []chainkd.XPub `json:"root_xpubs"`
+       Quorum    int            `json:"quorum"`
+       Alias     string         `json:"alias"`
 }) Response {
        acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias)
        if err != nil {
@@ -30,18 +30,21 @@ func (a *API) createAccount(ctx context.Context, ins struct {
        return NewSuccessResponse(annotatedAccount)
 }
 
+// AccountInfo is request struct for deleteAccount
+type AccountInfo struct {
+       Info string `json:"account_info"`
+}
+
 // POST /delete-account
-func (a *API) deleteAccount(ctx context.Context, in struct {
-       AccountInfo string `json:"account_info"`
-}) Response {
-       if err := a.wallet.AccountMgr.DeleteAccount(in); err != nil {
+func (a *API) deleteAccount(ctx context.Context, in AccountInfo) Response {
+       if err := a.wallet.AccountMgr.DeleteAccount(in.Info); err != nil {
                return NewErrorResponse(err)
        }
        return NewSuccessResponse(nil)
 }
 
 type validateAddressResp struct {
-       Vaild   bool `json:"vaild"`
+       Valid   bool `json:"valid"`
        IsLocal bool `json:"is_local"`
 }
 
@@ -50,10 +53,10 @@ func (a *API) validateAddress(ctx context.Context, ins struct {
        Address string `json:"address"`
 }) Response {
        resp := &validateAddressResp{
-               Vaild:   false,
+               Valid:   false,
                IsLocal: false,
        }
-       address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
+       address, err := common.DecodeAddress(ins.Address, &consensus.ActiveNetParams)
        if err != nil {
                return NewSuccessResponse(resp)
        }
@@ -72,7 +75,7 @@ func (a *API) validateAddress(ctx context.Context, ins struct {
                return NewSuccessResponse(resp)
        }
 
-       resp.Vaild = true
+       resp.Valid = true
        resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
        return NewSuccessResponse(resp)
 }
@@ -84,18 +87,36 @@ type addressResp struct {
        Change       bool   `json:"change"`
 }
 
+type addressRespIndex struct {
+       KeyIndex    uint64
+       AddressResp addressResp
+}
+
+// SortByIndex implements sort.Interface for addressRespIndex slices
+type SortByIndex []addressRespIndex
+
+func (a SortByIndex) Len() int           { return len(a) }
+func (a SortByIndex) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
+func (a SortByIndex) Less(i, j int) bool { return a[i].KeyIndex < a[j].KeyIndex }
+
 func (a *API) listAddresses(ctx context.Context, ins struct {
        AccountID    string `json:"account_id"`
        AccountAlias string `json:"account_alias"`
 }) Response {
        accountID := ins.AccountID
+       var target *account.Account
        if ins.AccountAlias != "" {
                acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
                if err != nil {
                        return NewErrorResponse(err)
                }
-
-               accountID = acc.ID
+               target = acc
+       } else {
+               acc, err := a.wallet.AccountMgr.FindByID(ctx, accountID)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               target = acc
        }
 
        cps, err := a.wallet.AccountMgr.ListControlProgram()
@@ -103,19 +124,30 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
                return NewErrorResponse(err)
        }
 
-       var addresses []*addressResp
+       addressRespIndexes := []addressRespIndex{}
        for _, cp := range cps {
-               if cp.Address == "" || (len(accountID) != 0 && strings.Compare(accountID, cp.AccountID) != 0) {
+               if cp.Address == "" || cp.AccountID != target.ID {
                        continue
                }
 
-               accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
-               addresses = append(addresses, &addressResp{
-                       AccountAlias: accountAlias,
-                       AccountID:    cp.AccountID,
-                       Address:      cp.Address,
-                       Change:       cp.Change,
-               })
+               addressIndex := addressRespIndex{
+                       KeyIndex: cp.KeyIndex,
+                       AddressResp: addressResp{
+                               AccountAlias: target.Alias,
+                               AccountID:    cp.AccountID,
+                               Address:      cp.Address,
+                               Change:       cp.Change,
+                       },
+               }
+               addressRespIndexes = append(addressRespIndexes, addressIndex)
        }
+
+       // sort AddressResp by KeyIndex
+       addresses := []addressResp{}
+       sort.Sort(SortByIndex(addressRespIndexes))
+       for _, addr := range addressRespIndexes {
+               addresses = append(addresses, addr.AddressResp)
+       }
+
        return NewSuccessResponse(addresses)
 }