5 log "github.com/sirupsen/logrus"
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"
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"`
20 acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias)
22 return NewErrorResponse(err)
25 annotatedAccount := account.Annotated(acc)
26 log.WithField("account ID", annotatedAccount.ID).Info("Created account")
28 return NewSuccessResponse(annotatedAccount)
32 type AccountInfo struct {
33 Info string `json:"account_info"`
36 // POST /delete-account
37 func (a *API) deleteAccount(ctx context.Context, in AccountInfo) Response {
38 if err := a.wallet.AccountMgr.DeleteAccount(in.Info); err != nil {
39 return NewErrorResponse(err)
41 return NewSuccessResponse(nil)
44 type validateAddressResp struct {
45 Vaild bool `json:"vaild"`
46 IsLocal bool `json:"is_local"`
49 // POST /validate-address
50 func (a *API) validateAddress(ctx context.Context, ins struct {
51 Address string `json:"address"`
53 resp := &validateAddressResp{
57 address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
59 return NewSuccessResponse(resp)
62 redeemContract := address.ScriptAddress()
64 switch address.(type) {
65 case *common.AddressWitnessPubKeyHash:
66 program, err = vmutil.P2WPKHProgram(redeemContract)
67 case *common.AddressWitnessScriptHash:
68 program, err = vmutil.P2WSHProgram(redeemContract)
70 return NewSuccessResponse(resp)
73 return NewSuccessResponse(resp)
77 resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
78 return NewSuccessResponse(resp)
81 type addressResp struct {
82 AccountAlias string `json:"account_alias"`
83 AccountID string `json:"account_id"`
84 Address string `json:"address"`
85 Change bool `json:"change"`
88 func (a *API) listAddresses(ctx context.Context, ins struct {
89 AccountID string `json:"account_id"`
90 AccountAlias string `json:"account_alias"`
92 accountID := ins.AccountID
93 if ins.AccountAlias != "" {
94 acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
96 return NewErrorResponse(err)
102 cps, err := a.wallet.AccountMgr.ListControlProgram()
104 return NewErrorResponse(err)
107 var addresses []*addressResp
108 for _, cp := range cps {
109 if cp.Address == "" || (accountID != "" && accountID != cp.AccountID) {
113 accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
114 addresses = append(addresses, &addressResp{
115 AccountAlias: accountAlias,
116 AccountID: cp.AccountID,
121 return NewSuccessResponse(addresses)