Create an Externally Owned Wallet using Web3J and Spring Boot
Tags: wallet, web3, ethereum, blockchain
Published: 2024-05-14 10:28:15

In this blog, we will create an Externally Owned Wallet that a person with the right private keys for the wallet fully controls. 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](https://blockchain.oodles.io/ethereum-application-development-services/?utm_source=devto).

## Create an Externally Owned Wallet using Web3J and Spring Boot
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:

Spring Web
MySQL Driver(choose the database as per your requirement)
Spring Data JPA
Lombok
Web3J Core
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

`<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 we are 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](https://blockchain.oodles.io/dev-blog/How-to-Use-a-Web3-JS-Call-Contract-Function./?utm_source=devto)

## Conclusion
If the steps above are appropriately followed, we may construct an Externally Owned Wallet for the user. All that we have utilized is Web3J, which offers all the tools required to let us develop a wallet in only a few lines of code.

For more about Ethereum or smart contract development-related projects, connect with our skilled [Solidity developers](https://blockchain.oodles.io/about-us/?utm_source=devto).