Keysets
define-keyset
Use define-keyset to define a keyset as name with keyset, or if unspecified, read name from the message payload as a keyset, similarly to read-keyset. If the keyset name already exists, the keyset will be enforced before updating to the new value.
Basic syntax
To define a keyset as name with keyset, or read name from the message payload, use the following syntax:
(define-keyset name keyset)
(define-keyset name)
Arguments
Use the following arguments to specify the inputs for the define-keyset Pact function:
| Argument | Type | Description |
|---|---|---|
name | string | Specifies the name of the keyset to define or read. |
keyset | string | Specifies 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 examples demonstrate the define-keyset function:
- Define a keyset named 'admin-keyset' with a specified keyset:
(define-keyset 'admin-keyset "my-keyset")(define-keyset 'admin-keyset "my-keyset")- Read the keyset from the message payload and associate it with 'admin-keyset':
(define-keyset 'admin-keyset)(define-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 or a defined keyset to enforce desired predicate logic, use the following syntax:
(enforce-keyset GUARD)
(enforce-keyset KEYSETNAME)
Arguments
Use the following arguments to specify the GUARD or KEYSETNAME for the enforce-keyset Pact function:
| Argument | Type | Description |
|---|---|---|
GUARD | guard | Specifies the guard to execute. |
KEYSETNAME | string | Specifies the name of the defined keyset to enforce. |
Return values
The enforce-keyset function returns a boolean value indicating whether the guard or keyset enforced the desired predicate logic.
Examples
The following examples demonstrate the enforce-keyset function:
- Execute a guard named 'admin-keyset' to enforce desired logic:
(enforce-keyset 'admin-keyset)(enforce-keyset 'admin-keyset)- Execute a row guard named 'row-guard' to enforce desired logic:
(enforce-keyset row-guard)(enforce-keyset row-guard)In these examples, the enforce-keyset function is used to execute the specified guard or keyset to enforce the desired predicate logic. The function returns a boolean value indicating whether the guard or keyset enforced the desired logic successfully.
keys-2
Use keys-2 as a keyset predicate function to determine if at least two keys are matched in the keyset.
Basic syntax
To use keys-2 to check if at least two keys are matched in a keyset, use the following syntax:
keys-2 count matched
Arguments
Use the following arguments to specify the count of keys in the keyset and the count of matched keys using the keys-2 Pact function.
| Argument | Type | Description |
|---|---|---|
count | integer | Specifies the total count of keys in the keyset. |
matched | integer | Specifies the count of matched keys. |
Return value
The keys-2 function returns a boolean value indicating whether at least two keys are matched in the keyset.
Examples
The following example demonstrates the use of keys-2 in the Pact REPL:
pact>(keys-2 3 1)falsepact>(keys-2 3 1)falseIn this example, keys-2 checks if at least two keys are matched in a keyset where the total count of keys is 3 and only 1 key is matched. The function returns false, indicating that 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 keys in the keyset are matched.
Basic syntax
To use keys-all to check if all keys in a keyset are matched, use the following syntax:
(keys-all count matched)
Arguments
Use the following arguments to specify the count of keys in the keyset and the count of matched keys using the keys-all Pact function.
| Argument | Type | Description |
|---|---|---|
count | integer | Specifies the total count of keys in the keyset. |
matched | integer | Specifies the count 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 the use of keys-all in the Pact REPL:
pact>(keys-all 3 3)truepact>(keys-all 3 3)trueIn this example, keys-all checks if all keys are matched in a keyset where the total count of keys is 3 and all 3 keys are matched. The function returns true, indicating that 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:
(keys table)
Arguments
Use the following argument to specify the table from which you want to retrieve keys using the keys Pact function.
| Argument | Type | Description |
|---|---|---|
table | table<{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 the use of keys in the Pact REPL to retrieve all keys present in the "accounts" table:
(keys accounts)(keys accounts)In this example, all keys present in the "accounts" table are returned as a list of strings.