OSDN Git Service

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