WalletUtil.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.ichaoj.ams.common.util;
  2. import cn.hutool.crypto.symmetric.AES;
  3. import com.alibaba.fastjson.JSON;
  4. import com.ichaoj.ams.common.bean.Wallet;
  5. import com.ichaoj.ams.script.model.AirdropParam;
  6. import com.ichaoj.ams.script.model.AirdropWallet;
  7. import com.ichaoj.common.model.PublicUserInfo;
  8. import com.ichaoj.web.context.SuperWhaleContext;
  9. import lombok.SneakyThrows;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.web3j.crypto.Credentials;
  12. import org.web3j.crypto.WalletUtils;
  13. import java.io.File;
  14. import java.math.BigInteger;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import static com.ichaoj.ams.constant.AmsConstant.WALLET_FILE_PATH;
  19. /**
  20. * @author : cjwen
  21. * @date : 2023/04/25 10:02
  22. */
  23. @Slf4j
  24. public class WalletUtil {
  25. private static final cn.hutool.crypto.symmetric.AES AES = new AES("NYJtaLxWBpdwOZZM06qpSL2fmchxoPoK".getBytes());
  26. public static void main(String[] args) {
  27. byte[] decrypt = AES.decrypt("0NyiikW1Dvj9bseaZpyyYQ==");
  28. String base64 = AES.encryptBase64(decrypt);
  29. System.out.println(base64);
  30. }
  31. /**
  32. * 批量创建钱包
  33. *
  34. * @param userId
  35. * @param numWallets 钱包数量
  36. * @param groupName
  37. * @return 创建的钱包列表
  38. */
  39. @SneakyThrows
  40. public static List<Wallet> batchCreateWallet(String userId, Integer numWallets, String password, String groupName) {
  41. List<Wallet> wallets = new ArrayList<>();
  42. groupName = getEncryptPwd(groupName);
  43. for (int i = 0; i < numWallets; i++) {
  44. Wallet wallet = createWallet(userId, password, groupName);
  45. wallets.add(wallet);
  46. }
  47. return wallets;
  48. }
  49. /**
  50. * 创建钱包
  51. *
  52. * @return 钱包
  53. */
  54. @SneakyThrows
  55. public static Wallet createWallet(String userId, String password, String groupName) {
  56. File destinationDirectory = new File(WALLET_FILE_PATH + userId + File.separator + groupName);
  57. if (!destinationDirectory.exists()) {
  58. boolean mkdir = destinationDirectory.mkdirs();
  59. if (!mkdir) {
  60. destinationDirectory.delete();
  61. throw new RuntimeException("create eth wallet file error");
  62. }
  63. }
  64. String walletFile = WalletUtils.generateNewWalletFile(password, destinationDirectory, true);
  65. String fullName = WALLET_FILE_PATH + userId + File.separator + groupName + File.separator + walletFile;
  66. Credentials credentials = WalletUtils.loadCredentials(password, fullName);
  67. Wallet wallet = new Wallet();
  68. wallet.setKeystore(walletFile);
  69. wallet.setAddress(credentials.getAddress());
  70. return wallet;
  71. }
  72. public static String getEncryptPwd(String password) {
  73. return AES.encryptHex(password, StandardCharsets.UTF_8);
  74. }
  75. @SneakyThrows
  76. public static AirdropWallet getWalletByKeystore(String keystore, String userId, String groupName, String password) {
  77. Credentials credentials = WalletUtils.loadCredentials(password, WALLET_FILE_PATH + userId + File.separator + getEncryptPwd(groupName) + File.separator + keystore);
  78. String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16);
  79. AirdropWallet wallet = new AirdropWallet();
  80. wallet.setPrivateKey(privateKey);
  81. wallet.setAddress(credentials.getAddress());
  82. return wallet;
  83. }
  84. }