|
| 1 | +# Stellar Access Control |
| 2 | + |
| 3 | +Access Control, Ownable, and Role Transfer utilities for Stellar contracts. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This package provides three main modules for managing access control in Soroban smart contracts: |
| 8 | + |
| 9 | +- **Access Control**: Role-based access control with hierarchical permissions |
| 10 | +- **Ownable**: Simple single-owner access control pattern |
| 11 | +- **Role Transfer**: Utility module for secure role and ownership transfers |
| 12 | + |
| 13 | +## Modules |
| 14 | + |
| 15 | +### Access Control |
| 16 | + |
| 17 | +The `access_control` module provides comprehensive role-based access control functionality: |
| 18 | + |
| 19 | +- **Admin Management**: Single overarching admin with full privileges |
| 20 | +- **Role Hierarchy**: Roles can have admin roles that can grant/revoke permissions |
| 21 | +- **Secure Transfers**: Two-step admin transfer process for security |
| 22 | + |
| 23 | +#### Usage Examples |
| 24 | + |
| 25 | +```rust |
| 26 | +use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env}; |
| 27 | +use stellar_access::access_control::{self as access_control, AccessControl}; |
| 28 | +use stellar_macros::default_impl; |
| 29 | + |
| 30 | +#[contract] |
| 31 | +pub struct MyContract; |
| 32 | + |
| 33 | +#[contractimpl] |
| 34 | +impl MyContract { |
| 35 | + pub fn __constructor(e: &Env, admin: Address) { |
| 36 | + access_control::set_admin(e, &admin); |
| 37 | + } |
| 38 | + |
| 39 | + pub fn admin_restricted_function(e: &Env) { |
| 40 | + access_control::enforce_admin_auth(e); |
| 41 | + // ... |
| 42 | + } |
| 43 | + |
| 44 | + pub fn mint(e: &Env, to: Address, token_id: u32, caller: Address) { |
| 45 | + access_control::ensure_role(e, &caller, &symbol_short!("minter")); |
| 46 | + caller.require_auth(); |
| 47 | + // minting |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[default_impl] |
| 52 | +#[contractimpl] |
| 53 | +impl AccessControl for MyContract {} |
| 54 | +``` |
| 55 | + |
| 56 | +**With Macros** (requires `stellar-macros` dependency): |
| 57 | + |
| 58 | +```rust |
| 59 | +use stellar_macros::{only_admin, only_role}; |
| 60 | + |
| 61 | +#[only_admin] |
| 62 | +pub fn admin_restricted_function(e: &Env) { |
| 63 | + // ... |
| 64 | +} |
| 65 | + |
| 66 | +#[only_role(caller, "minter")] |
| 67 | +fn mint(e: &Env, to: Address, token_id: u32, caller: Address) { |
| 68 | + // ... |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Ownable |
| 73 | + |
| 74 | +The `ownable` module implements a simple ownership pattern: |
| 75 | + |
| 76 | +- **Single Owner**: Contract has one owner with exclusive access |
| 77 | +- **Ownership Transfer**: Secure two-step ownership transfer |
| 78 | +- **Ownership Renouncement**: Owner can renounce ownership |
| 79 | + |
| 80 | +#### Usage Examples |
| 81 | + |
| 82 | +```rust |
| 83 | +use soroban_sdk::{contract, contractimpl, Address, Env}; |
| 84 | +use stellar_access::ownable::{self as ownable, Ownable}; |
| 85 | +use stellar_macros::default_impl; |
| 86 | + |
| 87 | +#[contract] |
| 88 | +pub struct MyContract; |
| 89 | + |
| 90 | +#[contractimpl] |
| 91 | +impl MyContract { |
| 92 | + pub fn __constructor(e: &Env, owner: Address) { |
| 93 | + ownable::set_owner(e, &owner); |
| 94 | + } |
| 95 | + |
| 96 | + pub fn owner_restricted_function(e: &Env) { |
| 97 | + ownable::enforce_owner_auth(e); |
| 98 | + // ... |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[default_impl] |
| 103 | +#[contractimpl] |
| 104 | +impl Ownable for MyContract {} |
| 105 | +``` |
| 106 | + |
| 107 | +**With Macros** (requires `stellar-macros` dependency): |
| 108 | + |
| 109 | +```rust |
| 110 | +use stellar_macros::only_owner; |
| 111 | + |
| 112 | +#[only_owner] |
| 113 | +pub fn owner_restricted_function(e: &Env) { |
| 114 | + // ... |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Role Transfer |
| 119 | + |
| 120 | +The `role_transfer` module is a utility module that provides the underlying infrastructure for secure two-step role and ownership transfers used by both Access Control and Ownable modules. |
| 121 | + |
| 122 | +## Security Model |
| 123 | + |
| 124 | +Both Access Control and Ownable modules implement a **two-step transfer process** for critical role changes: |
| 125 | + |
| 126 | +1. **Initiate Transfer**: Current admin/owner specifies the new recipient and expiration |
| 127 | +2. **Accept Transfer**: Designated recipient must explicitly accept the transfer |
| 128 | + |
| 129 | +This mechanism prevents accidental transfers to wrong addresses or loss of control due to typos or errors. |
| 130 | + |
| 131 | +**Note**: Unlike OpenZeppelin's Solidity library where role transfers can be immediate, **all role transfers in this Stellar library are always two-step processes** for enhanced security. This applies to both ownership transfers and admin role transfers. |
| 132 | + |
| 133 | +## Installation |
| 134 | + |
| 135 | +Add this to your `Cargo.toml`: |
| 136 | + |
| 137 | +```toml |
| 138 | +[dependencies] |
| 139 | +# We recommend pinning to a specific version, because rapid iterations are expected as the library is in an active development phase. |
| 140 | +stellar-access = "=0.4.0" |
| 141 | +# Add this if you want to use macros |
| 142 | +stellar-macros = "=0.4.0" |
| 143 | +``` |
| 144 | + |
| 145 | +## Examples |
| 146 | + |
| 147 | +See the following examples in the repository: |
| 148 | +- [`examples/ownable/`](https://github.com/OpenZeppelin/stellar-contracts/tree/main/examples/ownable) - Simple ownership pattern |
| 149 | +- [`examples/nft-access-control/`](https://github.com/OpenZeppelin/stellar-contracts/tree/main/examples/nft-access-control) - Role-based access control |
| 150 | + |
| 151 | +## License |
| 152 | + |
| 153 | +This package is part of the Stellar Contracts library and follows the same licensing terms. |
0 commit comments