OSDN Git Service

Merge pull request #657 from Bytom/coinbase-tx-test
[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 // POST /delete-account
32 func (a *API) deleteAccount(ctx context.Context, in struct {
33         AccountInfo string `json:"account_info"`
34 }) Response {
35         if err := a.wallet.AccountMgr.DeleteAccount(in); err != nil {
36                 return NewErrorResponse(err)
37         }
38         return NewSuccessResponse(nil)
39 }
40
41 type validateAddressResp struct {
42         Vaild   bool `json:"vaild"`
43         IsLocal bool `json:"is_local"`
44 }
45
46 // POST /validate-address
47 func (a *API) validateAddress(ctx context.Context, ins struct {
48         Address string `json:"address"`
49 }) Response {
50         resp := &validateAddressResp{
51                 Vaild:   false,
52                 IsLocal: false,
53         }
54         address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
55         if err != nil {
56                 return NewSuccessResponse(resp)
57         }
58
59         redeemContract := address.ScriptAddress()
60         program := []byte{}
61         switch address.(type) {
62         case *common.AddressWitnessPubKeyHash:
63                 program, err = vmutil.P2WPKHProgram(redeemContract)
64         case *common.AddressWitnessScriptHash:
65                 program, err = vmutil.P2WSHProgram(redeemContract)
66         default:
67                 return NewSuccessResponse(resp)
68         }
69         if err != nil {
70                 return NewSuccessResponse(resp)
71         }
72
73         resp.Vaild = true
74         resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
75         return NewSuccessResponse(resp)
76 }
77
78 type addressResp struct {
79         AccountAlias string `json:"account_alias"`
80         AccountID    string `json:"account_id"`
81         Address      string `json:"address"`
82         Change       bool   `json:"change"`
83 }
84
85 func (a *API) listAddresses(ctx context.Context, ins struct {
86         AccountID    string `json:"account_id"`
87         AccountAlias string `json:"account_alias"`
88 }) Response {
89         accountID := ins.AccountID
90         if ins.AccountAlias != "" {
91                 acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
92                 if err != nil {
93                         return NewErrorResponse(err)
94                 }
95
96                 accountID = acc.ID
97         }
98
99         cps, err := a.wallet.AccountMgr.ListControlProgram()
100         if err != nil {
101                 return NewErrorResponse(err)
102         }
103
104         var addresses []*addressResp
105         for _, cp := range cps {
106                 if cp.Address == "" || (accountID != "" && accountID != cp.AccountID) {
107                         continue
108                 }
109
110                 accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
111                 addresses = append(addresses, &addressResp{
112                         AccountAlias: accountAlias,
113                         AccountID:    cp.AccountID,
114                         Address:      cp.Address,
115                         Change:       cp.Change,
116                 })
117         }
118         return NewSuccessResponse(addresses)
119 }