Skip to content

Commit

Permalink
feat(logic): return List of byte for crypto_hash/2
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 17, 2023
1 parent 0ba365a commit 6534b9b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 1 addition & 2 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package predicate

import (
"context"
"encoding/hex"
"fmt"

"github.com/ichiban/prolog/engine"
Expand All @@ -16,7 +15,7 @@ func CryptoHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *en
switch d := env.Resolve(data).(type) {
case engine.Atom:
result := crypto.Sha256([]byte(d.String()))
return engine.Unify(vm, hash, engine.NewAtom(hex.EncodeToString(result)), cont, env)
return engine.Unify(vm, hash, BytesToList(result), cont, env)
default:
return engine.Error(fmt.Errorf("crypto_hash/2: invalid data type: %T, should be Atom", d))
}
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 @@ -26,7 +26,7 @@ func TestCryptoHash(t *testing.T) {
}{
{
query: `crypto_hash('foo', Hash).`,
wantResult: []types.TermResults{{"Hash": "'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'"}},
wantResult: []types.TermResults{{"Hash": "[44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]"}},
},
{
query: `crypto_hash(Foo, Hash).`,
Expand Down
8 changes: 8 additions & 0 deletions x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ func CoinsToTerm(coins sdk.Coins) engine.Term {
func Tuple(args ...engine.Term) engine.Term {
return engine.Atom(0).Apply(args...)
}

func BytesToList(bt []byte) engine.Term {
terms := make([]engine.Term, 0, len(bt))
for _, b := range bt {
terms = append(terms, engine.Integer(b))
}
return engine.List(terms...)
}

0 comments on commit 6534b9b

Please sign in to comment.