addPolicy

suspend fun addPolicy(contextRuleId: UInt, policyAddress: String, installParams: SCValXdr, selectedSigners: List<SelectedSigner> = emptyList(), forceMethod: SubmissionMethod? = null): TransactionResult

Adds a policy to a context rule with custom installation parameters.

This is the generic method that addSimpleThreshold, addWeightedThreshold, and addSpendingLimit delegate to. Use this method directly when adding custom policy contracts that are not covered by the convenience methods.

Flow:

  1. Validates inputs (connected wallet, policy address format)

  2. Builds contract invocation for add_policy

  3. Submits transaction via transactionOps (handles simulation, signing, polling)

IMPORTANT: This operation requires the connected wallet to have authorization on the smart account. The user will be prompted for biometric authentication to sign the transaction.

Contract limits:

Note: The contract returns a u32 policy ID for the newly added policy. The ID is available via ParsedContextRule.policyIds after fetching the context rule and can be used with OZPolicyManager.removePolicy to remove the policy.

Return

TransactionResult indicating success or failure

Parameters

contextRuleId

The context rule ID to add the policy to (0 for Default rule)

policyAddress

The policy contract address (C-address)

installParams

Policy-specific installation parameters encoded as SCValXdr. The structure depends on the policy contract. For built-in policy types, use PolicyInstallParams.SimpleThreshold.toScVal, PolicyInstallParams.WeightedThreshold.toScVal, or PolicyInstallParams.SpendingLimit.toScVal.

selectedSigners

Optional list of signers for multi-signer authorization. When empty (default), uses single-signer auth with the connected passkey. When non-empty, coordinates signatures from all listed signers.

forceMethod

Optional submission method override. When null (default), uses the configured submission method (relayer if available, RPC otherwise).

Throws

if validation fails

if transaction submission fails

Example:

// Add a custom policy with manually constructed install parameters
val installParams = Scv.toMap(linkedMapOf(
Scv.toSymbol("max_operations") to Scv.toUint32(5u),
Scv.toSymbol("allowed_contracts") to Scv.toVec(listOf(
Scv.toAddress(Address("CBCD1234...").toSCAddress())
))
))

val result = policyManager.addPolicy(
contextRuleId = 0u,
policyAddress = "CBCD5678...",
installParams = installParams
)

if (result.success) {
println("Custom policy added: ${result.hash ?: ""}")
} else {
println("Failed to add policy: ${result.error ?: ""}")
}