对于普通小白用户来说,发行nft,可能明白的只是找个opensea平台,上传自己的nft作品,那么对于程序员来说,如何开发一款nft合约,又如何将nft商品上链呢。
.首先国内的平台我们选择蚂蚁链平台,合法合规,前期准备工作如下:
(1)登录开放联盟链;
可以快读搭建自己的应用项目,搭建应用项目后获取到对应的应用id。
(2)准备开发nft项目并进行发行上链;
创建自己的nft并且发布上链包含以下几步:
1.初始化文件存证
2.上传文件
3.通知文件上传完成
4.获取文件存证状态
5.查询文件存证
下面通过java来实现
引入依赖:
<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版:
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;
}
发布完毕后我们可以再蚂蚁链区块链浏览器查看。欢迎关注链集云公众号,进行相关文章查看。