Added faucet function
This commit is contained in:
parent
4d3b939f62
commit
528013ba12
5 changed files with 208 additions and 17 deletions
|
@ -55,6 +55,8 @@ dependencies {
|
|||
compile 'com.google.zxing:core:3.3.1'
|
||||
compile 'me.dm7.barcodescanner:zxing:1.9.8';
|
||||
|
||||
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile 'org.mockito:mockito-core:1.10.19'
|
||||
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
|
||||
|
|
|
@ -48,7 +48,8 @@ public class IntroActivity extends AppCompatActivity {
|
|||
//If the user doesn't have any seeds created, then
|
||||
//send the user to create/import an account
|
||||
//Intent intent = new Intent(this, AccountSeedsManagementActivity.class);
|
||||
Intent intent = new Intent(this, ImportSeedActivity.class);
|
||||
//Intent intent = new Intent(this, ImportSeedActivity.class);
|
||||
Intent intent = new Intent(this, CreateSeedActivity.class);
|
||||
startActivity(intent);
|
||||
} else {
|
||||
//Intent intent = new Intent(this, CreateSeedActivity.class);
|
||||
|
|
|
@ -1,7 +1,28 @@
|
|||
package cy.agorise.crystalwallet.apigenerator;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* This maanges the calls for the creation of accounts using the bitshares faucet
|
||||
*
|
||||
|
@ -20,10 +41,10 @@ public abstract class BitsharesFaucetApiGenerator {
|
|||
* @param url The url of the faucet
|
||||
* @return The bitshares id of the registered account, or null
|
||||
*/
|
||||
public static String registerBitsharesAccount(String accountName, String ownerKey,
|
||||
public static boolean registerBitsharesAccount(String accountName, String ownerKey,
|
||||
String activeKey, String memoKey, String url){
|
||||
CreateAccountPetition petition = new CreateAccountPetition();
|
||||
Account account = new Account();
|
||||
final Account account = new Account();
|
||||
account.name=accountName;
|
||||
account.owner_key=ownerKey;
|
||||
account.active_key=activeKey;
|
||||
|
@ -34,7 +55,79 @@ public abstract class BitsharesFaucetApiGenerator {
|
|||
System.out.println("create account petition :" + jsonPetition);
|
||||
|
||||
//TODO faucet function
|
||||
return null;
|
||||
|
||||
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);
|
||||
final boolean[] answer = {false};
|
||||
final Object SYNC = new Object();
|
||||
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()) {
|
||||
RegisterAccountResponse resp = response.body();
|
||||
if (resp.account != null) {
|
||||
try {
|
||||
if(resp.account.name.equals(account.name)) {
|
||||
synchronized (SYNC){
|
||||
answer[0] = true;
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
}else{
|
||||
//ERROR
|
||||
synchronized (SYNC) {
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
synchronized (SYNC) {
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//ERROR
|
||||
synchronized (SYNC) {
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
|
||||
}
|
||||
}else{
|
||||
//ERROR
|
||||
synchronized (SYNC) {
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<RegisterAccountResponse> call, Throwable t) {
|
||||
t.printStackTrace();
|
||||
synchronized (SYNC) {
|
||||
SYNC.notifyAll();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (SYNC) {
|
||||
SYNC.wait(60000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return answer[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,4 +159,94 @@ public abstract class BitsharesFaucetApiGenerator {
|
|||
*/
|
||||
String memo_key;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
clientBuilder.readTimeout(5, TimeUnit.MINUTES);
|
||||
clientBuilder.connectTimeout(5, TimeUnit.MINUTES);
|
||||
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"})
|
||||
@POST("/api/v1/accounts")
|
||||
Call<RegisterAccountResponse> getReg(@Body Map<String, HashMap> params);
|
||||
}
|
||||
|
||||
public class RegisterAccountResponse {
|
||||
public Account account;
|
||||
public Error error;
|
||||
|
||||
public class Error {
|
||||
public String[] base;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,22 +59,23 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
|||
if(account instanceof GrapheneAccount) {
|
||||
|
||||
GrapheneAccount grapheneAccount = (GrapheneAccount) account;
|
||||
String btsIdAccount = BitsharesFaucetApiGenerator.registerBitsharesAccount(grapheneAccount.getName(),
|
||||
boolean created = BitsharesFaucetApiGenerator.registerBitsharesAccount(grapheneAccount.getName(),
|
||||
new Address(grapheneAccount.getOwnerKey(context),"BTS").toString(),
|
||||
new Address(grapheneAccount.getActiveKey(context),"BTS").toString(),
|
||||
new Address(grapheneAccount.getMemoKey(context),"BTS").toString(),GrapheneApiGenerator.faucetUrl);
|
||||
if(btsIdAccount !=null) {
|
||||
grapheneAccount.setAccountId(btsIdAccount);
|
||||
|
||||
if(created) {
|
||||
GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName());
|
||||
|
||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||
long idAccount = db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount)[0];
|
||||
grapheneAccount.setId(idAccount);
|
||||
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
|
||||
long idAccount = db.cryptoNetAccountDao().insertCryptoNetAccount(fetch)[0];
|
||||
fetch.setId(idAccount);
|
||||
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(fetch));
|
||||
|
||||
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
|
||||
BitsharesAccountManager.refreshAccountTransactions(grapheneAccount.getId(), context);
|
||||
GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
|
||||
return grapheneAccount;
|
||||
GrapheneApiGenerator.subscribeBitsharesAccount(fetch.getId(), fetch.getAccountId(), context);
|
||||
BitsharesAccountManager.refreshAccountTransactions(fetch.getId(), context);
|
||||
GrapheneApiGenerator.getAccountBalance(fetch.getId(), fetch.getAccountId(), context);
|
||||
return fetch;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -55,11 +55,15 @@ public class GrapheneAccount extends CryptoNetAccount {
|
|||
*/
|
||||
public ECKey getOwnerKey(Context context){
|
||||
AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId()).getValue();
|
||||
if(seed == null)
|
||||
if(seed == null){
|
||||
System.out.println("Error: Seed null " + this.getSeedId());
|
||||
return null;
|
||||
}
|
||||
if(seed.getType().equals(SeedType.BRAINKEY)){
|
||||
System.out.println("Seed type barinkey");
|
||||
return seed.getPrivateKey();
|
||||
}else{
|
||||
System.out.println("Seed type bip39");
|
||||
DeterministicKey masterKey = (DeterministicKey) seed.getPrivateKey();
|
||||
DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey,
|
||||
new ChildNumber(48, true));
|
||||
|
@ -70,7 +74,7 @@ public class GrapheneAccount extends CryptoNetAccount {
|
|||
DeterministicKey permission = HDKeyDerivation.deriveChildKey(accountIndexKey,
|
||||
new ChildNumber(0, true));
|
||||
DeterministicKey address = HDKeyDerivation.deriveChildKey(permission,
|
||||
new ChildNumber(0, true));
|
||||
new ChildNumber(0, false));
|
||||
return address;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue