From 5c70aba06fbccf0ac73893ae41a883e62d23f248 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 16 Feb 2023 16:45:35 +0100 Subject: [PATCH] feat(logic): add crypto_hash/2 predicate --- x/logic/interpreter/registry.go | 1 + x/logic/predicate/crypto.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 x/logic/predicate/crypto.go diff --git a/x/logic/interpreter/registry.go b/x/logic/interpreter/registry.go index 920688cb..42eb70a9 100644 --- a/x/logic/interpreter/registry.go +++ b/x/logic/interpreter/registry.go @@ -115,6 +115,7 @@ var Registry = map[string]RegistryEntry{ "bank_spendable_balances/2": {predicate.BankSpendableBalances, 1}, "bank_locked_balances/2": {predicate.BankLockedBalances, 1}, "did_components/2": {predicate.DIDComponents, 1}, + "crypto_hash/2": {predicate.CryptoHash, 1}, } // RegistryNames is the list of the predicate names in the Registry. diff --git a/x/logic/predicate/crypto.go b/x/logic/predicate/crypto.go new file mode 100644 index 00000000..fd61ecb5 --- /dev/null +++ b/x/logic/predicate/crypto.go @@ -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)) + } + }) +}