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.

This article examines ERC-1948, which proposes allowing NFT owners to update 32 bytes of data associated with their tokens.

This explanation translates, summarizes, and expands on the following specification.

For other EIPs, see https://cardene.dev/proposals.

Overview

NFT implementations commonly use static metadata established at minting. ERC-1948 introduces a mutable 32-byte data field that only the token owner may update.

The contract implements functions to read and update this field and emits an event recording the old and new values whenever it changes.

Motivation

Static metadata set at minting cannot express information that changes during a token's lifecycle. Examples include CryptoKitties whose attributes change through breeding, intellectual property tokens with changing rights holders, and data used to carry state across sidechain or Plasma chain bridges.

ERC-1948 standardizes mutable token data to improve interoperability among NFT-based protocols and bridges.

Specification

IERC1948.sol
pragma solidity ^0.5.2;

/**
 * @dev Interface of the ERC1948 contract.
 */
interface IERC1948 {

  /**
   * @dev Emitted when `oldData` is replaced with `newData` in storage of `tokenId`.
   *
   * Note that `oldData` or `newData` may be empty bytes.
   */
  event DataUpdated(uint256 indexed tokenId, bytes32 oldData, bytes32 newData);

  /**
   * @dev Reads the data of a specified token. Returns the current data in
   * storage of `tokenId`.
   *
   * @param tokenId The token to read the data off.
   *
   * @return A bytes32 representing the current data stored in the token.
   */
  function readData(uint256 tokenId) external view returns (bytes32);

  /**
   * @dev Updates the data of a specified token. Writes `newData` into storage
   * of `tokenId`.
   *
   * @param tokenId The token to write data to.
   * @param newData The data to be written to the token.
   *
   * Emits a `DataUpdated` event.
   */
  function writeData(uint256 tokenId, bytes32 newData) external;

}

DataUpdated

javascript
event DataUpdated(uint256 indexed tokenId, bytes32 oldData, bytes32 newData);

This event is emitted whenever a token's data field is replaced. When the owner calls writeData, the old and new values are recorded on-chain, allowing external systems to track the history.

Parameters

  • tokenId: the ID of the token being updated.
  • oldData: the previous 32-byte value.
  • newData: the new 32-byte value.

readData

javascript
function readData(uint256 tokenId) external view returns (bytes32);

Reads the current data field of the specified token and returns its 32-byte value without changing on-chain state.

Parameter

  • tokenId: the ID of the token to read.

Return Value

  • bytes32: the current value stored for the token.

writeData

javascript
function writeData(uint256 tokenId, bytes32 newData) external;

Updates the data field of the specified token only if the caller owns that token. The previous and new values are emitted through DataUpdated to notify external observers.

Parameters

  • tokenId: the ID of the token to update.
  • newData: the new 32-byte value to store.

Additional Considerations

The 32-byte field is intended for simple information that can be stored directly on the token, such as counters or addresses.

For larger or more complex data structures, authenticated data structures such as Merkle or Patricia trees are recommended. Originally motivated by integration with decentralized breeding systems, the proposal also aims to improve interoperability with sidechain implementations such as xDAI, Skale, POA, and LeapDAO.

Tests

The test first mints a token in a newly deployed contract and verifies that its initial data field is 32 zero bytes. It then calls writeData with an arbitrary value and reads it again using readData to confirm that the value was stored correctly.

This read-write-read sequence demonstrates the expected basic behavior of the contract.

js
const ERC1948 = artifacts.require('./ERC1948.sol');

contract('ERC1948', (accounts) => {
  const firstTokenId = 100;
  const empty = '0x0000000000000000000000000000000000000000000000000000000000000000';
  const data = '0x0101010101010101010101010101010101010101010101010101010101010101';
  let dataToken;

  beforeEach(async () => {
    dataToken = await ERC1948.new();
    await dataToken.mint(accounts[0], firstTokenId);
  });

  it('should allow to write and read', async () => {
    let rsp = await dataToken.readData(firstTokenId);
    assert.equal(rsp, empty);
    await dataToken.writeData(firstTokenId, data);
    rsp = await dataToken.readData(firstTokenId);
    assert.equal(rsp, data);
  });

});

Reference Implementation

ERC1948.sol
/**
 * @dev Implementation of ERC721 token and the `IERC1948` interface.
 *
 * ERC1948 is a non-fungible token (NFT) extended with the ability to store
 * dynamic data. The data is a bytes32 field for each tokenId. If 32 bytes
 * do not suffice to store the data, an authenticated data structure (hash or
 * merkle tree) shall be used.
 */
contract ERC1948 is IERC1948, ERC721 {

  mapping(uint256 => bytes32) data;

  /**
   * @dev See `IERC1948.readData`.
   *
   * Requirements:
   *
   * - `tokenId` needs to exist.
   */
  function readData(uint256 tokenId) external view returns (bytes32) {
    require(_exists(tokenId));
    return data[tokenId];
  }

  /**
   * @dev See `IERC1948.writeData`.
   *
   * Requirements:
   *
   * - `msg.sender` needs to be owner of `tokenId`.
   */
  function writeData(uint256 tokenId, bytes32 newData) external {
    require(msg.sender == ownerOf(tokenId));
    emit DataUpdated(tokenId, data[tokenId], newData);
    data[tokenId] = newData;
  }

}

Citation

Johann Barbie (@johannbarbie), Ben Bollen ben@ost.com, pinkiebell (@pinkiebell), "ERC-1948: Non-fungible Data Token [DRAFT]," Ethereum Improvement Proposals, no. 1948, April 2019. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1948.

Conclusion

ERC-1948 standardizes owner-controlled updates to a 32-byte data field attached to an NFT.

Further reading and author links:

https://chaldene.net/