Author: Lin Guanhong/The Ghost at my Fingertips. Transporters, please: Be sure to identify the source.

The Denver nuggets: juejin. Im/user / 178526…

Blog: www.cnblogs.com/linguanh/

Making: github.com/af913337456…

Published books:

  • 1.0- Blockchain Ethereum DApp Development Combat
  • “2.0 — Blockchain DApp Development: Based on Ethereum and Bitcoin Public Chain”

directory

  • Step 1: Make the ID
    • How to operate?
    • Gets the unique ID of the picture
    • Gets the unique ID of the garment
  • Step 2: Tokenization
    • Processes based on different public chains
    • Smart contracts for NFT
  • Step 3: Show and modify
    • Display NFT content
    • Modify the NFT content
  • Ownership consensus
  • Third Party Platform

NFT (non-fungible Token), this 2 years again fire up, as early as 18 years has been a wave of fire.

This article only introduces NFT from the technical solution level of writing code to achieve NFT, not from the level of its financial significance, cases, etc., because such content can be searched in the browser, and I will talk about the content, shallow search, not much.

Step 1: Make the ID

Making an ID, that’s the first step in making a substance into an NFT. What are the substances? A piece of text, a picture, a piece of clothing, etc., can be tokenized, literally, in the real world, whether virtual (game gear) or physical.

How to operate?

Unique signature intermediates of matter obtained through third party technical means.

So the first step in making NFT is generalized to the following equation:

  • id = F(I)
    1. I is equal to the input
    2. F = processing function, which stands for one method
    3. Id’s unique signature intermediate

The simplest example is a hash function, which can output different hashes depending on the content, regardless of hash collisions. Let’s not limit our thinking here to hash functions.

Gets the unique ID of the picture

Here a picture represents a series of file-like data.

  1. We can convert the image to an array of []byte bytes and then compute its hash value. Although this operation is relatively simple, but the picture is not accessible to others, can not see;
  2. If we want to provide read permission to an image to anyone outside of us, after calculating the ID, we have two options:
    1. Upload images to a file server that anyone can access via a URL link. The server here is centralized;
    2. Add blockchain attributes. Upload files to IPFS so that they can be accessed by others while also having the decentralized properties of blockchain. In IPFS, after uploading the file, it will use its algorithm to calculate the return hash value for you. You can use its ID directly.

Gets the unique ID of the garment

Here clothes represent a set of physical substances. What if I get their unique IDS? Practice can free your mind to think, such as:

  • The factory information, scanning content, photos and other series of information about the clothes are digitized, and then these data are used to make files. Finally, we refer to the method of pictures.

Step 2: Tokenization

In the first step, we got the ids of the substances, and now we tokenize them. One thing to keep in mind: currently recognized NFT is based on blockchain public chain, so will this be the case in the future? I don’t know. I don’t know. There’s a new consensus.

Processes based on different public chains

The tokenization process is as follows:

  1. Select a blockchain public chain. The choice here will determine the restIntelligent contractAnd so on the technology stack of system components, this is the core;
  2. Develop smart contracts on selected public chains;
  3. Smart contracts are developed to follow some basic conventions, such as at least ensuring that the id of the substance can be verified and de-weighted. What does that mean? That is, if A uploads the same id=1 to the chain today, and B uploads the same ID =1 to the chain tomorrow, the contract should be able to tell B that you can’t upload, the ID already exists;
  4. Deploy a smart contract to the chain, where it becomes a DApp;
  5. Through the way of transaction, the method of the smart contract is called, and the relevant data such as ID is stored on the chain.

Smart contracts for NFT

NFT smart contracts can be developed based on different public chains and are not limited to any one public chain. Different public chains have different smart contract schemes. The following uses the Ethereum public chain as an example.

On Ethereum, there are already many standards for developing NFT smart contracts, such as ERC-721 \1155 \998, which have their own characteristics, but their characteristics are extended from the basic attributes. (the standard document: eips.ethereum.org/EIPS/eip-72…).

If erC-721 standard is selected to develop NFT smart contract, in the metadata storage part, there is the item tokenUrl, which is equivalent to the unique ID of the substance, like the following, _tokenURIs stores the current count ID of the token and its corresponding tokenUrl. The tokenUrl here is a string format, typically a file URL, a link to a file stored on IPFS or some other service, but not just a link, but something else.

/ / pseudo codecontract MyERC721 is IERC721Metadata, ... {... mapping(uint256= > address) private _tokenOwner;
    mapping(uint256= > string) private _tokenURIs;
    
    uint256 public tokenCounter; // Count, current total NFT number, increment
    
    constructor () public ERC721 ("name"."symbol"){
        tokenCounter = 0;
    }
    // The external caller calls this function, passing the tokenURI id of the substance, tokenURI is unique
    function createNFT(string memory tokenURI) public returns (uint256) {
        uint256 tokenId = tokenCounter;
        _mint(msg.sender, tokenId); // Bind the transaction sender to the current tokenId
        _setTokenURI(tokenId, tokenURI); // tokenId maps to tokenUrl
        tokenCounter = tokenCounter + 1; / / accumulation
        return tokenId;
    }
    // the _tokenOwner[tokenId] function checks whether tokenId exists.
    // Read the corresponding URL according to the id
    function tokenURI(uint256 tokenId) external view returns (string memory) {
        require(_exists(tokenId));
        return _tokenURIs[tokenId];
    }
    // Create map data relationship based on tokenId and URL
    function _setTokenURI(uint256 tokenId, string memory uri) internal {
        require(_exists(tokenId)); // _exists_tokenURIs[tokenId] = uri; }...// Omit serial interfaces, including read interfaces
}
Copy the code

The tokenUrl above is the stored data item required by the standard. The entire contract has the following constraint functions:

  1. The NFT holder, MSG. Sender (owner) and tokenId one-to-many relationship, represents that one person can have multiple NFT;
  2. The one-to-one relationship between tokenId and tokenUrl represents the unique ID on each chain of data. Meanwhile, tokenUrl is not required to be unique, but the caller will generally set tokenUrl to be unique, even if it is not unique, it does not matter. In case of conflict, the smaller the tokenId is, The earlier it was set;
  3. After writing data to the chain, the NFT holder can obtain the unique CHAIN ID of the NFT, and then perform a series of read and write operations according to the ID.

In general, our regular NFT is sufficient to have one item that relates to the data, but it is not limited to that. The contract can add custom data items and their read and write functions after implementing the interface required by the standard.

Step 3: Show and modify

Display NFT content

The so-called display is to read and display NFT data. The general process is as follows:

  1. The smart contract reads the information according to the ID obtained when the NFT data is set to the chain;
  2. The obtained information is restored to the original NFT data through a media application.

For example, show the image NFT. (Take the 721 contract standard and IPFS as an example)

  1. If the tokenId obtained by calling the contract to store data is 3, then the tokenId is used to call the contract’s read data method.
  2. After performing step 1, you get tokenUrl, which is the link to the file stored in IPFS.
  3. Open the tokenUrl link directly in your browser and see the image.

Modify the NFT content

Modification is an extension function of NFT smart contract, which is optional. The specific way depends entirely on the realization of requirements. Such as:

  1. Allows tokenId to be reset.
  2. Add other fields to the NFT data and allow modification of these fields;
  3. To transfer NFT, the NFT information corresponding to a tokenId can be transferred to other owners to achieve the purpose of transfer.
  4. Sale of NFT,Auction NFTOperations such as…

Ownership consensus

Currently NFT, non-homogenous token. The essence is to use the attributes of blockchain to mark the ownership proof of an asset.

For example, Everydays: The First 5000 Days, a digital work that was auctioned for more than $60 million, gives The winning bidder The original image and The NFT of The image. These two things, one is a substantial work, and the other is proof of its ownership.

Let’s say that the owner of A work, an anonymous person NAMED A, years later, the work itself is accidentally stolen and recovered. Then how to prove that A is the real owner? At this point, A only needs to show his NFT ownership of the work.

So is NFT like our real certificates? Not really. Two points:

  1. Both NFT and certificate can prove ownership of an asset;
  2. Contrast the storage media with the eternal aging:
    1. The certificate may need to be maintained in a safe, but it will eventually occupy one side of the land, and only the safe accommodated by one side of the land will ensure safety. Under the influence of the passage of time, the continuous storage time will be shorter.
    2. NFT is stored on a blockchain, which is protected by chain nodes throughout the Internet. It can survive until the entire network collapses, which is almost as likely for a public chain with so many nodes as the end of the Internet.

Third Party Platform

There are already many third-party NFT production and distribution platforms. For example, OpenSea, Rarible, etc., these platforms have their own implementation of NFT smart contract and NFT display application (media application – website), convenient for the public 0 code base to experience NFT. But there are also some barriers, need to have a wallet and hair transaction fuel costs.