OSDN Git Service

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