AssetMintService.java 6.3 KB

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