Skip to content

Commit

Permalink
wip-vote
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoukoul committed Mar 21, 2024
1 parent c9c9794 commit 70458a1
Show file tree
Hide file tree
Showing 45 changed files with 485 additions and 197 deletions.
3 changes: 2 additions & 1 deletion .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions app/src/main/java/co/anode/anodium/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class MainActivity : AppCompatActivity() {
searchForInternetSharing()
}

override fun onResume() {
super.onResume()
Timber.i("MainActivity onResume starting PktMainActivity")
startActivity(Intent(this, PktMainActivity::class.java))
}

private fun initializeApp() {
//Initialize Util before Client
AnodeUtil.init(applicationContext)
Expand All @@ -83,8 +89,7 @@ class MainActivity : AppCompatActivity() {
initTimber()
AnodeUtil.initializeApp()
AnodeUtil.launchCJDNS()
//PLD will be launched after creating a wallet
// AnodeUtil.launchPld()
AnodeUtil.launchPld("")
AnodeUtil.serviceThreads()

val prefs = getSharedPreferences(BuildConfig.APPLICATION_ID, MODE_PRIVATE)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/co/anode/anodium/PktMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -41,6 +42,7 @@ class PktMainActivity : AppCompatActivity() {
checkPermissions()

startNotificationsCheck()
Timber.i("PktMainActivity onCreate")
}

private fun startNotificationsCheck() {
Expand All @@ -59,6 +61,7 @@ class PktMainActivity : AppCompatActivity() {
}
override fun onResume() {
super.onResume()
Timber.i("PktMainActivity onResume")
startNotificationsCheck()
}
override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,38 @@ class GeneralRepositoryImpl @Inject constructor() : GeneralRepository {
}

override suspend fun createPldWallet(password: String, pin: String, wallet: String): Result<String> {
runCatching {
val seed = AnodeUtil.createPldWallet(password, wallet)
if (seed.isEmpty()) {
return Result.failure(Exception("Wallet creation failed"))
}
val encryptedPassword = AnodeUtil.encrypt(password, pin)
AnodeUtil.storeWalletPassword(encryptedPassword, wallet)
if (pin.isNotEmpty()) {
AnodeUtil.storeWalletPin(pin, wallet)
}

Timber.d("createWallet: success")
return Result.success(seed)
val seed = AnodeUtil.createPldWallet(password, wallet)
if (seed.isEmpty()) {
return Result.failure(Exception("Wallet creation failed"))
}
return Result.failure(Exception("Wallet creation failed"))
val encryptedPassword = AnodeUtil.encrypt(password, pin)
AnodeUtil.storeWalletPassword(encryptedPassword, wallet)
if (pin.isNotEmpty()) {
AnodeUtil.storeWalletPin(pin, wallet)
}

Timber.d("createWallet: success")
return Result.success(seed)
}

override fun launchPLD() {
AnodeUtil.launchPld("")
override suspend fun recoverPldWallet(password: String, seed: String, seedPassword:String, pin: String, wallet: String): Result<String> {
if (seed.isEmpty()) {
return Result.failure(Exception("Seed is empty"))
}
val returnedSeed = AnodeUtil.recoverPldWallet(password, wallet, seed, seedPassword)
if (returnedSeed.isEmpty()) {
return Result.failure(Exception("Wallet creation failed"))
}
val encryptedPassword = AnodeUtil.encrypt(password, pin)
AnodeUtil.storeWalletPassword(encryptedPassword, wallet)
if (pin.isNotEmpty()) {
AnodeUtil.storeWalletPin(pin, wallet)
}

Timber.d("createWallet: success")
return Result.success(seed)
}
override fun launchPLD(wallet: String) {
AnodeUtil.launchPld(wallet)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
private var activeWallet = AnodeUtil.DEFAULT_WALLET_NAME

override fun getAllWalletNames(): List<String> {
return AnodeUtil.getWalletFiles()
val wallets = AnodeUtil.getWalletFiles()
return wallets.map { it.removePrefix("wallet_") }
}

override fun getActiveWallet(): String {
AnodeUtil.context?.getSharedPreferences(AnodeUtil.ApplicationID, Context.MODE_PRIVATE)?.getString("activeWallet", "wallet")?.let {
activeWallet = it
}
Timber.i("getActiveWallet: $activeWallet")
//Check if wallet file exists, otherwise choose other available wallet
if (!AnodeUtil.getWalletFiles().contains(activeWallet)) {
if ((!AnodeUtil.getWalletFiles().contains(activeWallet)) && (!AnodeUtil.getWalletFiles().contains("wallet_$activeWallet"))) {
Timber.e("Active wallet file not found, choosing another wallet")
activeWallet = AnodeUtil.getWalletFiles().firstOrNull() ?: AnodeUtil.DEFAULT_WALLET_NAME
}
return activeWallet
}

override suspend fun setActiveWallet(walletName: String) {
Timber.i("setActiveWallet: $walletName")
//delete existing chain back up file when changing active wallet
if (walletName != activeWallet) {
AnodeUtil.deleteWalletChainBackupFile()
Expand Down Expand Up @@ -78,11 +82,15 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
return Result.success(balance)
}

override suspend fun getVote(address: String): Result<Vote> {
override suspend fun getVote(address: String): Result<Vote?> {
val addresses = walletAPI.getWalletBalances(true)
for (addr in addresses.addrs) {
if (addr.address == address) {
return Result.success(addr.vote)
return if (addr.vote != null) {
Result.success(addr.vote!!)
} else {
Result.success(null)
}
}
}
return Result.failure(Exception("Address not found"))
Expand Down Expand Up @@ -149,9 +157,9 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
}
}
address
}.onSuccess { address ->
}.onSuccess {
Timber.d("getWalletAddress: success")
address
return Result.success(it)
}.onFailure {
Timber.e(it, "getWalletAddress: failure")
}
Expand Down Expand Up @@ -208,10 +216,25 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
}

override suspend fun unlockWallet(passphrase: String): Result<Boolean> {
Timber.d("unlockWallet")
// For new pld, we need to restart it
// AnodeUtil.stopPld()
// AnodeUtil.launchPld(activeWallet)
val request = UnlockWalletRequest(passphrase)
val response = walletAPI.unlockWalletAPI(request)
return Result.success(response)
Timber.d("unlockWallet")
var attempts = 0
var response: Boolean
do {
response = walletAPI.unlockWalletAPI(request)
attempts++
} while (!response && attempts < 3)

return if (response) {
Timber.d("unlockWallet: success")
Result.success(true)
} else {
Timber.e("unlockWallet: failed")
Result.failure(Exception("Failed to unlock wallet"))
}
}

override suspend fun unlockWalletWithPIN(pin: String): Result<Boolean> {
Expand Down Expand Up @@ -260,17 +283,32 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
}

override suspend fun renameWallet(name: String, srcName: String): Result<String?> {
Timber.d("renameWallet: $name")
checkWalletName(name).onSuccess {
var dstName = name
if (name != AnodeUtil.DEFAULT_WALLET_NAME) {
dstName = "wallet_$name"
}
Timber.d("renameWallet: $dstName")
checkWalletName(dstName).onSuccess {
if (srcName.isNotEmpty()) {
activeWallet = srcName

}
val walletFile = File("${AnodeUtil.filesDirectory}/pkt/$activeWallet.db")
walletFile.renameTo(File("${AnodeUtil.filesDirectory}/pkt/$name.db"))

if ((activeWallet != AnodeUtil.DEFAULT_WALLET_NAME) && (!activeWallet.startsWith("wallet_"))) {
Timber.i("renameWallet: add wallet_ prefix to activeWallet name")
val walletFile = File("${AnodeUtil.filesDirectory}/pkt/wallet_$activeWallet.db")
walletFile.renameTo(File("${AnodeUtil.filesDirectory}/pkt/$dstName.db"))
Timber.i("renameWallet: renaming wallet wallet_$activeWallet to $dstName")
} else {
Timber.i("renameWallet: renaming wallet $activeWallet to $dstName")
val walletFile = File("${AnodeUtil.filesDirectory}/pkt/$activeWallet.db")
walletFile.renameTo(File("${AnodeUtil.filesDirectory}/pkt/$dstName.db"))
}

//update stored PIN
AnodeUtil.renameEncryptedWalletPreferences(activeWallet,name)
setActiveWallet(name)
return Result.success(name)
AnodeUtil.renameEncryptedWalletPreferences(activeWallet,dstName)
setActiveWallet(dstName)
return Result.success(dstName)
}.onFailure {
Timber.e(it, "renameWallet: failed")
return Result.failure(Exception("A wallet already exists with this name"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.pkt.core.presentation.start.StartFragment
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class StartWalletActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(
android.R.id.content,
StartFragment().apply{}
)
.commit()
}
Timber.i("StartWalletActivity onCreate")
// if (savedInstanceState == null) {
// }
supportFragmentManager.beginTransaction()
.replace(
android.R.id.content,
StartFragment().apply{}
)
.commit()
}


companion object {
fun getIntent(context: Context) = Intent(context, StartFragment::class.java).apply {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ActivityNavigationHandler @Inject constructor() : AppNavigationHandler() {
fragment.startActivity(walletActivity)
}

override fun openSendConfirm(fragment: Fragment, fromaddress: String,toaddress: String, amount: Double, maxAmount: Boolean) {
override fun openSendConfirm(fragment: Fragment, fromaddress: String,toaddress: String, amount: Double, maxAmount: Boolean, isVote: Boolean, isVoteCandidate: Boolean) {
TODO("Not yet implemented")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FragmentNavigationHandler @Inject constructor() : AppNavigationHandler() {
}

override fun openConfirmTransactionVPNPremium(fragment: Fragment, fromAddress: String, toAddress: String, amount: Double) {
navigate(fragment, NavGraphDirections.toSendConfirm(fromAddress, toAddress, amount.toFloat(), false, true))
navigate(fragment, NavGraphDirections.toSendConfirm(fromAddress, toAddress, amount.toFloat(), false, true, false, false))
}

override fun openEnterWallet(fragment: Fragment) {
Expand All @@ -59,8 +59,8 @@ class FragmentNavigationHandler @Inject constructor() : AppNavigationHandler() {
navigate(fragment, NavGraphDirections.toStart())
}

override fun openSendConfirm(fragment: Fragment, fromAddress:String, toAddress: String, amount: Double, maxAmount: Boolean) {
navigate(fragment, NavGraphDirections.toSendConfirm(fromAddress, toAddress, amount.toFloat(), maxAmount, false))
override fun openSendConfirm(fragment: Fragment, fromAddress:String, toAddress: String, amount: Double, maxAmount: Boolean, isVote: Boolean,isVoteCandidate: Boolean) {
navigate(fragment, NavGraphDirections.toSendConfirm(fromAddress, toAddress, amount.toFloat(), maxAmount, false, isVote, isVoteCandidate))
}

override fun openSendSuccess(fragment: Fragment, transactionId: String, premiumVpn: Boolean, address: String) {
Expand Down
Loading

0 comments on commit 70458a1

Please sign in to comment.