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"`
21 Tags map[string]interface{} `json:"tags"`
23 acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias, ins.Tags)
25 return NewErrorResponse(err)
28 annotatedAccount, err := account.Annotated(acc)
30 return NewErrorResponse(err)
33 log.WithField("account ID", annotatedAccount.ID).Info("Created account")
35 return NewSuccessResponse(annotatedAccount)
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"`
43 err := a.wallet.AccountMgr.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags)
45 return NewErrorResponse(err)
48 return NewSuccessResponse(nil)
52 // POST /delete-account
53 func (a *API) deleteAccount(ctx context.Context, in struct {
54 AccountInfo string `json:"account_info"`
56 if err := a.wallet.AccountMgr.DeleteAccount(in); err != nil {
57 return NewErrorResponse(err)
59 return NewSuccessResponse(nil)
62 type validateAddressResp struct {
63 Vaild bool `json:"vaild"`
64 IsLocal bool `json:"is_local"`
67 // POST /validate-address
68 func (a *API) validateAddress(ctx context.Context, ins struct {
69 Address string `json:"address"`
71 resp := &validateAddressResp{
75 address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
77 return NewSuccessResponse(resp)
80 redeemContract := address.ScriptAddress()
82 switch address.(type) {
83 case *common.AddressWitnessPubKeyHash:
84 program, err = vmutil.P2WPKHProgram(redeemContract)
85 case *common.AddressWitnessScriptHash:
86 program, err = vmutil.P2WSHProgram(redeemContract)
88 return NewSuccessResponse(resp)
91 return NewSuccessResponse(resp)
95 resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
96 return NewSuccessResponse(resp)
99 type addressResp struct {
100 AccountAlias string `json:"account_alias"`
101 AccountID string `json:"account_id"`
102 Address string `json:"address"`
105 func (a *API) listAddresses(ctx context.Context, ins struct {
106 AccountID string `json:"account_id"`
107 AccountAlias string `json:"account_alias"`
109 accountID := ins.AccountID
110 if ins.AccountAlias != "" {
111 account, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
113 return NewErrorResponse(err)
116 accountID = account.ID
119 cps, err := a.wallet.AccountMgr.ListControlProgram()
121 return NewErrorResponse(err)
124 addresses := []*addressResp{}
125 for _, cp := range cps {
126 if cp.Address == "" || (len(accountID) != 0 && strings.Compare(accountID, cp.AccountID) != 0) {
130 accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
131 addresses = append(addresses, &addressResp{AccountAlias: accountAlias, AccountID: cp.AccountID, Address: cp.Address})
133 return NewSuccessResponse(addresses)