Skip to content

Commit

Permalink
refactor(logic): use go-enum for algo hash algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Nov 7, 2023
1 parent 9d628b3 commit 8177f62
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
10 changes: 5 additions & 5 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SHAHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engin
return engine.Delay(func(ctx context.Context) *engine.Promise {
switch d := env.Resolve(data).(type) {
case engine.Atom:
result, err := util.Hash(util.Sha256, []byte(d.String()))
result, err := util.Hash(util.HashAlgSha256, []byte(d.String()))
if err != nil {
engine.Error(fmt.Errorf("sha_hash/2: failed to hash data: %w", err))
}
Expand Down Expand Up @@ -102,9 +102,9 @@ func CryptoDataHash(
return engine.Error(fmt.Errorf("%s: failed to decode data: %w", functor, err))
}

switch algorithm {
case engine.NewAtom("sha256"):
result, err := util.Hash(util.Sha256, decodedData)
switch algorithm.String() {
case util.HashAlgSha256.String():
result, err := util.Hash(util.HashAlgSha256, decodedData)
if err != nil {
engine.Error(fmt.Errorf("sha_hash/2: failed to hash data: %w", err))
}
Expand All @@ -114,7 +114,7 @@ func CryptoDataHash(
return engine.Error(fmt.Errorf("%s: invalid algorithm: %s. Possible values: %s",
functor,
algorithm.String(),
strings.Join(util.Map([]util.HashAlg{util.Sha256}, func(a util.HashAlg) string { return a.String() }), ", ")))
util.HashAlgNames()))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/logic/predicate/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestCrypto(t *testing.T) {
},
{
query: ` crypto_data_hash('hello world', Hash, [algorithm(cheh)]).`,
wantError: fmt.Errorf("crypto_data_hash/3: invalid algorithm: cheh. Possible values: sha256"),
wantError: fmt.Errorf("crypto_data_hash/3: invalid algorithm: cheh. Possible values: [sha256]"),
wantSuccess: false,
},
}
Expand Down
25 changes: 10 additions & 15 deletions x/logic/util/crypto.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:generate go-enum --names
package util

import (
Expand All @@ -10,6 +11,12 @@ import (
"github.com/dustinxie/ecc"
)

const (
Secp256k1 KeyAlg = "secp256k1"
Secp256r1 KeyAlg = "secp256r1"
Ed25519 KeyAlg = "ed25519"
)

// KeyAlg is the type of key algorithm supported by the crypto util functions.
type KeyAlg string

Expand All @@ -19,20 +26,8 @@ func (a KeyAlg) String() string {
}

// HashAlg is the type of hash algorithm supported by the crypto util functions.
type HashAlg string

// String returns the string representation of the hash algorithm.
func (a HashAlg) String() string {
return string(a)
}

const (
Secp256k1 KeyAlg = "secp256k1"
Secp256r1 KeyAlg = "secp256r1"
Ed25519 KeyAlg = "ed25519"

Sha256 HashAlg = "sha256"
)
// ENUM(sha256)
type HashAlg int

// VerifySignature verifies the signature of the given message with the given public key using the given algorithm.
func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err error) {
Expand All @@ -57,7 +52,7 @@ func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err er
// Hash hashes the given data using the given algorithm.
func Hash(alg HashAlg, bytes []byte) ([]byte, error) {
switch alg {
case Sha256:
case HashAlgSha256:
hasher := sha256.New()
hasher.Write(bytes)
return hasher.Sum(nil), nil
Expand Down
63 changes: 63 additions & 0 deletions x/logic/util/crypto_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8177f62

Please sign in to comment.