This commit is contained in:
Javier Varona 2018-05-21 21:38:30 -04:00
commit 4f8df9deaf
5 changed files with 234 additions and 1 deletions

5
.gitignore vendored
View file

@ -35,6 +35,11 @@ proguard/
# Android Studio captures folder
captures/
# Local configuration file (sdk path, etc)
.classpath
.project
.settings
# Intellij
*.iml

View file

@ -0,0 +1,144 @@
package cy.agorise.crystalwallet.apigenerator.grapheneoperation;
import com.google.common.primitives.Bytes;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.BaseOperation;
import cy.agorise.graphenej.OperationType;
import cy.agorise.graphenej.UserAccount;
/**
* Created by henry on 16/5/2018.
*/
public class AccountUpgradeOperation extends BaseOperation {
private static final String KEY_ACCOUNT = "account_to_upgrade";
private static final String KEY_UPGRADE = "upgrade_to_lifetime_member";
private AssetAmount fee;
private UserAccount accountToUpgrade;
private boolean upgradeToLifeTimeMember;
public AccountUpgradeOperation(UserAccount accountToUpgrade, boolean upgradeToLifeTimeMember) {
super(OperationType.ACCOUNT_UPGRADE_OPERATION);
this.accountToUpgrade = accountToUpgrade;
this.upgradeToLifeTimeMember = upgradeToLifeTimeMember;
}
public AccountUpgradeOperation(UserAccount accountToUpgrade, boolean upgradeToLifeTimeMember, AssetAmount fee) {
super(OperationType.ACCOUNT_UPGRADE_OPERATION);
this.accountToUpgrade = accountToUpgrade;
this.upgradeToLifeTimeMember = upgradeToLifeTimeMember;
this.fee = fee;
}
public AssetAmount getFee() {
return fee;
}
public UserAccount getAccountToUpgrade() {
return accountToUpgrade;
}
public void setAccountToUpgrade(UserAccount accountToUpgrade) {
this.accountToUpgrade = accountToUpgrade;
}
public boolean isUpgradeToLifeTimeMember() {
return upgradeToLifeTimeMember;
}
public void setUpgradeToLifeTimeMember(boolean upgradeToLifeTimeMember) {
this.upgradeToLifeTimeMember = upgradeToLifeTimeMember;
}
@Override
public void setFee(AssetAmount assetAmount) {
this.fee = assetAmount;
}
@Override
public byte[] toBytes() {
byte[] feeBytes = fee.toBytes();
byte[] accountBytes = accountToUpgrade.toBytes();
byte[] upgradeToLifeTimeMemberBytes = this.upgradeToLifeTimeMember ? new byte[]{ 0x1 } : new byte[]{ 0x0 };
byte[] extensions = this.extensions.toBytes();
return Bytes.concat(feeBytes, accountBytes, upgradeToLifeTimeMemberBytes, extensions);
}
@Override
public String toJsonString() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AccountUpgradeOperation.class, new AccountUpgradeSerializer());
return gsonBuilder.create().toJson(this);
}
@Override
public JsonElement toJsonObject() {
JsonArray array = new JsonArray();
array.add(this.getId());
JsonObject jsonObject = new JsonObject();
if(fee != null)
jsonObject.add(KEY_FEE, fee.toJsonObject());
jsonObject.addProperty(KEY_ACCOUNT, accountToUpgrade.getObjectId());
jsonObject.addProperty(KEY_UPGRADE, this.upgradeToLifeTimeMember ? "true" : "false");
jsonObject.add(KEY_EXTENSIONS, new JsonArray());
array.add(jsonObject);
return array;
}
public static class AccountUpgradeSerializer implements JsonSerializer<AccountUpgradeOperation> {
@Override
public JsonElement serialize(AccountUpgradeOperation accountUpgrade, Type type, JsonSerializationContext jsonSerializationContext) {
return accountUpgrade.toJsonObject();
}
}
public static class AccountUpgradeDeserializer implements JsonDeserializer<AccountUpgradeOperation> {
@Override
public AccountUpgradeOperation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if(json.isJsonArray()){
// This block is used just to check if we are in the first step of the deserialization
// when we are dealing with an array.
JsonArray serializedAccountUpgrade = json.getAsJsonArray();
if(serializedAccountUpgrade.get(0).getAsInt() != OperationType.ACCOUNT_UPGRADE_OPERATION.ordinal()){
// If the operation type does not correspond to a transfer operation, we return null
return null;
}else{
// Calling itself recursively, this is only done once, so there will be no problems.
return context.deserialize(serializedAccountUpgrade.get(1), AccountUpgradeOperation.class);
}
}else{
JsonObject jsonObject = json.getAsJsonObject();
// Deserializing AssetAmount objects
AssetAmount fee = context.deserialize(jsonObject.get(KEY_FEE), AssetAmount.class);
// Deserializing UserAccount objects
UserAccount accountToUpgrade = new UserAccount(jsonObject.get(KEY_ACCOUNT).getAsString());
boolean upgradeToLifeTime = jsonObject.get(KEY_UPGRADE).getAsBoolean();
AccountUpgradeOperation upgrade = new AccountUpgradeOperation(accountToUpgrade, upgradeToLifeTime, fee);
return upgrade;
}
}
}
}

View file

@ -0,0 +1,47 @@
package cy.agorise.crystalwallet.apigenerator.grapheneoperation;
import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.errors.MalformedOperationException;
import cy.agorise.graphenej.operations.BaseOperationBuilder;
/**
* Created by henry on 16/5/2018.
*/
public class AccountUpgradeOperationBuilder extends BaseOperationBuilder {
private UserAccount accountToUpgrade;
private AssetAmount fee;
private boolean isUpgrade = true;
public AccountUpgradeOperationBuilder setAccountToUpgrade(UserAccount accountToUpgrade) {
this.accountToUpgrade = accountToUpgrade;
return this;
}
public AccountUpgradeOperationBuilder setFee(AssetAmount fee) {
this.fee = fee;
return this;
}
public AccountUpgradeOperationBuilder setIsUpgrade(Boolean isUpgrade) {
this.isUpgrade = isUpgrade;
return this;
}
@Override
public AccountUpgradeOperation build(){
AccountUpgradeOperation accountUpgrade;
if(accountToUpgrade == null ){
throw new MalformedOperationException("Missing account to upgrade information");
}
if(fee != null){
accountUpgrade = new AccountUpgradeOperation(accountToUpgrade, isUpgrade, fee);
}else{
accountUpgrade = new AccountUpgradeOperation(accountToUpgrade, isUpgrade);
}
return accountUpgrade;
}
}

View file

@ -18,6 +18,8 @@ import cy.agorise.crystalwallet.apigenerator.ApiRequest;
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator;
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperation;
import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperationBuilder;
import cy.agorise.crystalwallet.application.constant.BitsharesConstant;
import cy.agorise.crystalwallet.models.seed.BIP39;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest;
@ -444,8 +446,36 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
*/
private void validateLTMAccountUpgrade(final ValidateBitsharesLTMUpgradeRequest sendRequest) {
//TODO check internet, server connection
sendRequest.setStatus(ValidateBitsharesLTMUpgradeRequest.StatusCode.PETITION_FAILED);
Asset feeAsset = new Asset(sendRequest.getIdAsset());
UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId());
CrystalDatabase db = CrystalDatabase.getAppDatabase(sendRequest.getContext());
AccountUpgradeOperationBuilder builder = new AccountUpgradeOperationBuilder()
.setAccountToUpgrade(fromUserAccount)
.setFee(new AssetAmount(UnsignedLong.valueOf(0), feeAsset));
ArrayList<BaseOperation> operationList = new ArrayList<>();
operationList.add(builder.build());
ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext());
Transaction transaction = new Transaction(privateKey, null, operationList);
transaction.setChainId(CryptoNetManager.getChaindId(CryptoNet.BITSHARES));
ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() {
@Override
public void success(Object answer, int idPetition) {
sendRequest.setStatus(ValidateBitsharesLTMUpgradeRequest.StatusCode.SUCCEEDED);
}
@Override
public void fail(int idPetition) {
sendRequest.setStatus(ValidateBitsharesLTMUpgradeRequest.StatusCode.PETITION_FAILED);
}
});
GrapheneApiGenerator.broadcastTransaction(transaction,feeAsset, transactionRequest);
}
/**

View file

@ -33,6 +33,9 @@ public class ValidateBitsharesLTMUpgradeRequest extends CryptoNetInfoRequest {
// The state of this request
private StatusCode status = StatusCode.NOT_STARTED;
//TODO change asset
private String idAsset = "1.3.0"; //default to bTS
public ValidateBitsharesLTMUpgradeRequest(Context context, GrapheneAccount sourceAccount) {
super(CryptoCoin.BITSHARES);
this.mContext = context;
@ -48,6 +51,10 @@ public class ValidateBitsharesLTMUpgradeRequest extends CryptoNetInfoRequest {
return mSourceAccount;
}
public String getIdAsset() {
return idAsset;
}
public void validate(){
if ((this.status != StatusCode.NOT_STARTED)){
this._fireOnCarryOutEvent();