github.com/MetalBlockchain/metalgo@v1.11.9/genesis/unparsed_config.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package genesis
     5  
     6  import (
     7  	"encoding/hex"
     8  	"errors"
     9  
    10  	"github.com/MetalBlockchain/metalgo/ids"
    11  	"github.com/MetalBlockchain/metalgo/utils/formatting/address"
    12  	"github.com/MetalBlockchain/metalgo/vms/platformvm/signer"
    13  )
    14  
    15  var errInvalidETHAddress = errors.New("invalid eth address")
    16  
    17  type UnparsedAllocation struct {
    18  	ETHAddr        string         `json:"ethAddr"`
    19  	AVAXAddr       string         `json:"avaxAddr"`
    20  	InitialAmount  uint64         `json:"initialAmount"`
    21  	UnlockSchedule []LockedAmount `json:"unlockSchedule"`
    22  }
    23  
    24  func (ua UnparsedAllocation) Parse() (Allocation, error) {
    25  	a := Allocation{
    26  		InitialAmount:  ua.InitialAmount,
    27  		UnlockSchedule: ua.UnlockSchedule,
    28  	}
    29  
    30  	if len(ua.ETHAddr) < 2 {
    31  		return a, errInvalidETHAddress
    32  	}
    33  
    34  	ethAddrBytes, err := hex.DecodeString(ua.ETHAddr[2:])
    35  	if err != nil {
    36  		return a, err
    37  	}
    38  	ethAddr, err := ids.ToShortID(ethAddrBytes)
    39  	if err != nil {
    40  		return a, err
    41  	}
    42  	a.ETHAddr = ethAddr
    43  
    44  	_, _, avaxAddrBytes, err := address.Parse(ua.AVAXAddr)
    45  	if err != nil {
    46  		return a, err
    47  	}
    48  	avaxAddr, err := ids.ToShortID(avaxAddrBytes)
    49  	if err != nil {
    50  		return a, err
    51  	}
    52  	a.AVAXAddr = avaxAddr
    53  
    54  	return a, nil
    55  }
    56  
    57  type UnparsedStaker struct {
    58  	NodeID        ids.NodeID                `json:"nodeID"`
    59  	RewardAddress string                    `json:"rewardAddress"`
    60  	DelegationFee uint32                    `json:"delegationFee"`
    61  	Signer        *signer.ProofOfPossession `json:"signer,omitempty"`
    62  }
    63  
    64  func (us UnparsedStaker) Parse() (Staker, error) {
    65  	s := Staker{
    66  		NodeID:        us.NodeID,
    67  		DelegationFee: us.DelegationFee,
    68  		Signer:        us.Signer,
    69  	}
    70  
    71  	_, _, avaxAddrBytes, err := address.Parse(us.RewardAddress)
    72  	if err != nil {
    73  		return s, err
    74  	}
    75  	avaxAddr, err := ids.ToShortID(avaxAddrBytes)
    76  	if err != nil {
    77  		return s, err
    78  	}
    79  	s.RewardAddress = avaxAddr
    80  	return s, nil
    81  }
    82  
    83  // UnparsedConfig contains the genesis addresses used to construct a genesis
    84  type UnparsedConfig struct {
    85  	NetworkID uint32 `json:"networkID"`
    86  
    87  	Allocations []UnparsedAllocation `json:"allocations"`
    88  
    89  	StartTime                  uint64           `json:"startTime"`
    90  	InitialStakeDuration       uint64           `json:"initialStakeDuration"`
    91  	InitialStakeDurationOffset uint64           `json:"initialStakeDurationOffset"`
    92  	InitialStakedFunds         []string         `json:"initialStakedFunds"`
    93  	InitialStakers             []UnparsedStaker `json:"initialStakers"`
    94  
    95  	CChainGenesis string `json:"cChainGenesis"`
    96  
    97  	Message string `json:"message"`
    98  }
    99  
   100  func (uc UnparsedConfig) Parse() (Config, error) {
   101  	c := Config{
   102  		NetworkID:                  uc.NetworkID,
   103  		Allocations:                make([]Allocation, len(uc.Allocations)),
   104  		StartTime:                  uc.StartTime,
   105  		InitialStakeDuration:       uc.InitialStakeDuration,
   106  		InitialStakeDurationOffset: uc.InitialStakeDurationOffset,
   107  		InitialStakedFunds:         make([]ids.ShortID, len(uc.InitialStakedFunds)),
   108  		InitialStakers:             make([]Staker, len(uc.InitialStakers)),
   109  		CChainGenesis:              uc.CChainGenesis,
   110  		Message:                    uc.Message,
   111  	}
   112  	for i, ua := range uc.Allocations {
   113  		a, err := ua.Parse()
   114  		if err != nil {
   115  			return c, err
   116  		}
   117  		c.Allocations[i] = a
   118  	}
   119  	for i, isa := range uc.InitialStakedFunds {
   120  		_, _, avaxAddrBytes, err := address.Parse(isa)
   121  		if err != nil {
   122  			return c, err
   123  		}
   124  		avaxAddr, err := ids.ToShortID(avaxAddrBytes)
   125  		if err != nil {
   126  			return c, err
   127  		}
   128  		c.InitialStakedFunds[i] = avaxAddr
   129  	}
   130  	for i, uis := range uc.InitialStakers {
   131  		is, err := uis.Parse()
   132  		if err != nil {
   133  			return c, err
   134  		}
   135  		c.InitialStakers[i] = is
   136  	}
   137  	return c, nil
   138  }