Skip to content

Commit

Permalink
feat(logic): bech32_address/3 predicate conversion in one way
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 21, 2023
1 parent 9a4ef44 commit ba1195a
Show file tree
Hide file tree
Showing 2 changed files with 28 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 @@ -117,6 +117,7 @@ var Registry = map[string]RegistryEntry{
"did_components/2": {predicate.DIDComponents, 1},
"sha_hash/2": {predicate.SHAHash, 1},
"hex_bytes/2": {predicate.HexBytes, 1},
"bech32_address/3": {predicate.Bech32Address, 1},
}

// RegistryNames is the list of the predicate names in the Registry.
Expand Down
27 changes: 27 additions & 0 deletions x/logic/predicate/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package predicate

import (
"context"
"fmt"

bech322 "github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/util"
)

func Bech32Address(vm *engine.VM, hrp, address, bech32 engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
return engine.Delay(func(ctx context.Context) *engine.Promise {
switch b := env.Resolve(bech32).(type) {
case engine.Variable:
return nil
case engine.Atom:
h, a, err := bech322.DecodeAndConvert(b.String())
if err != nil {
return engine.Error(fmt.Errorf("bech32_address/3: failed convert bech32 encoded string to base64: %w", err))
}
return engine.Unify(vm, Tuple(hrp, address), Tuple(util.StringToTerm(h), BytesToList(a)), cont, env)
default:
return engine.Error(fmt.Errorf("bech32_address/3: invalid data type: %T, should be Atom or Variable", b))
}
})
}

0 comments on commit ba1195a

Please sign in to comment.