Skip to main content

Tutorial 1: time based restriction on token transfers

Imagine a multi-sig account contract where certain actions, like transferring tokens, need to be controlled by the time elapsed between consecutive transfers. For example, a token transfer should only be allowed if a certain period of time has passed since the last transfer. This can be useful in scenarios where you want to limit the frequency of transactions to prevent misuse or to implement rate limiting.

Base concepts

Time-based restrictions

The idea is to enforce a minimum time interval between consecutive token transfers. Each token can have its own time limit, and the contract will track the last transfer time for each token.

Data storage

The contract will store the time limit and last transfer time for each token in its storage to keep track of these values across transactions.

Code walkthrough

Contract structure and imports

Contract initialization

  1. Initializes the contract with a list of signers’ public keys
  2. Stores the count of signers in the contract’s storage

Setting time limits

  1. Allows setting a time limit for a specific token
  2. Ensures that only the contract itself can set these limits by requiring the contract’s authorization

Custom authentication and authorization

  1. Authenticates the signatures
  2. Checks if all required signers have signed
  3. Iterates through the authorization context to verify the authorization policy

Authentication logic

  1. Verifies that the signatures are in the correct order and that each signer is authorized
  2. Uses ed25519_verify function to verify each signature

Authorization policy verification

  1. Checks if the function being called is a transfer or approve function
  2. Enforces the time-based restriction by comparing the current time with the last transfer time
  3. Updates the last transfer time if the transfer is allowed

Summary

The contract begins by initializing with a set of authorized signers. It allows setting a time limit for token transfers, which controls how frequently a token can be transferred. The __check_auth function is the core of the authorization process, ensuring that all necessary signatures are valid and checking the time-based restriction for token transfers. If the required time has not passed since the last transfer, the contract will deny the operation, enforcing the desired rate limiting. By tracking the last transfer time and enforcing a minimum time interval between transfers, the contract effectively limits the frequency of token transfers, resolving the issue of potential abuse through rapid consecutive transactions.

Complete code

Here are all the snippets stacked together in a single file for convenience:
These are the test cases:

Tutorial 2: implementing a smart wallet (WebAuthn)

Imagine a world where traditional passwords are obsolete. In this world, WebAuthn (Web Authentication) has become the standard for secure online interactions. Alice, a blockchain enthusiast, wants to create a wallet that leverages WebAuthn technology for enhanced security. She decides to implement a WebAuthn wallet on Stellar, allowing users to manage their digital assets using their device’s biometric features or security keys (e.g., YubiKey, Google Titan Security Key, etc.).

Base concepts

WebAuthn is a web standard for passwordless authentication. It allows users to authenticate using biometrics (like fingerprints or facial recognition). The WebAuthn wallet implemented in this tutorial will be able to:
  1. Register and manage user credentials (public keys)
  2. Authenticate users for transaction signing
  3. Differentiate between admin and regular users
  4. Allow contract updates for future improvements
This tutorial’s code credit goes to @kalepail’s work on passkeys, which you can explore more here.

Code walkthrough

Contract structure and imports

This section sets up the contract structure and imports necessary components from the Soroban SDK.

Error definitions

This enum defines possible errors that can occur during contract execution.

Core contract functions

These functions handle adding and removing signers, updating the contract, and managing admin counts.

Signature structure

This structure represents a WebAuthn signature.

CustomAccountInterface implementation

This implements the core authentication logic for the WebAuthn wallet.

Base64 url encoding

This function provides Base64 URL encoding functionality used in the WebAuthn process.

Written explanation of code

The WebAuthn wallet contract manages user credentials and authentication. It allows adding and removing signers, distinguishing between admin and regular users. The add function registers new signers, storing admin keys persistently and regular keys temporarily. The remove function deletes signers, and update allows contract upgrades. The core of the wallet’s security is the __check_auth function, which verifies WebAuthn signatures. It checks the signature against the stored public key, verifies the client data JSON, and ensures the challenge matches the expected value. The contract uses Soroban’s storage capabilities to manage keys and admin counts, with different TTLs (Time To Live) for persistent and temporary storage.

Test cases

These are test cases to ensure our WebAuthn wallet is functioning correctly. We’ll use the Soroban SDK’s testing utilities to create and run these tests.

Summary

This WebAuthn wallet implementation provides a secure and user-friendly way to manage digital assets on Stellar. It leverages the security benefits of WebAuthn while maintaining the flexibility needed for blockchain interactions.

Complete code

Here are all the snippets stacked together in a single file for convenience: