Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

In this article, I use Bunzz's DeCipher feature to examine the Nouns project's NounsSeeder contract.

DeCipher is a service that uses AI to generate smart contract documentation.

For a guide to using the service, see my earlier article.

This article reflects my experience using DeCipher. The generated explanations may contain mistakes; corrections are welcome. The original article was published in August 2023.

The DeCipher page used in this article is:

The corresponding Etherscan page is:

Overview

NounsSeeder plays an important role in the Nouns project on Ethereum.

What is a smart contract?

In blockchain and Web3 development, a smart contract is code deployed on a blockchain. It resembles a class in object-oriented programming in that it can contain state variables and functions and interact with other contracts. Smart contracts are fundamental building blocks of decentralized applications (dApps).

What is NounsSeeder?

NounsSeeder generates pseudorandom trait selections for the digital assets known as Nouns.

What is a seed?

In computing, a seed is an input used to initialize a pseudorandom process. With the same inputs and algorithm, the result can be reproduced. This property is useful when generating digital assets such as Nouns.

How does NounsSeeder work?

The contract provides a function called generateSeed, which accepts a Noun ID and a descriptor. The ID uniquely identifies a Noun token. The descriptor is another contract that supplies information about the available backgrounds, bodies, accessories, heads, and glasses.

The function hashes the previous block's hash together with the Noun ID. It uses portions of the resulting value to select an index for each trait category, using the corresponding trait count obtained from the descriptor.

Why is NounsSeeder important?

NounsSeeder determines the visual traits of each Noun. The resulting combinations create variation among the NFTs. However, the code does not enforce unique combinations: different token IDs may produce identical sets of traits. The previous block hash is also not a cryptographically secure source of unpredictable randomness for applications with adversarial selection incentives.

NounsSeeder provides the pseudorandom trait-generation mechanism for Nouns. It determines the appearance of each token using its ID, the previous block hash, and the available descriptor traits. Token IDs are unique, but visual trait combinations are not guaranteed to be unique.

Usage

The following steps illustrate how another Solidity contract can use NounsSeeder to obtain trait indices.

Using NounsSeeder

1. Import the contract

Use Solidity's import statement to import NounsSeeder.

solidity
import { NounsSeeder } from './NounsSeeder.sol';

2. Declare a contract instance

Declare a variable that will reference the deployed NounsSeeder contract. Initialize it with the appropriate address before use.

solidity
NounsSeeder public seeder;

3. Call generateSeed

Call generateSeed with the following arguments.

  • nounId
    • The unique identifier of the Noun whose seed is being generated.
  • descriptor
    • An instance of INounsDescriptor providing the counts of available backgrounds, bodies, accessories, heads, and glasses.
solidity
function generateNounSeed(uint256 nounId) public view returns (NounsSeeder.Seed memory) {
return seeder.generateSeed(nounId, descriptor);
}

The function returns a Seed struct containing the selected trait indices as uint48 values.

Example

The following example illustrates how to use NounsSeeder from a separate contract.

solidity
pragma solidity ^0.8.6;

import { NounsSeeder } from './NounsSeeder.sol';
import { INounsDescriptor } from './interfaces/INounsDescriptor.sol';

contract MyContract {
    NounsSeeder public seeder;
    INounsDescriptor public descriptor;

    constructor(NounsSeeder _seeder, INounsDescriptor _descriptor) {
        seeder = _seeder;
        descriptor = _descriptor;
    }

    function generateNounSeed(uint256 nounId) public view returns (NounsSeeder.Seed memory) {
        return seeder.generateSeed(nounId, descriptor);
    }
}

MyContract imports NounsSeeder and stores references to the seeder and descriptor. Calling generateNounSeed requests a seed for the supplied Noun ID.

Because the result depends on the previous block hash, calling the function in a different block can produce different trait indices. Reproducing an already minted Noun requires its stored seed or the original inputs, rather than simply calling this function again at a later time.

Contract

NounsSeeder

backgroundCount

solidity
uint256 backgroundCount = descriptor.backgroundCount();

Overview

The number of backgrounds available from the descriptor implementing INounsDescriptor.

Details

Calling descriptor.backgroundCount() retrieves the number of background options. This count may differ between descriptor instances or configurations. It determines the range of valid background indices used during trait selection.


bodyCount

solidity
uint256 bodyCount = descriptor.bodyCount();

Overview

The number of bodies available from the descriptor.

Details

Calling descriptor.bodyCount() retrieves the number of body options. The selected body forms part of the resulting NFT's appearance.


accessoryCount

solidity
uint256 accessoryCount = descriptor.accessoryCount();

Overview

The number of accessories available from the descriptor.

Details

Calling descriptor.accessoryCount() retrieves the number of accessory options. Accessories contribute to the visual variation of the generated NFTs.


headCount

solidity
uint256 headCount = descriptor.headCount();

Overview

The number of heads available from the descriptor.

Details

Calling descriptor.headCount() retrieves the number of head options. The selected head forms part of the resulting NFT's appearance.


glassesCount

solidity
uint256 glassesCount = descriptor.glassesCount();

Overview

The number of glasses available from the descriptor.

Details

Calling descriptor.glassesCount() retrieves the number of glasses options. The selected glasses contribute to the visual variation of the NFTs.


generateSeed

generateSeed
solidity
// SPDX-License-Identifier: GPL-3.0

/// @title The NounsToken pseudo-random seed generator

/*********************************
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░██░░░████░░██░░░████░░░ *
 * ░░██████░░░████████░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 *********************************/

pragma solidity ^0.8.6;

import { INounsSeeder } from './interfaces/INounsSeeder.sol';
import { INounsDescriptor } from './interfaces/INounsDescriptor.sol';

contract NounsSeeder is INounsSeeder {
    /**
     * @notice Generate a pseudo-random Noun seed using the previous blockhash and noun ID.
     */
    // prettier-ignore
    function generateSeed(uint256 nounId, INounsDescriptor descriptor) external view override returns (Seed memory) {
        uint256 pseudorandomness = uint256(
            keccak256(abi.encodePacked(blockhash(block.number - 1), nounId))
        );

        uint256 backgroundCount = descriptor.backgroundCount();
        uint256 bodyCount = descriptor.bodyCount();
        uint256 accessoryCount = descriptor.accessoryCount();
        uint256 headCount = descriptor.headCount();
        uint256 glassesCount = descriptor.glassesCount();

        return Seed({
            background: uint48(
                uint48(pseudorandomness) % backgroundCount
            ),
            body: uint48(
                uint48(pseudorandomness >> 48) % bodyCount
            ),
            accessory: uint48(
                uint48(pseudorandomness >> 96) % accessoryCount
            ),
            head: uint48(
                uint48(pseudorandomness >> 144) % headCount
            ),
            glasses: uint48(
                uint48(pseudorandomness >> 192) % glassesCount
            )
        });
}

Overview

Generates pseudorandom trait indices using the previous block hash and the Noun ID.

Details

The function hashes the previous block hash and Noun ID to produce a 256-bit value. It extracts successive 48-bit portions by shifting and casting the result, then uses modulo operations with each trait count to select valid indices.

The descriptor must provide nonzero counts for all five categories, otherwise a modulo operation will revert. The first excerpt above is reproduced from the original article and omits the final contract-closing brace; the complete contract appears below.

Parameters

  • nounId
    • The ID of the Noun NFT being generated.
  • descriptor
    • The interface providing the available trait counts.

Return value

  • Seed
    • A struct containing five pseudorandomly selected trait indices that determine the NFT's appearance.

Events

NounsSeeder does not define or emit any events in the displayed implementation.

Source code

NounsSeeder.sol

NounsSeeder.sol
solidity
// SPDX-License-Identifier: GPL-3.0

/// @title The NounsToken pseudo-random seed generator

/*********************************
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░██░░░████░░██░░░████░░░ *
 * ░░██████░░░████████░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 *********************************/

pragma solidity ^0.8.6;

import { INounsSeeder } from './interfaces/INounsSeeder.sol';
import { INounsDescriptor } from './interfaces/INounsDescriptor.sol';

contract NounsSeeder is INounsSeeder {
    /**
     * @notice Generate a pseudo-random Noun seed using the previous blockhash and noun ID.
     */
    // prettier-ignore
    function generateSeed(uint256 nounId, INounsDescriptor descriptor) external view override returns (Seed memory) {
        uint256 pseudorandomness = uint256(
            keccak256(abi.encodePacked(blockhash(block.number - 1), nounId))
        );

        uint256 backgroundCount = descriptor.backgroundCount();
        uint256 bodyCount = descriptor.bodyCount();
        uint256 accessoryCount = descriptor.accessoryCount();
        uint256 headCount = descriptor.headCount();
        uint256 glassesCount = descriptor.glassesCount();

        return Seed({
            background: uint48(
                uint48(pseudorandomness) % backgroundCount
            ),
            body: uint48(
                uint48(pseudorandomness >> 48) % bodyCount
            ),
            accessory: uint48(
                uint48(pseudorandomness >> 96) % accessoryCount
            ),
            head: uint48(
                uint48(pseudorandomness >> 144) % headCount
            ),
            glasses: uint48(
                uint48(pseudorandomness >> 192) % glassesCount
            )
        });
    }
}

INounsSeeder.sol

INounsSeeder.sol
solidity
// SPDX-License-Identifier: GPL-3.0

/// @title Interface for NounsSeeder

/*********************************
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░██░░░████░░██░░░████░░░ *
 * ░░██████░░░████████░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 *********************************/

pragma solidity ^0.8.6;

import { INounsDescriptor } from './INounsDescriptor.sol';

interface INounsSeeder {
    struct Seed {
        uint48 background;
        uint48 body;
        uint48 accessory;
        uint48 head;
        uint48 glasses;
    }

    function generateSeed(uint256 nounId, INounsDescriptor descriptor) external view returns (Seed memory);
}

INounsDescriptor.sol

INounsDescriptor.sol
solidity
// SPDX-License-Identifier: GPL-3.0

/// @title Interface for NounsDescriptor

/*********************************
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░██░░░████░░██░░░████░░░ *
 * ░░██████░░░████████░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░██░░██░░░████░░██░░░████░░░ *
 * ░░░░░░█████████░░█████████░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
 *********************************/

pragma solidity ^0.8.6;

import { INounsSeeder } from './INounsSeeder.sol';

interface INounsDescriptor {
    event PartsLocked();

    event DataURIToggled(bool enabled);

    event BaseURIUpdated(string baseURI);

    function arePartsLocked() external returns (bool);

    function isDataURIEnabled() external returns (bool);

    function baseURI() external returns (string memory);

    function palettes(uint8 paletteIndex, uint256 colorIndex) external view returns (string memory);

    function backgrounds(uint256 index) external view returns (string memory);

    function bodies(uint256 index) external view returns (bytes memory);

    function accessories(uint256 index) external view returns (bytes memory);

    function heads(uint256 index) external view returns (bytes memory);

    function glasses(uint256 index) external view returns (bytes memory);

    function backgroundCount() external view returns (uint256);

    function bodyCount() external view returns (uint256);

    function accessoryCount() external view returns (uint256);

    function headCount() external view returns (uint256);

    function glassesCount() external view returns (uint256);

    function addManyColorsToPalette(uint8 paletteIndex, string[] calldata newColors) external;

    function addManyBackgrounds(string[] calldata backgrounds) external;

    function addManyBodies(bytes[] calldata bodies) external;

    function addManyAccessories(bytes[] calldata accessories) external;

    function addManyHeads(bytes[] calldata heads) external;

    function addManyGlasses(bytes[] calldata glasses) external;

    function addColorToPalette(uint8 paletteIndex, string calldata color) external;

    function addBackground(string calldata background) external;

    function addBody(bytes calldata body) external;

    function addAccessory(bytes calldata accessory) external;

    function addHead(bytes calldata head) external;

    function addGlasses(bytes calldata glasses) external;

    function lockParts() external;

    function toggleDataURIEnabled() external;

    function setBaseURI(string calldata baseURI) external;

    function tokenURI(uint256 tokenId, INounsSeeder.Seed memory seed) external view returns (string memory);

    function dataURI(uint256 tokenId, INounsSeeder.Seed memory seed) external view returns (string memory);

    function genericDataURI(
        string calldata name,
        string calldata description,
        INounsSeeder.Seed memory seed
    ) external view returns (string memory);

    function generateSVGImage(INounsSeeder.Seed memory seed) external view returns (string memory);
}

Conclusion

This article examined the NounsSeeder contract using Bunzz's DeCipher feature. The contract provides a compact example of deriving NFT trait indices from on-chain inputs and descriptor data.

I plan to continue examining specific NFTs and smart contracts. I also publish articles about blockchain development and AI on my blog and Qiita.

https://chaldene.net/