From f4a8c96e1f9ee1a69c67542a29523462d2b8cba8 Mon Sep 17 00:00:00 2001 From: Joey Harward Date: Wed, 12 Aug 2020 11:23:39 -0400 Subject: [PATCH 1/2] added account availability function added private key signature function fixed worker init updated version to 0.5.3 --- buildSrc/src/main/kotlin/Dependencies.kt | 4 +-- .../com/metallicus/protonsdk/AccountModule.kt | 14 +++++++--- .../java/com/metallicus/protonsdk/Proton.kt | 27 +++++++++++++++++++ .../com/metallicus/protonsdk/WorkersModule.kt | 17 ++++++++---- .../protonsdk/model/ActiveAccount.kt | 3 +++ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 8bbc7053..a77553d0 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -23,8 +23,8 @@ const val kotlinVersion = "1.3.72" const val orchidVersion = "0.21.1" object ProtonSdk { - const val versionCode = 14 - const val versionName = "0.5.2" + const val versionCode = 15 + const val versionName = "0.5.3" } object BuildPlugins { diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt index eda9f026..0495e5a9 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt @@ -31,7 +31,6 @@ import com.metallicus.protonsdk.eosio.commander.ec.EosPrivateKey import com.metallicus.protonsdk.model.* import com.metallicus.protonsdk.repository.AccountContactRepository import com.metallicus.protonsdk.repository.AccountRepository -import com.metallicus.protonsdk.repository.ChainProviderRepository import timber.log.Timber import java.nio.charset.Charset import javax.inject.Inject @@ -43,9 +42,6 @@ class AccountModule { @Inject lateinit var context: Context - @Inject - lateinit var chainProviderRepository: ChainProviderRepository - @Inject lateinit var accountRepository: AccountRepository @@ -66,6 +62,16 @@ class AccountModule { return prefs.getActiveAccountName().isNotEmpty() } + suspend fun accountAvailable(chainUrl: String, accountName: String): Boolean { + val response = accountRepository.fetchAccount(chainUrl, accountName) + return if (response.isSuccessful) { + false + } else { + // TODO: check for network errors + true + } + } + suspend fun fetchAccountsForKey(chainId: String, chainUrl: String, hyperionHistoryUrl: String, publicKey: String): Resource> { val accounts = mutableListOf() diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt index 19481f7c..d1367ce2 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt @@ -29,6 +29,7 @@ import com.metallicus.protonsdk.common.Resource import com.metallicus.protonsdk.common.SingletonHolder import com.metallicus.protonsdk.di.DaggerInjector import com.metallicus.protonsdk.di.ProtonModule +import com.metallicus.protonsdk.eosio.commander.digest.Sha256 import com.metallicus.protonsdk.eosio.commander.ec.EosPrivateKey import com.metallicus.protonsdk.model.* import kotlinx.coroutines.* @@ -136,6 +137,28 @@ class Proton private constructor(context: Context) { return EosPrivateKey() } + fun signWithPrivateKey(privateKeyStr: String, valueToSign: String): String { + return try { + val privateKey = EosPrivateKey(privateKeyStr) + val sha256 = Sha256.from(valueToSign.toByteArray()) + privateKey.sign(sha256).toString() + } catch (e: Exception) { "" } + } + + fun accountAvailable(accountName: String): LiveData> = liveData { + emit(Resource.loading()) + + try { + val chainProvider = getChainProviderAsync() + + emit(Resource.success(accountModule.accountAvailable(chainProvider.chainUrl, accountName))) + } catch (e: ProtonException) { + emit(Resource.error(e)) + } catch (e: Exception) { + emit(Resource.error(e.localizedMessage.orEmpty())) + } + } + private suspend fun findAccounts(publicKeyStr: String): Resource> { return try { val chainProvider = getChainProviderAsync() @@ -174,6 +197,10 @@ class Proton private constructor(context: Context) { } } + fun hasActiveAccount(): Boolean { + return accountModule.hasActiveAccount() + } + fun setActiveAccount(activeAccount: ActiveAccount): LiveData> = liveData { emit(Resource.loading()) diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt index 6c568e27..61293b8a 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt @@ -83,11 +83,18 @@ class WorkersModule { val initActiveAccount = OneTimeWorkRequest.Builder(InitActiveAccountWorker::class.java) .setConstraints(constraints).build() - workManager - .beginUniqueWork(INIT, ExistingWorkPolicy.REPLACE, initChainProvider) - .then(initTokenContracts) - .then(initActiveAccount) - .enqueue() + if (prefs.getActiveAccountName().isNotEmpty()) { + workManager + .beginUniqueWork(INIT, ExistingWorkPolicy.REPLACE, initChainProvider) + .then(initTokenContracts) + .then(initActiveAccount) + .enqueue() + } else { + workManager + .beginUniqueWork(INIT, ExistingWorkPolicy.REPLACE, initChainProvider) + .then(initTokenContracts) + .enqueue() + } // start periodic worker to update exchange rates val updateTokenContractRates = PeriodicWorkRequest.Builder(UpdateTokenContractRatesWorker::class.java, 15L, TimeUnit.MINUTES) diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/model/ActiveAccount.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/model/ActiveAccount.kt index fbe7a800..a103bf2e 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/model/ActiveAccount.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/model/ActiveAccount.kt @@ -23,6 +23,9 @@ package com.metallicus.protonsdk.model import com.metallicus.protonsdk.eosio.commander.ec.EosPrivateKey +//fun activeAccount(accountName: String, builder: ActiveAccount.Builder.() -> Unit): ActiveAccount = +// ActiveAccount.Builder(accountName).apply(builder).create() + class ActiveAccount(builder: Builder) { val accountName: String = builder.accountName val publicKey: String = builder.publicKey From 9d5b44e89bd867bb726ce88063b89472387caa2c Mon Sep 17 00:00:00 2001 From: Joey Harward Date: Wed, 12 Aug 2020 12:08:58 -0400 Subject: [PATCH 2/2] updated docs for v0.5.3 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb62c7a9..4fe59c8d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Then add the following dependency to your module's build.gradle ```gradle dependencies { ... - implementation "com.metallicus:protonsdk:0.5.2" + implementation "com.metallicus:protonsdk:0.5.3" } ```