Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"contracts/vesting_schedule",
"contracts/insurance",
"contracts/whitelist",
"contracts/content_license",
"contracts/anti_bot",
"contracts/skill_rating",
"contracts/governance",
Expand Down
16 changes: 16 additions & 0 deletions contracts/content_license/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "content_license"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }

[features]
testutils = ["soroban-sdk/testutils"]
106 changes: 106 additions & 0 deletions contracts/content_license/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Content License Registry (Soroban Smart Contract)

A decentralized puzzle and quest content licensing smart contract built for Soroban on the Stellar network. It allows creators to register, version, and manage licensing terms for game content on-chain.

---

## 🚀 Features

- **Creator-Gated Registration**: Only the authenticated creator of a puzzle can publish or update its license terms.
- **Immutable Term Versions**: Published terms cannot be modified in place. Any update requires publishing a new version, strictly preserving historical license records.
- **Flexible License Types**: Supports `FreeToPlay`, `PaidPerPlay` (with customizable fees), and `RemixAllowed`.
- **Fast Lookups**: Exposes `get_current_terms` for the latest active terms, as well as `get_terms_by_version` for auditability.
- **Event Emission**: Emits standard Soroban events `(license, puzzle_id) -> (creator, version)` on every published terms release.
- **Admin Governance**: Supports secure role transfer with two-way cryptographic verification.

---

## 📦 Data Structures

### `LicenseType`

```rust
#[contracttype]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum LicenseType {
FreeToPlay = 1,
PaidPerPlay = 2,
RemixAllowed = 3,
}
```

### `LicenseTerms`

```rust
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LicenseTerms {
pub creator: Address,
pub puzzle_id: u64,
pub version: u32,
pub license_type: LicenseType,
pub fee_amount: u64,
pub terms_uri: Symbol,
pub published_at: u64,
}
```

### Storage Keys

| Key | Storage Tier | Value Type | Description |
|:---|:---|:---|:---|
| `Symbol("ADMIN")` | Instance | `Address` | Current contract administrator |
| `(Symbol("CREATOR"), u64)` | Persistent | `Address` | Registered creator of a puzzle |
| `(Symbol("LATEST"), u64)` | Persistent | `u32` | Latest published version number for a puzzle |
| `(Symbol("TERMS"), u64, u32)` | Persistent | `LicenseTerms` | Immutable terms record for a puzzle version |

---

## 🛡️ Error Codes

```rust
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum LicenseError {
NotAuthorized = 1, // Caller is not the registered creator or admin
PuzzleNotFound = 2, // Puzzle ID has not been registered
VersionNotFound = 3, // Requested version number does not exist
AdminNotFound = 4, // Admin address has not been set
AlreadyInitialized = 5, // Contract has already been initialized
}
```

---

## 📡 Events

| Event Topic | Event Payload | Description |
|:---|:---|:---|
| `(Symbol("license"), u64)` | `(Address, u32)` | Emitted when terms are published: `(creator, new_version)` |

---

## 💻 Contract Interface

```rust
pub trait ContentLicenseInterface {
fn initialize(env: Env, admin: Address) -> Result<(), LicenseError>;
fn register_terms(env: Env, creator: Address, puzzle_id: u64, license_type: LicenseType, fee_amount: u64, terms_uri: Symbol) -> Result<u32, LicenseError>;
fn get_current_terms(env: Env, puzzle_id: u64) -> Option<LicenseTerms>;
fn get_terms_by_version(env: Env, puzzle_id: u64, version: u32) -> Option<LicenseTerms>;
fn get_creator(env: Env, puzzle_id: u64) -> Option<Address>;
fn get_latest_version(env: Env, puzzle_id: u64) -> u32;
fn transfer_admin(env: Env, current_admin: Address, new_admin: Address) -> Result<(), LicenseError>;
fn get_admin(env: Env) -> Option<Address>;
}
```

---

## 🧪 Testing

```bash
cargo check --package content_license
```
183 changes: 183 additions & 0 deletions contracts/content_license/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#![no_std]

use soroban_sdk::{
contract, contractimpl, contracttype, contracterror, contractmeta,
Address, Env, Symbol, symbol_short,
};

mod test;

// Contract metadata
contractmeta!(
key = "Description",
val = "Content License Registry contract for managing immutable versioned puzzle licenses"
);

#[contracttype]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum LicenseType {
FreeToPlay = 1,
PaidPerPlay = 2,
RemixAllowed = 3,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LicenseTerms {
pub creator: Address,
pub puzzle_id: u64,
pub version: u32,
pub license_type: LicenseType,
pub fee_amount: u64,
pub terms_uri: Symbol,
pub published_at: u64,
}

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum LicenseError {
NotAuthorized = 1,
PuzzleNotFound = 2,
VersionNotFound = 3,
AdminNotFound = 4,
AlreadyInitialized = 5,
}

// Storage keys
const ADMIN: Symbol = symbol_short!("ADMIN");
const CREATOR: Symbol = symbol_short!("CREATOR");
const LATEST_VER: Symbol = symbol_short!("LATEST");
const TERMS: Symbol = symbol_short!("TERMS");

#[contract]
pub struct ContentLicenseContract;

#[contractimpl]
impl ContentLicenseContract {
/// Initialize the contract with an admin address
pub fn initialize(env: Env, admin: Address) -> Result<(), LicenseError> {
admin.require_auth();
if env.storage().instance().has(&ADMIN) {
return Err(LicenseError::AlreadyInitialized);
}
env.storage().instance().set(&ADMIN, &admin);
Ok(())
}

/// Register license terms for a puzzle. Only the puzzle creator can register terms.
/// If registering for the first time, the caller becomes the registered creator.
/// Each publication increments the version. Terms once published are strictly immutable.
pub fn register_terms(
env: Env,
creator: Address,
puzzle_id: u64,
license_type: LicenseType,
fee_amount: u64,
terms_uri: Symbol,
) -> Result<u32, LicenseError> {
creator.require_auth();

let creator_key = (CREATOR, puzzle_id);
let stored_creator: Option<Address> = env.storage().persistent().get(&creator_key);

match stored_creator {
Some(existing_creator) => {
if existing_creator != creator {
return Err(LicenseError::NotAuthorized);
}
}
None => {
env.storage().persistent().set(&creator_key, &creator);
}
}

let ver_key = (LATEST_VER, puzzle_id);
let current_version: u32 = env.storage().persistent().get(&ver_key).unwrap_or(0);
let new_version = current_version.checked_add(1).unwrap();

let terms = LicenseTerms {
creator: creator.clone(),
puzzle_id,
version: new_version,
license_type,
fee_amount,
terms_uri: terms_uri.clone(),
published_at: env.ledger().timestamp(),
};

let terms_key = (TERMS, puzzle_id, new_version);
env.storage().persistent().set(&terms_key, &terms);
env.storage().persistent().set(&ver_key, &new_version);

// Emit registration event: (license, puzzle_id) -> (creator, new_version)
env.events().publish(
(symbol_short!("license"), puzzle_id),
(creator, new_version),
);

Ok(new_version)
}

/// Retrieve the current (latest version) terms for a puzzle
pub fn get_current_terms(env: Env, puzzle_id: u64) -> Option<LicenseTerms> {
let ver_key = (LATEST_VER, puzzle_id);
let latest_version: u32 = env.storage().persistent().get(&ver_key)?;
let terms_key = (TERMS, puzzle_id, latest_version);
env.storage().persistent().get(&terms_key)
}

/// Retrieve specific version terms for a puzzle
pub fn get_terms_by_version(
env: Env,
puzzle_id: u64,
version: u32,
) -> Option<LicenseTerms> {
let terms_key = (TERMS, puzzle_id, version);
env.storage().persistent().get(&terms_key)
}

/// Get registered creator for a puzzle
pub fn get_creator(env: Env, puzzle_id: u64) -> Option<Address> {
let creator_key = (CREATOR, puzzle_id);
env.storage().persistent().get(&creator_key)
}

/// Get latest version number for a puzzle
pub fn get_latest_version(env: Env, puzzle_id: u64) -> u32 {
let ver_key = (LATEST_VER, puzzle_id);
env.storage().persistent().get(&ver_key).unwrap_or(0)
}

/// Transfer contract admin role. Admin only.
pub fn transfer_admin(
env: Env,
current_admin: Address,
new_admin: Address,
) -> Result<(), LicenseError> {
Self::require_admin(&env, &current_admin)?;
new_admin.require_auth();

env.storage().instance().set(&ADMIN, &new_admin);
Ok(())
}

/// Get current admin
pub fn get_admin(env: Env) -> Option<Address> {
env.storage().instance().get(&ADMIN)
}

// Helper functions
fn require_admin(env: &Env, admin: &Address) -> Result<(), LicenseError> {
admin.require_auth();

let stored_admin: Option<Address> = env.storage().instance().get(&ADMIN);

match stored_admin {
Some(stored) if stored == *admin => Ok(()),
Some(_) => Err(LicenseError::NotAuthorized),
None => Err(LicenseError::AdminNotFound),
}
}
}
Loading