AssetMintService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private UserService userService;
  35. @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
  36. public void mint(Long assetId) {
  37. Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("asset不存在"));
  38. User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  39. if (StringUtils.isEmpty(user.getNftAccount())) {
  40. NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  41. user.setNftAccount(account.getAccountId());
  42. user.setKmsId(account.getAccountKmsId());
  43. user.setPublicKey(account.getPublicKey());
  44. userService.save(user);
  45. }
  46. try {
  47. NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  48. if (nft != null) {
  49. asset.setTokenId(nft.getTokenId());
  50. asset.setBlockNumber(nft.getBlockNumber());
  51. asset.setTxHash(nft.getTxHash());
  52. asset.setGasUsed(nft.getGasUsed());
  53. if (asset.getIpfsUrl() == null) {
  54. asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  55. }
  56. assetRepo.save(asset);
  57. applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  58. } else {
  59. log.error("铸造失败");
  60. }
  61. } catch (Exception e) {
  62. log.error("铸造失败", e);
  63. }
  64. }
  65. // @Async
  66. // public void mint(Asset asset) {
  67. // User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  68. // if (StringUtils.isEmpty(user.getPublicKey())) {
  69. // NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  70. // user.setNftAccount(account.getAccountId());
  71. // user.setKmsId(account.getAccountKmsId());
  72. // user.setPublicKey(account.getPublicKey());
  73. // userService.save(user);
  74. // }
  75. // try {
  76. // NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  77. // if (nft != null) {
  78. // asset.setTokenId(nft.getTokenId());
  79. // asset.setBlockNumber(nft.getBlockNumber());
  80. // asset.setTxHash(nft.getTxHash());
  81. // asset.setGasUsed(nft.getGasUsed());
  82. // if (asset.getIpfsUrl() == null) {
  83. // asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  84. // }
  85. // assetRepo.save(asset);
  86. // }
  87. // } catch (Exception e) {
  88. // log.error("铸造失败", e);
  89. // }
  90. // applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  91. // }
  92. public String ipfsUpload(String url) {
  93. try {
  94. url = url.replace("raex-meta.oss-cn-shenzhen.aliyuncs.com",
  95. "raex-meta.oss-cn-shenzhen-internal.aliyuncs.com")
  96. .replace("cdn.raex.vip",
  97. "raex-meta.oss-cn-shenzhen-internal.aliyuncs.com");
  98. String host = "120.24.204.226";
  99. if (Arrays.asList(env.getActiveProfiles()).contains("prod")) {
  100. host = "172.29.50.102";
  101. }
  102. IPFS ipfs = new IPFS(host, 5001);
  103. HttpRequest request = HttpRequest.get(url);
  104. File file = File.createTempFile("ipfs", ".tmp");
  105. request.receive(file);
  106. NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
  107. MerkleNode put = ipfs.add(file1).get(0);
  108. Multihash multihash = ipfs.pin.add(put.hash).get(0);
  109. log.info("上传ipfs成功 {}", multihash.toBase58());
  110. file.delete();
  111. return multihash.toBase58();
  112. } catch (Exception e) {
  113. }
  114. return null;
  115. }
  116. }