Skip to content

Commit

Permalink
feat(logic): add crypto_hash/2 predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 16, 2023
1 parent 8ca4e15 commit 5c70aba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions x/logic/predicate/crypto.go
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))
}
})
}

0 comments on commit 5c70aba

Please sign in to comment.