code.vegaprotocol.io/vega@v0.79.0/wallet/crypto/ed25519.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  
    21  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    22  
    23  	"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
    24  )
    25  
    26  type ed25519Sig struct{}
    27  
    28  func newEd25519() *ed25519Sig {
    29  	return &ed25519Sig{}
    30  }
    31  
    32  func (e *ed25519Sig) Sign(priv crypto.PrivateKey, buf []byte) ([]byte, error) {
    33  	privBytes, ok := priv.([]byte)
    34  	if !ok {
    35  		return nil, ErrCouldNotCastPrivateKeyToBytes
    36  	}
    37  	// Avoid panic by checking key length
    38  	if len(privBytes) != ed25519.PrivateKeySize {
    39  		return nil, ErrBadED25519PrivateKeyLength
    40  	}
    41  	return ed25519.Sign(privBytes, vgcrypto.Hash(buf)), nil
    42  }
    43  
    44  func (e *ed25519Sig) Verify(pub crypto.PublicKey, message, sig []byte) (bool, error) {
    45  	pubBytes, ok := pub.([]byte)
    46  	if !ok {
    47  		return false, ErrCouldNotCastPublicKeyToBytes
    48  	}
    49  	// Avoid panic by checking key length
    50  	if len(pubBytes) != ed25519.PublicKeySize {
    51  		return false, ErrBadED25519PublicKeyLength
    52  	}
    53  	return ed25519.Verify(pubBytes, vgcrypto.Hash(message), sig), nil
    54  }
    55  
    56  func (e *ed25519Sig) Name() string {
    57  	return "vega/ed25519"
    58  }
    59  
    60  func (e *ed25519Sig) Version() uint32 {
    61  	return 1
    62  }