OZContextRuleManager
public final class OZContextRuleManager : OZContextRuleManagerProtocol, OZManagerHelpers, @unchecked Sendable
Manages context rules for an OpenZeppelin Smart Account.
Context rules define authorization requirements per operation category. Each
rule specifies a context type (default, callContract, or createContract),
a name, signers, optional policies, and an optional expiration ledger. The
smart-account contract evaluates inbound transactions against the rule set to
determine which signers must sign and which policy contracts must approve.
Contract limits enforced at validation time: maxSigners signers
and maxPolicies policies per rule.
All state-changing methods accept an optional selectedSigners list. An
empty list routes through the single-signer path; a non-empty list routes
through the kit’s multi-signer manager.
Example:
let result = try await kit.contextRuleManager.addContextRule(
contextType: .callContract(contractAddress: tokenContractAddress),
name: "TokenTransfers",
signers: [signer1, signer2, signer3],
policies: [policyAddress: installParamsScVal]
)
-
addContextRule(contextType:Asynchronousname: validUntil: signers: policies: selectedSigners: forceMethod: ) Adds a new context rule to the connected smart account.
Validates the inputs, builds the matching
add_context_ruleinvocation, and routes the resulting host function through either the single-signer or the multi-signer submission path.Contract limits enforced before submission:
- Maximum
maxSignerssigners per rule. Maximum
maxPoliciespolicies per rule.
Throws
Throws:
NotConnectedwhen no wallet is connected.InvalidInputwhen validation fails.InvalidAddresswhen a policy address is malformed.SmartAccountTransactionExceptionfor simulation, signing, or submission failures.
Declaration
Swift
public func addContextRule( contextType: OZContextRuleType, name: String, validUntil: UInt32? = nil, signers: [any OZSmartAccountSigner], policies: [String: SCValXDR] = [:], selectedSigners: [OZSelectedSigner] = [], forceMethod: OZSubmissionMethod? = nil ) async throws -> OZTransactionResultParameters
contextTypeOperation-matching type for the rule.
nameHuman-readable rule name (must be non-empty).
validUntilOptional ledger number when this rule expires;
nilmeans the rule never expires.signersSigners authorized by this rule. Either
signersorpoliciesmust be non-empty.policiesMap of policy contract addresses (
C…strkey) to their installation parameters encoded asSCValXDR. Keys are sorted by XDR-byte order before submission to satisfy Soroban’sSCMapordering invariant.selectedSignersOptional multi-signer participants list. Empty routes through single-signer submission; non-empty routes through the multi-signer collaborator.
forceMethodOptional submission-method override.
Return Value
An
OZTransactionResultdescribing the on-chain outcome. - Maximum
-
getContextRule(id:Asynchronous) Returns the raw
SCValXDRpayload for the rule with the givenid. For a typed view, parse withparseContextRule(scVal:)or uselistContextRules().Declaration
Swift
public func getContextRule(id: UInt32) async throws -> SCValXDRParameters
idThe context-rule identifier to look up.
Return Value
The raw
SCValXDRreturned by the contract. -
getContextRulesCount()AsynchronousRetrieves the number of context rules currently configured on the connected smart account.
This is a read-only operation that issues a simulated invocation against the connected contract.
Throws
NotConnectedwhen no wallet is connected.SmartAccountTransactionExceptionwhen the simulation fails.InvalidInputwhen the on-chain result is not aU32.
Declaration
Swift
public func getContextRulesCount() async throws -> UInt32Return Value
The active rule count parsed as
UInt32. -
getAllContextRules(maxScanId:Asynchronous) Retrieves every active context rule on the connected contract as raw
SCValXDRmap payloads, with an optional per-call scan upper bound override.When
maxScanIdis non-nilthe supplied value is used as the scan upper bound in place of the kit-level default (maxContextRuleScanId). Passnilto use the kit default.The contract assigns monotonically increasing identifiers; when a rule is removed its slot becomes empty but the identifier is never reused, creating gaps. This method iterates identifiers from zero upward, skipping gaps reported as
SimulationFailedby the underlying simulation, until either the active rule count has been collected or the effective scan upper bound is reached.Throws
NotConnectedwhen no wallet is connected.SmartAccountTransactionExceptionfor non-gap simulation failures.SmartAccountValidationExceptionwhengetContextRulesCount()returns a non-U32value.
Declaration
Swift
public func getAllContextRules(maxScanId: UInt32? = nil) async throws -> [SCValXDR]Parameters
maxScanIdPer-call scan upper bound override. When
nil, the kit configuration’smaxContextRuleScanIdis used.Return Value
One raw
SCValXDRper active context rule in ascending id order. -
listContextRules(maxScanId:Asynchronous) Returns the parsed view of every active context rule on the connected smart account contract, with an optional per-call scan upper bound override.
When
maxScanIdis non-nilthe supplied value is used as the scan upper bound in place of the kit-level default (maxContextRuleScanId). Passnilto use the kit default.Fetches the raw payloads through
getAllContextRules(maxScanId:)and parses each viaparseContextRule(scVal:).Throws
NotConnectedwhen no wallet is connected.SmartAccountTransactionExceptionfor simulation failures.SmartAccountValidationExceptionwhen a payload cannot be parsed or the count is malformed.
Declaration
Swift
public func listContextRules(maxScanId: UInt32? = nil) async throws -> [OZParsedContextRule]Parameters
maxScanIdPer-call scan upper bound override. When
nil, the kit configuration’smaxContextRuleScanIdis used.Return Value
Parsed context rules in ascending rule-id order.
-
updateName(id:Asynchronousname: selectedSigners: forceMethod: ) Updates the human-readable name of an existing context rule.
The on-chain
namefield is metadata only — it has no effect on rule matching or enforcement.Declaration
Swift
public func updateName( id: UInt32, name: String, selectedSigners: [OZSelectedSigner] = [], forceMethod: OZSubmissionMethod? = nil ) async throws -> OZTransactionResultParameters
idThe context-rule identifier to update.
nameThe new name (must be non-empty).
selectedSignersOptional multi-signer participants list.
forceMethodOptional submission-method override.
Return Value
An
OZTransactionResultdescribing the on-chain outcome. -
Updates the expiration ledger of an existing context rule.
Pass
nilto clear the expiration (the rule becomes non-expiring). On chain the field isOption<u32>, encoded asVoidforNoneandU32forSome.Declaration
Swift
public func updateValidUntil( id: UInt32, validUntil: UInt32?, selectedSigners: [OZSelectedSigner] = [], forceMethod: OZSubmissionMethod? = nil ) async throws -> OZTransactionResultParameters
idThe context-rule identifier to update.
validUntilThe new expiration ledger, or
nilto clear.selectedSignersOptional multi-signer participants list.
forceMethodOptional submission-method override.
Return Value
An
OZTransactionResultdescribing the on-chain outcome. -
removeContextRule(id:AsynchronousselectedSigners: forceMethod: ) Removes a context rule from the connected smart account.
Removed rules leave a numeric gap in the identifier sequence that
getAllContextRules()skips during enumeration.Declaration
Swift
public func removeContextRule( id: UInt32, selectedSigners: [OZSelectedSigner] = [], forceMethod: OZSubmissionMethod? = nil ) async throws -> OZTransactionResultParameters
idThe context-rule identifier to remove.
selectedSignersOptional multi-signer participants list.
forceMethodOptional submission-method override.
Return Value
An
OZTransactionResultdescribing the on-chain outcome.
View on GitHub
Install in Dash