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): bech32_address/3 predicate conversion in one way
- Loading branch information
Showing
2 changed files
with
28 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,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)) | ||
} | ||
}) | ||
} |