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]
)

Public API

  • Adds a new context rule to the connected smart account.

    Validates the inputs, builds the matching add_context_rule invocation, and routes the resulting host function through either the single-signer or the multi-signer submission path.

    Contract limits enforced before submission:

    Throws

    Throws:

    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 -> OZTransactionResult

    Parameters

    contextType

    Operation-matching type for the rule.

    name

    Human-readable rule name (must be non-empty).

    validUntil

    Optional ledger number when this rule expires; nil means the rule never expires.

    signers

    Signers authorized by this rule. Either signers or policies must be non-empty.

    policies

    Map of policy contract addresses (C… strkey) to their installation parameters encoded as SCValXDR. Keys are sorted by XDR-byte order before submission to satisfy Soroban’s SCMap ordering invariant.

    selectedSigners

    Optional multi-signer participants list. Empty routes through single-signer submission; non-empty routes through the multi-signer collaborator.

    forceMethod

    Optional submission-method override.

    Return Value

    An OZTransactionResult describing the on-chain outcome.

  • getContextRule(id:) Asynchronous

    Returns the raw SCValXDR payload for the rule with the given id. For a typed view, parse with parseContextRule(scVal:) or use listContextRules().

    Declaration

    Swift

    public func getContextRule(id: UInt32) async throws -> SCValXDR

    Parameters

    id

    The context-rule identifier to look up.

    Return Value

    The raw SCValXDR returned by the contract.

  • getContextRulesCount() Asynchronous

    Retrieves 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

    Declaration

    Swift

    public func getContextRulesCount() async throws -> UInt32

    Return Value

    The active rule count parsed as UInt32.

  • Retrieves every active context rule on the connected contract as raw SCValXDR map payloads, with an optional per-call scan upper bound override.

    When maxScanId is non-nil the supplied value is used as the scan upper bound in place of the kit-level default (maxContextRuleScanId). Pass nil to 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 SimulationFailed by the underlying simulation, until either the active rule count has been collected or the effective scan upper bound is reached.

    Throws

    Declaration

    Swift

    public func getAllContextRules(maxScanId: UInt32? = nil) async throws -> [SCValXDR]

    Parameters

    maxScanId

    Per-call scan upper bound override. When nil, the kit configuration’s maxContextRuleScanId is used.

    Return Value

    One raw SCValXDR per active context rule in ascending id order.

  • 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 maxScanId is non-nil the supplied value is used as the scan upper bound in place of the kit-level default (maxContextRuleScanId). Pass nil to use the kit default.

    Fetches the raw payloads through getAllContextRules(maxScanId:) and parses each via parseContextRule(scVal:).

    Throws

    Declaration

    Swift

    public func listContextRules(maxScanId: UInt32? = nil) async throws -> [OZParsedContextRule]

    Parameters

    maxScanId

    Per-call scan upper bound override. When nil, the kit configuration’s maxContextRuleScanId is used.

    Return Value

    Parsed context rules in ascending rule-id order.

Private helpers