github.com/skoak/go-ethereum@v1.9.7/core/gen_genesis_account.go (about)

     1  // Code generated by github.com/fjl/gencodec. DO NOT EDIT.
     2  
     3  package core
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  	"math/big"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/common/hexutil"
    12  	"github.com/ethereum/go-ethereum/common/math"
    13  )
    14  
    15  var _ = (*genesisAccountMarshaling)(nil)
    16  
    17  func (g GenesisAccount) MarshalJSON() ([]byte, error) {
    18  	type GenesisAccount struct {
    19  		Code       hexutil.Bytes               `json:"code,omitempty"`
    20  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    21  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    22  		Nonce      math.HexOrDecimal64         `json:"nonce,omitempty"`
    23  		PrivateKey hexutil.Bytes               `json:"secretKey,omitempty"`
    24  	}
    25  	var enc GenesisAccount
    26  	enc.Code = g.Code
    27  	if g.Storage != nil {
    28  		enc.Storage = make(map[storageJSON]storageJSON, len(g.Storage))
    29  		for k, v := range g.Storage {
    30  			enc.Storage[storageJSON(k)] = storageJSON(v)
    31  		}
    32  	}
    33  	enc.Balance = (*math.HexOrDecimal256)(g.Balance)
    34  	enc.Nonce = math.HexOrDecimal64(g.Nonce)
    35  	enc.PrivateKey = g.PrivateKey
    36  	return json.Marshal(&enc)
    37  }
    38  
    39  func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
    40  	type GenesisAccount struct {
    41  		Code       *hexutil.Bytes              `json:"code,omitempty"`
    42  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    43  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    44  		Nonce      *math.HexOrDecimal64        `json:"nonce,omitempty"`
    45  		PrivateKey *hexutil.Bytes              `json:"secretKey,omitempty"`
    46  	}
    47  	var dec GenesisAccount
    48  	if err := json.Unmarshal(input, &dec); err != nil {
    49  		return err
    50  	}
    51  	if dec.Code != nil {
    52  		g.Code = *dec.Code
    53  	}
    54  	if dec.Storage != nil {
    55  		g.Storage = make(map[common.Hash]common.Hash, len(dec.Storage))
    56  		for k, v := range dec.Storage {
    57  			g.Storage[common.Hash(k)] = common.Hash(v)
    58  		}
    59  	}
    60  	if dec.Balance == nil {
    61  		return errors.New("missing required field 'balance' for GenesisAccount")
    62  	}
    63  	g.Balance = (*big.Int)(dec.Balance)
    64  	if dec.Nonce != nil {
    65  		g.Nonce = uint64(*dec.Nonce)
    66  	}
    67  	if dec.PrivateKey != nil {
    68  		g.PrivateKey = *dec.PrivateKey
    69  	}
    70  	return nil
    71  }