7 log "github.com/sirupsen/logrus"
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"
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"`
22 acc, err := a.wallet.AccountMgr.Create(ins.RootXPubs, ins.Quorum, ins.Alias)
24 return NewErrorResponse(err)
27 annotatedAccount := account.Annotated(acc)
28 log.WithField("account ID", annotatedAccount.ID).Info("Created account")
30 return NewSuccessResponse(annotatedAccount)
33 // AccountInfo is request struct for deleteAccount
34 type AccountInfo struct {
35 Info string `json:"account_info"`
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)
43 return NewSuccessResponse(nil)
46 type validateAddressResp struct {
47 Valid bool `json:"valid"`
48 IsLocal bool `json:"is_local"`
51 // POST /validate-address
52 func (a *API) validateAddress(ctx context.Context, ins struct {
53 Address string `json:"address"`
55 resp := &validateAddressResp{
59 address, err := common.DecodeAddress(ins.Address, &consensus.ActiveNetParams)
61 return NewSuccessResponse(resp)
64 redeemContract := address.ScriptAddress()
66 switch address.(type) {
67 case *common.AddressWitnessPubKeyHash:
68 program, err = vmutil.P2WPKHProgram(redeemContract)
69 case *common.AddressWitnessScriptHash:
70 program, err = vmutil.P2WSHProgram(redeemContract)
72 return NewSuccessResponse(resp)
75 return NewSuccessResponse(resp)
79 resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
80 return NewSuccessResponse(resp)
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:"-"`
91 // SortByIndex implements sort.Interface for addressResp slices
92 type SortByIndex []addressResp
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 }
98 func (a *API) listAddresses(ctx context.Context, ins struct {
99 AccountID string `json:"account_id"`
100 AccountAlias string `json:"account_alias"`
102 accountID := ins.AccountID
103 var target *account.Account
104 if ins.AccountAlias != "" {
105 acc, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
107 return NewErrorResponse(err)
111 acc, err := a.wallet.AccountMgr.FindByID(accountID)
113 return NewErrorResponse(err)
118 cps, err := a.wallet.AccountMgr.ListControlProgram()
120 return NewErrorResponse(err)
123 addresses := []addressResp{}
124 for _, cp := range cps {
125 if cp.Address == "" || cp.AccountID != target.ID {
128 addresses = append(addresses, addressResp{
129 AccountAlias: target.Alias,
130 AccountID: cp.AccountID,
133 KeyIndex: cp.KeyIndex,
137 // sort AddressResp by KeyIndex
138 sort.Sort(SortByIndex(addresses))
139 return NewSuccessResponse(addresses)