github.com/xxRanger/go-ethereum@v1.8.23/signer/core/types.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"math/big"
    25  
    26  	"github.com/ethereum/go-ethereum/accounts"
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/common/hexutil"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  )
    31  
    32  type Accounts []Account
    33  
    34  func (as Accounts) String() string {
    35  	var output []string
    36  	for _, a := range as {
    37  		output = append(output, a.String())
    38  	}
    39  	return strings.Join(output, "\n")
    40  }
    41  
    42  type Account struct {
    43  	Typ     string         `json:"type"`
    44  	URL     accounts.URL   `json:"url"`
    45  	Address common.Address `json:"address"`
    46  }
    47  
    48  func (a Account) String() string {
    49  	s, err := json.Marshal(a)
    50  	if err == nil {
    51  		return string(s)
    52  	}
    53  	return err.Error()
    54  }
    55  
    56  type ValidationInfo struct {
    57  	Typ     string `json:"type"`
    58  	Message string `json:"message"`
    59  }
    60  type ValidationMessages struct {
    61  	Messages []ValidationInfo
    62  }
    63  
    64  const (
    65  	WARN = "WARNING"
    66  	CRIT = "CRITICAL"
    67  	INFO = "Info"
    68  )
    69  
    70  func (vs *ValidationMessages) crit(msg string) {
    71  	vs.Messages = append(vs.Messages, ValidationInfo{CRIT, msg})
    72  }
    73  func (vs *ValidationMessages) warn(msg string) {
    74  	vs.Messages = append(vs.Messages, ValidationInfo{WARN, msg})
    75  }
    76  func (vs *ValidationMessages) info(msg string) {
    77  	vs.Messages = append(vs.Messages, ValidationInfo{INFO, msg})
    78  }
    79  
    80  /// getWarnings returns an error with all messages of type WARN of above, or nil if no warnings were present
    81  func (v *ValidationMessages) getWarnings() error {
    82  	var messages []string
    83  	for _, msg := range v.Messages {
    84  		if msg.Typ == WARN || msg.Typ == CRIT {
    85  			messages = append(messages, msg.Message)
    86  		}
    87  	}
    88  	if len(messages) > 0 {
    89  		return fmt.Errorf("Validation failed: %s", strings.Join(messages, ","))
    90  	}
    91  	return nil
    92  }
    93  
    94  // SendTxArgs represents the arguments to submit a transaction
    95  type SendTxArgs struct {
    96  	From     common.MixedcaseAddress  `json:"from"`
    97  	To       *common.MixedcaseAddress `json:"to"`
    98  	Gas      hexutil.Uint64           `json:"gas"`
    99  	GasPrice hexutil.Big              `json:"gasPrice"`
   100  	Value    hexutil.Big              `json:"value"`
   101  	Nonce    hexutil.Uint64           `json:"nonce"`
   102  	// We accept "data" and "input" for backwards-compatibility reasons.
   103  	Data  *hexutil.Bytes `json:"data"`
   104  	Input *hexutil.Bytes `json:"input"`
   105  }
   106  
   107  func (args SendTxArgs) String() string {
   108  	s, err := json.Marshal(args)
   109  	if err == nil {
   110  		return string(s)
   111  	}
   112  	return err.Error()
   113  }
   114  
   115  func (args *SendTxArgs) toTransaction() *types.Transaction {
   116  	var input []byte
   117  	if args.Data != nil {
   118  		input = *args.Data
   119  	} else if args.Input != nil {
   120  		input = *args.Input
   121  	}
   122  	if args.To == nil {
   123  		return types.NewContractCreation(uint64(args.Nonce), (*big.Int)(&args.Value), uint64(args.Gas), (*big.Int)(&args.GasPrice), input)
   124  	}
   125  	return types.NewTransaction(uint64(args.Nonce), args.To.Address(), (*big.Int)(&args.Value), (uint64)(args.Gas), (*big.Int)(&args.GasPrice), input)
   126  }