-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from zhangcongcobo/feature/prime_broker_client
feat: add prime broker client
- Loading branch information
Showing
9 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/cobo/custody/api/client/CoboMPCPrimeBrokerRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.cobo.custody.api.client; | ||
|
||
import com.cobo.custody.api.client.domain.ApiResponse; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerAddress; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerBinderInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserAuthInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserBindInfo; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
import java.util.List; | ||
|
||
public interface CoboMPCPrimeBrokerRestClient { | ||
|
||
ApiResponse<PrimeBrokerBinderInfo> createBinding(String userId); | ||
|
||
ApiResponse<PrimeBrokerUserBindInfo> queryBinding(String binderId); | ||
|
||
ApiResponse<PrimeBrokerUserAuthInfo> queryUserAuth(String userId); | ||
|
||
ApiResponse<PrimeBrokerUserAuthInfo> bindAddresses(String userId, List<PrimeBrokerAddress> addresses) throws JsonProcessingException; | ||
|
||
ApiResponse<PrimeBrokerBinderInfo> changeBinding(String userId); | ||
|
||
ApiResponse<Void> unbindBinding(String userId); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/cobo/custody/api/client/domain/account/PrimeBrokerAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.cobo.custody.api.client.domain.account; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class PrimeBrokerAddress { | ||
@JsonProperty(value = "chain_coin") | ||
private String chainCoin; | ||
|
||
@JsonProperty(value = "address") | ||
private String address; | ||
|
||
public PrimeBrokerAddress() {} | ||
|
||
public PrimeBrokerAddress(String chainCoin, String address) { | ||
this.chainCoin = chainCoin; | ||
this.address = address; | ||
} | ||
|
||
public String getChainCoin() { | ||
return chainCoin; | ||
} | ||
|
||
public void setChainCoin(String chainCoin) { | ||
this.chainCoin = chainCoin; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrimeBrokerAddress{" + | ||
"chainCoin='" + chainCoin + '\'' + | ||
", address='" + address + '\'' + | ||
'}'; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/cobo/custody/api/client/domain/account/PrimeBrokerBinderInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cobo.custody.api.client.domain.account; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class PrimeBrokerBinderInfo { | ||
@JsonProperty(value = "binder_id") | ||
private String binderId; | ||
|
||
public String getBinderId() { | ||
return binderId; | ||
} | ||
|
||
public void setBinderId(String binderId) { | ||
this.binderId = binderId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrimeBrokerBinderInfo{" + | ||
"binderId='" + binderId + '\'' + | ||
'}'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/cobo/custody/api/client/domain/account/PrimeBrokerUserAuthInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.cobo.custody.api.client.domain.account; | ||
|
||
import java.util.List; | ||
|
||
public class PrimeBrokerUserAuthInfo { | ||
private String pubkey; | ||
|
||
private List<PrimeBrokerAddress> addresses; | ||
|
||
public String getPubkey() { | ||
return pubkey; | ||
} | ||
|
||
public void setPubkey(String pubkey) { | ||
this.pubkey = pubkey; | ||
} | ||
|
||
public List<PrimeBrokerAddress> getAddresses() { | ||
return addresses; | ||
} | ||
|
||
public void setAddresses(List<PrimeBrokerAddress> addresses) { | ||
this.addresses = addresses; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrimeBrokerUserAuthInfo{" + | ||
"pubkey='" + pubkey + '\'' + | ||
", addresses=" + addresses + | ||
'}'; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/cobo/custody/api/client/domain/account/PrimeBrokerUserBindInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.cobo.custody.api.client.domain.account; | ||
|
||
import java.util.List; | ||
|
||
public class PrimeBrokerUserBindInfo { | ||
private String pubkey; | ||
|
||
private Integer status; | ||
|
||
private List<PrimeBrokerAddress> addresses; | ||
|
||
public String getPubkey() { | ||
return pubkey; | ||
} | ||
|
||
public void setPubkey(String pubkey) { | ||
this.pubkey = pubkey; | ||
} | ||
|
||
public Integer getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(Integer status) { | ||
this.status = status; | ||
} | ||
|
||
public List<PrimeBrokerAddress> getAddresses() { | ||
return addresses; | ||
} | ||
|
||
public void setAddresses(List<PrimeBrokerAddress> addresses) { | ||
this.addresses = addresses; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrimeBrokerUserBindInfo{" + | ||
"pubkey='" + pubkey + '\'' + | ||
", status=" + status + | ||
", addresses=" + addresses + | ||
'}'; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/cobo/custody/api/client/impl/CoboMPCPrimeBrokerRestClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.cobo.custody.api.client.impl; | ||
|
||
import com.cobo.custody.api.client.ApiSigner; | ||
import com.cobo.custody.api.client.CoboMPCPrimeBrokerRestClient; | ||
import com.cobo.custody.api.client.config.Env; | ||
import com.cobo.custody.api.client.domain.ApiResponse; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerAddress; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerBinderInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserAuthInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserBindInfo; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.List; | ||
|
||
import static com.cobo.custody.api.client.impl.CoboApiServiceGenerator.createService; | ||
import static com.cobo.custody.api.client.impl.CoboApiServiceGenerator.executeSync; | ||
|
||
public class CoboMPCPrimeBrokerRestClientImpl implements CoboMPCPrimeBrokerRestClient { | ||
private final CoboMPCPrimeBrokerService coboMPCPrimeBrokerService; | ||
|
||
public CoboMPCPrimeBrokerRestClientImpl(ApiSigner signer, Env env, boolean debug) { | ||
this.coboMPCPrimeBrokerService = createService(CoboMPCPrimeBrokerService.class, signer, env, debug); | ||
} | ||
@Override | ||
public ApiResponse<PrimeBrokerBinderInfo> createBinding(String userId) { | ||
return executeSync(coboMPCPrimeBrokerService.createBinding(userId)); | ||
} | ||
|
||
@Override | ||
public ApiResponse<PrimeBrokerUserBindInfo> queryBinding(String binderId) { | ||
return executeSync(coboMPCPrimeBrokerService.queryBinding(binderId)); | ||
} | ||
|
||
@Override | ||
public ApiResponse<PrimeBrokerUserAuthInfo> queryUserAuth(String userId) { | ||
return executeSync(coboMPCPrimeBrokerService.queryUserAuth(userId)); | ||
} | ||
|
||
@Override | ||
public ApiResponse<PrimeBrokerUserAuthInfo> bindAddresses(String userId, List<PrimeBrokerAddress> addresses) throws JsonProcessingException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
return executeSync(coboMPCPrimeBrokerService.bindAddresses(userId, mapper.writeValueAsString(addresses))); | ||
} | ||
|
||
@Override | ||
public ApiResponse<PrimeBrokerBinderInfo> changeBinding(String userId) { | ||
return executeSync(coboMPCPrimeBrokerService.changeBinding(userId)); | ||
} | ||
|
||
@Override | ||
public ApiResponse<Void> unbindBinding(String userId) { | ||
return executeSync(coboMPCPrimeBrokerService.unbindBinding(userId)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/cobo/custody/api/client/impl/CoboMPCPrimeBrokerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.cobo.custody.api.client.impl; | ||
|
||
import com.cobo.custody.api.client.domain.ApiResponse; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerBinderInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserAuthInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserBindInfo; | ||
import retrofit2.Call; | ||
import retrofit2.http.*; | ||
|
||
public interface CoboMPCPrimeBrokerService { | ||
@FormUrlEncoded | ||
@POST("/v1/custody/auth/create_binding/") | ||
Call<ApiResponse<PrimeBrokerBinderInfo>> createBinding(@Field("user_id") String userId); | ||
|
||
@GET("/v1/custody/auth/query_binding/") | ||
Call<ApiResponse<PrimeBrokerUserBindInfo>> queryBinding(@Query("binder_id") String binderId); | ||
|
||
@GET("/v1/custody/auth/query_user_auth/") | ||
Call<ApiResponse<PrimeBrokerUserAuthInfo>> queryUserAuth(@Query("user_id") String userId); | ||
|
||
@FormUrlEncoded | ||
@POST("/v1/custody/auth/bind_addresses/") | ||
Call<ApiResponse<PrimeBrokerUserAuthInfo>> bindAddresses(@Field("user_id") String userId, @Field("addresses") String addresses); | ||
|
||
@FormUrlEncoded | ||
@POST("/v1/custody/auth/change_binding/") | ||
Call<ApiResponse<PrimeBrokerBinderInfo>> changeBinding(@Field("user_id") String userId); | ||
|
||
@FormUrlEncoded | ||
@POST("/v1/custody/auth/unbind_binding/") | ||
Call<ApiResponse<Void>> unbindBinding(@Field("user_id") String userId); | ||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/com/cobo/custody/api/client/impl/CoboMPCPrimeBrokerRestClientImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.cobo.custody.api.client.impl; | ||
|
||
import com.cobo.custody.api.client.CoboApiClientFactory; | ||
import com.cobo.custody.api.client.CoboMPCPrimeBrokerRestClient; | ||
import com.cobo.custody.api.client.config.Env; | ||
import com.cobo.custody.api.client.domain.ApiResponse; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerAddress; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerBinderInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserAuthInfo; | ||
import com.cobo.custody.api.client.domain.account.PrimeBrokerUserBindInfo; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
public class CoboMPCPrimeBrokerRestClientImplTest { | ||
private String MPCAPISecret = ""; | ||
private CoboMPCPrimeBrokerRestClient primeBrokerClient; | ||
private Env TestEnv = Env.SANDBOX; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
MPCAPISecret = System.getProperty("MPCApiSecret"); | ||
primeBrokerClient = CoboApiClientFactory.newInstance( | ||
new LocalSigner(MPCAPISecret), | ||
TestEnv, | ||
false).newMPCPrimeBrokerRestClient(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
} | ||
|
||
@Test | ||
public void testCreateBinding() { | ||
String userId = String.valueOf(System.currentTimeMillis()); | ||
ApiResponse<PrimeBrokerBinderInfo> res = primeBrokerClient.createBinding(userId); | ||
System.out.println(res); | ||
assertTrue(res.isSuccess()); | ||
} | ||
|
||
@Test | ||
public void testQueryBinding() { | ||
String binderId = "xmxGDg2hQsiqoo8D5WffyQ=="; | ||
ApiResponse<PrimeBrokerUserBindInfo> res = primeBrokerClient.queryBinding(binderId); | ||
System.out.println(res); | ||
assertTrue(res.isSuccess()); | ||
} | ||
|
||
@Test | ||
public void testQueryUserAuth() { | ||
String userId = ""; | ||
ApiResponse<PrimeBrokerUserAuthInfo> res = primeBrokerClient.queryUserAuth(userId); | ||
System.out.println(res); | ||
assertTrue(res.isSuccess()); | ||
} | ||
|
||
@Test | ||
public void testChangeBinding() { | ||
String userId = "yangming0407"; | ||
ApiResponse<PrimeBrokerBinderInfo> res = primeBrokerClient.changeBinding(userId); | ||
System.out.println(res); | ||
assertTrue(res.isSuccess()); | ||
} | ||
|
||
@Test | ||
public void testBindAddresses() throws JsonProcessingException { | ||
String userId = ""; | ||
List<PrimeBrokerAddress> addresses = Arrays.asList(new PrimeBrokerAddress("ETH", "0x542b14c29b506e586e18c784f85419cca86cc185")); | ||
ApiResponse<PrimeBrokerUserAuthInfo> res = primeBrokerClient.bindAddresses(userId, addresses); | ||
System.out.println(res); | ||
assertTrue(res.isSuccess()); | ||
} | ||
|
||
@Test | ||
public void testUnbindBinding() throws JsonProcessingException { | ||
String userId = "yangming0407"; | ||
ApiResponse<Void> res = primeBrokerClient.unbindBinding(userId); | ||
assertTrue(res.isSuccess()); | ||
} | ||
} |