AssetMintService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.retry.annotation.Backoff;
  20. import org.springframework.retry.annotation.Retryable;
  21. import org.springframework.stereotype.Service;
  22. import java.io.File;
  23. @Service
  24. @Slf4j
  25. @AllArgsConstructor
  26. public class AssetMintService {
  27. private AssetRepo assetRepo;
  28. private UserRepo userRepo;
  29. private NFTService nftService;
  30. private ApplicationContext applicationContext;
  31. @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
  32. public void mint(Long assetId) {
  33. Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("asset不存在"));
  34. User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  35. if (StringUtils.isEmpty(user.getNftAccount())) {
  36. NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  37. user.setNftAccount(account.getAccountId());
  38. user.setKmsId(account.getAccountKmsId());
  39. user.setPublicKey(account.getPublicKey());
  40. userRepo.save(user);
  41. }
  42. try {
  43. NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  44. if (nft != null) {
  45. asset.setTokenId(nft.getTokenId());
  46. asset.setBlockNumber(nft.getBlockNumber());
  47. asset.setTxHash(nft.getTxHash());
  48. asset.setGasUsed(nft.getGasUsed());
  49. if (asset.getIpfsUrl() == null) {
  50. asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  51. }
  52. assetRepo.save(asset);
  53. applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  54. } else {
  55. log.error("铸造失败");
  56. }
  57. } catch (Exception e) {
  58. log.error("铸造失败", e);
  59. }
  60. }
  61. // @Async
  62. // public void mint(Asset asset) {
  63. // User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  64. // if (StringUtils.isEmpty(user.getPublicKey())) {
  65. // NFTAccount account = nftService.createAccount(user.getUsername() + "_");
  66. // user.setNftAccount(account.getAccountId());
  67. // user.setKmsId(account.getAccountKmsId());
  68. // user.setPublicKey(account.getPublicKey());
  69. // userRepo.save(user);
  70. // }
  71. // try {
  72. // NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
  73. // if (nft != null) {
  74. // asset.setTokenId(nft.getTokenId());
  75. // asset.setBlockNumber(nft.getBlockNumber());
  76. // asset.setTxHash(nft.getTxHash());
  77. // asset.setGasUsed(nft.getGasUsed());
  78. // if (asset.getIpfsUrl() == null) {
  79. // asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
  80. // }
  81. // assetRepo.save(asset);
  82. // }
  83. // } catch (Exception e) {
  84. // log.error("铸造失败", e);
  85. // }
  86. // applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
  87. // }
  88. public String ipfsUpload(String url) {
  89. try {
  90. IPFS ipfs = new IPFS("120.24.204.226", 5001);
  91. HttpRequest request = HttpRequest.get(url);
  92. File file = File.createTempFile("ipfs", ".tmp");
  93. request.receive(file);
  94. NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
  95. MerkleNode put = ipfs.add(file1).get(0);
  96. Multihash multihash = ipfs.pin.add(put.hash).get(0);
  97. log.info("上传ipfs成功 {}", multihash.toBase58());
  98. file.delete();
  99. return multihash.toBase58();
  100. } catch (Exception e) {
  101. }
  102. return null;
  103. }
  104. }