package com.ichaoj.ams.common.util; import cn.hutool.crypto.symmetric.AES; import com.alibaba.fastjson.JSON; import com.ichaoj.ams.common.bean.Wallet; import com.ichaoj.ams.script.model.AirdropParam; import com.ichaoj.ams.script.model.AirdropWallet; import com.ichaoj.common.model.PublicUserInfo; import com.ichaoj.web.context.SuperWhaleContext; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.web3j.crypto.Credentials; import org.web3j.crypto.WalletUtils; import java.io.File; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import static com.ichaoj.ams.constant.AmsConstant.WALLET_FILE_PATH; /** * @author : cjwen * @date : 2023/04/25 10:02 */ @Slf4j public class WalletUtil { private static final cn.hutool.crypto.symmetric.AES AES = new AES("NYJtaLxWBpdwOZZM06qpSL2fmchxoPoK".getBytes()); public static void main(String[] args) { byte[] decrypt = AES.decrypt("0NyiikW1Dvj9bseaZpyyYQ=="); String base64 = AES.encryptBase64(decrypt); System.out.println(base64); } /** * 批量创建钱包 * * @param userId * @param numWallets 钱包数量 * @param groupName * @return 创建的钱包列表 */ @SneakyThrows public static List batchCreateWallet(String userId, Integer numWallets, String password, String groupName) { List wallets = new ArrayList<>(); groupName = getEncryptPwd(groupName); for (int i = 0; i < numWallets; i++) { Wallet wallet = createWallet(userId, password, groupName); wallets.add(wallet); } return wallets; } /** * 创建钱包 * * @return 钱包 */ @SneakyThrows public static Wallet createWallet(String userId, String password, String groupName) { File destinationDirectory = new File(WALLET_FILE_PATH + userId + File.separator + groupName); if (!destinationDirectory.exists()) { boolean mkdir = destinationDirectory.mkdirs(); if (!mkdir) { destinationDirectory.delete(); throw new RuntimeException("create eth wallet file error"); } } String walletFile = WalletUtils.generateNewWalletFile(password, destinationDirectory, true); String fullName = WALLET_FILE_PATH + userId + File.separator + groupName + File.separator + walletFile; Credentials credentials = WalletUtils.loadCredentials(password, fullName); Wallet wallet = new Wallet(); wallet.setKeystore(walletFile); wallet.setAddress(credentials.getAddress()); return wallet; } public static String getEncryptPwd(String password) { return AES.encryptHex(password, StandardCharsets.UTF_8); } @SneakyThrows public static AirdropWallet getWalletByKeystore(String keystore, String userId, String groupName, String password) { Credentials credentials = WalletUtils.loadCredentials(password, WALLET_FILE_PATH + userId + File.separator + getEncryptPwd(groupName) + File.separator + keystore); String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16); AirdropWallet wallet = new AirdropWallet(); wallet.setPrivateKey(privateKey); wallet.setAddress(credentials.getAddress()); return wallet; } }