Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Commands Reference

Put NFT


The Put NFT command in the DID Spaces SDK allows you to upload and associate display data with your Non-Fungible Tokens (NFTs) stored within DID Spaces. This is crucial for ensuring that your NFTs have rich, verifiable content linked directly to their DID.

For an overview of all NFT-related commands, refer to the NFT Commands Reference.

How it Works#

The Put NFT command streamlines the complex process of associating a media asset with an NFT's DID. It performs several critical steps behind the scenes:

  1. Temporary Storage: The provided display data stream is first written to a temporary file on the local system.
  2. Hashing: A cryptographic hash of the temporary file's content is calculated. This hash ensures data integrity and verifiability.
  3. DID Document Generation: A DID Document is constructed. This document includes a NFTDisplay service endpoint that points to the location of the uploaded display data and embeds the calculated content hash.
  4. DID Document Signing: The generated DID Document is signed using the provided controller wallet, establishing cryptographic proof of ownership and control over the NFT's display information.
  5. Data Upload: The original display data (from the temporary file) and the signed DID Document are packaged into a multipart form data request.
  6. Request Signing: The entire HTTP request (including the form data) is signed using the SDK's security mechanisms to authenticate the request with the DID Space.
  7. Upload to DID Space: The signed request is sent to the DID Space, which then stores the display data and registers the updated DID Document.
  8. Cleanup: The temporary file created during the process is automatically removed.

Parameters#

The PutNftObjectCommandInput defines the parameters required for the Put NFT command:

Name

Type

Description

did

string

The Decentralized Identifier (DID) of the NFT asset to which the display data will be associated.
Example: z8iZnaYxnkMD5AKRjTKiCb8pQr1ut8UantAcf

controller

WalletObject

The WalletObject representing the DID Wallet that has control over the NFT's DID Document. This wallet will be used to sign the DID Document.

chainHost

string

The endpoint URL of the ArcBlock OCAP Blockchain node where the NFT's DID is registered.
Example: https://beta.abtnetwork.io/api/

display

object

An object containing details about the NFT's display data.

display.key

string

The filename for the display data. This should not include any path information.
Example: my-nft-image.png

display.data

Readable

A Node.js Readable stream containing the binary data of the NFT's display content (e.g., an image, video, or HTML file).

Return Value#

The PutNftObjectCommandOutput indicates the result of the command execution:

Name

Type

Description

void

void

The command successfully completes its operation without returning any specific data.

Usage Example#

The following example demonstrates how to upload an image file as NFT display data using the Put NFT command.

import { SpaceClient } from '@did-space/client';
import { createReadStream } from 'fs';
import { resolve } from 'path';

async function putNftDisplayObject(client, nftDid, controllerWallet, chainHost) {
try {
const filePath = resolve(__dirname, 'path/to/your/nft-image.png'); // Replace with your actual file path
const fileName = 'nft-image.png'; // Corresponding filename

const command = new client.PutNftObjectCommand({
did: nftDid,
controller: controllerWallet,
chainHost: chainHost,
display: {
key: fileName,
data: createReadStream(filePath),
},
});

await client.send(command);
console.log(`Successfully uploaded display data for NFT: ${nftDid}`);
} catch (error) {
console.error(`Failed to upload NFT display data for ${nftDid}:`, error);
}
}

// Example usage (assuming client, nftDid, controllerWallet, chainHost are defined)
// const client = new SpaceClient({ ... });
// const nftDid = 'z8i...';
// const controllerWallet = { address: 'z8i...', publicKey: '...', sign: async () => {} };
// const chainHost = 'https://beta.abtnetwork.io/api/';
// putNftDisplayObject(client, nftDid, controllerWallet, chainHost);

This example shows how to initialize the PutNftObjectCommand with the necessary NFT details, controller wallet, chain host, and the display data (read from a file stream). The client.send() method then executes the command, uploading the display data and linking it to your NFT.

Detailed Process Flow#

The internal process of PutNftObjectCommand involves several asynchronous steps to ensure data integrity, security, and proper DID Document association. The following sequence diagram illustrates this flow:

DID Space ServiceArcBlock BlockchainFile SystemDID Spaces SDK (PutNftObjectCommand)Caller (Your Application)DID Space ServiceArcBlock BlockchainFile SystemDID Spaces SDK (PutNftObjectCommand)Caller (Your Application)new PutNftObjectCommand(input)Initialize temp file pathgetUrl()getDisplayUrl()getStreams(): Create Write Stream (temp file)input.display.data (Readable Stream)Pipe data to temp fileStream to file completegetStreams(): Create Read Streams (temp file)Return streamForUpload, streamForHashgetDidDocument(streamForHash)Calculate hash (getHash)Construct DID Document (with NFTDisplay service, hash)Resolve controller DID (toABTDid)Sign DID Document (signDidDocument)Return signed DID DocumentgetAxiosRequestConfig()Create FormData (append streamForUpload, signed DID Document)Sign Request (signRequest)Authenticate request with wallet/delegationReturn signed headersSend PUT request (Axios)Process upload, update DID object/documentCommand completes (void)destroy(): Remove temp fileTemp file removed

This diagram highlights how the SDK manages the data flow, from temporary file creation and hashing to DID Document signing and the final secure upload to the DID Space.

Cleanup#

The SDK automatically handles the cleanup of temporary files created during the upload process. The destroy() method is called in a finally block within getAxiosRequestConfig(), ensuring that the temporary file used for streaming and hashing is removed, regardless of whether the upload succeeds or fails.

Next Steps#

You have now learned how to upload and link display data to your NFTs in DID Spaces. To explore other NFT-related operations, refer back to the NFT Commands Overview. If you need to manage other types of objects, consider the Object Commands section.