Jürgen Eckel cb9f762675
Eckelj/fix store resolve issues ()
* added upper and lower case TA resolution testing

* added more detailed error reporting to the ValidateSignature method.
* extended test cases to test and verify these errs and their differences

* fixed CID attestation issue. CIDs are send in web compatible encoding that is not hex encoded and can be utilized without any further decoding on the server side.

* added checks and error handling for the Ta store object storage/loading

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-09-15 10:10:04 +02:00

55 lines
1.5 KiB
Go

package util
import (
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
)
func ValidateSignature(message string, signature string, publicKey string) (bool, error) {
// Convert the message, signature, and public key from hex to bytes
messageBytes, err := hex.DecodeString(message)
if err != nil {
return false, errors.New("invalid message hex string")
}
return ValidateSignatureByteMsg(messageBytes, signature, publicKey)
}
func ValidateSignatureByteMsg(message []byte, signature string, publicKey string) (bool, error) {
// Convert signature, and public key from hex to bytes
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
return false, errors.New("invalid signature hex string")
}
publicKeyBytes, err := hex.DecodeString(publicKey)
if err != nil {
return false, errors.New("invalid public key hex string")
}
// Create a secp256k1 public key object
pubKey := &secp256k1.PubKey{Key: publicKeyBytes}
// Verify the signature
isValid := pubKey.VerifySignature(message, signatureBytes)
if !isValid {
return false, errors.New("invalid signature")
} else {
return isValid, nil
}
}
func GetHexPubKey(ext_pub_key string) (string, error) {
xpubKey, err := hdkeychain.NewKeyFromString(ext_pub_key)
if err != nil {
return "", err
}
pubKey, err := xpubKey.ECPubKey()
if err != nil {
return "", err
}
byte_key := pubKey.SerializeCompressed()
return hex.EncodeToString(byte_key), nil
}