How to use your own development capabilities to create digital collections in China----how to create contract-issued digital collections (nft) in China

For ordinary novice users, when issuing nft, what they may understand is to find an opensea platform and upload their own nft works, so for programmers, how to develop an nft contract and how to put nft products on the chain.

.First of all, we choose the AntChain platform as a domestic platform, which is legal and compliant. The preliminary preparations are as follows :

(1) Log in to the open alliance chain;

You can quickly read and build your own application project, and get the corresponding application id after building the application project.

(2) Prepare to develop the nft project and issue it on the chain;

Creating your own nft and publishing it on the chain involves the following steps:

1. Initialize file storage certificate

2. Upload files

3. Notify that the file upload is complete

4. Obtain the status of file storage

5. Query file deposit evidence

The following is implemented through java

Import dependencies:

<dependency>
    <groupId>cn.com.antcloud.api</groupId>
    <artifactId>antcloud-api-sdk</artifactId>
    <version>3.3.0</version>
</dependency>
    <dependency>
    <groupId>cn.com.antcloud.api</groupId>
    <artifactId>antcloud-api-appex</artifactId>
    <version>1.1.0</version>
    </dependency>
<!--可以选择其他的http client-->
<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>

Java version:

public static void main(String[] args) throws InterruptedException, IOException, UnirestException {
    String endpoint = "https://prodapigw.cloud.alipay.com";
    String accessKey = "开发者配置";
    String secretKey = "开发者配置";
    String instanceId = "appex";
    String fileAppDid = "应用id";
    AntFinTechProfile antFinTechProfile = AntFinTechProfile.getProfile(endpoint, accessKey, secretKey);
    AntFinTechApiClient antFinTechApiClient = new AntFinTechApiClient(antFinTechProfile);
    //1. init oss 地址
    InitSolutionFilenotaryRequest initSolutionFilenotaryRequest = new InitSolutionFilenotaryRequest(instanceId);
    initSolutionFilenotaryRequest.setAppDid(fileAppDid);
    InitSolutionFilenotaryResponse initSolutionFilenotaryResponse = antFinTechApiClient.execute(initSolutionFilenotaryRequest);
    System.out.println(JSON.toJSONString(initSolutionFilenotaryResponse));
    String fileNotaryId = initSolutionFilenotaryResponse.getFileNotaryId();
    //2.通过http PUT请求你上传文件
    HttpResponse<String> resp = Unirest.put(initSolutionFilenotaryResponse.getUrl())
            .header("Content-Type", null)
            .body(getBytesByFile("文件路径"))
            .asString();
 
    if (resp.getStatus()!=200) {
        //文件上传失败,此处可添加上传失败后的处理
        return;
    }else{
        System.out.println("文件上传成功!");
    }
    //3. 通知服务端完成上传,开始存证
    SyncSolutionFilenotaryRequest syncSolutionFilenotaryRequest = new SyncSolutionFilenotaryRequest(instanceId);
    syncSolutionFilenotaryRequest.setAppDid(fileAppDid);
    syncSolutionFilenotaryRequest.setFileNotaryId(fileNotaryId);
    SyncSolutionFilenotaryResponse syncSolutionFilenotaryResponse = antFinTechApiClient.execute(syncSolutionFilenotaryRequest);
    System.out.println(JSON.toJSONString(initSolutionFilenotaryResponse));
    if (!syncSolutionFilenotaryResponse.getAccepted()) {
        //存证被拒绝了,此处可自行添加被拒绝后的处理逻辑
        return;
    }
    //4.获取存证状态
    GetSolutionFilenotarystatusRequest getSolutionFilenotarystatusRequest = new GetSolutionFilenotarystatusRequest(instanceId);
    getSolutionFilenotarystatusRequest.setAppDid(fileAppDid);
    getSolutionFilenotarystatusRequest.setFileNotaryId(fileNotaryId);
    String txHash;
    //此处轮询查询状态接口获得异步存证是否成功的结果
    //实际代码中请自行处理轮询逻辑和超时逻辑
    while (true) {
        GetSolutionFilenotarystatusResponse getSolutionFilenotarystatusResponse = antFinTechApiClient.execute(getSolutionFilenotarystatusRequest);
        if (getSolutionFilenotarystatusResponse.getFinished()) {
            txHash = getSolutionFilenotarystatusResponse.getTxHash();
            System.out.println("完成存证交易hash为:"+txHash);
            break;
        }
        Thread.sleep(3 * 1000);
    }
    //4.存证查询
    QuerySolutionFilenotaryRequest querySolutionFilenotaryRequest = new QuerySolutionFilenotaryRequest(instanceId);
    querySolutionFilenotaryRequest.setAppDid(fileAppDid);
    querySolutionFilenotaryRequest.setTxHash(txHash);
    QuerySolutionFilenotaryResponse querySolutionFilenotaryResponse = antFinTechApiClient.execute(querySolutionFilenotaryRequest);
    if (querySolutionFilenotaryResponse != null) {
        //不抛错且能够获得返回值即表示查询成功
        System.out.println("存证信息如下:");
        System.out.println("url :" + querySolutionFilenotaryResponse.getUrl());
        System.out.println("qrCodeUrl :" + querySolutionFilenotaryResponse.getQrCodeUrl());
        System.out.println("fileContentHash" + querySolutionFilenotaryResponse.getFileContentHash());
    }
}
 
//将文件转换成Byte数组
public static byte[] getBytesByFile(String pathStr) {
    File file = new File(pathStr);
    try {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        byte[] data = bos.toByteArray();
        bos.close();
        return data;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

After the release, we can view it in the AntChain blockchain browser. Welcome to pay attention to the official account of Lianjiyun and view related articles.

Guess you like

Origin blog.csdn.net/wuaofei/article/details/128353999