add Policy
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:
Validates inputs (connected wallet, policy address format)
Builds contract invocation for add_policy
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:
Maximum OZConstants.MAX_POLICIES policies per context rule
Policy address must be a valid C-address
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
The context rule ID to add the policy to (0 for Default rule)
The policy contract address (C-address)
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.
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.
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 ?: ""}")
}