2017-10-16 00:23:44 +00:00
|
|
|
package cy.agorise.crystalwallet.apigenerator;
|
|
|
|
|
2017-11-05 03:22:53 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
2018-01-10 02:00:46 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import okhttp3.Interceptor;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
import retrofit2.http.Body;
|
|
|
|
import retrofit2.http.Headers;
|
|
|
|
import retrofit2.http.POST;
|
|
|
|
|
2017-10-16 00:23:44 +00:00
|
|
|
/**
|
2017-11-12 19:20:03 +00:00
|
|
|
* This maanges the calls for the creation of accounts using the bitshares faucet
|
|
|
|
*
|
2017-10-16 00:23:44 +00:00
|
|
|
* Created by henry on 15/10/2017.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
public abstract class BitsharesFaucetApiGenerator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to register a new Bitshares Account
|
|
|
|
*
|
|
|
|
* @param accountName The name of the Account to be register
|
|
|
|
* @param ownerKey The owner key public address
|
|
|
|
* @param activeKey The active key public address
|
|
|
|
* @param memoKey the memo key public address
|
2017-11-12 19:20:03 +00:00
|
|
|
* @param url The url of the faucet
|
2017-10-18 02:55:42 +00:00
|
|
|
* @return The bitshares id of the registered account, or null
|
|
|
|
*/
|
2018-01-29 01:18:38 +00:00
|
|
|
public static void registerBitsharesAccount(String accountName, String ownerKey,
|
|
|
|
String activeKey, String memoKey, String url,
|
|
|
|
final ApiRequest request){
|
2017-11-05 03:22:53 +00:00
|
|
|
CreateAccountPetition petition = new CreateAccountPetition();
|
2018-01-10 02:00:46 +00:00
|
|
|
final Account account = new Account();
|
2017-11-05 03:22:53 +00:00
|
|
|
account.name=accountName;
|
|
|
|
account.owner_key=ownerKey;
|
|
|
|
account.active_key=activeKey;
|
|
|
|
account.memo_key=memoKey;
|
|
|
|
petition.account=account;
|
|
|
|
Gson gson = new Gson();
|
|
|
|
String jsonPetition = gson.toJson(petition);
|
|
|
|
System.out.println("create account petition :" + jsonPetition);
|
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
//TODO faucet function
|
2018-01-10 02:00:46 +00:00
|
|
|
|
|
|
|
HashMap<String, Object> hm = new HashMap<>();
|
|
|
|
hm.put("name", account.name);
|
|
|
|
hm.put("owner_key", account.owner_key);
|
|
|
|
hm.put("active_key", account.active_key);
|
|
|
|
hm.put("memo_key", account.memo_key);
|
|
|
|
hm.put("refcode", "agorise");
|
|
|
|
hm.put("referrer", "agorise");
|
|
|
|
|
|
|
|
HashMap<String, HashMap> hashMap = new HashMap<>();
|
|
|
|
hashMap.put("account", hm);
|
2018-01-29 01:18:38 +00:00
|
|
|
|
2018-01-10 02:00:46 +00:00
|
|
|
try {
|
|
|
|
ServiceGenerator sg = new ServiceGenerator(url);
|
|
|
|
IWebService service = sg.getService(IWebService.class);
|
|
|
|
final Call<RegisterAccountResponse> postingService = service.getReg(hashMap);
|
|
|
|
postingService.enqueue(new Callback<RegisterAccountResponse>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<RegisterAccountResponse> call, Response<RegisterAccountResponse> response) {
|
|
|
|
if (response.isSuccessful()) {
|
2018-01-11 03:07:30 +00:00
|
|
|
System.out.println("faucet answer " + response);
|
2018-01-10 02:00:46 +00:00
|
|
|
RegisterAccountResponse resp = response.body();
|
|
|
|
if (resp.account != null) {
|
|
|
|
try {
|
|
|
|
if(resp.account.name.equals(account.name)) {
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().success(true,request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
}else{
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
}
|
|
|
|
}else{
|
2018-01-11 03:07:30 +00:00
|
|
|
System.out.println("ERROR response doesn't have account " + response.message());
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}else{
|
2018-01-11 03:07:30 +00:00
|
|
|
System.out.println("ERROR fetching info");
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<RegisterAccountResponse> call, Throwable t) {
|
|
|
|
t.printStackTrace();
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2018-01-29 01:18:38 +00:00
|
|
|
request.getListener().fail(request.getId());
|
2018-01-10 02:00:46 +00:00
|
|
|
|
2018-01-29 01:18:38 +00:00
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
}
|
2017-11-05 03:22:53 +00:00
|
|
|
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* Class used for the json serializer. this represents a peitition
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
private static class CreateAccountPetition{
|
2017-11-12 19:20:03 +00:00
|
|
|
// The account to be created
|
2017-11-05 03:22:53 +00:00
|
|
|
Account account;
|
|
|
|
}
|
|
|
|
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* Class used for the json serializer. This represents the account on the petition
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
private static class Account{
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* The name of the account
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
String name;
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* The owner key address
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
String owner_key;
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* The active key address
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
String active_key;
|
2017-11-12 19:20:03 +00:00
|
|
|
/**
|
|
|
|
* The memo key address
|
|
|
|
*/
|
2017-11-05 03:22:53 +00:00
|
|
|
String memo_key;
|
|
|
|
}
|
2018-01-10 02:00:46 +00:00
|
|
|
|
|
|
|
public static class ServiceGenerator{
|
|
|
|
public static String TAG = "ServiceGenerator";
|
|
|
|
public static String API_BASE_URL;
|
|
|
|
private static HttpLoggingInterceptor logging;
|
|
|
|
private static OkHttpClient.Builder clientBuilder;
|
|
|
|
private static Retrofit.Builder builder;
|
2018-09-18 19:47:15 +00:00
|
|
|
private static final long TIMEOUT = 30;
|
2018-01-10 02:00:46 +00:00
|
|
|
|
|
|
|
private static HashMap<Class<?>, Object> Services;
|
|
|
|
|
|
|
|
public ServiceGenerator(String apiBaseUrl) {
|
|
|
|
API_BASE_URL= apiBaseUrl;
|
|
|
|
logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
|
|
clientBuilder = new OkHttpClient.Builder().addInterceptor(logging);
|
|
|
|
builder = new Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
|
|
|
|
Services = new HashMap<Class<?>, Object>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> void setService(Class<T> klass, T thing) {
|
|
|
|
Services.put(klass, thing);
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T> T getService(Class<T> serviceClass) {
|
|
|
|
|
|
|
|
T service = serviceClass.cast(Services.get(serviceClass));
|
|
|
|
if (service == null) {
|
|
|
|
service = createService(serviceClass);
|
|
|
|
setService(serviceClass, service);
|
|
|
|
}
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <S> S createService(Class<S> serviceClass) {
|
|
|
|
|
|
|
|
clientBuilder.interceptors().add(new Interceptor() {
|
|
|
|
@Override
|
|
|
|
public okhttp3.Response intercept(Chain chain) throws IOException {
|
|
|
|
okhttp3.Request original = chain.request();
|
|
|
|
okhttp3.Request.Builder requestBuilder = original.newBuilder().method(original.method(), original.body());
|
|
|
|
|
|
|
|
okhttp3.Request request = requestBuilder.build();
|
|
|
|
return chain.proceed(request);
|
|
|
|
}
|
|
|
|
});
|
2018-09-18 19:47:15 +00:00
|
|
|
clientBuilder.readTimeout(TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(TIMEOUT,TimeUnit.SECONDS);
|
2018-01-10 02:00:46 +00:00
|
|
|
OkHttpClient client = clientBuilder.build();
|
|
|
|
Retrofit retrofit = builder.client(client).build();
|
|
|
|
return retrofit.create(serviceClass);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IWebService Create() {
|
|
|
|
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
|
|
|
|
httpClient.interceptors().add(new Interceptor() {
|
|
|
|
@Override
|
|
|
|
public okhttp3.Response intercept(Chain chain) throws IOException {
|
|
|
|
okhttp3.Request original = chain.request();
|
|
|
|
|
|
|
|
// Customize the request
|
|
|
|
okhttp3.Request request = original.newBuilder().method(original.method(), original.body()).build();
|
|
|
|
|
|
|
|
okhttp3.Response response = chain.proceed(request);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
OkHttpClient client = httpClient.build();
|
|
|
|
Retrofit retrofit = new Retrofit.Builder().baseUrl(API_BASE_URL).client(client).build();
|
|
|
|
|
|
|
|
return retrofit.create(IWebService.class);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface IWebService {
|
|
|
|
@Headers({"Content-Type: application/json"})
|
2018-09-19 19:38:17 +00:00
|
|
|
@POST("/api/v1/accounts")
|
2018-01-10 02:00:46 +00:00
|
|
|
Call<RegisterAccountResponse> getReg(@Body Map<String, HashMap> params);
|
2018-08-18 05:18:59 +00:00
|
|
|
|
2018-01-10 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class RegisterAccountResponse {
|
|
|
|
public Account account;
|
|
|
|
public Error error;
|
|
|
|
|
|
|
|
public class Error {
|
|
|
|
public String[] base;
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 00:23:44 +00:00
|
|
|
}
|