AssetMintService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.izouma.nineth.service;
  2. import com.github.kevinsawicki.http.HttpRequest;
  3. import com.izouma.nineth.domain.Asset;
  4. import com.izouma.nineth.domain.User;
  5. import com.izouma.nineth.dto.NFT;
  6. import com.izouma.nineth.dto.NFTAccount;
  7. import com.izouma.nineth.event.CreateAssetEvent;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.AssetRepo;
  10. import com.izouma.nineth.repo.UserRepo;
  11. import io.ipfs.api.IPFS;
  12. import io.ipfs.api.MerkleNode;
  13. import io.ipfs.api.NamedStreamable;
  14. import io.ipfs.multihash.Multihash;
  15. import lombok.AllArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.context.ApplicationContext;
  19. import org.springframework.core.env.Environment;
  20. import org.springframework.retry.annotation.Backoff;
  21. import org.springframework.retry.annotation.Retryable;
  22. import org.springframework.stereotype.Service;
  23. import java.io.File;
  24. import java.util.Arrays;
  25. @Service
  26. @Slf4j
  27. @AllArgsConstructor
  28. public class AssetMintService {
  29. private AssetRepo assetRepo;
  30. private UserRepo userRepo;
  31. private NFTService nftService;
  32. private ApplicationContext applicationContext;
  33. private Environment env;
  34. @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
  35. public void mint(Long assetId) {
  36. Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("asset不存在"));
  37. User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  38. if (StringUtils.isEmpty(user.getNftAccount())) {
  39. NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  40. user.setNftAccount(account.getAccountId());
  41. user.setKmsId(account.getAccountKmsId());
  42. user.setPublicKey(account.getPublicKey());
  43. userRepo.save(user);
  44. }
  45. try {
  46. NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  47. if (nft != null) {
  48. asset.setTokenId(nft.getTokenId());
  49. asset.setBlockNumber(nft.getBlockNumber());
  50. asset.setTxHash(nft.getTxHash());
  51. asset.setGasUsed(nft.getGasUsed());
  52. if (asset.getIpfsUrl() == null) {
  53. asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  54. }
  55. assetRepo.save(asset);
  56. applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  57. } else {
  58. log.error("铸造失败");
  59. }
  60. } catch (Exception e) {
  61. log.error("铸造失败", e);
  62. }
  63. }
  64. // @Async
  65. // public void mint(Asset asset) {
  66. // User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  67. // if (StringUtils.isEmpty(user.getPublicKey())) {
  68. // NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  69. // user.setNftAccount(account.getAccountId());
  70. // user.setKmsId(account.getAccountKmsId());
  71. // user.setPublicKey(account.getPublicKey());
  72. // userRepo.save(user);
  73. // }
  74. // try {
  75. // NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  76. // if (nft != null) {
  77. // asset.setTokenId(nft.getTokenId());
  78. // asset.setBlockNumber(nft.getBlockNumber());
  79. // asset.setTxHash(nft.getTxHash());
  80. // asset.setGasUsed(nft.getGasUsed());
  81. // if (asset.getIpfsUrl() == null) {
  82. // asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  83. // }
  84. // assetRepo.save(asset);
  85. // }
  86. // } catch (Exception e) {
  87. // log.error("铸造失败", e);
  88. // }
  89. // applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  90. // }
  91. public String ipfsUpload(String url) {
  92. try {
  93. url = url.replace("raex-meta.oss-cn-shenzhen.aliyuncs.com",
  94. "raex-meta.oss-cn-shenzhen-internal.aliyuncs.com")
  95. .replace("cdn.raex.vip",
  96. "raex-meta.oss-cn-shenzhen-internal.aliyuncs.com");
  97. String host = "120.24.204.226";
  98. if (Arrays.asList(env.getActiveProfiles()).contains("prod")) {
  99. host = "172.29.50.102";
  100. }
  101. IPFS ipfs = new IPFS(host, 5001);
  102. HttpRequest request = HttpRequest.get(url);
  103. File file = File.createTempFile("ipfs", ".tmp");
  104. request.receive(file);
  105. NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
  106. MerkleNode put = ipfs.add(file1).get(0);
  107. Multihash multihash = ipfs.pin.add(put.hash).get(0);
  108. log.info("上传ipfs成功 {}", multihash.toBase58());
  109. file.delete();
  110. return multihash.toBase58();
  111. } catch (Exception e) {
  112. }
  113. return null;
  114. }
  115. }