github.com/turingchain2020/turingchain@v1.1.21/wallet/wallet_msg.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wallet
     6  
     7  import (
     8  	"github.com/turingchain2020/turingchain/queue"
     9  	"github.com/turingchain2020/turingchain/types"
    10  	wcom "github.com/turingchain2020/turingchain/wallet/common"
    11  )
    12  
    13  // ProcRecvMsg 处理消息循环
    14  func (wallet *Wallet) ProcRecvMsg() {
    15  	defer wallet.wg.Done()
    16  	for msg := range wallet.client.Recv() {
    17  		id := msg.ID
    18  		walletlog.Debug("wallet recv", "msg", types.GetEventName(int(id)), "Id", id)
    19  		beg := types.Now()
    20  		reply, err := wallet.ExecWallet(msg)
    21  		if err != nil {
    22  			//only for test ,del when test end
    23  			msg.Reply(wallet.api.NewMessage("", 0, err))
    24  		} else {
    25  			msg.Reply(wallet.api.NewMessage("", 0, reply))
    26  		}
    27  		walletlog.Debug("end process", "msg.id", id, "cost", types.Since(beg))
    28  	}
    29  }
    30  
    31  // On_WalletGetAccountList 响应获取账户列表
    32  func (wallet *Wallet) On_WalletGetAccountList(req *types.ReqAccountList) (types.Message, error) {
    33  	reply, err := wallet.ProcGetAccountList(req)
    34  	if err != nil {
    35  		walletlog.Error("onWalletGetAccountList", "err", err.Error())
    36  	}
    37  	return reply, err
    38  }
    39  
    40  // On_NewAccount 响应新建账号
    41  func (wallet *Wallet) On_NewAccount(req *types.ReqNewAccount) (types.Message, error) {
    42  	reply, err := wallet.ProcCreateNewAccount(req)
    43  	if err != nil {
    44  		walletlog.Error("onNewAccount", "err", err.Error())
    45  	}
    46  	return reply, err
    47  }
    48  
    49  // On_WalletTransactionList 响应获取钱包交易列表
    50  func (wallet *Wallet) On_WalletTransactionList(req *types.ReqWalletTransactionList) (types.Message, error) {
    51  	reply, err := wallet.ProcWalletTxList(req)
    52  	if err != nil {
    53  		walletlog.Error("ProcWalletTxList", "err", err.Error())
    54  	}
    55  	return reply, err
    56  }
    57  
    58  // On_WalletImportPrivkey 响应导入私钥
    59  func (wallet *Wallet) On_WalletImportPrivkey(req *types.ReqWalletImportPrivkey) (types.Message, error) {
    60  	reply, err := wallet.ProcImportPrivKey(req)
    61  	if err != nil {
    62  		walletlog.Error("ProcImportPrivKey", "err", err.Error())
    63  	}
    64  	return reply, err
    65  }
    66  
    67  // On_WalletSendToAddress 响应钱包想地址转账
    68  func (wallet *Wallet) On_WalletSendToAddress(req *types.ReqWalletSendToAddress) (types.Message, error) {
    69  	reply, err := wallet.ProcSendToAddress(req)
    70  	if err != nil {
    71  		walletlog.Error("ProcSendToAddress", "err", err.Error())
    72  	}
    73  	return reply, err
    74  }
    75  
    76  // On_WalletSetFee 响应设置钱包手续费
    77  func (wallet *Wallet) On_WalletSetFee(req *types.ReqWalletSetFee) (types.Message, error) {
    78  	reply := &types.Reply{
    79  		IsOk: true,
    80  	}
    81  	err := wallet.ProcWalletSetFee(req)
    82  	if err != nil {
    83  		walletlog.Error("ProcWalletSetFee", "err", err.Error())
    84  		reply.IsOk = false
    85  		reply.Msg = []byte(err.Error())
    86  	}
    87  	return reply, err
    88  }
    89  
    90  // On_WalletSetLabel 处理钱包设置标签
    91  func (wallet *Wallet) On_WalletSetLabel(req *types.ReqWalletSetLabel) (types.Message, error) {
    92  	reply, err := wallet.ProcWalletSetLabel(req)
    93  	if err != nil {
    94  		walletlog.Error("ProcWalletSetLabel", "err", err.Error())
    95  	}
    96  	return reply, err
    97  }
    98  
    99  // On_WalletGetAccount 通过账户标签获取账户地址
   100  func (wallet *Wallet) On_WalletGetAccount(req *types.ReqGetAccount) (types.Message, error) {
   101  	reply, err := wallet.ProcGetAccount(req)
   102  	if err != nil {
   103  		walletlog.Error("On_WalletGetAccount", "err", err.Error())
   104  	}
   105  	return reply, err
   106  }
   107  
   108  // On_WalletMergeBalance 响应钱包合并金额
   109  func (wallet *Wallet) On_WalletMergeBalance(req *types.ReqWalletMergeBalance) (types.Message, error) {
   110  	reply, err := wallet.ProcMergeBalance(req)
   111  	if err != nil {
   112  		walletlog.Error("ProcMergeBalance", "err", err.Error())
   113  	}
   114  	return reply, err
   115  }
   116  
   117  // On_WalletSetPasswd 处理钱包设置密码
   118  func (wallet *Wallet) On_WalletSetPasswd(req *types.ReqWalletSetPasswd) (types.Message, error) {
   119  	reply := &types.Reply{
   120  		IsOk: true,
   121  	}
   122  	err := wallet.ProcWalletSetPasswd(req)
   123  	if err != nil {
   124  		walletlog.Error("ProcWalletSetPasswd", "err", err.Error())
   125  		reply.IsOk = false
   126  		reply.Msg = []byte(err.Error())
   127  	}
   128  	return reply, nil
   129  }
   130  
   131  // On_WalletLock 处理钱包加锁
   132  func (wallet *Wallet) On_WalletLock(req *types.ReqNil) (types.Message, error) {
   133  	reply := &types.Reply{
   134  		IsOk: true,
   135  	}
   136  	err := wallet.ProcWalletLock()
   137  	if err != nil {
   138  		walletlog.Error("ProcWalletLock", "err", err.Error())
   139  		reply.IsOk = false
   140  		reply.Msg = []byte(err.Error())
   141  	}
   142  	return reply, err
   143  }
   144  
   145  // On_WalletUnLock 处理钱包解锁
   146  func (wallet *Wallet) On_WalletUnLock(req *types.WalletUnLock) (types.Message, error) {
   147  	reply := &types.Reply{
   148  		IsOk: true,
   149  	}
   150  	err := wallet.ProcWalletUnLock(req)
   151  	if err != nil {
   152  		walletlog.Error("ProcWalletLock", "err", err.Error())
   153  		reply.IsOk = false
   154  		reply.Msg = []byte(err.Error())
   155  	}
   156  	return reply, nil
   157  }
   158  
   159  // On_AddBlock 处理新增区块
   160  func (wallet *Wallet) On_AddBlock(block *types.BlockDetail) (types.Message, error) {
   161  	err := wallet.updateLastHeader(block, 1)
   162  	if err != nil {
   163  		walletlog.Error("On_AddBlock updateLastHeader", "height", block.Block.Height, "err", err)
   164  	}
   165  	wallet.ProcWalletAddBlock(block)
   166  	return nil, nil
   167  }
   168  
   169  // On_DelBlock 处理删除区块
   170  func (wallet *Wallet) On_DelBlock(block *types.BlockDetail) (types.Message, error) {
   171  	err := wallet.updateLastHeader(block, -1)
   172  	if err != nil {
   173  		walletlog.Error("On_DelBlock updateLastHeader", "height", block.Block.Height, "err", err)
   174  	}
   175  	wallet.ProcWalletDelBlock(block)
   176  	return nil, nil
   177  }
   178  
   179  // On_GenSeed 处理创建SEED
   180  func (wallet *Wallet) On_GenSeed(req *types.GenSeedLang) (types.Message, error) {
   181  	reply, err := wallet.genSeed(req.Lang)
   182  	if err != nil {
   183  		walletlog.Error("genSeed", "err", err.Error())
   184  	}
   185  	return reply, err
   186  }
   187  
   188  // On_GetSeed 处理获取Seed
   189  func (wallet *Wallet) On_GetSeed(req *types.GetSeedByPw) (types.Message, error) {
   190  	reply := &types.ReplySeed{}
   191  	seed, err := wallet.GetSeed(req.Passwd)
   192  	if err != nil {
   193  		walletlog.Error("getSeed", "err", err.Error())
   194  	} else {
   195  		reply.Seed = seed
   196  	}
   197  	return reply, err
   198  }
   199  
   200  // On_SaveSeed 处理保存SEED
   201  func (wallet *Wallet) On_SaveSeed(req *types.SaveSeedByPw) (types.Message, error) {
   202  	reply := &types.Reply{
   203  		IsOk: true,
   204  	}
   205  	ok, err := wallet.saveSeed(req.Passwd, req.Seed)
   206  	if !ok {
   207  		walletlog.Error("[saveSeed]", "err", err.Error())
   208  		reply.IsOk = false
   209  		reply.Msg = []byte(err.Error())
   210  	}
   211  	return reply, nil
   212  }
   213  
   214  // On_GetWalletStatus 处理获取钱包状态
   215  func (wallet *Wallet) On_GetWalletStatus(req *types.ReqNil) (types.Message, error) {
   216  	reply := wallet.GetWalletStatus()
   217  	return reply, nil
   218  }
   219  
   220  // On_DumpPrivkey 处理到处私钥
   221  func (wallet *Wallet) On_DumpPrivkey(req *types.ReqString) (types.Message, error) {
   222  	reply := &types.ReplyString{}
   223  	privkey, err := wallet.ProcDumpPrivkey(req.Data)
   224  	if err != nil {
   225  		walletlog.Error("ProcDumpPrivkey", "err", err.Error())
   226  	} else {
   227  		reply.Data = privkey
   228  	}
   229  	return reply, err
   230  }
   231  
   232  // On_SignRawTx 处理交易签名
   233  func (wallet *Wallet) On_SignRawTx(req *types.ReqSignRawTx) (types.Message, error) {
   234  	reply := &types.ReplySignRawTx{}
   235  	txhex, err := wallet.ProcSignRawTx(req)
   236  	if err != nil {
   237  		walletlog.Error("ProcSignRawTx", "err", err.Error())
   238  	} else {
   239  		reply.TxHex = txhex
   240  	}
   241  	return reply, err
   242  }
   243  
   244  // On_ErrToFront 错误通知
   245  func (wallet *Wallet) On_ErrToFront(req *types.ReportErrEvent) (types.Message, error) {
   246  	wallet.setFatalFailure(req)
   247  	return nil, nil
   248  }
   249  
   250  // On_FatalFailure 定时查询是否有致命性故障产生
   251  func (wallet *Wallet) On_FatalFailure(req *types.ReqNil) (types.Message, error) {
   252  	reply := &types.Int32{
   253  		Data: wallet.getFatalFailure(),
   254  	}
   255  	return reply, nil
   256  }
   257  
   258  // ExecWallet 执行钱包的功能
   259  func (wallet *Wallet) ExecWallet(msg *queue.Message) (types.Message, error) {
   260  	if param, ok := msg.Data.(*types.ChainExecutor); ok {
   261  		return wallet.execWallet(param, 0)
   262  	}
   263  	var data []byte
   264  	if msg.Data != nil {
   265  		if d, ok := msg.Data.(types.Message); ok {
   266  			data = types.Encode(types.Clone(d))
   267  		} else {
   268  			return nil, types.ErrInvalidParam
   269  		}
   270  	}
   271  	param := &types.ChainExecutor{
   272  		Driver: "wallet",
   273  		Param:  data,
   274  	}
   275  	return wallet.execWallet(param, msg.Ty)
   276  }
   277  
   278  func (wallet *Wallet) execWallet(param *types.ChainExecutor, eventID int64) (reply types.Message, err error) {
   279  	if param.FuncName == "" && eventID > 0 {
   280  		param.FuncName = types.GetEventName(int(eventID))
   281  		if len(param.FuncName) <= 5 {
   282  			return nil, types.ErrActionNotSupport
   283  		}
   284  		param.FuncName = param.FuncName[5:]
   285  	}
   286  	var paramIn types.Message
   287  	if param.Param == nil {
   288  		paramIn = &types.ReqNil{}
   289  	} else {
   290  		paramIn, err = wcom.QueryData.Decode(param.Driver, param.FuncName, param.Param)
   291  		if err != nil {
   292  			return nil, err
   293  		}
   294  	}
   295  	//这里不判断类型是否可以调用,直接按照名字调用,如果发生panic,用recover 恢复
   296  	return wcom.QueryData.Call(param.Driver, param.FuncName, paramIn)
   297  }
   298  
   299  // On_NewAccountByIndex 获取指定index的私钥,返回私钥的hex字符串ReqString
   300  func (wallet *Wallet) On_NewAccountByIndex(req *types.Int32) (types.Message, error) {
   301  	reply, err := wallet.createNewAccountByIndex(uint32(req.Data))
   302  	if err != nil {
   303  		walletlog.Error("On_NewAccountByIndex", "err", err.Error())
   304  	}
   305  	return &types.ReplyString{Data: reply}, err
   306  }
   307  
   308  // On_DumpPrivkeysFile 处理到处私钥
   309  func (wallet *Wallet) On_DumpPrivkeysFile(req *types.ReqPrivkeysFile) (types.Message, error) {
   310  	reply := &types.Reply{
   311  		IsOk: true,
   312  	}
   313  	err := wallet.ProcDumpPrivkeysFile(req.FileName, req.Passwd)
   314  	if err != nil {
   315  		walletlog.Error("ProcDumpPrivkeysFile", "err", err.Error())
   316  		reply.IsOk = false
   317  		reply.Msg = []byte(err.Error())
   318  	}
   319  	return reply, err
   320  }
   321  
   322  //On_ImportPrivkeysFile 响应导入多个私钥
   323  func (wallet *Wallet) On_ImportPrivkeysFile(req *types.ReqPrivkeysFile) (types.Message, error) {
   324  	reply := &types.Reply{
   325  		IsOk: true,
   326  	}
   327  	err := wallet.ProcImportPrivkeysFile(req.FileName, req.Passwd)
   328  	if err != nil {
   329  		walletlog.Error("ProcImportPrivkeysFile", "err", err.Error())
   330  		reply.IsOk = false
   331  		reply.Msg = []byte(err.Error())
   332  	}
   333  	return reply, err
   334  }