github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/api/wallet.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/bytom/bytom/account"
     7  	"github.com/bytom/bytom/asset"
     8  	"github.com/bytom/bytom/blockchain/pseudohsm"
     9  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    10  	"github.com/bytom/bytom/errors"
    11  )
    12  
    13  // POST /wallet error
    14  func (a *API) walletError() Response {
    15  	return NewErrorResponse(errors.New("wallet not found, please check that the wallet is open"))
    16  }
    17  
    18  // WalletImage hold the ziped wallet data
    19  type WalletImage struct {
    20  	AccountImage *account.Image      `json:"account_image"`
    21  	AssetImage   *asset.Image        `json:"asset_image"`
    22  	KeyImages    *pseudohsm.KeyImage `json:"key_images"`
    23  }
    24  
    25  func (a *API) restoreWalletImage(ctx context.Context, image WalletImage) Response {
    26  	if err := a.wallet.Hsm.Restore(image.KeyImages); err != nil {
    27  		return NewErrorResponse(errors.Wrap(err, "restore key images"))
    28  	}
    29  	if err := a.wallet.AssetReg.Restore(image.AssetImage); err != nil {
    30  		return NewErrorResponse(errors.Wrap(err, "restore asset image"))
    31  	}
    32  	if err := a.wallet.AccountMgr.Restore(image.AccountImage); err != nil {
    33  		return NewErrorResponse(errors.Wrap(err, "restore account image"))
    34  	}
    35  
    36  	var allAccounts []*account.Account
    37  	for _, acctImage := range image.AccountImage.Slice {
    38  		allAccounts = append(allAccounts, acctImage.Account)
    39  	}
    40  
    41  	if err := a.wallet.RecoveryMgr.AddrResurrect(allAccounts); err != nil {
    42  		return NewErrorResponse(err)
    43  	}
    44  
    45  	a.wallet.RescanBlocks()
    46  	return NewSuccessResponse(nil)
    47  }
    48  
    49  func (a *API) backupWalletImage() Response {
    50  	keyImages, err := a.wallet.Hsm.Backup()
    51  	if err != nil {
    52  		return NewErrorResponse(errors.Wrap(err, "backup key images"))
    53  	}
    54  	assetImage, err := a.wallet.AssetReg.Backup()
    55  	if err != nil {
    56  		return NewErrorResponse(errors.Wrap(err, "backup asset image"))
    57  	}
    58  	accountImage, err := a.wallet.AccountMgr.Backup()
    59  	if err != nil {
    60  		return NewErrorResponse(errors.Wrap(err, "backup account image"))
    61  	}
    62  
    63  	image := &WalletImage{
    64  		KeyImages:    keyImages,
    65  		AssetImage:   assetImage,
    66  		AccountImage: accountImage,
    67  	}
    68  	return NewSuccessResponse(image)
    69  }
    70  
    71  func (a *API) rescanWallet() Response {
    72  	allAccounts, err := a.wallet.AccountMgr.ListAccounts("")
    73  	if err != nil {
    74  		return NewErrorResponse(err)
    75  	}
    76  
    77  	if err := a.wallet.RecoveryMgr.AddrResurrect(allAccounts); err != nil {
    78  		return NewErrorResponse(err)
    79  	}
    80  
    81  	a.wallet.RescanBlocks()
    82  	return NewSuccessResponse(nil)
    83  }
    84  
    85  // WalletInfo return wallet information
    86  type WalletInfo struct {
    87  	BestBlockHeight uint64 `json:"best_block_height"`
    88  	WalletHeight    uint64 `json:"wallet_height"`
    89  }
    90  
    91  func (a *API) getWalletInfo() Response {
    92  	bestBlockHeight := a.chain.BestBlockHeight()
    93  	walletStatus := a.wallet.GetWalletStatusInfo()
    94  
    95  	return NewSuccessResponse(&WalletInfo{
    96  		BestBlockHeight: bestBlockHeight,
    97  		WalletHeight:    walletStatus.WorkHeight,
    98  	})
    99  }
   100  
   101  func (a *API) recoveryFromRootXPubs(ctx context.Context, in struct {
   102  	XPubs []chainkd.XPub `json:"xpubs"`
   103  }) Response {
   104  	if err := a.wallet.RecoveryMgr.AcctResurrect(in.XPubs); err != nil {
   105  		return NewErrorResponse(err)
   106  	}
   107  
   108  	a.wallet.RescanBlocks()
   109  	return NewSuccessResponse(nil)
   110  }