diff --git a/build.gradle b/build.gradle index d1ff2b6..122eeaf 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.web3j:core:4.12.1' + } tasks.named('test') { diff --git a/src/main/java/com/lovecloud/blockchain/application/WalletManager.java b/src/main/java/com/lovecloud/blockchain/application/WalletManager.java new file mode 100644 index 0000000..5a93943 --- /dev/null +++ b/src/main/java/com/lovecloud/blockchain/application/WalletManager.java @@ -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(); + } + } +} diff --git a/src/main/java/com/lovecloud/blockchain/exception/FailCreateKeyPairException.java b/src/main/java/com/lovecloud/blockchain/exception/FailCreateKeyPairException.java new file mode 100644 index 0000000..ea88a04 --- /dev/null +++ b/src/main/java/com/lovecloud/blockchain/exception/FailCreateKeyPairException.java @@ -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, "키 페어 생성에 실패했습니다.")); + } +} + diff --git a/src/main/java/com/lovecloud/blockchain/exception/FailCreateWalletException.java b/src/main/java/com/lovecloud/blockchain/exception/FailCreateWalletException.java new file mode 100644 index 0000000..d182c3e --- /dev/null +++ b/src/main/java/com/lovecloud/blockchain/exception/FailCreateWalletException.java @@ -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, "지갑 생성에 실패했습니다.")); + } +} \ No newline at end of file diff --git a/src/main/java/com/lovecloud/blockchain/exception/FailVerifyWalletException.java b/src/main/java/com/lovecloud/blockchain/exception/FailVerifyWalletException.java new file mode 100644 index 0000000..dccb2bb --- /dev/null +++ b/src/main/java/com/lovecloud/blockchain/exception/FailVerifyWalletException.java @@ -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, "지갑 검증에 실패했습니다.")); + } +} diff --git a/src/main/java/com/lovecloud/global/config/Web3jConfig.java b/src/main/java/com/lovecloud/global/config/Web3jConfig.java new file mode 100644 index 0000000..7a10ee7 --- /dev/null +++ b/src/main/java/com/lovecloud/global/config/Web3jConfig.java @@ -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}")); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index dfaeb3c..b42100a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,7 +6,7 @@ spring: password: ${SPRING_DATASOURCE_PASSWORD} jpa: hibernate: - ddl-auto: create + ddl-auto: update properties: hibernate: format_sql: true @@ -53,3 +53,5 @@ jwt: token-expiration-time: 360000000 issuer: ${JWT_ISSUER} +web3j: + private-network: ${WEB3J_PRIVATE_NETWORK}