6 log "github.com/sirupsen/logrus"
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"
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"`
21 acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias)
23 return NewErrorResponse(err)
26 annotatedAccount := account.Annotated(acc)
27 log.WithField("account ID", annotatedAccount.ID).Info("Created account")
29 return NewSuccessResponse(annotatedAccount)
33 type AccountInfo struct {
34 Info string `json:"account_info"`
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)
42 return NewSuccessResponse(nil)
45 type validateAddressResp struct {
46 Valid bool `json:"valid"`
47 IsLocal bool `json:"is_local"`
50 // POST /validate-address
51 func (a *API) validateAddress(ctx context.Context, ins struct {
52 Address string `json:"address"`
54 resp := &validateAddressResp{
58 address, err := common.DecodeAddress(ins.Address, &consensus.ActiveNetParams)
60 return NewSuccessResponse(resp)
63 redeemContract := address.ScriptAddress()
65 switch address.(type) {
66 case *common.AddressWitnessPubKeyHash:
67 program, err = vmutil.P2WPKHProgram(redeemContract)
68 case *common.AddressWitnessScriptHash:
69 program, err = vmutil.P2WSHProgram(redeemContract)
71 return NewSuccessResponse(resp)
74 return NewSuccessResponse(resp)
78 resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
79 return NewSuccessResponse(resp)
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"`
89 func (a *API) listAddresses(ctx context.Context, ins struct {
90 AccountID string `json:"account_id"`
91 AccountAlias string `json:"account_alias"`
93 accountID := ins.AccountID
94 var target *account.Account
95 if ins.AccountAlias != "" {
96 acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
98 return NewErrorResponse(err)
102 acc, err := a.wallet.AccountMgr.FindByID(ctx, accountID)
104 return NewErrorResponse(err)
109 cps, err := a.wallet.AccountMgr.ListControlProgram()
111 return NewErrorResponse(err)
114 addresses := []*addressResp{}
115 for _, cp := range cps {
116 if cp.Address == "" || cp.AccountID != target.ID {
119 addresses = append(addresses, &addressResp{
120 AccountAlias: target.Alias,
121 AccountID: cp.AccountID,
126 return NewSuccessResponse(addresses)