2017-09-27 02:48:34 +00:00
|
|
|
package cy.agorise.crystalwallet.manager;
|
|
|
|
|
2017-10-10 02:44:44 +00:00
|
|
|
import android.arch.lifecycle.LiveData;
|
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
import com.google.common.primitives.UnsignedLong;
|
|
|
|
|
|
|
|
import org.bitcoinj.core.ECKey;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2017-10-03 02:49:17 +00:00
|
|
|
import cy.agorise.crystalwallet.apigenerator.ApiRequest;
|
|
|
|
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
|
|
|
|
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
|
2017-10-02 02:18:56 +00:00
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest;
|
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendRequest;
|
2017-10-08 16:02:11 +00:00
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest;
|
2017-10-02 02:18:56 +00:00
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
|
2017-10-10 02:44:44 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
|
|
|
import cy.agorise.crystalwallet.models.AccountSeed;
|
2017-09-27 02:48:34 +00:00
|
|
|
import cy.agorise.crystalwallet.models.CryptoNetAccount;
|
2017-10-12 03:37:05 +00:00
|
|
|
import cy.agorise.crystalwallet.models.GrapheneAccount;
|
2017-10-05 03:28:19 +00:00
|
|
|
import cy.agorise.graphenej.Address;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.Asset;
|
|
|
|
import cy.agorise.graphenej.AssetAmount;
|
|
|
|
import cy.agorise.graphenej.BaseOperation;
|
2017-10-04 01:31:16 +00:00
|
|
|
import cy.agorise.graphenej.BrainKey;
|
|
|
|
import cy.agorise.graphenej.PublicKey;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.Transaction;
|
|
|
|
import cy.agorise.graphenej.UserAccount;
|
2017-10-04 01:31:16 +00:00
|
|
|
import cy.agorise.graphenej.models.AccountProperties;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.operations.TransferOperationBuilder;
|
2017-09-27 02:48:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by henry on 26/9/2017.
|
|
|
|
*/
|
|
|
|
|
2017-10-02 02:18:56 +00:00
|
|
|
public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener {
|
2017-09-27 02:48:34 +00:00
|
|
|
@Override
|
|
|
|
public CryptoNetAccount createAccountFromSeed(CryptoNetAccount account) {
|
2017-10-12 03:37:05 +00:00
|
|
|
//TODO generate account keys
|
|
|
|
//TODO register account
|
|
|
|
//TODO save account on DB
|
|
|
|
//TODO subscribe account
|
2017-09-27 02:48:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CryptoNetAccount importAccountFromSeed(CryptoNetAccount account) {
|
2017-10-12 03:37:05 +00:00
|
|
|
//TODO check account info
|
|
|
|
//TODO save account on DB
|
|
|
|
//TODO subscribe account
|
|
|
|
//TODO refresh account transaction
|
|
|
|
//TODO refresh account balance
|
2017-09-27 02:48:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadAccountFromDB(CryptoNetAccount account) {
|
2017-10-12 03:37:05 +00:00
|
|
|
if(account instanceof GrapheneAccount){
|
|
|
|
GrapheneAccount grapheneAccount = (GrapheneAccount) account;
|
|
|
|
if(grapheneAccount.getAccountId() == null){
|
|
|
|
//TODO find account data by name
|
|
|
|
}else if(grapheneAccount.getName() == null){
|
|
|
|
//TODO find account data by id
|
|
|
|
}
|
2017-09-27 02:48:34 +00:00
|
|
|
|
2017-10-12 03:37:05 +00:00
|
|
|
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getAccountId());
|
|
|
|
//TODO refresh account transactions
|
|
|
|
//TODO refresh account balance
|
|
|
|
}
|
2017-09-27 02:48:34 +00:00
|
|
|
}
|
2017-10-02 02:18:56 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNewRequest(CryptoNetInfoRequest request) {
|
|
|
|
if (request instanceof ValidateImportBitsharesAccountRequest){
|
2017-10-09 00:10:46 +00:00
|
|
|
this.validateImportAccount((ValidateImportBitsharesAccountRequest) request);
|
|
|
|
} else if (request instanceof ValidateExistBitsharesAccountRequest){
|
|
|
|
this.validateExistAcccount((ValidateExistBitsharesAccountRequest) request);
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 02:04:42 +00:00
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
private void validateImportAccount(final ValidateImportBitsharesAccountRequest importRequest){
|
|
|
|
ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() {
|
|
|
|
@Override
|
|
|
|
public void success(Object answer, int idPetition) {
|
|
|
|
importRequest.setAccountExists(true);
|
|
|
|
ApiRequest getAccountInfo = new ApiRequest(1,new ApiRequestListener(){
|
|
|
|
@Override
|
|
|
|
public void success(Object answer, int idPetition) {
|
|
|
|
if(answer != null && answer instanceof AccountProperties) {
|
|
|
|
AccountProperties prop = (AccountProperties) answer;
|
|
|
|
//TODO change the way to compare keys
|
|
|
|
|
|
|
|
BrainKey bk = new BrainKey(importRequest.getMnemonic(), 0);
|
|
|
|
for(PublicKey activeKey : prop.active.getKeyAuthList()){
|
2017-10-05 03:28:19 +00:00
|
|
|
if((new Address(activeKey.getKey(),"BTS")).toString().equals(bk.getPublicAddress("BTS").toString())){
|
2017-10-04 01:31:16 +00:00
|
|
|
importRequest.setMnemonicIsCorrect(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 00:10:46 +00:00
|
|
|
importRequest.setMnemonicIsCorrect(false);
|
2017-10-03 02:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
}
|
2017-10-03 02:49:17 +00:00
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
});
|
|
|
|
GrapheneApiGenerator.getAccountById((String)answer,getAccountInfo);
|
|
|
|
}
|
2017-10-03 02:49:17 +00:00
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
GrapheneApiGenerator.getAccountIdByName(importRequest.getAccountName(),checkAccountName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateExistAcccount(final ValidateExistBitsharesAccountRequest validateRequest){
|
|
|
|
ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() {
|
|
|
|
@Override
|
|
|
|
public void success(Object answer, int idPetition) {
|
|
|
|
validateRequest.setAccountExists(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
|
|
|
//TODO verified
|
|
|
|
validateRequest.setAccountExists(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
GrapheneApiGenerator.getAccountIdByName(validateRequest.getAccountName(),checkAccountName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){
|
|
|
|
Asset feeAsset = new Asset(sendRequest.getFeeAsset());
|
|
|
|
TransferOperationBuilder builder = new TransferOperationBuilder()
|
2017-10-10 02:44:44 +00:00
|
|
|
.setSource(new UserAccount(sendRequest.getSourceAccount().getAccountId()))
|
2017-10-09 00:10:46 +00:00
|
|
|
.setDestination(new UserAccount(sendRequest.getToAccount()))
|
|
|
|
.setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getBaseAmount()), new Asset(sendRequest.getBaseAsset())))
|
|
|
|
.setFee(new AssetAmount(UnsignedLong.valueOf(sendRequest.getFeeAmount()), feeAsset));
|
|
|
|
//TODO memo
|
|
|
|
ArrayList<BaseOperation> operationList = new ArrayList();
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
2017-10-10 02:44:44 +00:00
|
|
|
//TODO get privateKey with seed model
|
|
|
|
LiveData<AccountSeed> seed = CrystalDatabase.getAppDatabase(sendRequest.getContext()).accountSeedDao().findById(sendRequest.getSourceAccount().getSeedId());
|
|
|
|
|
|
|
|
ECKey privateKey = new BrainKey(seed.getValue().getMasterSeed(),0).getPrivateKey();
|
|
|
|
Transaction transaction = new Transaction(privateKey, null, operationList);
|
2017-10-09 00:10:46 +00:00
|
|
|
|
|
|
|
ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() {
|
|
|
|
@Override
|
|
|
|
public void success(Object answer, int idPetition) {
|
|
|
|
sendRequest.setSend(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
|
|
|
sendRequest.setSend(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
GrapheneApiGenerator.broadcastTransaction(transaction,feeAsset, transactionRequest);
|
2017-10-02 02:18:56 +00:00
|
|
|
}
|
2017-09-27 02:48:34 +00:00
|
|
|
}
|