github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/personal_args.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  	"encoding/json"
    21  
    22  	"github.com/ethereum/go-ethereum/rpc/shared"
    23  )
    24  
    25  type NewAccountArgs struct {
    26  	Passphrase string
    27  }
    28  
    29  func (args *NewAccountArgs) UnmarshalJSON(b []byte) (err error) {
    30  	var obj []interface{}
    31  	if err := json.Unmarshal(b, &obj); err != nil {
    32  		return shared.NewDecodeParamError(err.Error())
    33  	}
    34  
    35  	if len(obj) < 1 {
    36  		return shared.NewInsufficientParamsError(len(obj), 1)
    37  	}
    38  
    39  	if passhrase, ok := obj[0].(string); ok {
    40  		args.Passphrase = passhrase
    41  		return nil
    42  	}
    43  
    44  	return shared.NewInvalidTypeError("passhrase", "not a string")
    45  }
    46  
    47  type DeleteAccountArgs struct {
    48  	Address    string
    49  	Passphrase string
    50  }
    51  
    52  func (args *DeleteAccountArgs) UnmarshalJSON(b []byte) (err error) {
    53  	var obj []interface{}
    54  	if err := json.Unmarshal(b, &obj); err != nil {
    55  		return shared.NewDecodeParamError(err.Error())
    56  	}
    57  
    58  	if len(obj) < 2 {
    59  		return shared.NewInsufficientParamsError(len(obj), 2)
    60  	}
    61  
    62  	if addr, ok := obj[0].(string); ok {
    63  		args.Address = addr
    64  	} else {
    65  		return shared.NewInvalidTypeError("address", "not a string")
    66  	}
    67  
    68  	if passhrase, ok := obj[1].(string); ok {
    69  		args.Passphrase = passhrase
    70  	} else {
    71  		return shared.NewInvalidTypeError("passhrase", "not a string")
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  type UnlockAccountArgs struct {
    78  	Address    string
    79  	Passphrase string
    80  	Duration   int
    81  }
    82  
    83  func (args *UnlockAccountArgs) UnmarshalJSON(b []byte) (err error) {
    84  	var obj []interface{}
    85  	if err := json.Unmarshal(b, &obj); err != nil {
    86  		return shared.NewDecodeParamError(err.Error())
    87  	}
    88  
    89  	args.Duration = 0
    90  
    91  	if len(obj) < 1 {
    92  		return shared.NewInsufficientParamsError(len(obj), 1)
    93  	}
    94  
    95  	if addrstr, ok := obj[0].(string); ok {
    96  		args.Address = addrstr
    97  	} else {
    98  		return shared.NewInvalidTypeError("address", "not a string")
    99  	}
   100  
   101  	if len(obj) >= 2 && obj[1] != nil {
   102  		if passphrasestr, ok := obj[1].(string); ok {
   103  			args.Passphrase = passphrasestr
   104  		} else {
   105  			return shared.NewInvalidTypeError("passphrase", "not a string")
   106  		}
   107  	}
   108  
   109  	if len(obj) >= 3 && obj[2] != nil {
   110  		if duration, ok := obj[2].(float64); ok {
   111  			args.Duration = int(duration)
   112  		}
   113  	}
   114  
   115  	return nil
   116  }