From a92ec86ca74594755547818a7ae9e95617222624 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 5 Oct 2023 15:15:15 +0200 Subject: [PATCH] feat(logic): implement crypto signature verification --- x/logic/predicate/crypto.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/x/logic/predicate/crypto.go b/x/logic/predicate/crypto.go index 0f9bfa3c..7359cc86 100644 --- a/x/logic/predicate/crypto.go +++ b/x/logic/predicate/crypto.go @@ -2,13 +2,15 @@ package predicate import ( "context" + "crypto" + "crypto/ed25519" "encoding/hex" "fmt" + "github.com/cometbft/cometbft/crypto/secp256k1" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" "github.com/ichiban/prolog/engine" - "github.com/cometbft/cometbft/crypto" - "github.com/okp4/okp4d/x/logic/util" ) @@ -97,3 +99,33 @@ func HexBytes(vm *engine.VM, hexa, bts engine.Term, cont engine.Cont, env *engin } }) } + +type Alg string + +const ( + Secp256k1 Alg = "secp256k1" + Secp256r1 Alg = "secp256r1" + Ed25519 Alg = "ed25519" +) + +func verifySignature(alg Alg, pubKey crypto.PublicKey, msg, sig []byte) (bool, error) { + switch alg { + case Ed25519: + if key, ok := pubKey.(ed25519.PublicKey); ok { + return ed25519.Verify(key, msg, sig), nil + } + return false, fmt.Errorf("public key is not ed25519 compatible") + case Secp256r1: + if key, ok := pubKey.(secp256r1.PubKey); ok { + return key.VerifySignature(msg, sig), nil + } + return false, fmt.Errorf("public key is not secp256r1 compatible") + case Secp256k1: + if key, ok := pubKey.(secp256k1.PubKey); ok { + return key.VerifySignature(msg, sig), nil + } + return false, fmt.Errorf("public key is not secp256k1 compatible") + default: + return false, fmt.Errorf("pub key format not implemented") + } +}