Skip to content

Commit

Permalink
feat(logic): implement crypto signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux authored and ccamel committed Oct 27, 2023
1 parent c51665e commit a92ec86
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
}

0 comments on commit a92ec86

Please sign in to comment.