Skip to content

Commit

Permalink
Merge pull request #5 from ProtonProtocol/develop
Browse files Browse the repository at this point in the history
Account availability and private key signature functions
  • Loading branch information
Joey Harward authored Aug 12, 2020
2 parents d04436b + 9d5b44e commit 21fdd8e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,9 +42,6 @@ class AccountModule {
@Inject
lateinit var context: Context

@Inject
lateinit var chainProviderRepository: ChainProviderRepository

@Inject
lateinit var accountRepository: AccountRepository

Expand All @@ -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<List<Account>> {
val accounts = mutableListOf<Account>()

Expand Down
27 changes: 27 additions & 0 deletions protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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<Resource<Boolean>> = 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<List<Account>> {
return try {
val chainProvider = getChainProviderAsync()
Expand Down Expand Up @@ -174,6 +197,10 @@ class Proton private constructor(context: Context) {
}
}

fun hasActiveAccount(): Boolean {
return accountModule.hasActiveAccount()
}

fun setActiveAccount(activeAccount: ActiveAccount): LiveData<Resource<ChainAccount>> = liveData {
emit(Resource.loading())

Expand Down
17 changes: 12 additions & 5 deletions protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21fdd8e

Please sign in to comment.