OSDN Git Service

list address return the key_index
[bytom/bytom.git] / api / accounts.go
index bc194e3..b84e25d 100644 (file)
@@ -2,7 +2,8 @@ package api
 
 import (
        "context"
-       "strings"
+       "encoding/hex"
+       "sort"
 
        log "github.com/sirupsen/logrus"
 
@@ -15,52 +16,36 @@ 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"`
-       Tags      map[string]interface{} `json:"tags"`
+       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, strings.TrimSpace(ins.Alias), ins.Tags)
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       annotatedAccount, err := account.Annotated(acc)
+       acc, err := a.wallet.AccountMgr.Create(ins.RootXPubs, ins.Quorum, ins.Alias)
        if err != nil {
                return NewErrorResponse(err)
        }
 
+       annotatedAccount := account.Annotated(acc)
        log.WithField("account ID", annotatedAccount.ID).Info("Created account")
 
        return NewSuccessResponse(annotatedAccount)
 }
 
-// POST /update-account-tags
-func (a *API) updateAccountTags(ctx context.Context, updateTag struct {
-       AccountInfo string                 `json:"account_info"`
-       Tags        map[string]interface{} `json:"tags"`
-}) Response {
-       err := a.wallet.AccountMgr.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags)
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       return NewSuccessResponse(nil)
+// 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"`
 }
 
@@ -69,10 +54,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)
        }
@@ -91,29 +76,47 @@ 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)
 }
 
 type addressResp struct {
-       AccountAlias string `json:"account_alias"`
-       AccountID    string `json:"account_id"`
-       Address      string `json:"address"`
+       AccountAlias   string `json:"account_alias"`
+       AccountID      string `json:"account_id"`
+       Address        string `json:"address"`
+       ControlProgram string `json:"control_program"`
+       Change         bool   `json:"change"`
+       KeyIndex       uint64 `json:"key_index"`
 }
 
+// SortByIndex implements sort.Interface for addressResp slices
+type SortByIndex []addressResp
+
+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"`
+       From         uint   `json:"from"`
+       Count        uint   `json:"count"`
 }) Response {
        accountID := ins.AccountID
+       var target *account.Account
        if ins.AccountAlias != "" {
-               account, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
+               acc, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
                if err != nil {
                        return NewErrorResponse(err)
                }
-
-               accountID = account.ID
+               target = acc
+       } else {
+               acc, err := a.wallet.AccountMgr.FindByID(accountID)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               target = acc
        }
 
        cps, err := a.wallet.AccountMgr.ListControlProgram()
@@ -121,14 +124,50 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
                return NewErrorResponse(err)
        }
 
-       addresses := []*addressResp{}
+       addresses := []addressResp{}
        for _, cp := range cps {
-               if cp.Address == "" || (len(accountID) != 0 && strings.Compare(accountID, cp.AccountID) != 0) {
+               if cp.Address == "" || cp.AccountID != target.ID {
                        continue
                }
+               addresses = append(addresses, addressResp{
+                       AccountAlias:   target.Alias,
+                       AccountID:      cp.AccountID,
+                       Address:        cp.Address,
+                       ControlProgram: hex.EncodeToString(cp.ControlProgram),
+                       Change:         cp.Change,
+                       KeyIndex:       cp.KeyIndex,
+               })
+       }
+
+       // sort AddressResp by KeyIndex
+       sort.Sort(SortByIndex(addresses))
+       start, end := getPageRange(len(addresses), ins.From, ins.Count)
+       return NewSuccessResponse(addresses[start:end])
+}
+
+type minigAddressResp struct {
+       MiningAddress string `json:"mining_address"`
+}
 
-               accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
-               addresses = append(addresses, &addressResp{AccountAlias: accountAlias, AccountID: cp.AccountID, Address: cp.Address})
+func (a *API) getMiningAddress(ctx context.Context) Response {
+       miningAddress, err := a.wallet.AccountMgr.GetMiningAddress()
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+       return NewSuccessResponse(minigAddressResp{
+               MiningAddress: miningAddress,
+       })
+}
+
+// POST /set-mining-address
+func (a *API) setMiningAddress(ctx context.Context, in struct {
+       MiningAddress string `json:"mining_address"`
+}) Response {
+       miningAddress, err := a.wallet.AccountMgr.SetMiningAddress(in.MiningAddress)
+       if err != nil {
+               return NewErrorResponse(err)
        }
-       return NewSuccessResponse(addresses)
+       return NewSuccessResponse(minigAddressResp{
+               MiningAddress: miningAddress,
+       })
 }