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)
31 // POST /delete-account
32 func (a *API) deleteAccount(ctx context.Context, in struct {
33 AccountInfo string `json:"account_info"`
35 if err := a.wallet.AccountMgr.DeleteAccount(in); err != nil {
36 return NewErrorResponse(err)
38 return NewSuccessResponse(nil)
41 type validateAddressResp struct {
42 Vaild bool `json:"vaild"`
43 IsLocal bool `json:"is_local"`
46 // POST /validate-address
47 func (a *API) validateAddress(ctx context.Context, ins struct {
48 Address string `json:"address"`
50 resp := &validateAddressResp{
54 address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams)
56 return NewSuccessResponse(resp)
59 redeemContract := address.ScriptAddress()
61 switch address.(type) {
62 case *common.AddressWitnessPubKeyHash:
63 program, err = vmutil.P2WPKHProgram(redeemContract)
64 case *common.AddressWitnessScriptHash:
65 program, err = vmutil.P2WSHProgram(redeemContract)
67 return NewSuccessResponse(resp)
70 return NewSuccessResponse(resp)
74 resp.IsLocal = a.wallet.AccountMgr.IsLocalControlProgram(program)
75 return NewSuccessResponse(resp)
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"`
85 func (a *API) listAddresses(ctx context.Context, ins struct {
86 AccountID string `json:"account_id"`
87 AccountAlias string `json:"account_alias"`
89 accountID := ins.AccountID
90 if ins.AccountAlias != "" {
91 acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
93 return NewErrorResponse(err)
99 cps, err := a.wallet.AccountMgr.ListControlProgram()
101 return NewErrorResponse(err)
104 var addresses []*addressResp
105 for _, cp := range cps {
106 if cp.Address == "" || (accountID != "" && accountID != cp.AccountID) {
110 accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
111 addresses = append(addresses, &addressResp{
112 AccountAlias: accountAlias,
113 AccountID: cp.AccountID,
118 return NewSuccessResponse(addresses)