OSDN Git Service

api add createAccount and createKey (#393)
[bytom/vapor.git] / api / accounts.go
index a0d551a..b75aa51 100644 (file)
@@ -15,12 +15,14 @@ import (
        "github.com/vapor/protocol/vm/vmutil"
 )
 
-// POST /create-account
-func (a *API) createAccount(ctx context.Context, ins struct {
+type CreateAccountReq struct {
        RootXPubs []chainkd.XPub `json:"root_xpubs"`
        Quorum    int            `json:"quorum"`
        Alias     string         `json:"alias"`
-}) Response {
+}
+
+// POST /create-account
+func (a *API) createAccount(ctx context.Context, ins CreateAccountReq) Response {
        acc, err := a.wallet.AccountMgr.Create(ins.RootXPubs, ins.Quorum, ins.Alias, signers.BIP0044)
        if err != nil {
                return NewErrorResponse(err)
@@ -58,10 +60,7 @@ type AccountInfo struct {
 }
 
 // POST /delete-account
-func (a *API) deleteAccount(ctx context.Context, filter struct {
-       AccountID    string `json:"account_id"`
-       AccountAlias string `json:"account_alias"`
-}) Response {
+func (a *API) deleteAccount(ctx context.Context, filter AccountFilter) Response {
        accountID := filter.AccountID
        if filter.AccountAlias != "" {
                acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
@@ -114,7 +113,14 @@ func (a *API) validateAddress(ctx context.Context, ins struct {
        return NewSuccessResponse(resp)
 }
 
-type addressResp struct {
+type AddressReq struct {
+       AccountID    string `json:"account_id"`
+       AccountAlias string `json:"account_alias"`
+       From         uint   `json:"from"`
+       Count        uint   `json:"count"`
+}
+
+type AddressResp struct {
        AccountAlias   string `json:"account_alias"`
        AccountID      string `json:"account_id"`
        Address        string `json:"address"`
@@ -124,18 +130,13 @@ type addressResp struct {
 }
 
 // SortByIndex implements sort.Interface for addressResp slices
-type SortByIndex []addressResp
+type SortByIndex []AddressResp
 
 func (a SortByIndex) Len() int           { return len(a) }
 func (a SortByIndex) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
 func (a SortByIndex) Less(i, j int) bool { return a[i].KeyIndex < a[j].KeyIndex }
 
-func (a *API) listAddresses(ctx context.Context, ins struct {
-       AccountID    string `json:"account_id"`
-       AccountAlias string `json:"account_alias"`
-       From         uint   `json:"from"`
-       Count        uint   `json:"count"`
-}) Response {
+func (a *API) listAddresses(ctx context.Context, ins AddressReq) Response {
        accountID := ins.AccountID
        var target *account.Account
        if ins.AccountAlias != "" {
@@ -157,12 +158,12 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
                return NewErrorResponse(err)
        }
 
-       addresses := []addressResp{}
+       addresses := []AddressResp{}
        for _, cp := range cps {
                if cp.Address == "" || cp.AccountID != target.ID {
                        continue
                }
-               addresses = append(addresses, addressResp{
+               addresses = append(addresses, AddressResp{
                        AccountAlias:   target.Alias,
                        AccountID:      cp.AccountID,
                        Address:        cp.Address,