@ -12,6 +12,7 @@ import android.content.pm.PackageManager;
import android.os.AsyncTask ;
import android.os.Handler ;
import android.preference.PreferenceManager ;
import android.support.v7.app.AlertDialog ;
import android.text.TextUtils ;
import android.text.format.DateFormat ;
import android.util.Log ;
@ -22,8 +23,8 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser ;
import com.wallet.crypto.trustapp.R ;
import com.wallet.crypto.trustapp.model.CMTicker ;
import com.wallet.crypto.trustapp.model.ES Transaction ;
import com.wallet.crypto.trustapp.model.ES TransactionListResponse ;
import com.wallet.crypto.trustapp.model.TR Transaction ;
import com.wallet.crypto.trustapp.model.TR TransactionListResponse ;
import com.wallet.crypto.trustapp.model.VMAccount ;
import com.wallet.crypto.trustapp.model.VMNetwork ;
import com.wallet.crypto.trustapp.util.KS ;
@ -53,6 +54,7 @@ import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount ;
import org.web3j.protocol.core.methods.response.EthSendTransaction ;
import org.web3j.protocol.core.methods.response.Web3ClientVersion ;
import org.web3j.protocol.http.HttpService ;
import org.web3j.protocol.infura.InfuraHttpService ;
import org.web3j.utils.Numeric ;
@ -106,9 +108,8 @@ public class Controller {
// View models
private ArrayList < VMNetwork > mNetworks ;
private ArrayList < String > mEtherscanKeys ;
private ArrayList < VMAccount > mAccounts ;
private Map < String , List < ES Transaction> > mTransactions ;
private Map < String , List < TR Transaction> > mTransactions ;
private CMTicker mEthTicker = null ; // if null, no data available
// Views
@ -144,19 +145,14 @@ public class Controller {
mEtherStore = new EtherStore ( mKeystoreBaseDir , this ) ;
// Create etherscan key list
mEtherscanKeys = new ArrayList < > ( ) ;
mEtherscanKeys . add ( "{etherscan_key1}" ) ;
mEtherscanKeys . add ( "{etherscan_key2}" ) ;
mEtherscanKeys . add ( "{etherscan_key3}" ) ;
// Create networks list
mNetworks = new ArrayList < > ( ) ;
mNetworks . add ( new VMNetwork ( "mainnet" , "https://mainnet.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://api.etherscan.io" , 1 ) ) ;
mNetworks . add ( new VMNetwork ( "kovan" , "https://kovan.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://kovan.etherscan.io" , 42 ) ) ;
mNetworks . add ( new VMNetwork ( "ropstein" , "https://ropstein.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://ropstein.etherscan.io" , 3 ) ) ;
mNetworks . add ( new VMNetwork ( "rinkeby" , "https://rinkeby.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://rinkeby.etherscan.io" , 4 ) ) ;
mNetworks . add ( new VMNetwork ( "Ethereum" , "ETH" , "https://mainnet.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://api.trustwalletapp.com/" ,
"https://etherscan.io/" , 1 ) ) ;
mNetworks . add ( new VMNetwork ( "Kovan (Test)" , "ETH(Kovan)" , "https://kovan.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://kovan.trustwalletapp.com/" , "https://kovan.etherscan.io" , 42 ) ) ;
mNetworks . add ( new VMNetwork ( "Ropsten (Test)" , "ETH(Ropsten)" , "https://ropsten.infura.io/llyrtzQ3YhkdESt2Fzrk" , "https://ropsten.trustwalletapp.com/" , "https://ropsten.etherscan.io" , 3 ) ) ;
//mNetworks.add(new VMNetwork("oracles", "POA", "http://testnet.oracles.org:8545/", "https://oracles.trustwalletapp.com", 12648430));
// Load current from app preferences
SharedPreferences sharedPref = PreferenceManager . getDefaultSharedPreferences ( mAppContext ) ;
@ -186,7 +182,7 @@ public class Controller {
}
for ( VMAccount a : mAccounts ) {
mTransactions . put ( a . getAddress ( ) , new ArrayList < ES Transaction> ( ) ) ;
mTransactions . put ( a . getAddress ( ) , new ArrayList < TR Transaction> ( ) ) ;
}
mHandler = new Handler ( ) ;
@ -266,8 +262,8 @@ public class Controller {
return out ;
}
public List < ES Transaction> getTransactions ( String address ) {
List < ES Transaction> txns = mTransactions . get ( address ) ;
public List < TR Transaction> getTransactions ( String address ) {
List < TR Transaction> txns = mTransactions . get ( address ) ;
if ( txns = = null ) {
return new ArrayList < > ( ) ;
}
@ -302,7 +298,7 @@ public class Controller {
public void navigateToSend ( Context context , String to_address ) {
Intent intent = new Intent ( context , SendActivity . class ) ;
intent . putExtra ( getString ( R . string . address_keyword ) , to_address ) ;
intent . putExtra ( Controller . KEY_ADDRESS , to_address ) ;
context . startActivity ( intent ) ;
}
@ -322,7 +318,7 @@ public class Controller {
VMAccount account = createAccount ( password ) ;
mAccounts . add ( account ) ;
mTransactions . put ( account . getAddress ( ) , new ArrayList < ES Transaction> ( ) ) ;
mTransactions . put ( account . getAddress ( ) , new ArrayList < TR Transaction> ( ) ) ;
Intent intent = new Intent ( activity . getApplicationContext ( ) , WarningBackupActivity . class ) ;
intent . setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
@ -504,15 +500,15 @@ public class Controller {
}
}
public ES Transaction findTransaction ( String address , String txn_hash ) {
List < ES Transaction> txns = mTransactions . get ( address ) ;
public TR Transaction findTransaction ( String address , String txn_hash ) {
List < TR Transaction> txns = mTransactions . get ( address ) ;
if ( txns = = null ) {
Log . e ( TAG , "Can't find transactions with given address: " + address ) ;
return null ;
}
for ( ES Transaction txn : txns ) {
for ( TR Transaction txn : txns ) {
if ( txn . getHash ( ) . equals ( txn_hash ) ) {
return txn ;
}
@ -555,7 +551,7 @@ public class Controller {
}
protected Void doInBackground ( Void . . . params ) {
Web3j web3 = Web3jFactory . build ( new Infura HttpService( mCurrentNetwork . getInfura Url ( ) ) ) ;
Web3j web3 = Web3jFactory . build ( new HttpService ( mCurrentNetwork . getRpc Url ( ) ) ) ;
/ *
Transaction transaction = Transaction . createFunctionCallTransaction (
from , gasPrice , gasLimit , contractAddress , amount , encodedFunction ) ;
@ -573,7 +569,7 @@ public class Controller {
private class GetWeb3ClientVersionTask extends AsyncTask < Void , Void , Void > {
protected Void doInBackground ( Void . . . params ) {
try {
Web3j web3 = Web3jFactory . build ( new Infura HttpService( mCurrentNetwork . getInfura Url ( ) ) ) ;
Web3j web3 = Web3jFactory . build ( new HttpService ( mCurrentNetwork . getRpc Url ( ) ) ) ;
Web3ClientVersion web3ClientVersion = web3 . web3ClientVersion ( ) . sendAsync ( ) . get ( ) ;
String clientVersion = web3ClientVersion . getWeb3ClientVersion ( ) ;
Log . d ( "INFO" , "web3 client version: " + clientVersion ) ;
@ -595,7 +591,7 @@ public class Controller {
private void getBalance ( VMAccount account ) {
try {
Web3j web3 = Web3jFactory . build ( new InfuraHttpService ( mCurrentNetwork . getInfura Url ( ) ) ) ;
Web3j web3 = Web3jFactory . build ( new InfuraHttpService ( mCurrentNetwork . getRpc Url ( ) ) ) ;
EthGetBalance ethGetBalance = web3
. ethGetBalance ( account . getAddress ( ) , DefaultBlockParameterName . LATEST )
. sendAsync ( )
@ -743,14 +739,6 @@ public class Controller {
// }
// }
// Randomize etherscan key selection stay below rate limit
private String getRandomEtherscanKey ( ) {
assert ( mEtherscanKeys . size ( ) > 0 ) ;
final int random = new Random ( ) . nextInt ( mEtherscanKeys . size ( ) ) ;
return mEtherscanKeys . get ( random ) ;
}
private class SendTransactionTask extends AsyncTask < Void , Void , Void > {
private String fromAddress ;
private String toAddress ;
@ -774,8 +762,9 @@ public class Controller {
}
protected Void doInBackground ( Void . . . params ) {
String txnHash = "" ;
try {
Web3j web3j = Web3jFactory . build ( new InfuraHttpService ( mCurrentNetwork . getInfura Url ( ) ) ) ;
Web3j web3j = Web3jFactory . build ( new InfuraHttpService ( mCurrentNetwork . getRpc Url ( ) ) ) ;
Account fromAccount = mEtherStore . getAccount ( fromAddress ) ;
if ( fromAccount = = null ) {
@ -806,6 +795,7 @@ public class Controller {
. get ( ) ;
String result = raw . getTransactionHash ( ) ;
Log . d ( TAG , "Transaction hash " + result ) ;
if ( raw . hasError ( ) ) {
@ -814,15 +804,17 @@ public class Controller {
listener . onTaskCompleted ( new TaskResult ( TaskStatus . FAILURE , "Transaction error: " + raw . getError ( ) . getMessage ( ) ) ) ;
return null ;
}
Log . d ( TAG , "Transaction JSON-RPC" + raw . getJsonrpc ( ) ) ;
Log . d ( TAG , "Transaction result: " + raw . getResult ( ) ) ;
Log . d ( TAG , "Transaction hash: " + raw . getTransactionHash ( ) ) ;
txnHash = raw . getTransactionHash ( ) ;
} catch ( Exception e ) {
Log . e ( TAG , e . toString ( ) ) ;
listener . onTaskCompleted ( new TaskResult ( TaskStatus . FAILURE , "Transaction error: " + e . toString ( ) ) ) ;
return null ;
}
listener . onTaskCompleted ( new TaskResult ( TaskStatus . SUCCESS , "Payment sent" ) ) ;
listener . onTaskCompleted ( new TaskResult ( TaskStatus . SUCCESS , txnHash ) ) ;
return null ;
}
}
@ -882,32 +874,21 @@ public class Controller {
Retrofit mRetrofit = new Retrofit . Builder ( )
. addConverterFactory ( GsonConverterFactory . create ( ) )
. baseUrl ( mCurrentNetwork . getEtherscan Url ( ) )
. baseUrl ( mCurrentNetwork . getBackend Url ( ) )
. build ( ) ;
EtherscanService service = mRetrofit . create ( EtherscanService . class ) ;
final String etherscanKey = getRandomEtherscanKey ( ) ;
Log . d ( TAG , "Using etherscan service: " + mCurrentNetwork . getName ( ) + ", " + mCurrentNetwork . getEtherscanUrl ( ) + ", " + etherscanKey ) ;
Call < ESTransactionListResponse > call =
service . getTransactionList (
"account" ,
"txlist" ,
address ,
"0" ,
"desc" ,
etherscanKey
) ;
TrustRayService service = mRetrofit . create ( TrustRayService . class ) ;
Call < TRTransactionListResponse > call =
service . getTransactionList ( address , "50" ) ;
Log . d ( "INFO" , "Request query:" + call . request ( ) . url ( ) . query ( ) ) ;
call . enqueue ( new Callback < ES TransactionListResponse> ( ) {
call . enqueue ( new Callback < TRTransactionListResponse > ( ) {
@Override
public void onResponse ( Call < ES TransactionListResponse> call , Response < ES TransactionListResponse> response ) {
public void onResponse ( Call < TRTransactionListResponse > call , Response < TRTransactionListResponse > response ) {
try {
List < ES Transaction> transactions = response . body ( ) . getTransactionList ( ) ;
List < TRTransaction > transactions = response . body ( ) . getTransactionList ( ) ;
mTransactions . put ( address , transactions ) ;
Log . d ( "INFO" , "Number of transactions: " + transactions . size ( ) ) ;
if ( transactions . size ( ) > 0 ) {
@ -920,7 +901,7 @@ public class Controller {
}
@Override
public void onFailure ( Call < ES TransactionListResponse> call , Throwable t ) {
public void onFailure ( Call < TR TransactionListResponse> call , Throwable t ) {
Log . e ( "ERROR" , t . toString ( ) ) ;
Toast . makeText ( mAppContext , "Error contacting RPC service. Check internet connection." , Toast . LENGTH_SHORT ) . show ( ) ;
}