OSDN Git Service

edit go version (#1153)
[bytom/bytom.git] / api / accounts.go
1 package api
2
3 import (
4         "context"
5         "sort"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/bytom/account"
10         "github.com/bytom/common"
11         "github.com/bytom/consensus"
12         "github.com/bytom/crypto/ed25519/chainkd"
13         "github.com/bytom/protocol/vm/vmutil"
14 )
15
16 // POST /create-account
17 func (a *API) createAccount(ctx context.Context, ins struct {
18         RootXPubs []chainkd.XPub `json:"root_xpubs"`
19         Quorum    int            `json:"quorum"`
20         Alias     string         `json:"alias"`
21 }) Response {
22         acc, err := a.wallet.AccountMgr.Create(ins.RootXPubs, ins.Quorum, ins.Alias)
23         if err != nil {
24                 return NewErrorResponse(err)
25         }
26
27         annotatedAccount := account.Annotated(acc)
28         log.WithField("account ID", annotatedAccount.ID).Info("Created account")
29
30         return NewSuccessResponse(annotatedAccount)
31 }
32
33 // AccountInfo is request struct for deleteAccount
34 type AccountInfo struct {
35         Info string `json:"account_info"`
36 }
37
38 // POST /delete-account
39 func (a *API) deleteAccount(ctx context.Context, in AccountInfo) Response {
40         if err := a.wallet.AccountMgr.DeleteAccount(in.Info); err != nil {
41                 return NewErrorResponse(err)
42         }
43         return NewSuccessResponse(nil)
44 }
45
46 type validateAddressResp struct {
47         Valid   bool `json:"valid"`
48         IsLocal bool `json:"is_local"`
49 }
50
51 // POST /validate-address
52 func (a *API) validateAddress(ctx context.Context, ins struct {
53         Address string `json:"address"`
54 }) Response {
55         resp := &validateAddressResp{
56                 Valid:   false,
57                 IsLocal: false,
58         }
59         address, err := common.DecodeAddress(ins.Address, &consensus.ActiveNetParams)
60         if err != nil {
61                 return NewSuccessResponse(resp)
62         }
63
64         redeemContract := address.ScriptAddress()
65         program := []byte{}
66         switch address.(type) {
67         case *common.AddressWitnessPubKeyHash:
68                 program, err = vmutil.P2WPKHProgram(redeemContract)
69         case *common.AddressWitnessScriptHash:
70                 program, err = vmutil.P2WSHProgram(redeemContract)
71         default:
72                 return NewSuccessResponse(resp)
73         }
74         if err != nil {
75                 return NewSuccessResponse(resp)
76         }
77
78         resp.Valid = true
79         resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
80         return NewSuccessResponse(resp)
81 }
82
83 type addressResp struct {
84         AccountAlias string `json:"account_alias"`
85         AccountID    string `json:"account_id"`
86         Address      string `json:"address"`
87         Change       bool   `json:"change"`
88         KeyIndex     uint64 `json:"-"`
89 }
90
91 // SortByIndex implements sort.Interface for addressResp slices
92 type SortByIndex []addressResp
93
94 func (a SortByIndex) Len() int           { return len(a) }
95 func (a SortByIndex) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
96 func (a SortByIndex) Less(i, j int) bool { return a[i].KeyIndex < a[j].KeyIndex }
97
98 func (a *API) listAddresses(ctx context.Context, ins struct {
99         AccountID    string `json:"account_id"`
100         AccountAlias string `json:"account_alias"`
101 }) Response {
102         accountID := ins.AccountID
103         var target *account.Account
104         if ins.AccountAlias != "" {
105                 acc, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
106                 if err != nil {
107                         return NewErrorResponse(err)
108                 }
109                 target = acc
110         } else {
111                 acc, err := a.wallet.AccountMgr.FindByID(accountID)
112                 if err != nil {
113                         return NewErrorResponse(err)
114                 }
115                 target = acc
116         }
117
118         cps, err := a.wallet.AccountMgr.ListControlProgram()
119         if err != nil {
120                 return NewErrorResponse(err)
121         }
122
123         addresses := []addressResp{}
124         for _, cp := range cps {
125                 if cp.Address == "" || cp.AccountID != target.ID {
126                         continue
127                 }
128                 addresses = append(addresses, addressResp{
129                         AccountAlias: target.Alias,
130                         AccountID:    cp.AccountID,
131                         Address:      cp.Address,
132                         Change:       cp.Change,
133                         KeyIndex:     cp.KeyIndex,
134                 })
135         }
136
137         // sort AddressResp by KeyIndex
138         sort.Sort(SortByIndex(addresses))
139         return NewSuccessResponse(addresses)
140 }