predicate

package
v10.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package predicate provides a set of predicates for use with the logic module.

Index

Constants

View Source
const DIDPrefix = "did"

DIDPrefix is the prefix for a DID.

Variables

This section is empty.

Functions

func AllBalancesSorted

func AllBalancesSorted(sdkContext sdk.Context, bankKeeper types.BankKeeper, bech32Addr sdk.AccAddress) sdk.Coins

AllBalancesSorted returns the list of balances for the given address, sorted by coin denomination.

func BankBalances

func BankBalances(vm *engine.VM, account, balances engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

BankBalances is a predicate which unifies the given terms with the list of balances (coins) of the given account.

The signature is as follows:

bank_balances(?Account, ?Balances)

where:

  • Account represents the account address (in Bech32 format).
  • Balances represents the balances of the account as a list of pairs of coin denomination and amount.

Examples:

# Query the balances of the account.
- bank_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', X).

# Query the balances of all accounts. The result is a list of pairs of account address and balances.
- bank_balances(X, Y).

# Query the first balance of the given account by unifying the denomination and amount with the given terms.
- bank_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', [-(D, A), _]).

func BankLockedBalances

func BankLockedBalances(vm *engine.VM, account, balances engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

BankLockedBalances is a predicate which unifies the given terms with the list of locked coins of the given account.

The signature is as follows:

bank_locked_balances(?Account, ?Balances)

where:

  • Account represents the account address (in Bech32 format).
  • Balances represents the locked balances of the account as a list of pairs of coin denomination and amount.

Examples:

# Query the locked coins of the account.
- bank_locked_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', X).

# Query the locked balances of all accounts. The result is a list of pairs of account address and balances.
- bank_locked_balances(X, Y).

# Query the first locked balances of the given account by unifying the denomination and amount with the given terms.
- bank_locked_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', [-(D, A), _]).

func BankSpendableBalances

func BankSpendableBalances(vm *engine.VM, account, balances engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

BankSpendableBalances is a predicate which unifies the given terms with the list of spendable coins of the given account.

The signature is as follows:

bank_spendable_balances(?Account, ?Balances)

where:

  • Account represents the account address (in Bech32 format).
  • Balances represents the spendable balances of the account as a list of pairs of coin denomination and amount.

Examples:

# Query the spendable balances of the account.
- bank_spendable_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', X).

# Query the spendable balances of all accounts. The result is a list of pairs of account address and balances.
- bank_spendable_balances(X, Y).

# Query the first spendable balances of the given account by unifying the denomination and amount with the given terms.
- bank_spendable_balances('axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa', [-(D, A), _]).

func Bech32Address

func Bech32Address(_ *engine.VM, address, bech32 engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

Bech32Address is a predicate that convert a bech32 encoded string into base64 bytes and give the address prefix, or convert a prefix (HRP) and base64 encoded bytes to bech32 encoded string.

Signature

bech32_address(-Address, +Bech32) is det
bech32_address(+Address, -Bech32) is det

where:

  • Address is a pair of the HRP (Human-Readable Part) which holds the address prefix and a list of numbers ranging from 0 to 255 that represent the base64 encoded bech32 address string.
  • Bech32 is an Atom or string representing the bech32 encoded string address

func BlockHeight

func BlockHeight(vm *engine.VM, height engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

BlockHeight is a predicate which unifies the given term with the current block height.

Signature

block_height(?Height) is det

where:

  • Height represents the current chain height at the time of the query.

func BlockTime

func BlockTime(vm *engine.VM, time engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

BlockTime is a predicate which unifies the given term with the current block time.

Signature

block_time(?Time) is det

where:

  • Time represents the current chain time at the time of the query.

func ChainID

func ChainID(vm *engine.VM, chainID engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

ChainID is a predicate which unifies the given term with the current chain ID. The signature is:

The signature is as follows:

chain_id(?ID)

where:

  • ID represents the current chain ID at the time of the query.

Examples:

# Query the current chain ID.
- chain_id(ID).

func CoinsToTerm

func CoinsToTerm(coins sdk.Coins) engine.Term

CoinsToTerm converts the given coins to a term of the form:

[-(Denom, Amount), -(Denom, Amount), ...]

func Consult

func Consult(vm *engine.VM, file engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

Consult is a predicate which read files as Prolog source code.

Signature

consult(+Files) is det

where:

  • Files represents the source files to be loaded. It can be an atom or a list of atoms representing the source files.

The Files argument are typically URIs that point to the sources file to be loaded through the Virtual File System (VFS). Please refer to the open/4 predicate for more information about the VFS.

func CryptoDataHash

func CryptoDataHash(
	vm *engine.VM, data, hash, options engine.Term, cont engine.Cont, env *engine.Env,
) *engine.Promise

CryptoDataHash is a predicate that computes the Hash of the given Data using different algorithms.

The signature is as follows:

crypto_data_hash(+Data, -Hash, +Options) is det
crypto_data_hash(+Data, +Hash, +Options) is det

Where:

  • Data represents the data to be hashed, given as an atom, or code-list.
  • Hash represents the Hashed value of Data, which can be given as an atom or a variable.
  • Options are additional configurations for the hashing process. Supported options include: encoding(+Format) which specifies the encoding used for the Data, and algorithm(+Alg) which chooses the hashing algorithm among the supported ones (see below for details).

For Format, the supported encodings are:

  • utf8 (default), the UTF-8 encoding represented as an atom.
  • text, the plain text encoding represented as an atom.
  • hex, the hexadecimal encoding represented as an atom.
  • octet, the raw byte encoding depicted as a list of integers ranging from 0 to 255.

For Alg, the supported algorithms are:

  • sha256 (default): The SHA-256 algorithm.
  • sha512: The SHA-512 algorithm.
  • md5: (insecure) The MD5 algorithm.

Note: Due to the principles of the hash algorithm (pre-image resistance), this predicate can only compute the hash value from input data, and cannot compute the original input data from the hash value.

Examples:

# Compute the SHA-256 hash of the given data and unify it with the given Hash.
- crypto_data_hash('Hello AXONE', Hash).

# Compute the SHA-256 hash of the given hexadecimal data and unify it with the given Hash.
- crypto_data_hash('9b038f8ef6918cbb56040dfda401b56b...', Hash, encoding(hex)).

# Compute the SHA-256 hash of the given hexadecimal data and unify it with the given Hash.
- crypto_data_hash([127, ...], Hash, encoding(octet)).

func CurrentOutput

func CurrentOutput(vm *engine.VM, output engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

CurrentOutput is a predicate that unifies the given term with the current output stream.

Signature

current_output(-Stream) is det

where:

  • Stream represents the current output stream.

This predicate connects to the default output stream available for user interactions, allowing the user to perform write operations.

The outcome of the stream's content throughout the execution of a query is provided as a string within the user_output field in the query's response. However, it's important to note that the maximum length of the output is constrained by the max_query_output_size setting, meaning only the final max_query_output_size bytes (not characters) of the output are included in the response.

func DIDComponents

func DIDComponents(vm *engine.VM, did, components engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

DIDComponents is a predicate which breaks down a DID into its components according to the W3C DID specification.

The signature is as follows:

did_components(+DID, -Components) is det
did_components(-DID, +Components) is det

where:

  • DID represent DID URI, given as an Atom, compliant with W3C DID specification.
  • Components is a compound Term in the format did(Method, ID, Path, Query, Fragment), aligned with the DID syntax, where: Method is the method name, ID is the method-specific identifier, Path is the path component, Query is the query component and Fragment is the fragment component. Values are given as an Atom and are url encoded. For any component not present, its value will be null and thus will be left as an uninstantiated variable.

Examples:

# Decompose a DID into its components.
- did_components('did:example:123456?versionId=1', did_components(Method, ID, Path, Query, Fragment)).

# Reconstruct a DID from its components.
- did_components(DID, did_components('example', '123456', _, 'versionId=1', _42)).

func ECDSAVerify

func ECDSAVerify(_ *engine.VM, key, data, sig, options engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

ECDSAVerify determines if a given signature is valid as per the ECDSA algorithm for the provided data, using the specified public key.

The signature is as follows:

ecdsa_verify(+PubKey, +Data, +Signature, +Options), which is semi-deterministic.

Where:

  • PubKey is the 33-byte compressed public key, as specified in section 4.3.6 of ANSI X9.62.

  • Data is the hash of the signed message, which can be either an atom or a list of bytes.

  • Signature represents the ASN.1 encoded signature corresponding to the Data.

  • Options are additional configurations for the verification process. Supported options include: encoding(+Format) which specifies the encoding used for the data, and type(+Alg) which chooses the algorithm within the ECDSA family (see below for details).

For Format, the supported encodings are:

  • hex (default), the hexadecimal encoding represented as an atom.
  • octet, the plain byte encoding depicted as a list of integers ranging from 0 to 255.
  • text, the plain text encoding represented as an atom.
  • utf8 (default), the UTF-8 encoding represented as an atom.

For Alg, the supported algorithms are:

  • secp256r1 (default): Also known as P-256 and prime256v1.
  • secp256k1: The Koblitz elliptic curve used in Bitcoin's public-key cryptography.

Examples:

# Verify a signature for hexadecimal data using the ECDSA secp256r1 algorithm.
- ecdsa_verify([127, ...], '9b038f8ef6918cbb56040dfda401b56b...', [23, 56, ...], encoding(hex))

# Verify a signature for binary data using the ECDSA secp256k1 algorithm.
- ecdsa_verify([127, ...], [56, 90, ..], [23, 56, ...], [encoding(octet), type(secp256k1)])

func EDDSAVerify

func EDDSAVerify(_ *engine.VM, key, data, sig, options engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

EDDSAVerify determines if a given signature is valid as per the EdDSA algorithm for the provided data, using the specified public key.

The signature is as follows:

eddsa_verify(+PubKey, +Data, +Signature, +Options) is semi-det

Where:

  • PubKey is the encoded public key as a list of bytes.
  • Data is the message to verify, represented as either a hexadecimal atom or a list of bytes. It's important that the message isn't pre-hashed since the Ed25519 algorithm processes messages in two passes when signing.
  • Signature represents the signature corresponding to the data, provided as a list of bytes.
  • Options are additional configurations for the verification process. Supported options include: encoding(+Format) which specifies the encoding used for the Data, and type(+Alg) which chooses the algorithm within the EdDSA family (see below for details).

For Format, the supported encodings are:

  • hex (default), the hexadecimal encoding represented as an atom.
  • octet, the plain byte encoding depicted as a list of integers ranging from 0 to 255.
  • text, the plain text encoding represented as an atom.
  • utf8 (default), the UTF-8 encoding represented as an atom.

For Alg, the supported algorithms are:

  • ed25519 (default): The EdDSA signature scheme using SHA-512 (SHA-2) and Curve25519.

Examples:

# Verify a signature for a given hexadecimal data.
- eddsa_verify([127, ...], '9b038f8ef6918cbb56040dfda401b56b...', [23, 56, ...], [encoding(hex), type(ed25519)])

# Verify a signature for binary data.
- eddsa_verify([127, ...], [56, 90, ..], [23, 56, ...], [encoding(octet), type(ed25519)])

func HexBytes

func HexBytes(vm *engine.VM, hexa, bts engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

HexBytes is a predicate that unifies hexadecimal encoded bytes to a list of bytes.

The signature is as follows:

hex_bytes(?Hex, ?Bytes) is det

Where:

  • Hex is an Atom, string or list of characters in hexadecimal encoding.
  • Bytes is the list of numbers between 0 and 255 that represent the sequence of bytes.

Examples:

# Convert hexadecimal atom to list of bytes.
- hex_bytes('2c26b46b68ffc68ff99b453c1d3041341342d706483bfa0f98a5e886266e7ae', Bytes).

func JSONProlog

func JSONProlog(vm *engine.VM, j, term engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

JSONProlog is a predicate that will unify a JSON string into prolog terms and vice versa.

The signature is as follows:

json_prolog(?Json, ?Term) is det

Where:

  • Json is the string representation of the json
  • Term is an Atom that would be unified by the JSON representation as Prolog terms.

In addition, when passing Json and Term, this predicate return true if both result match.

Examples:

# JSON conversion to Prolog.
- json_prolog('{"foo": "bar"}', json([foo-bar])).

func LockedCoinsSorted

func LockedCoinsSorted(sdkContext sdk.Context, bankKeeper types.BankKeeper, bech32Addr sdk.AccAddress) sdk.Coins

LockedCoinsSorted returns the list of spendable coins for the given address, sorted by coin denomination.

func Open

func Open(vm *engine.VM, sourceSink, mode, stream, options engine.Term, k engine.Cont, env *engine.Env) *engine.Promise

Open is a predicate which opens a stream to a source or sink.

Signature

open(+SourceSink, +Mode, -Stream, +Options)

where:

  • SourceSink is an atom representing the source or sink of the stream, which is typically a URI.
  • Mode is an atom representing the mode of the stream to be opened. It can be one of "read", "write", or "append".
  • Stream is the stream to be opened.
  • Options is a list of options. No options are currently defined, so the list should be empty.

open/4 gives True when SourceSink can be opened in Mode with the given Options.

Virtual File System (VFS)

The logical module interprets on-chain Prolog programs, relying on a Virtual Machine that isolates execution from the external environment. Consequently, the open/4 predicate doesn't access the physical file system as one might expect. Instead, it operates with a Virtual File System (VFS), a conceptual layer that abstracts the file system. This abstraction offers a unified view across various storage systems, adhering to the constraints imposed by blockchain technology.

This VFS extends the file concept to resources, which are identified by a Uniform Resource Identifier (URI). A URI specifies the access protocol for the resource, its path, and any necessary parameters.

CosmWasm URI

The cosmwasm URI enables interaction with instantiated CosmWasm smart contract on the blockchain. The URI is used to query the smart contract and retrieve the response. The query is executed on the smart contract, and the response is returned as a stream. Query parameters are passed as part of the URI to customize the interaction with the smart contract.

Its format is as follows:

cosmwasm:{contract_name}:{contract_address}?query={contract_query}[&base64Decode={true|false}]

where:

  • {contract_name}: For informational purposes, indicates the name or type of the smart contract (e.g., "axone-objectarium").
  • {contract_address}: Specifies the smart contract instance to query.
  • {contract_query}: The query to be executed on the smart contract. It is a JSON object that specifies the query payload.
  • base64Decode: (Optional) If true, the response is base64-decoded. Otherwise, the response is returned as is.

func Open3

func Open3(vm *engine.VM, sourceSink, mode, stream engine.Term, k engine.Cont, env *engine.Env) *engine.Promise

Open3 is a predicate which opens a stream to a source or sink. This predicate is a shorthand for open/4 with an empty list of options.

Signature

open(+SourceSink, +Mode, -Stream)

where:

  • SourceSink is an atom representing the source or sink of the stream, which is typically a URI.
  • Mode is an atom representing the mode of the stream to be opened. It can be one of "read", "write", or "append".
  • Stream is the stream to be opened.

open/3 gives True when SourceSink can be opened in Mode.

func ReadString

func ReadString(vm *engine.VM, stream, length, result engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

ReadString is a predicate that reads characters from the provided Stream and unifies them with String. Users can optionally specify a maximum length for reading; if the stream reaches this length, the reading stops. If Length remains unbound, the entire Stream is read, and upon completion, Length is unified with the count of characters read.

The signature is as follows:

read_string(+Stream, ?Length, -String) is det

Where:

  • Stream is the input stream to read from.
  • Length is the optional maximum number of characters to read from the Stream. If unbound, denotes the full length of Stream.
  • String is the resultant string after reading from the Stream.

Examples:

# Given a file `foo.txt` that contains `Hello World`:

 file_to_string(File, String, Length) :-
 open(File, read, In),
 read_string(In, Length, String),
 close(Stream).

# It gives:
?- file_to_string('path/file/foo.txt', String, Length).

String = 'Hello World'
Length = 11

func SortBalances

func SortBalances(balances sdk.Coins)

SortBalances by coin denomination.

func SourceFile

func SourceFile(vm *engine.VM, file engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

SourceFile is a predicate which unifies the given term with the source file that is currently loaded.

Signature

source_file(?File) is det

where:

  • File represents the loaded source file.

func SpendableCoinsSorted

func SpendableCoinsSorted(sdkContext sdk.Context, bankKeeper types.BankKeeper, bech32Addr sdk.AccAddress) sdk.Coins

SpendableCoinsSorted returns the list of spendable coins for the given address, sorted by coin denomination.

func StringBytes

func StringBytes(
	_ *engine.VM, str, bts, encodingTerm engine.Term, cont engine.Cont, env *engine.Env,
) *engine.Promise

StringBytes is a predicate that unifies a string with a list of bytes, returning true when the (Unicode) String is represented by Bytes in Encoding.

The signature is as follows:

string_bytes(?String, ?Bytes, +Encoding)

Where:

  • String is the string to convert to bytes. It can be an Atom, string or list of characters codes.
  • Bytes is the list of numbers between 0 and 255 that represent the sequence of bytes.
  • Encoding is the encoding to use for the conversion.

Encoding can be one of the following: - 'text' considers the string as a sequence of Unicode characters. - 'octet' considers the string as a sequence of bytes. - '<encoding>' considers the string as a sequence of characters in the given encoding.

At least one of String or Bytes must be instantiated.

Examples:

# Convert a string to a list of bytes.
- string_bytes('Hello World', Bytes, octet).

# Convert a list of bytes to a string.
- string_bytes(String, [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100], octet).

func URIEncoded

func URIEncoded(_ *engine.VM, component, decoded, encoded engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise

URIEncoded is a predicate that unifies the given URI component with the given encoded or decoded string.

The signature is as follows:

uri_encoded(+Component, +Value, -Encoded) is det
uri_encoded(+Component, -Value, +Encoded) is det

Where:

  • Component represents the component of the URI to be escaped. It can be the atom 'query_path', 'fragment', 'path' or 'segment'.
  • Decoded represents the decoded string to be escaped.
  • Encoded represents the encoded string.

For more information on URI encoding, refer to RFC 3986.

Examples:

# Escape the given string to be used in the path component.
- uri_encoded(path, "foo/bar", Encoded).

# Unescape the given string to be used in the path component.
- uri_encoded(path, Decoded, foo%2Fbar).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL