Fund Unlocking

Complete the escrow lifecycle by implementing fund unlocking functionality to allow users to retrieve their locked funds.

Introduction

In this final part, we'll implement the fund unlocking functionality to complete the escrow lifecycle. This will allow users to retrieve their funds from the escrow contract once they're ready. The unlocking process requires signature verification to ensure that only the rightful owner can access the funds.

Understanding the Fund Unlocking Process

The fund unlocking process involves these steps:

  1. Transaction Verification: The system checks that the transaction exists and is in the CONFIRMED(i.e. Locked) state

  2. Ownership Validation: The request must come from the same wallet that originally locked the funds

  3. Transaction Building: The application builds an unlock transaction that spends the locked UTXO

  4. Transaction Signing: The user signs the transaction with their wallet

  5. Transaction Submission: The signed transaction is submitted to the blockchain

  6. Status Update: The transaction status is updated to UNLOCKED in the database

Understanding the Smart Contract Validation

Our escrow application uses the Hello Aiken smart contract with two unlock conditions:

  1. Redeemer Message Validation: The redeemer must contain the exact message "Hello, World!" (hex-encoded)

  2. Owner Signature Verification: The transaction must be signed by the owner specified in the datum

The code implements these requirements through:

This dual validation ensures only the rightful owner can unlock their funds, while demonstrating the flexibility of Cardano's eUTXO model for custom validation logic.

Implementation Steps

1. Add Unlock Functions to the Anvil API Module

First, let's add the necessary functions to the Anvil API module for unlocking funds. Add unlockFunds to your existing src/lib/anvil-api.ts file:

2. Create the Unlock API Endpoint

Create an API endpoint for unlocking funds src/app/api/escrow/unlock/route.ts:

3. Update the Submit Transaction Endpoint

Update the submit endpoint to handle unlock transactions. This is the updated src/app/api/escrow/submit/route.ts file:

4. Update the Transaction Operations Hook

Update the transaction operations hook to include unlock functionality. This is the updated src/hooks/useTransactions.ts file:

useTransactions.ts

5. Update the MyTransactions Component

Finally, update the MyTransactions component src/components/MyTransactions.tsx to include unlock buttons for confirmed transactions:

  • Import the useTransactionOperations hook.

  • Add the Unlock Button to the table row.

  • Add click handler function that uses unlockFunds from useTransactionOperations hook.

  • Add loading state for the unlock button.

  • Add error state for the unlock button.

This is the updated MyTransactions component:

MyTransactions.tsx

Security Features of the Unlocking Process

Several security measures are implemented in the unlocking process:

  1. Status Verification: Only transactions in the CONFIRMED state can be unlocked

  2. Address Validation: The transaction is validated against the owner's address

  3. Smart Contract Validation: The escrow script verifies that the unlocking is performed by the original owner

Testing the Complete Escrow Cycle

Let's test the full escrow cycle:

  1. Start your development server:

  1. Navigate to http://localhost:3000 in your browser

  2. Connect your wallet

  3. Lock funds using the lock funds form from Part 3

  4. Wait for the transaction to be confirmed (You'll see the status change in the MyTransactions component)

  5. Click the "Unlock Funds" button on the confirmed transaction

  6. Sign the transaction with your wallet

  7. Observe the transaction status changing to UNLOCKED

  8. Verify that the funds have been returned to your wallet (minus network fees)

Troubleshooting

Transaction Not Unlockable

If you can't unlock a transaction:

  1. Verify that the transaction status is CONFIRMED (only confirmed transactions can be unlocked)

  2. Ensure you're using the same wallet that created the transaction

  3. Check that the network has processed the original transaction (check explorers like cardanoscan.io)

Signing Failures

If signing the unlock transaction fails:

  1. Make sure your wallet is unlocked and connected

  2. Check that your wallet has sufficient balance for the transaction fee

  3. Try disconnecting and reconnecting your wallet

Enhancements for a Production Application

For a production-ready escrow application, consider these enhancements:

  1. Time-locked Escrow: Add support for time-based unlocking conditions

  2. Multi-signature Escrow: Implement escrow that requires approval from multiple parties

  3. Improved Error Handling: Add more detailed error messages and recovery options

  4. Transaction Monitoring: Implement more robust transaction status monitoring

  5. Sign Lock/Unlock: Add support for signing lock and unlock transactions after users leave the page.

  6. Enhanced Security: Add additional verification steps for high-value transactions

Conclusion

In this guide series, you've built a fully functional Cardano escrow application that allows users to:

  1. Connect their Cardano wallets

  2. Lock funds in an escrow contract

  3. Monitor transaction status in real-time

  4. Unlock funds when they're ready

You've learned how to:

  • Integrate with Cardano wallets using Weld

  • Build and submit transactions using the Anvil API

  • Store and track transaction data in a SQLite database

  • Implement real-time updates with webhooks

  • Create a complete fund locking and unlocking cycle

This application provides a foundation for building more complex Cardano applications that leverage smart contracts for secure and transparent financial interactions.

Last updated

Was this helpful?