code.vegaprotocol.io/vega@v0.79.0/wallet/crypto/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 crypto
    17  
    18  import (
    19  	"crypto"
    20  	"encoding/json"
    21  	"errors"
    22  )
    23  
    24  const (
    25  	Ed25519 string = "vega/ed25519"
    26  )
    27  
    28  var ErrUnsupportedSignatureAlgorithm = errors.New("unsupported signature algorithm")
    29  
    30  type SignatureAlgorithm struct {
    31  	impl signatureAlgorithmImpl
    32  }
    33  
    34  type signatureAlgorithmImpl interface {
    35  	Sign(priv crypto.PrivateKey, buf []byte) ([]byte, error)
    36  	Verify(pub crypto.PublicKey, message, sig []byte) (bool, error)
    37  	Name() string
    38  	Version() uint32
    39  }
    40  
    41  func NewEd25519() SignatureAlgorithm {
    42  	return SignatureAlgorithm{
    43  		impl: newEd25519(),
    44  	}
    45  }
    46  
    47  func NewSignatureAlgorithm(name string, version uint32) (SignatureAlgorithm, error) {
    48  	if name == Ed25519 && version == 1 {
    49  		return NewEd25519(), nil
    50  	}
    51  	return SignatureAlgorithm{}, ErrUnsupportedSignatureAlgorithm
    52  }
    53  
    54  func (a *SignatureAlgorithm) Sign(priv crypto.PrivateKey, buf []byte) ([]byte, error) {
    55  	return a.impl.Sign(priv, buf)
    56  }
    57  
    58  func (a *SignatureAlgorithm) Verify(pub crypto.PublicKey, message, sig []byte) (bool, error) {
    59  	return a.impl.Verify(pub, message, sig)
    60  }
    61  
    62  func (a *SignatureAlgorithm) Name() string {
    63  	return a.impl.Name()
    64  }
    65  
    66  func (a *SignatureAlgorithm) Version() uint32 {
    67  	return a.impl.Version()
    68  }
    69  
    70  func (a *SignatureAlgorithm) MarshalJSON() ([]byte, error) {
    71  	if a == nil {
    72  		return nil, ErrSignatureIsNil
    73  	}
    74  	return json.Marshal(&jsonAlgorithm{
    75  		Name:    a.Name(),
    76  		Version: a.Version(),
    77  	})
    78  }
    79  
    80  func (a *SignatureAlgorithm) UnmarshalJSON(data []byte) error {
    81  	jsonAlgo := &jsonAlgorithm{}
    82  	if err := json.Unmarshal(data, &jsonAlgo); err != nil {
    83  		return err
    84  	}
    85  
    86  	algo, err := NewSignatureAlgorithm(jsonAlgo.Name, jsonAlgo.Version)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	*a = algo
    92  	return nil
    93  }
    94  
    95  type jsonAlgorithm struct {
    96  	Name    string `json:"name"`
    97  	Version uint32 `json:"version"`
    98  }