Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/138 Web3j 환경 설정 및 walletManager 구현 #144

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dependencies {

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

implementation 'org.web3j:core:4.12.1'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.lovecloud.blockchain.application;

import com.lovecloud.blockchain.exception.FailCreateKeyPairException;
import com.lovecloud.blockchain.exception.FailCreateWalletException;
import com.lovecloud.blockchain.exception.FailVerifyWalletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.web3j.crypto.WalletUtils;

import java.io.File;

@Slf4j
@Service
public class WalletManager {

private final String password = "password";

public String creatWallet() {

ECKeyPair ecKeyPair = null;

try{
ecKeyPair = Keys.createEcKeyPair();
} catch (Exception e) {
throw new FailCreateKeyPairException();
}

String walletDirectory = "./";

try{
return WalletUtils.generateWalletFile(password, ecKeyPair, new File(walletDirectory), true);
} catch (Exception e) {
throw new FailCreateWalletException();
}

}


public Credentials verifyWallet(String walletFileName) {
String walletDirectory = "./";
Credentials credentials = null;
try {
credentials = WalletUtils.loadCredentials(password, walletDirectory + walletFileName);
return credentials;
} catch (Exception e) {
throw new FailVerifyWalletException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.lovecloud.blockchain.exception;

import com.lovecloud.global.exception.ErrorCode;
import com.lovecloud.global.exception.LoveCloudException;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;


public class FailCreateKeyPairException extends LoveCloudException{
public FailCreateKeyPairException() {
super(new ErrorCode(INTERNAL_SERVER_ERROR, "키 페어 생성에 실패했습니다."));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.lovecloud.blockchain.exception;

import com.lovecloud.global.exception.ErrorCode;
import com.lovecloud.global.exception.LoveCloudException;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

public class FailCreateWalletException extends LoveCloudException{
public FailCreateWalletException() {
super(new ErrorCode(INTERNAL_SERVER_ERROR, "지갑 생성에 실패했습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.lovecloud.blockchain.exception;

import com.lovecloud.global.exception.ErrorCode;
import com.lovecloud.global.exception.LoveCloudException;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

public class FailVerifyWalletException extends LoveCloudException {
public FailVerifyWalletException() {
super(new ErrorCode(INTERNAL_SERVER_ERROR, "지갑 검증에 실패했습니다."));
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/lovecloud/global/config/Web3jConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lovecloud.global.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

@Configuration
public class Web3jConfig {

@Bean
public Web3j web3j() {
return Web3j.build(new HttpService("${web3j.private_network}"));
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
password: ${SPRING_DATASOURCE_PASSWORD}
jpa:
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
format_sql: true
Expand Down Expand Up @@ -53,3 +53,5 @@ jwt:
token-expiration-time: 360000000
issuer: ${JWT_ISSUER}

web3j:
private-network: ${WEB3J_PRIVATE_NETWORK}