OSDN Git Service

extract api from blockchain (#478)
[bytom/bytom.git] / api / accounts.go
1 package api
2
3 import (
4         "context"
5         "strings"
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         Tags      map[string]interface{} `json:"tags"`
22 }) Response {
23         acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias, ins.Tags)
24         if err != nil {
25                 return NewErrorResponse(err)
26         }
27
28         annotatedAccount, err := account.Annotated(acc)
29         if err != nil {
30                 return NewErrorResponse(err)
31         }
32
33         log.WithField("account ID", annotatedAccount.ID).Info("Created account")
34
35         return NewSuccessResponse(annotatedAccount)
36 }
37
38 // POST /update-account-tags
39 func (a *API) updateAccountTags(ctx context.Context, updateTag struct {
40         AccountInfo string                 `json:"account_info"`
41         Tags        map[string]interface{} `json:"tags"`
42 }) Response {
43         err := a.wallet.AccountMgr.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags)
44         if err != nil {
45                 return NewErrorResponse(err)
46         }
47
48         return NewSuccessResponse(nil)
49 }
50
51 //
52 // POST /delete-account
53 func (a *API) deleteAccount(ctx context.Context, in struct {
54         AccountInfo string `json:"account_info"`
55 }) Response {
56         if err := a.wallet.AccountMgr.DeleteAccount(in); err != nil {
57                 return NewErrorResponse(err)
58         }
59         return NewSuccessResponse(nil)
60 }
61
62 type validateAddressResp struct {
63         Vaild   bool `json:"vaild"`
64         IsLocal bool `json:"is_local"`
65 }
66
67 // POST /validate-address
68 func (a *API) validateAddress(ctx context.Context, ins struct {
69         Address string `json:"address"`
70 }) Response {
71         resp := &validateAddressResp{
72                 Vaild:   false,
73                 IsLocal: false,
74         }
75         address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
76         if err != nil {
77                 return NewSuccessResponse(resp)
78         }
79
80         redeemContract := address.ScriptAddress()
81         program := []byte{}
82         switch address.(type) {
83         case *common.AddressWitnessPubKeyHash:
84                 program, err = vmutil.P2WPKHProgram(redeemContract)
85         case *common.AddressWitnessScriptHash:
86                 program, err = vmutil.P2WSHProgram(redeemContract)
87         default:
88                 return NewSuccessResponse(resp)
89         }
90         if err != nil {
91                 return NewSuccessResponse(resp)
92         }
93
94         resp.Vaild = true
95         resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
96         return NewSuccessResponse(resp)
97 }
98
99 type addressResp struct {
100         AccountAlias string `json:"account_alias"`
101         AccountID    string `json:"account_id"`
102         Address      string `json:"address"`
103 }
104
105 func (a *API) listAddresses(ctx context.Context, ins struct {
106         AccountID    string `json:"account_id"`
107         AccountAlias string `json:"account_alias"`
108 }) Response {
109         accountID := ins.AccountID
110         if ins.AccountAlias != "" {
111                 account, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
112                 if err != nil {
113                         return NewErrorResponse(err)
114                 }
115
116                 accountID = account.ID
117         }
118
119         cps, err := a.wallet.AccountMgr.ListControlProgram()
120         if err != nil {
121                 return NewErrorResponse(err)
122         }
123
124         addresses := []*addressResp{}
125         for _, cp := range cps {
126                 if cp.Address == "" || (len(accountID) != 0 && strings.Compare(accountID, cp.AccountID) != 0) {
127                         continue
128                 }
129
130                 accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
131                 addresses = append(addresses, &addressResp{AccountAlias: accountAlias, AccountID: cp.AccountID, Address: cp.Address})
132         }
133         return NewSuccessResponse(addresses)
134 }