generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): add crypto_hash/2 predicate
- Loading branch information
Showing
2 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
"github.com/tendermint/tendermint/crypto" | ||
) | ||
|
||
func CryptoHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
return engine.Delay(func(ctx context.Context) *engine.Promise { | ||
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) | ||
default: | ||
return engine.Error(fmt.Errorf("crypto_hash/2: cannot unify %s from %s", data, hash)) | ||
} | ||
}) | ||
} |