@Configuration
public class EthereumConnectionConfig {
private static Logger LOGGER = LoggerFactory.getLogger(EthereumConnectionConfig.class);
@Value("${ethereum.service.url}")
private String url;
@Bean
Web3j getEthereumCononnection() throws IOException {
String serverIp = url;
LOGGER.info("Ethereum sync serverIp : " + serverIp);
Web3j web3 = Web3j.build(new HttpService(serverIp));
LOGGER.info("connected to ethereum server 123" + web3);
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
LOGGER.info("Ethereum client version" + clientVersion);
return web3;
}
}
@RestController
public class UserWalletController {
private static final Logger logger = LoggerFactory.getLogger(EthereumListener.class);
@Autowired
private WalletService walletService;
@PostMapping(CREATE_WALLET)
public ResponseEntity<Object> setUsersWallet() {
return ResponseHandler.response(walletService.createEtherWallet(),
LocaleService.toLocale("wallet.created"));
}
}
public Map<String, Object> createEtherWallet() {
Map<String, Object> map = new HashMap<>();
map = createCredentials();
Object jsonFile = map.get("jsonFile");
Credentials credentials = (Credentials) map.get("credentials");
String passwordKey = (String) map.get("passwordKey");
String encPwd = (String) map.get("encPwd");
UserWallet userWallet = new UserWallet(userId, credentials.getAddress(), jsonFile.toString(), encPwd, passwordKey, ETHEREUM);
userWalletRepository.save(userWallet);
result.put("address", credentials.getAddress());
}
return result;
}
private Map<String, Object> createCredentials() {
Map<String, Object> map = new HashMap<>();
try {
File file = new File(ethWalletLocation);
String fileName;
String password = UUID.randomUUID().toString().replaceAll("-", "");
fileName = WalletUtils.generateFullNewWalletFile(password, file);
logger.info("wallet file name {} ", fileName);
String passwordKey = CryptoUtil.getSecretKey();
logger.debug("wallet file passwordKey: {}", passwordKey);
String encPwd = CryptoUtil.encrypt(password, passwordKey);
logger.debug("wallet file encPwd: {}", encPwd);
File jsonFile = new File(file + File.separator + fileName);
logger.debug("wallet file jsonFile: {}", jsonFile.toString());
Credentials credentials = WalletUtils.loadCredentials(password, jsonFile);
logger.debug("wallet address: {}", credentials.getAddress());
map.put("jsonFile", jsonFile);
map.put("credentials", credentials);
map.put("encPwd", encPwd);
map.put("passwordKey", passwordKey);
} catch (IOException | NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException
| CipherException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException e) {
e.printStackTrace();
}
return map;
}
Now, we have successfully created a user's wallet on the Ethereum's test network. You can verify your address in etherscan.io by giving your address.