code.vegaprotocol.io/vega@v0.79.0/datanode/entities/ethereum_key_rotation.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package entities
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"time"
    22  
    23  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    25  )
    26  
    27  type EthereumKeyRotation struct {
    28  	NodeID      NodeID
    29  	OldAddress  EthereumAddress
    30  	NewAddress  EthereumAddress
    31  	BlockHeight uint64
    32  	TxHash      TxHash
    33  	VegaTime    time.Time
    34  	SeqNum      uint64
    35  }
    36  
    37  func EthereumKeyRotationFromProto(kr *eventspb.EthereumKeyRotation, txHash TxHash, vegaTime time.Time,
    38  	seqNum uint64,
    39  ) (EthereumKeyRotation, error) {
    40  	return EthereumKeyRotation{
    41  		NodeID:      NodeID(kr.NodeId),
    42  		OldAddress:  EthereumAddress(kr.OldAddress),
    43  		NewAddress:  EthereumAddress(kr.NewAddress),
    44  		BlockHeight: kr.BlockHeight,
    45  		TxHash:      txHash,
    46  		VegaTime:    vegaTime,
    47  		SeqNum:      seqNum,
    48  	}, nil
    49  }
    50  
    51  func (kr EthereumKeyRotation) ToProto() *eventspb.EthereumKeyRotation {
    52  	return &eventspb.EthereumKeyRotation{
    53  		NodeId:      kr.NodeID.String(),
    54  		OldAddress:  kr.OldAddress.String(),
    55  		NewAddress:  kr.NewAddress.String(),
    56  		BlockHeight: kr.BlockHeight,
    57  	}
    58  }
    59  
    60  func (kr EthereumKeyRotation) Cursor() *Cursor {
    61  	cursor := EthereumKeyRotationCursor{
    62  		VegaTime:   kr.VegaTime,
    63  		NodeID:     kr.NodeID,
    64  		OldAddress: kr.OldAddress,
    65  		NewAddress: kr.NewAddress,
    66  	}
    67  	return NewCursor(cursor.String())
    68  }
    69  
    70  func (kr EthereumKeyRotation) ToProtoEdge(_ ...any) (*v2.EthereumKeyRotationEdge, error) {
    71  	return &v2.EthereumKeyRotationEdge{
    72  		Node:   kr.ToProto(),
    73  		Cursor: kr.Cursor().Encode(),
    74  	}, nil
    75  }
    76  
    77  type EthereumKeyRotationCursor struct {
    78  	VegaTime   time.Time       `json:"vegaTime"`
    79  	NodeID     NodeID          `json:"nodeID"`
    80  	OldAddress EthereumAddress `json:"oldAddress"`
    81  	NewAddress EthereumAddress `json:"newAddress"`
    82  }
    83  
    84  func (ec EthereumKeyRotationCursor) String() string {
    85  	bs, err := json.Marshal(ec)
    86  	if err != nil {
    87  		// This should never happen.
    88  		panic(fmt.Errorf("couldn't marshal deposit cursor: %w", err))
    89  	}
    90  	return string(bs)
    91  }
    92  
    93  func (ec *EthereumKeyRotationCursor) Parse(cursorString string) error {
    94  	if cursorString == "" {
    95  		return nil
    96  	}
    97  	return json.Unmarshal([]byte(cursorString), ec)
    98  }