Skip to main content
Last updated

Keysets

define-keyset

Use define-keyset to define a keyset with the specified name and keyset guard. I you don't specify a keyset guard, the functions reads the specified keyset name from the message payload to define its keyset guard, which is similar to using the read-keyset function. If the keyset name already exists, the existing keyset is enforced before updating to the new value.

Basic syntax

To define a keyset as name with keyset, use the following syntax:

pact
(define-keyset name keyset)
pact
(define-keyset name keyset)

To read keyset information from the transaction message payload, use the following syntax:

pact
(define-keyset name)
pact
(define-keyset name)

Arguments

Use the following arguments to specify the inputs for the define-keyset Pact function:

ArgumentTypeDescription
namestringSpecifies the name of the keyset to define or read.
keysetstringSpecifies the keyset to associate with the name.

Return values

The define-keyset function returns a string representing the result of defining the keyset.

Examples

The following example demonstrates how to use the define-keyset function to define a keyset named "admin-keyset" to use the keys and predicate function from the specified "my-keyset" object:

pact
(define-keyset 'admin-keyset "my-keyset")
pact
(define-keyset 'admin-keyset "my-keyset")

The following example demonstrates how to read the keyset from the message payload and associate it with admin-keyset:

pact
(define-keyset 'admin-keyset)
pact
(define-keyset 'admin-keyset)

The following example illustrates how to define a keyset by reading an existing keyset:

pact
(define-keyset "admin-keyset" (read-keyset 'admin-keyset))
pact
(define-keyset "admin-keyset" (read-keyset 'admin-keyset))

enforce-keyset

Use enforce-keyset to execute a specified guard or a defined keyset named keysetname to enforce the desired predicate logic.

Basic syntax

To execute a guard to enforce desired predicate logic, use the following syntax:

pact
(enforce-keyset guard)
pact
(enforce-keyset guard)

To require a specified keyset to enforce desired predicate logic, use the following syntax:

pact
(enforce-keyset keysetname)
pact
(enforce-keyset keysetname)

Arguments

Use the following arguments to specify the guard or keysetname for the enforce-keyset Pact function:

ArgumentTypeDescription
guardguardSpecifies the guard to execute.
keysetnamestringSpecifies the name of the defined keyset to enforce.

Return values

The enforce-keyset function returns a boolean value indicating whether the specified guard or keyset predicate logic was enforced.

Examples

The following example demonstrates how to use the enforce-keyset function to enforce logic defined in the "admin-keyset" predicate function:

pact
(enforce-keyset 'admin-keyset)
pact
(enforce-keyset 'admin-keyset)

If the condition specified by the admin-keyset predicate function is satisfied, the enforce-keyset function returns a boolean value of true.

The following example enforces the logic defined in the row-guard predicate logic:

pact
(enforce-keyset row-guard)
pact
(enforce-keyset row-guard)

If the condition specified by the row-guard predicate function is satisfied, the enforce-keyset function returns a boolean value of true.

keys-2

Use keys-2 as a keyset predicate function to determine if there are at least two keys that match the keys defined in a keyset.

Basic syntax

To check whether there are at least two keys that match the keys defined in a keyset, use the following syntax:

pact
keys-2 count matched
pact
keys-2 count matched

Arguments

Use the following arguments to specify the count of keys in the keyset and the number of matched keys using the keys-2 Pact function.

ArgumentTypeDescription
countintegerSpecifies the total count of keys defined in the keyset.
matchedintegerSpecifies the number of matched keys.

Return value

The keys-2 function returns a boolean value indicating whether there are at least two keys that match the keys defined in the keyset.

Examples

The following example demonstrates how to use the keys-2 to check if at least two keys are matched in a keyset where the total number of keys defined in a keyset is three and only one key is matched:

pact
pact> (keys-2 3 1)false
pact
pact> (keys-2 3 1)false

The function returns false because the condition of having at least two keys matched is not met.

keys-all

Use keys-all as a keyset predicate function to determine if all of the keys defined in the keyset are matched.

Basic syntax

To check whether all of the keys defined in a keyset are matched, use the following syntax:

pact
(keys-all count matched)
pact
(keys-all count matched)

Arguments

Use the following arguments to specify the count of keys in the keyset and the number of matched keys using the keys-all Pact function.

ArgumentTypeDescription
countintegerSpecifies the total count of keys defined in the keyset.
matchedintegerSpecifies the number of matched keys.

Return value

The keys-all function returns a boolean value indicating whether all keys in the keyset are matched.

Examples

The following example demonstrates how to use the keys-all function to check whether all of the keys are matched in a keyset where the total number of keys defined is three:

pact
pact> (keys-all 3 3)true
pact
pact> (keys-all 3 3)true

The function returns true because all keys in the keyset are matched.

keys

Use keys to return all keys present in a specified table.

Basic syntax

To retrieve all keys present in a table, use the following syntax:

pact
(keys table)
pact
(keys table)

Arguments

Use the following argument to specify the table from which you want to retrieve keys using the keys Pact function.

ArgumentTypeDescription
tabletable: <{row}>Specifies the table from which keys will be retrieved.

Return value

The keys function returns a list of strings containing all keys present in the specified table.

Examples

The following example demonstrates how to use the keys function to retrieve all of the keys present in the "accounts" table:

pact
(keys accounts)
pact
(keys accounts)

In this example, all keys present in the "accounts" table are returned as a list of strings.