code.vegaprotocol.io/vega@v0.79.0/datanode/entities/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 KeyRotation struct {
    28  	NodeID      NodeID
    29  	OldPubKey   VegaPublicKey
    30  	NewPubKey   VegaPublicKey
    31  	BlockHeight uint64
    32  	TxHash      TxHash
    33  	VegaTime    time.Time
    34  }
    35  
    36  func KeyRotationFromProto(kr *eventspb.KeyRotation, txHash TxHash, vegaTime time.Time) (*KeyRotation, error) {
    37  	return &KeyRotation{
    38  		NodeID:      NodeID(kr.NodeId),
    39  		OldPubKey:   VegaPublicKey(kr.OldPubKey),
    40  		NewPubKey:   VegaPublicKey(kr.NewPubKey),
    41  		BlockHeight: kr.BlockHeight,
    42  		TxHash:      txHash,
    43  		VegaTime:    vegaTime,
    44  	}, nil
    45  }
    46  
    47  func (kr KeyRotation) ToProto() *eventspb.KeyRotation {
    48  	return &eventspb.KeyRotation{
    49  		NodeId:      kr.NodeID.String(),
    50  		OldPubKey:   kr.OldPubKey.String(),
    51  		NewPubKey:   kr.NewPubKey.String(),
    52  		BlockHeight: kr.BlockHeight,
    53  	}
    54  }
    55  
    56  func (kr KeyRotation) Cursor() *Cursor {
    57  	cursor := KeyRotationCursor{
    58  		VegaTime:  kr.VegaTime,
    59  		NodeID:    kr.NodeID,
    60  		OldPubKey: kr.OldPubKey,
    61  		NewPubKey: kr.NewPubKey,
    62  	}
    63  
    64  	return NewCursor(cursor.String())
    65  }
    66  
    67  func (kr KeyRotation) ToProtoEdge(_ ...any) (*v2.KeyRotationEdge, error) {
    68  	return &v2.KeyRotationEdge{
    69  		Node:   kr.ToProto(),
    70  		Cursor: kr.Cursor().Encode(),
    71  	}, nil
    72  }
    73  
    74  type KeyRotationCursor struct {
    75  	VegaTime  time.Time     `json:"vega_time"`
    76  	NodeID    NodeID        `json:"node_id"`
    77  	OldPubKey VegaPublicKey `json:"old_pub_key"`
    78  	NewPubKey VegaPublicKey `json:"new_pub_key"`
    79  }
    80  
    81  func (c KeyRotationCursor) String() string {
    82  	bs, err := json.Marshal(c)
    83  	// This should never fail so if it does, we should panic
    84  	if err != nil {
    85  		panic(fmt.Errorf("could not marshal key rotation cursor: %w", err))
    86  	}
    87  
    88  	return string(bs)
    89  }
    90  
    91  func (c *KeyRotationCursor) Parse(cursorString string) error {
    92  	if cursorString == "" {
    93  		return nil
    94  	}
    95  
    96  	return json.Unmarshal([]byte(cursorString), c)
    97  }