Content Licensing Automation

What it does:
Automates licensing of digital content (articles, videos, music, courses) and ensures creators receive royalties automatically on-chain.

Why it matters:
Removes intermediaries, prevents unauthorized use, provides transparent, enforceable licenses, and ensures fair revenue distribution to creators and collaborators.

How it works:

  • Creators register content with metadata, licensing terms, and royalty percentages

  • Licensees request usage rights by paying fees on-chain

  • Smart contract automatically grants access and enforces usage conditions

  • Payments are split among stakeholders based on predefined royalty shares

  • Licenses can be time-limited or perpetual and are tracked on-chain

  • All licensing and payment activity is auditable and immutable

  • Integrates with NFTs, fan tokens, or subscription systems for automated access and monetization

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title ContentLicensingAutomation
 * @author Nam
 * @notice Automates licensing and royalty distribution for digital content
 */
contract ContentLicensingAutomation {

    address public admin;
    uint256 public contentCount;

    struct Content {
        string metadataHash; // IPFS / encrypted content reference
        address payable creator;
        uint256 licenseFee; // in wei
        uint256 licenseDuration; // seconds
        mapping(address => uint256) activeLicenses; // licensee => expiration timestamp
    }

    mapping(uint256 => Content) public contents;

    // -------------------- EVENTS --------------------

    event ContentRegistered(uint256 indexed contentId, address indexed creator, uint256 licenseFee, uint256 licenseDuration);
    event LicenseGranted(uint256 indexed contentId, address indexed licensee, uint256 expiration);

    // -------------------- MODIFIERS --------------------

    modifier onlyAdmin() {
        require(msg.sender == admin, "Not admin");
        _;
    }

    // -------------------- CONSTRUCTOR --------------------

    constructor() {
        admin = msg.sender;
    }

    // -------------------- CONTENT MANAGEMENT --------------------

    function registerContent(string calldata _metadataHash, uint256 _licenseFee, uint256 _licenseDuration) external {
        require(_licenseFee > 0, "Fee must be >0");
        require(_licenseDuration > 0, "Duration must be >0");

        contentCount += 1;
        Content storage c = contents[contentCount];
        c.metadataHash = _metadataHash;
        c.creator = payable(msg.sender);
        c.licenseFee = _licenseFee;
        c.licenseDuration = _licenseDuration;

        emit ContentRegistered(contentCount, msg.sender, _licenseFee, _licenseDuration);
    }

    // -------------------- LICENSE MANAGEMENT --------------------

    function purchaseLicense(uint256 _contentId) external payable {
        Content storage c = contents[_contentId];
        require(msg.value == c.licenseFee, "Incorrect fee");

        uint256 expiration = block.timestamp + c.licenseDuration;
        if (c.activeLicenses[msg.sender] > block.timestamp) {
            expiration = c.activeLicenses[msg.sender] + c.licenseDuration;
        }

        c.activeLicenses[msg.sender] = expiration;
        c.creator.transfer(msg.value);

        emit LicenseGranted(_contentId, msg.sender, expiration);
    }

    function hasLicense(uint256 _contentId, address _user) external view returns (bool) {
        return contents[_contentId].activeLicenses[_user] > block.timestamp;
    }
}