2017-09-27 02:48:34 +00:00
|
|
|
package cy.agorise.crystalwallet.manager;
|
|
|
|
|
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-09-27 02:48:34 +00:00
|
|
|
import cy.agorise.crystalwallet.models.CryptoNetAccount;
|
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;
|
|
|
|
import cy.agorise.graphenej.BlockData;
|
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.TransferOperation;
|
|
|
|
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) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CryptoNetAccount importAccountFromSeed(CryptoNetAccount account) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadAccountFromDB(CryptoNetAccount account) {
|
|
|
|
|
|
|
|
}
|
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()
|
|
|
|
.setSource(new UserAccount(sendRequest.getSourceAccount()))
|
|
|
|
.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());
|
|
|
|
|
|
|
|
//TODO blockdata
|
|
|
|
BlockData blockData = null;
|
|
|
|
//TODO privateKey
|
|
|
|
ECKey privateKey = null;
|
|
|
|
Transaction transaction = new Transaction(privateKey, blockData, operationList);
|
|
|
|
|
|
|
|
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
|
|
|
}
|