A secure Clarity smart contract for Stacks blockchain that enables account recovery through trusted guardians with time-delayed execution.
This contract provides a robust account recovery mechanism that protects users from lost private keys while preventing unauthorized takeovers through mandatory time-lock periods.
A trusted individual or entity designated by the account owner who can initiate recovery on their behalf.
A mandatory waiting period (1-30 days by default) between recovery initiation and execution, giving the legitimate owner time to cancel fraudulent recovery attempts.
- Account owner sets up recovery configuration with a guardian
- If access is lost, guardian initiates recovery
- Time-lock period begins
- After time-lock expires, guardian executes recovery
- Account ownership transfers to the new address
- Configurable Time-Locks: Set custom waiting periods within admin-defined boundaries
- Guardian Management: Designate and update trusted recovery agents
- Recovery History: Complete audit trail of all recovery events
- Cancel Protection: Original owner can cancel pending recoveries
- Activation Controls: Enable/disable recovery without removing configuration
- Fee-Based Spam Prevention: Recovery initiation requires STX payment
- Multi-Recovery Support: Track multiple recovery events per account
(setup-recovery (guardian principal) (time-lock-period uint))Initialize recovery configuration for your account.
- guardian: Principal address of trusted recovery agent
- time-lock-period: Waiting period in blocks (144-4320 blocks)
(update-recovery-config (guardian principal) (time-lock-period uint))Modify existing recovery settings. Requires no pending recoveries.
(initiate-recovery (account principal) (new-owner principal))Guardian-only function to start recovery process. Requires recovery fee payment.
(execute-recovery (account principal))Guardian completes recovery after time-lock expires. Transfers ownership and configuration.
(cancel-recovery)Account owner cancels their pending recovery immediately.
Temporarily disable or re-enable recovery capability without deleting configuration.
get-recovery-config: View account's recovery settingsget-pending-recovery: Check active recovery attemptsget-recovery-history: Retrieve past recovery recordsget-recovery-count: Total recoveries performed on accountis-recovery-ready: Verify if time-lock has elapsedget-min-time-lock/get-max-time-lock: View time-lock boundariesget-recovery-fee: Check current recovery initiation cost
set-min-time-lock: Adjust minimum time-lock periodset-max-time-lock: Adjust maximum time-lock periodset-recovery-fee: Update recovery initiation fee
- Self-Recovery Prevention: Cannot set yourself as guardian or recovery address
- Time-Lock Enforcement: Mandatory waiting period prevents instant takeovers
- Single Recovery Limit: One pending recovery at a time
- Guardian Authorization: Only designated guardian can initiate/execute
- Cancellation Rights: Original owner maintains control during time-lock
The contract includes comprehensive error codes:
u100: Owner-only operationu101: Configuration not foundu102: Unauthorized accessu103: Configuration already existsu104: Time-lock still activeu105: No pending recoveryu106: Invalid time-lock periodu107: Self-recovery attemptu108: Recovery already executed
;; Alice sets up recovery with Bob as guardian, 1-day time-lock
(contract-call? .time-locked-recovery setup-recovery 'SP2BOB... u144);; Bob initiates recovery to new address for Alice
(contract-call? .time-locked-recovery initiate-recovery 'SP2ALICE... 'SP2ALICE-NEW...);; After 144 blocks, Bob completes the recovery
(contract-call? .time-locked-recovery execute-recovery 'SP2ALICE...);; Alice detects unauthorized recovery and cancels it
(contract-call? .time-locked-recovery cancel-recovery)- Minimum Time-Lock: 144 blocks (~1 day)
- Maximum Time-Lock: 4,320 blocks (~30 days)
- Recovery Fee: 1,000,000 microSTX (1 STX)
- Choose Trusted Guardians: Select someone reliable who won't collude against you
- Set Appropriate Time-Locks: Balance security (longer) vs. accessibility (shorter)
- Monitor Your Account: Regularly check for pending recoveries
- Secure Guardian's Keys: Guardian compromise can lead to account takeover
- Update Contact Info: Ensure guardian can reach you during time-lock period
- Test the Process: Consider test runs with minimal-value accounts first
- Guardian must maintain access to their keys
- No built-in dispute resolution mechanism
- Recovery fee is non-refundable
- Time-lock cannot be bypassed even in emergencies
- Contract owner has administrative privileges