code.vegaprotocol.io/vega@v0.79.0/datanode/entities/node_signature.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/hex"
    20  	"encoding/json"
    21  	"fmt"
    22  	"time"
    23  
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    26  )
    27  
    28  type _NodeSignature struct{}
    29  
    30  type NodeSignatureID = ID[_NodeSignature]
    31  
    32  type NodeSignature struct {
    33  	ResourceID NodeSignatureID
    34  	Sig        []byte
    35  	Kind       NodeSignatureKind
    36  	TxHash     TxHash
    37  	VegaTime   time.Time
    38  }
    39  
    40  // packNodeSignatures packs a list signatures into the form form:
    41  // 0x + sig1 + sig2 + ... + sigN in hex encoded form
    42  // If the list is empty, return an empty string instead.
    43  func PackNodeSignatures(signatures []NodeSignature) string {
    44  	pack := ""
    45  	if len(signatures) > 0 {
    46  		pack = "0x"
    47  	}
    48  
    49  	for _, v := range signatures {
    50  		pack = fmt.Sprintf("%v%v", pack, hex.EncodeToString(v.Sig))
    51  	}
    52  
    53  	return pack
    54  }
    55  
    56  func NodeSignatureFromProto(ns *commandspb.NodeSignature, txHash TxHash, vegaTime time.Time) (*NodeSignature, error) {
    57  	return &NodeSignature{
    58  		ResourceID: NodeSignatureID(ns.Id),
    59  		Sig:        ns.Sig,
    60  		Kind:       NodeSignatureKind(ns.Kind),
    61  		TxHash:     txHash,
    62  		VegaTime:   vegaTime,
    63  	}, nil
    64  }
    65  
    66  func (w NodeSignature) ToProto() *commandspb.NodeSignature {
    67  	return &commandspb.NodeSignature{
    68  		Id:   w.ResourceID.String(),
    69  		Sig:  w.Sig,
    70  		Kind: commandspb.NodeSignatureKind(w.Kind),
    71  	}
    72  }
    73  
    74  func (w NodeSignature) Cursor() *Cursor {
    75  	cursor := NodeSignatureCursor{
    76  		ResourceID: w.ResourceID,
    77  		Sig:        w.Sig,
    78  	}
    79  	return NewCursor(cursor.String())
    80  }
    81  
    82  func (w NodeSignature) ToProtoEdge(_ ...any) (*v2.NodeSignatureEdge, error) {
    83  	return &v2.NodeSignatureEdge{
    84  		Node:   w.ToProto(),
    85  		Cursor: w.Cursor().Encode(),
    86  	}, nil
    87  }
    88  
    89  type NodeSignatureCursor struct {
    90  	ResourceID NodeSignatureID `json:"resource_id"`
    91  	Sig        []byte          `json:"sig"`
    92  }
    93  
    94  func (c NodeSignatureCursor) String() string {
    95  	bs, err := json.Marshal(c)
    96  	// Should never error, so panic if it does
    97  	if err != nil {
    98  		panic(fmt.Errorf("could not marshal node signature cursor: %w", err))
    99  	}
   100  
   101  	return string(bs)
   102  }
   103  
   104  func (c *NodeSignatureCursor) Parse(cursorString string) error {
   105  	if cursorString == "" {
   106  		return nil
   107  	}
   108  
   109  	return json.Unmarshal([]byte(cursorString), c)
   110  }