OSDN Git Service

list address return the key_index
[bytom/bytom.git] / api / accounts.go
index a1bf521..b84e25d 100644 (file)
@@ -2,6 +2,7 @@ package api
 
 import (
        "context"
+       "encoding/hex"
        "sort"
 
        log "github.com/sirupsen/logrus"
@@ -19,7 +20,7 @@ func (a *API) createAccount(ctx context.Context, ins struct {
        Quorum    int            `json:"quorum"`
        Alias     string         `json:"alias"`
 }) Response {
-       acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias)
+       acc, err := a.wallet.AccountMgr.Create(ins.RootXPubs, ins.Quorum, ins.Alias)
        if err != nil {
                return NewErrorResponse(err)
        }
@@ -81,11 +82,12 @@ func (a *API) validateAddress(ctx context.Context, ins struct {
 }
 
 type addressResp struct {
-       AccountAlias string `json:"account_alias"`
-       AccountID    string `json:"account_id"`
-       Address      string `json:"address"`
-       Change       bool   `json:"change"`
-       KeyIndex     uint64 `json:"-"`
+       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
@@ -98,17 +100,19 @@ 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 != "" {
-               acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
+               acc, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
                if err != nil {
                        return NewErrorResponse(err)
                }
                target = acc
        } else {
-               acc, err := a.wallet.AccountMgr.FindByID(ctx, accountID)
+               acc, err := a.wallet.AccountMgr.FindByID(accountID)
                if err != nil {
                        return NewErrorResponse(err)
                }
@@ -126,15 +130,44 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
                        continue
                }
                addresses = append(addresses, addressResp{
-                       AccountAlias: target.Alias,
-                       AccountID:    cp.AccountID,
-                       Address:      cp.Address,
-                       Change:       cp.Change,
-                       KeyIndex:     cp.KeyIndex,
+                       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))
-       return NewSuccessResponse(addresses)
+       start, end := getPageRange(len(addresses), ins.From, ins.Count)
+       return NewSuccessResponse(addresses[start:end])
+}
+
+type minigAddressResp struct {
+       MiningAddress string `json:"mining_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(minigAddressResp{
+               MiningAddress: miningAddress,
+       })
 }