From dc6114bb105485bbffc370beeea7ca8e45b256e9 Mon Sep 17 00:00:00 2001 From: hvarona Date: Mon, 5 Nov 2018 23:06:12 -0400 Subject: [PATCH] Starting Bitocin like account balance to 0 Added Steem url verifier --- .../manager/GeneralAccountManager.java | 6 ++ .../network/GetDatabaseVersion.java | 68 +++++++++++++++++++ .../network/SteemCryptoNetVerifier.java | 49 +++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/GetDatabaseVersion.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/SteemCryptoNetVerifier.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java index c051b8b..e47d8e6 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -93,6 +93,12 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf final DeterministicKey changeKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(1, false)); + CryptoCoinBalance balance = new CryptoCoinBalance(); + balance.setBalance(0); + balance.setCryptoCurrencyId(db.cryptoCurrencyDao().getByName(cryptoCoin.name(),cryptoCoin.name()).getId()); + balance.setAccountId(account.getId()); + db.cryptoCoinBalanceDao().insertCryptoCoinBalance(balance); + long indexExternal = db.bitcoinAddressDao().getLastExternalAddress(account.getId()); if(indexExternal > 0){ for(int i = 0; i < indexExternal;i++){ diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/GetDatabaseVersion.java b/app/src/main/java/cy/agorise/crystalwallet/network/GetDatabaseVersion.java new file mode 100644 index 0000000..6625030 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/GetDatabaseVersion.java @@ -0,0 +1,68 @@ +package cy.agorise.crystalwallet.network; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketFrame; + +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import cy.agorise.graphenej.RPC; +import cy.agorise.graphenej.api.BaseGrapheneHandler; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.ApiCall; +import cy.agorise.graphenej.models.WitnessResponse; + +/** + * Created by henry on 28/2/2018. + */ + +public class GetDatabaseVersion extends BaseGrapheneHandler { + + private final WitnessResponseListener mListener; + + public GetDatabaseVersion(WitnessResponseListener listener) { + super(listener); + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ApiCall getAccountByName = new ApiCall(0, "database_api.get_version", new ArrayList(), RPC.VERSION, 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + System.out.println("<<< "+frame.getPayloadText()); + String response = frame.getPayloadText(); + + Type GetChainIdResponse = new TypeToken>(){}.getType(); + GsonBuilder builder = new GsonBuilder(); + WitnessResponse witnessResponse = builder.create().fromJson(response, GetChainIdResponse); + if(witnessResponse.error != null){ + this.mListener.onError(witnessResponse.error); + }else{ + this.mListener.onSuccess(witnessResponse); + } + + websocket.disconnect(); + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()) + System.out.println(">>> "+frame.getPayloadText()); + } + + public class VersionResponse{ + public String blockchain_version; + public String steem_revision; + public String fc_revision; + public String chain_id; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/SteemCryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/SteemCryptoNetVerifier.java new file mode 100644 index 0000000..3374883 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/SteemCryptoNetVerifier.java @@ -0,0 +1,49 @@ +package cy.agorise.crystalwallet.network; + +import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.BaseResponse; +import cy.agorise.graphenej.models.WitnessResponse; + +/** + * + * Created by henry on 28/2/2018. + */ + +public class SteemCryptoNetVerifier extends CryptoNetVerifier { + private final CryptoNet cryptoNet = CryptoNet.STEEM; + private final String CHAIN_ID = "0000000000000000000000000000000000000000000000000000000000000000";//mainnet + + @Override + public void checkURL(final String url) { + final long startTime = System.currentTimeMillis(); + WebSocketThread thread = new WebSocketThread(new GetChainId(new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + if(response.result instanceof GetDatabaseVersion.VersionResponse) { + GetDatabaseVersion.VersionResponse result = (GetDatabaseVersion.VersionResponse) response.result; + if(result.chain_id.equals(CHAIN_ID)) { + CryptoNetManager.verifiedCryptoNetURL(cryptoNet, url, System.currentTimeMillis() - startTime); + }else{ + System.out.println(" BitsharesCryptoNetVerifier Error we are not in the net current chain id " + result.chain_id + " excepted " + CHAIN_ID); + //TODO handle error bad chain + } + }else{ + //TODO handle error bad answer + } + } + + @Override + public void onError(BaseResponse.Error error) { + //TODO handle error + System.out.println("Bad server response " + url); + } + }),url); + thread.start(); + } + + @Override + public String getChainId() { + return CHAIN_ID; + } +} \ No newline at end of file