github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/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  	"strings"
    22  
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/accounts"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/common/hexutil"
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  )
    30  
    31  type Accounts []Account
    32  
    33  func (as Accounts) String() string {
    34  	var output []string
    35  	for _, a := range as {
    36  		output = append(output, a.String())
    37  	}
    38  	return strings.Join(output, "\n")
    39  }
    40  
    41  type Account struct {
    42  	Typ     string         `json:"type"`
    43  	URL     accounts.URL   `json:"url"`
    44  	Address common.Address `json:"address"`
    45  }
    46  
    47  func (a Account) String() string {
    48  	s, err := json.Marshal(a)
    49  	if err == nil {
    50  		return string(s)
    51  	}
    52  	return err.Error()
    53  }
    54  
    55  type ValidationInfo struct {
    56  	Typ     string `json:"type"`
    57  	Message string `json:"message"`
    58  }
    59  type ValidationMessages struct {
    60  	Messages []ValidationInfo
    61  }
    62  
    63  // SendTxArgs represents the arguments to submit a transaction
    64  type SendTxArgs struct {
    65  	From     common.MixedcaseAddress  `json:"from"`
    66  	To       *common.MixedcaseAddress `json:"to"`
    67  	Gas      hexutil.Uint64           `json:"gas"`
    68  	GasPrice hexutil.Big              `json:"gasPrice"`
    69  	Value    hexutil.Big              `json:"value"`
    70  	Nonce    hexutil.Uint64           `json:"nonce"`
    71  	// We accept "data" and "input" for backwards-compatibility reasons.
    72  	Data  *hexutil.Bytes `json:"data"`
    73  	Input *hexutil.Bytes `json:"input"`
    74  }
    75  
    76  func (args SendTxArgs) String() string {
    77  	s, err := json.Marshal(args)
    78  	if err == nil {
    79  		return string(s)
    80  	}
    81  	return err.Error()
    82  }
    83  
    84  func (args *SendTxArgs) toTransaction() *types.Transaction {
    85  	var input []byte
    86  	if args.Data != nil {
    87  		input = *args.Data
    88  	} else if args.Input != nil {
    89  		input = *args.Input
    90  	}
    91  	if args.To == nil {
    92  		return types.NewContractCreation(uint64(args.Nonce), (*big.Int)(&args.Value), uint64(args.Gas), (*big.Int)(&args.GasPrice), input)
    93  	}
    94  	return types.NewTransaction(uint64(args.Nonce), args.To.Address(), (*big.Int)(&args.Value), (uint64)(args.Gas), (*big.Int)(&args.GasPrice), input)
    95  }