code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_verify_message.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 api
    17  
    18  import (
    19  	"context"
    20  	"encoding/base64"
    21  	"encoding/hex"
    22  	"fmt"
    23  
    24  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    25  	"code.vegaprotocol.io/vega/wallet/crypto"
    26  
    27  	"github.com/mitchellh/mapstructure"
    28  )
    29  
    30  type AdminVerifyMessageParams struct {
    31  	PublicKey        string `json:"publicKey"`
    32  	EncodedMessage   string `json:"encodedMessage"`
    33  	EncodedSignature string `json:"encodedSignature"`
    34  }
    35  
    36  type AdminVerifyMessageResult struct {
    37  	IsValid bool `json:"isValid"`
    38  }
    39  
    40  type AdminVerifyMessage struct{}
    41  
    42  func (h *AdminVerifyMessage) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    43  	params, err := validateAdminVerifyMessageParams(rawParams)
    44  	if err != nil {
    45  		return nil, InvalidParams(err)
    46  	}
    47  
    48  	m, err := base64.StdEncoding.DecodeString(params.EncodedMessage)
    49  	if err != nil {
    50  		return nil, InvalidParams(ErrEncodedMessageIsNotValidBase64String)
    51  	}
    52  
    53  	s, err := base64.StdEncoding.DecodeString(params.EncodedSignature)
    54  	if err != nil {
    55  		return nil, InvalidParams(ErrEncodedSignatureIsNotValidBase64String)
    56  	}
    57  
    58  	decodedPubKey, err := hex.DecodeString(params.PublicKey)
    59  	if err != nil {
    60  		return nil, InvalidParams(fmt.Errorf("could not decode the public key: %w", err))
    61  	}
    62  
    63  	signatureAlgorithm := crypto.NewEd25519()
    64  	valid, err := signatureAlgorithm.Verify(decodedPubKey, m, s)
    65  	if err != nil {
    66  		return nil, InternalError(fmt.Errorf("could not verify the signature: %w", err))
    67  	}
    68  
    69  	return AdminVerifyMessageResult{
    70  		IsValid: valid,
    71  	}, nil
    72  }
    73  
    74  func NewAdminVerifyMessage() *AdminVerifyMessage {
    75  	return &AdminVerifyMessage{}
    76  }
    77  
    78  func validateAdminVerifyMessageParams(rawParams jsonrpc.Params) (AdminVerifyMessageParams, error) {
    79  	if rawParams == nil {
    80  		return AdminVerifyMessageParams{}, ErrParamsRequired
    81  	}
    82  
    83  	params := AdminVerifyMessageParams{}
    84  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    85  		return AdminVerifyMessageParams{}, ErrParamsDoNotMatch
    86  	}
    87  
    88  	if params.PublicKey == "" {
    89  		return AdminVerifyMessageParams{}, ErrPublicKeyIsRequired
    90  	}
    91  
    92  	if params.EncodedMessage == "" {
    93  		return AdminVerifyMessageParams{}, ErrMessageIsRequired
    94  	}
    95  
    96  	if params.EncodedSignature == "" {
    97  		return AdminVerifyMessageParams{}, ErrSignatureIsRequired
    98  	}
    99  
   100  	return AdminVerifyMessageParams{
   101  		PublicKey:        params.PublicKey,
   102  		EncodedMessage:   params.EncodedMessage,
   103  		EncodedSignature: params.EncodedSignature,
   104  	}, nil
   105  }