github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/warp/signer.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package warp
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/utils/crypto/bls"
    11  )
    12  
    13  var (
    14  	_ Signer = (*signer)(nil)
    15  
    16  	ErrWrongSourceChainID = errors.New("wrong SourceChainID")
    17  	ErrWrongNetworkID     = errors.New("wrong networkID")
    18  )
    19  
    20  type Signer interface {
    21  	// Returns this node's BLS signature over an unsigned message. If the caller
    22  	// does not have the authority to sign the message, an error will be
    23  	// returned.
    24  	//
    25  	// Assumes the unsigned message is correctly initialized.
    26  	Sign(msg *UnsignedMessage) ([]byte, error)
    27  }
    28  
    29  func NewSigner(sk *bls.SecretKey, networkID uint32, chainID ids.ID) Signer {
    30  	return &signer{
    31  		sk:        sk,
    32  		networkID: networkID,
    33  		chainID:   chainID,
    34  	}
    35  }
    36  
    37  type signer struct {
    38  	sk        *bls.SecretKey
    39  	networkID uint32
    40  	chainID   ids.ID
    41  }
    42  
    43  func (s *signer) Sign(msg *UnsignedMessage) ([]byte, error) {
    44  	if msg.SourceChainID != s.chainID {
    45  		return nil, ErrWrongSourceChainID
    46  	}
    47  	if msg.NetworkID != s.networkID {
    48  		return nil, ErrWrongNetworkID
    49  	}
    50  
    51  	msgBytes := msg.Bytes()
    52  	sig := bls.Sign(s.sk, msgBytes)
    53  	return bls.SignatureToBytes(sig), nil
    54  }