github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/personal.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/eth"
    25  	"github.com/ethereum/go-ethereum/rpc/codec"
    26  	"github.com/ethereum/go-ethereum/rpc/shared"
    27  	"github.com/ethereum/go-ethereum/xeth"
    28  )
    29  
    30  const (
    31  	PersonalApiVersion = "1.0"
    32  )
    33  
    34  var (
    35  	// mapping between methods and handlers
    36  	personalMapping = map[string]personalhandler{
    37  		"personal_listAccounts":  (*personalApi).ListAccounts,
    38  		"personal_newAccount":    (*personalApi).NewAccount,
    39  		"personal_deleteAccount": (*personalApi).DeleteAccount,
    40  		"personal_unlockAccount": (*personalApi).UnlockAccount,
    41  	}
    42  )
    43  
    44  // net callback handler
    45  type personalhandler func(*personalApi, *shared.Request) (interface{}, error)
    46  
    47  // net api provider
    48  type personalApi struct {
    49  	xeth     *xeth.XEth
    50  	ethereum *eth.Ethereum
    51  	methods  map[string]personalhandler
    52  	codec    codec.ApiCoder
    53  }
    54  
    55  // create a new net api instance
    56  func NewPersonalApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *personalApi {
    57  	return &personalApi{
    58  		xeth:     xeth,
    59  		ethereum: eth,
    60  		methods:  personalMapping,
    61  		codec:    coder.New(nil),
    62  	}
    63  }
    64  
    65  // collection with supported methods
    66  func (self *personalApi) Methods() []string {
    67  	methods := make([]string, len(self.methods))
    68  	i := 0
    69  	for k := range self.methods {
    70  		methods[i] = k
    71  		i++
    72  	}
    73  	return methods
    74  }
    75  
    76  // Execute given request
    77  func (self *personalApi) Execute(req *shared.Request) (interface{}, error) {
    78  	if callback, ok := self.methods[req.Method]; ok {
    79  		return callback(self, req)
    80  	}
    81  
    82  	return nil, shared.NewNotImplementedError(req.Method)
    83  }
    84  
    85  func (self *personalApi) Name() string {
    86  	return shared.PersonalApiName
    87  }
    88  
    89  func (self *personalApi) ApiVersion() string {
    90  	return PersonalApiVersion
    91  }
    92  
    93  func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
    94  	return self.xeth.Accounts(), nil
    95  }
    96  
    97  func (self *personalApi) NewAccount(req *shared.Request) (interface{}, error) {
    98  	args := new(NewAccountArgs)
    99  	if err := self.codec.Decode(req.Params, &args); err != nil {
   100  		return nil, shared.NewDecodeParamError(err.Error())
   101  	}
   102  
   103  	am := self.ethereum.AccountManager()
   104  	acc, err := am.NewAccount(args.Passphrase)
   105  	return acc.Address.Hex(), err
   106  }
   107  
   108  func (self *personalApi) DeleteAccount(req *shared.Request) (interface{}, error) {
   109  	args := new(DeleteAccountArgs)
   110  	if err := self.codec.Decode(req.Params, &args); err != nil {
   111  		return nil, shared.NewDecodeParamError(err.Error())
   112  	}
   113  
   114  	addr := common.HexToAddress(args.Address)
   115  	am := self.ethereum.AccountManager()
   116  	if err := am.DeleteAccount(addr, args.Passphrase); err == nil {
   117  		return true, nil
   118  	} else {
   119  		return false, err
   120  	}
   121  }
   122  
   123  func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
   124  	args := new(UnlockAccountArgs)
   125  	if err := self.codec.Decode(req.Params, &args); err != nil {
   126  		return nil, shared.NewDecodeParamError(err.Error())
   127  	}
   128  
   129  	if len(args.Passphrase) == 0 {
   130  		fe := self.xeth.Frontend()
   131  		if fe == nil {
   132  			return false, fmt.Errorf("No password provided")
   133  		}
   134  		return fe.UnlockAccount(common.HexToAddress(args.Address).Bytes()), nil
   135  	}
   136  
   137  	am := self.ethereum.AccountManager()
   138  	addr := common.HexToAddress(args.Address)
   139  
   140  	err := am.TimedUnlock(addr, args.Passphrase, time.Duration(args.Duration)*time.Second)
   141  	return err == nil, err
   142  }