github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/gen_genesis_account.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:35</date>
    10  //</624450079025008640>
    11  
    12  //代码由github.com/fjl/gencodec生成。不要编辑。
    13  
    14  package core
    15  
    16  import (
    17  	"encoding/json"
    18  	"errors"
    19  	"math/big"
    20  
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/ethereum/go-ethereum/common/hexutil"
    23  	"github.com/ethereum/go-ethereum/common/math"
    24  )
    25  
    26  var _ = (*genesisAccountMarshaling)(nil)
    27  
    28  func (g GenesisAccount) MarshalJSON() ([]byte, error) {
    29  	type GenesisAccount struct {
    30  		Code       hexutil.Bytes               `json:"code,omitempty"`
    31  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    32  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    33  		Nonce      math.HexOrDecimal64         `json:"nonce,omitempty"`
    34  		PrivateKey hexutil.Bytes               `json:"secretKey,omitempty"`
    35  	}
    36  	var enc GenesisAccount
    37  	enc.Code = g.Code
    38  	if g.Storage != nil {
    39  		enc.Storage = make(map[storageJSON]storageJSON, len(g.Storage))
    40  		for k, v := range g.Storage {
    41  			enc.Storage[storageJSON(k)] = storageJSON(v)
    42  		}
    43  	}
    44  	enc.Balance = (*math.HexOrDecimal256)(g.Balance)
    45  	enc.Nonce = math.HexOrDecimal64(g.Nonce)
    46  	enc.PrivateKey = g.PrivateKey
    47  	return json.Marshal(&enc)
    48  }
    49  
    50  func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
    51  	type GenesisAccount struct {
    52  		Code       *hexutil.Bytes              `json:"code,omitempty"`
    53  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    54  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    55  		Nonce      *math.HexOrDecimal64        `json:"nonce,omitempty"`
    56  		PrivateKey *hexutil.Bytes              `json:"secretKey,omitempty"`
    57  	}
    58  	var dec GenesisAccount
    59  	if err := json.Unmarshal(input, &dec); err != nil {
    60  		return err
    61  	}
    62  	if dec.Code != nil {
    63  		g.Code = *dec.Code
    64  	}
    65  	if dec.Storage != nil {
    66  		g.Storage = make(map[common.Hash]common.Hash, len(dec.Storage))
    67  		for k, v := range dec.Storage {
    68  			g.Storage[common.Hash(k)] = common.Hash(v)
    69  		}
    70  	}
    71  	if dec.Balance == nil {
    72  		return errors.New("missing required field 'balance' for GenesisAccount")
    73  	}
    74  	g.Balance = (*big.Int)(dec.Balance)
    75  	if dec.Nonce != nil {
    76  		g.Nonce = uint64(*dec.Nonce)
    77  	}
    78  	if dec.PrivateKey != nil {
    79  		g.PrivateKey = *dec.PrivateKey
    80  	}
    81  	return nil
    82  }
    83