planetmint-go/x/asset/keeper/msg_server_notarize_asset.go
Jürgen Eckel 34182773dc
Eckelj/removed hashing twice and decoding issue (#55)
removed not working hex.DecodeString(cid)

removed potential double hashing (hashing the hash with the same hash method)
removed the double hashing and the encoding issue from the serverside asset message verification code.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-07-31 08:12:30 +02:00

53 lines
1.3 KiB
Go

package keeper
import (
"context"
"encoding/hex"
"errors"
"planetmint-go/x/asset/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k msgServer) NotarizeAsset(goCtx context.Context, msg *types.MsgNotarizeAsset) (*types.MsgNotarizeAssetResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
_, found := k.machineKeeper.GetMachineIndex(ctx, msg.PubKey)
if !found {
return nil, errors.New("machine not found")
}
valid := ValidateSignature(msg.Hash, msg.Signature, msg.PubKey)
if !valid {
return nil, errors.New("invalid signature")
}
var asset = types.Asset{
Hash: msg.Hash,
Signature: msg.Signature,
Pubkey: msg.PubKey,
}
k.StoreAsset(ctx, asset)
return &types.MsgNotarizeAssetResponse{}, nil
}
func ValidateSignature(message string, signature string, publicKey string) bool {
// Convert the message, signature, and public key from hex to bytes
messageBytes := []byte(message)
signatureBytes, _ := hex.DecodeString(signature)
publicKeyBytes, _ := hex.DecodeString(publicKey)
// Create a secp256k1 public key object
pubKey := &secp256k1.PubKey{Key: publicKeyBytes}
// Verify the signature
isValid := pubKey.VerifySignature(messageBytes, signatureBytes)
return isValid
}