Skip to content

Commit 848c348

Browse files
committed
Fetch wallet address details
1 parent f2d15ad commit 848c348

13 files changed

Lines changed: 106 additions & 62 deletions

src/main/kotlin/io/openfuture/api/component/key/DefaultKeyApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class DefaultKeyApi(
3030
return response.body!!
3131
}
3232

33-
override fun getAllWalletsByApplication(applicationId: String): Array<KeyWalletDto> {
34-
val response = keyRestTemplate.getForEntity("/key?applicationId={applicationId}", Array<KeyWalletDto>::class.java, applicationId)
33+
override fun getAllWalletsByApplication(applicationId: String): Array<KeyWalletEncryptedDto> {
34+
val response = keyRestTemplate.getForEntity("/key?applicationId={applicationId}", Array<KeyWalletEncryptedDto>::class.java, applicationId)
3535
return response.body!!
3636
}
3737

src/main/kotlin/io/openfuture/api/component/key/KeyApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface KeyApi {
77
fun importWallet(request: ImportKeyRequest): KeyWalletDto
88
fun generateMultipleWallets(createMultipleKeyRequest: CreateMultipleKeyRequest): Array<KeyWalletDto>
99
fun updateWallets(createMultipleKeyRequest: CreateMultipleKeyRequest): Array<KeyWalletDto>
10-
fun getAllWalletsByApplication(applicationId: String): Array<KeyWalletDto>
10+
fun getAllWalletsByApplication(applicationId: String): Array<KeyWalletEncryptedDto>
1111
fun getAllWalletsByOrderKey(orderKey: String): Array<KeyWalletDto>
1212
fun getApplicationByAddress(address: String): WalletAddressResponse
1313
fun deleteAllWalletsByApplicationAddress(applicationId: String, address: String)

src/main/kotlin/io/openfuture/api/component/state/DefaultStateApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class DefaultStateApi(private val stateRestTemplate: RestTemplate) : StateApi {
1313

1414
override fun createWallet(address: String, webHook: String, blockchain: Blockchain, applicationId: String): StateWalletDto {
1515
val request = CreateStateWalletRequest(address, webHook, blockchain.getValue(), applicationId)
16-
println("Blockchain : $blockchain")
1716
val response = stateRestTemplate.postForEntity("/wallets/single", request, StateWalletDto::class.java)
1817
return response.body!!
1918
}
@@ -48,6 +47,7 @@ class DefaultStateApi(private val stateRestTemplate: RestTemplate) : StateApi {
4847

4948
override fun getPaymentDetailByOrder(orderKey: String): PaymentWidgetResponse {
5049
val url = "/orders/${orderKey}"
50+
println("Order key : $orderKey")
5151
return stateRestTemplate.getForEntity(url, PaymentWidgetResponse::class.java).body!!
5252
}
5353

src/main/kotlin/io/openfuture/api/config/WebMvcConfig.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import io.openfuture.api.annotation.resolver.CurrentUserArgumentResolver
44
import io.openfuture.api.service.UserService
55
import org.springframework.context.annotation.Configuration
66
import org.springframework.web.method.support.HandlerMethodArgumentResolver
7+
import org.springframework.web.servlet.config.annotation.CorsRegistry
78
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
89

10+
911
@Configuration
1012
class WebMvcConfig(
1113
private val userService: UserService
@@ -15,4 +17,11 @@ class WebMvcConfig(
1517
resolvers.add(CurrentUserArgumentResolver(userService))
1618
}
1719

20+
override fun addCorsMappings(registry: CorsRegistry) {
21+
registry.addMapping("/**")
22+
.allowedOrigins("*")
23+
.allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
24+
.maxAge(3600)
25+
}
26+
1827
}

src/main/kotlin/io/openfuture/api/config/filter/PublicApiAuthorizationFilter.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,41 @@ class PublicApiAuthorizationFilter(
3636
val accessKey = request.getHeader("X-API-KEY")
3737
val signature = request.getHeader("X-API-SIGNATURE")
3838

39-
val requestWrapper = CustomHttpRequestWrapper(request)
40-
val walletApiCreateRequest =
41-
mapper.readValue(requestWrapper.bodyInStringFormat, WalletApiCreateRequest::class.java)
42-
val mapper = jacksonObjectMapper()
43-
val str = mapper.writeValueAsString(walletApiCreateRequest)
44-
4539
val application = applicationService.getByAccessKey(accessKey)
4640

47-
if (!checkHash(accessKey, signature, walletApiCreateRequest.timestamp.toLong(), str)) {
48-
val exceptionResponse = ExceptionResponse(UNAUTHORIZED.value(), "Signature mismatch or request timeout")
49-
response.status = exceptionResponse.status
50-
response.writer.write(mapper.writeValueAsString(exceptionResponse))
41+
if (request.method == "POST") {
42+
43+
val requestWrapper = CustomHttpRequestWrapper(request)
44+
val walletApiCreateRequest =
45+
mapper.readValue(requestWrapper.bodyInStringFormat, WalletApiCreateRequest::class.java)
46+
val mapper = jacksonObjectMapper()
47+
val str = mapper.writeValueAsString(walletApiCreateRequest)
48+
49+
if (!checkHash(accessKey, signature, walletApiCreateRequest.timestamp.toLong(), str)) {
50+
val exceptionResponse =
51+
ExceptionResponse(UNAUTHORIZED.value(), "Signature mismatch or request timeout")
52+
response.status = exceptionResponse.status
53+
response.writer.write(mapper.writeValueAsString(exceptionResponse))
54+
return
55+
}
56+
57+
val token = UsernamePasswordAuthenticationToken(
58+
application.user,
59+
null,
60+
listOf(SimpleGrantedAuthority("ROLE_APPLICATION"))
61+
)
62+
SecurityContextHolder.getContext().authentication = token
63+
64+
chain.doFilter(requestWrapper, response)
5165
return
5266
}
67+
else {
68+
val token = UsernamePasswordAuthenticationToken(application.user, null, listOf(SimpleGrantedAuthority("ROLE_APPLICATION")))
69+
SecurityContextHolder.getContext().authentication = token
5370

54-
val token = UsernamePasswordAuthenticationToken(application.user, null, listOf(SimpleGrantedAuthority("ROLE_APPLICATION")))
55-
SecurityContextHolder.getContext().authentication = token
56-
57-
chain.doFilter(requestWrapper, response)
58-
return
71+
chain.doFilter(request, response)
72+
return
73+
}
5974
}
6075

6176
else if (request.requestURI.startsWith("/public") && request.getHeader("OPEN-API-KEY") != null) {

src/main/kotlin/io/openfuture/api/controller/api/ApplicationWalletApiController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.openfuture.api.annotation.CurrentUser
44
import io.openfuture.api.domain.key.GenerateWalletRequest
55
import io.openfuture.api.domain.key.ImportWalletRequest
66
import io.openfuture.api.domain.key.KeyWalletDto
7+
import io.openfuture.api.domain.key.KeyWalletEncryptedDto
78
import io.openfuture.api.domain.state.StateSignRequest
89
import io.openfuture.api.domain.state.StateWalletTransactionDetail
910
import io.openfuture.api.entity.auth.User
@@ -31,7 +32,7 @@ class ApplicationWalletApiController(
3132
}
3233

3334
@GetMapping("/{applicationId}")
34-
fun getAll(@PathVariable("applicationId") applicationId: Long): Array<KeyWalletDto> {
35+
fun getAll(@PathVariable("applicationId") applicationId: Long): Array<KeyWalletEncryptedDto> {
3536
return service.getAllWallets(applicationId)
3637
}
3738

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.openfuture.api.controller.api
22

33
import io.openfuture.api.domain.key.KeyWalletDto
4+
import io.openfuture.api.domain.key.KeyWalletEncryptedDto
45
import io.openfuture.api.domain.key.WalletApiCreateRequest
56
import io.openfuture.api.domain.state.WalletApiStateRequest
67
import io.openfuture.api.domain.state.WalletApiStateResponse
78
import io.openfuture.api.service.ApplicationService
9+
import io.openfuture.api.service.ApplicationWalletService
810
import io.openfuture.api.service.WalletApiService
911
import org.springframework.web.bind.annotation.*
1012
import org.web3j.protocol.core.methods.response.TransactionReceipt
@@ -13,40 +15,37 @@ import org.web3j.protocol.core.methods.response.TransactionReceipt
1315
@RestController
1416
@RequestMapping("/public/api/v1/wallet")
1517
class PublicWalletApiController(
16-
private val service: WalletApiService,
17-
private val applicationService: ApplicationService
18+
private val walletApiService: WalletApiService,
19+
private val applicationService: ApplicationService,
20+
private val applicationWalletService: ApplicationWalletService
1821
) {
1922

2023
//@PreAuthorize(value = "hasAnyRole('ROLE_APPLICATION')")
2124
@PostMapping("/process")
22-
fun generateWallet(
23-
@RequestBody walletApiCreateRequest: WalletApiCreateRequest,
24-
@RequestHeader("X-API-KEY") accessKey: String
25-
): Array<KeyWalletDto> {
25+
fun generateWallet(@RequestBody walletApiCreateRequest: WalletApiCreateRequest, @RequestHeader("X-API-KEY") accessKey: String): Array<KeyWalletDto> {
2626
val application = applicationService.getByAccessKey(accessKey)
27-
return service.processWalletSDK(walletApiCreateRequest, application, application.user)
27+
return walletApiService.processWalletSDK(walletApiCreateRequest, application, application.user)
28+
}
29+
30+
@GetMapping
31+
fun getWallets(@RequestHeader("X-API-KEY") accessKey: String): Array<KeyWalletEncryptedDto> {
32+
val application = applicationService.getByAccessKey(accessKey)
33+
return applicationWalletService.getAllWallets(application.id)
2834
}
2935

3036
@PostMapping("/save")
31-
fun saveWallet(
32-
@RequestBody walletApiStateRequest: WalletApiStateRequest,
33-
@RequestHeader("OPEN-API-KEY") accessKey: String
34-
): Boolean {
37+
fun saveWallet(@RequestBody walletApiStateRequest: WalletApiStateRequest, @RequestHeader("OPEN-API-KEY") accessKey: String): Boolean {
3538
val application = applicationService.getByAccessKey(accessKey)
36-
return service.saveWalletSDK(walletApiStateRequest, application, application.user)
39+
return walletApiService.saveWalletSDK(walletApiStateRequest, application, application.user)
3740
}
3841

3942
@PostMapping("/fetch")
40-
fun getWallet(
41-
@RequestBody walletApiStateRequest: WalletApiStateRequest
42-
): WalletApiStateResponse {
43-
return service.getWallet(walletApiStateRequest.address, walletApiStateRequest.blockchain)
43+
fun getWallet(@RequestBody walletApiStateRequest: WalletApiStateRequest): WalletApiStateResponse {
44+
return walletApiService.getWallet(walletApiStateRequest.address, walletApiStateRequest.blockchain)
4445
}
4546

4647
@PostMapping("/broadcast")
47-
fun broadcastTransaction(
48-
@RequestBody walletApiStateRequest: WalletApiStateRequest
49-
): TransactionReceipt {
50-
return service.broadcastTransaction(walletApiStateRequest.address, walletApiStateRequest.blockchain)
48+
fun broadcastTransaction(@RequestBody walletApiStateRequest: WalletApiStateRequest): TransactionReceipt {
49+
return walletApiService.broadcastTransaction(walletApiStateRequest.address, walletApiStateRequest.blockchain)
5150
}
5251
}

src/main/kotlin/io/openfuture/api/controller/base/ExceptionRestControllerAdvice.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.openfuture.api.domain.exception.ErrorDto
44
import io.openfuture.api.domain.exception.ExceptionResponse
55
import io.openfuture.api.exception.*
66
import org.springframework.http.HttpStatus.BAD_REQUEST
7+
import org.springframework.http.HttpStatus.NOT_FOUND
78
import org.springframework.http.converter.HttpMessageNotReadableException
89
import org.springframework.validation.FieldError
910
import org.springframework.web.bind.MethodArgumentNotValidException
@@ -74,4 +75,15 @@ class ExceptionRestControllerAdvice {
7475
ExceptionResponse(BAD_REQUEST.value(), exception.message ?: """Something went wrong. Please read the
7576
|documentation https://docs.openfuture.io/ or contact us openplatform@zensoft.io""".trimMargin())
7677

78+
@ResponseStatus(code = NOT_FOUND)
79+
@ExceptionHandler(NotFoundException::class)
80+
fun notFoundExceptionHandler(exception: NotFoundException): ExceptionResponse =
81+
ExceptionResponse(NOT_FOUND.value(), exception.message!!)
82+
83+
@ResponseStatus(code = BAD_REQUEST)
84+
@ExceptionHandler(RuntimeException::class)
85+
fun runtimeExceptionHandler(exception: RuntimeException): ExceptionResponse =
86+
ExceptionResponse(BAD_REQUEST.value(), exception.message ?: """Something went wrong. Please read the
87+
|documentation https://docs.openfuture.io/ or contact us openplatform@zensoft.io""".trimMargin())
88+
7789
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.openfuture.api.domain.key
2+
3+
data class KeyWalletEncryptedDto(
4+
val address: String,
5+
val blockchain: String,
6+
val walletType: String,
7+
val encrypted: String
8+
9+
)

src/main/kotlin/io/openfuture/api/domain/key/WalletMetaDto.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ data class WalletMetaDto(
1717
@JsonProperty("test")
1818
var test: Boolean,
1919
@JsonProperty("clientManaged")
20-
var clientManaged: Boolean
20+
var clientManaged: Boolean,
21+
@JsonProperty("clientPassword")
22+
var clientPassword: String
2123
)

0 commit comments

Comments
 (0)