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:
- Temporary Storage: The provided display data stream is first written to a temporary file on the local system.
- Hashing: A cryptographic hash of the temporary file's content is calculated. This hash ensures data integrity and verifiability.
- 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. - 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.
- Data Upload: The original display data (from the temporary file) and the signed DID Document are packaged into a multipart form data request.
- 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.
- 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.
- 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 |
---|---|---|
|
| The Decentralized Identifier (DID) of the NFT asset to which the display data will be associated. |
|
| The |
|
| The endpoint URL of the ArcBlock OCAP Blockchain node where the NFT's DID is registered. |
|
| An object containing details about the NFT's display data. |
|
| The filename for the display data. This should not include any path information. |
|
| A Node.js |
Return Value#
The PutNftObjectCommandOutput
indicates the result of the command execution:
Name | Type | Description |
---|---|---|
|
| 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:
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.