Posted By : Vikas
In this blog, we are going to create an Externally Owned Wallet that is fully controlled by a person having the right private keys for the wallet. To do this we will use Web3J which is a Java API that provides us all the tools to create wallets and Spring Boot which is the most famous framework when it comes to Java. For more about Ethereum blockchain, explore our Ethereum Blockchain development services.
Step 1: Create a Spring Boot project with the help of the Spring Initializr website. For this demonstration, I am using Maven as my project management tool. It includes the following dependencies:
Note: Web3J Core dependency is not provided by Spring. We need to set it up manually
in our pom.xml file. To do this simply add
You may also like | Deploying a contract using web3.js
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.11.2</version>
</dependency>
Step 2: Create a Wallet entity class to save wallet details in the database. For the sake of simplicity of this demonstration
I am assuming that there is a one-to-one mapping between the Wallet and the User entities.
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Wallet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(optional = false)
@JoinColumn(name = "user_id")
private User user;
private String walletAddress;
private String privateKey;
private BigDecimal balance;
private String currencyName;
private String currencyAbr;
private String walletJsonFilePath;
private String walletPassword;
}
Step 3: In this step, we are going to write a logic that allows us to create a wallet for a user. Here we will focus
only on the business logic and not on the controller layer or the repository layer.
private void createWalletForUser(User user) {
try {
// use wallet password of your choice
String walletPassword = user.getEmail() + random.nextInt(100, 500); // Use Encrypted form
// you can use any directory of your choice for the walletDirectory
String walletName = WalletUtils.generateNewWalletFile(walletPassword, new File(walletDirectory));
// create credentials object by safely using wallet file with the help of wallet password
Credentials credentials = WalletUtils.loadCredentials(walletPassword, walletDirectory + "/" + walletName);
// wallet address is used to refer to a particular wallet on the Ethereum blockchain
String walletAddress = credentials.getAddress();
// this private key is then used to transactions
String privateKey = credentials.getEcKeyPair().getPrivateKey().toString();
// set these values as per your requirements
BigDecimal balance = new BigDecimal("xxx");
String currencyName = "xxx";
String currencyAbr = "xxx";
String walletJsonFilePath = walletDirectory + File.separator + walletName;
// only persist the encrypted form of your wallet password and private key
String walletPasswordEncrypted = EncryptDecrypt.encrypt(walletPassword);
String privateKeyEncrypted = EncryptDecrypt.encrypt(privateKey);
Wallet wallet = new Wallet();
wallet.setUser(user);
wallet.setWalletAddress(walletAddress);
wallet.setPrivateKey(privateKeyEncrypted);
wallet.setBalance(balance);
wallet.setCurrencyName(currencyName);
wallet.setCurrencyAbr(currencyAbr);
wallet.setWalletJsonFilePath(walletJsonFilePath);
wallet.setWalletPassword(walletPasswordEncrypted);
walletRepository.save(wallet);
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("Error occurred while creating wallet: " + e.getMessage());
}
}
Also, Explore | How to Use a Web3js Call Contract Function
Following the steps mentioned above correctly, we can create an Externally Owned Wallet for a user. All we have used is the Web3J and it provides all the necessary tools that help us to create a wallet with almost a few lines of code.
For more about Ethereum or smart contract development-related project development, connect with our skilled Solidity developers.
November 21, 2024 at 11:35 am
Your comment is awaiting moderation.