AssetMintService.java 4.3 KB

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