Skip to content

Commit

Permalink
tonconnect fix
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 25, 2024
1 parent bcd8b77 commit b128f9d
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package com.tonapps.wallet.data.account.entities

import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import com.tonapps.blockchain.ton.TonNetwork
import com.tonapps.blockchain.ton.contract.BaseWalletContract
import com.tonapps.blockchain.ton.contract.WalletFeature
import com.tonapps.blockchain.ton.contract.WalletVersion
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import com.tonapps.blockchain.ton.extensions.toAccountId
import com.tonapps.blockchain.ton.extensions.toRawAddress
import com.tonapps.blockchain.ton.extensions.toWalletAddress
Expand Down Expand Up @@ -108,7 +107,7 @@ data class WalletEntity(

constructor(parcel: Parcel) : this(
id = parcel.readString()!!,
publicKey = parcel.readString()!!.publicKey(),
publicKey = parcel.readString()!!.publicKeyFromHex(),
type = parcel.readEnum(Wallet.Type::class.java)!!,
version = parcel.readEnum(WalletVersion::class.java)!!,
label = parcel.readParcelableCompat()!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.tonapps.wallet.data.dapps.entities

import android.net.Uri
import android.util.Base64
import android.util.Log
import com.tonapps.base64.decodeBase64
import com.tonapps.security.CryptoBox
import com.tonapps.security.Sodium
import com.tonapps.security.hex
Expand Down Expand Up @@ -61,8 +59,14 @@ data class AppConnectEntity(
}

fun decryptEventMessage(message: String): JSONObject {
val bytes = message.decodeBase64()
val decrypted = decryptMessage(bytes)
return JSONObject(decrypted.toString(Charsets.UTF_8))
var string = "..."
try {
val bytes = Base64.decode(message, Base64.NO_WRAP)
val decrypted = decryptMessage(bytes)
string = decrypted.toString(Charsets.UTF_8)
return JSONObject(string)
} catch (e: Throwable) {
throw IllegalArgumentException("Failed to decrypt message: $message;\nstring = $string", e)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tonapps.tonkeeper.core.signer

import android.net.Uri
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import org.ton.api.pub.PublicKeyEd25519

data class SingerArgs(
Expand All @@ -10,7 +10,7 @@ data class SingerArgs(
) {

constructor(uri: Uri): this(
publicKeyEd25519 = uri.getQueryParameter("pk")?.publicKey() ?: throw IllegalArgumentException("Public key is required"),
publicKeyEd25519 = uri.getQueryParameter("pk")?.publicKeyFromHex() ?: throw IllegalArgumentException("Public key is required"),
name = uri.getQueryParameter("name"),
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.tonapps.tonkeeper.deeplink

import android.net.Uri
import android.util.Log
import androidx.core.net.toUri
import com.tonapps.blockchain.ton.extensions.safePublicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import com.tonapps.extensions.hostOrNull
import com.tonapps.extensions.pathOrNull
import com.tonapps.extensions.query
Expand Down Expand Up @@ -128,7 +127,7 @@ sealed class DeepLinkRoute {
): DeepLinkRoute() {

constructor(uri: Uri) : this(
publicKey = uri.query("pk")?.safePublicKey() ?: throw IllegalArgumentException("Public key is required"),
publicKey = uri.query("pk")?.publicKeyFromHex() ?: throw IllegalArgumentException("Public key is required"),
name = uri.query("name"),
local = uri.queryBoolean("local")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.os.Bundle
import android.view.View
import androidx.camera.view.PreviewView
import androidx.lifecycle.lifecycleScope
import com.tonapps.blockchain.ton.extensions.safePublicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import com.tonapps.tonkeeper.helper.BrowserHelper
import com.tonapps.tonkeeper.core.signer.SignerApp
import com.tonapps.tonkeeper.extensions.toast
Expand Down Expand Up @@ -71,7 +71,7 @@ class SignerAddScreen: QRCameraScreen(R.layout.fragment_signer_add), BaseFragmen
readerFlow.map { Uri.parse(it) }.filter { it.host == "signer" }.catch {
navigation?.toast(Localization.unknown_error)
}.onEach { uri ->
val pk = uri.getQueryParameter("pk")?.safePublicKey() ?: return@onEach
val pk = uri.getQueryParameter("pk")?.publicKeyFromHex() ?: return@onEach
val name = uri.getQueryParameter("name") ?: ""
addAccount(pk, name)
}.launchIn(lifecycleScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.tonapps.tonkeeper.ui.screen.external.qr.signer.sign
import android.os.Bundle
import com.tonapps.blockchain.ton.extensions.cellFromHex
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import org.ton.api.pub.PublicKeyEd25519
import org.ton.cell.Cell
import uikit.base.BaseArgs
Expand All @@ -21,7 +21,7 @@ data class SignerSignArgs(
}

constructor(bundle: Bundle) : this(
publicKey = bundle.getString(ARG_PK)!!.publicKey(),
publicKey = bundle.getString(ARG_PK)!!.publicKeyFromHex(),
unsignedBody = bundle.getString(ARG_BODY)!!.cellFromHex(),
label = bundle.getString(ARG_LABEL)!!
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.tonapps.tonkeeper.ui.screen.init

import android.os.Bundle
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromBase64
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import com.tonapps.extensions.getEnum
import com.tonapps.extensions.getParcelableCompat
import com.tonapps.extensions.putEnum
import com.tonapps.ledger.ton.LedgerConnectData
import com.tonapps.tonkeeper.ui.screen.init.list.AccountItem
import com.tonapps.wallet.data.account.Wallet
import com.tonapps.wallet.data.account.entities.WalletEntity
import org.ton.api.pub.PublicKeyEd25519
import uikit.base.BaseArgs
Expand Down Expand Up @@ -51,7 +51,7 @@ data class InitArgs(
constructor(bundle: Bundle) : this(
type = bundle.getEnum(ARG_TYPE, Type.New),
name = bundle.getString(ARG_NAME),
publicKey = bundle.getString(ARG_PUBLIC_KEY)?.publicKey(),
publicKey = bundle.getString(ARG_PUBLIC_KEY)?.publicKeyFromBase64(),
accounts = bundle.getParcelableArrayList<AccountItem>(ARG_ACCOUNTS),
ledgerConnectData = bundle.getParcelableCompat<LedgerConnectData>(ARG_LEDGER_CONNECT_DATA),
keystone = bundle.getParcelableCompat(ARG_KEYSTONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import android.graphics.Color
import android.os.Parcel
import android.os.Parcelable
import androidx.lifecycle.SavedStateHandle
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.safePublicKey
import com.tonapps.blockchain.ton.extensions.publicKeyFromHex
import com.tonapps.extensions.readBooleanCompat
import com.tonapps.extensions.writeBooleanCompat
import com.tonapps.ledger.ton.LedgerConnectData
import com.tonapps.security.hex
import com.tonapps.tonkeeper.ui.screen.init.list.AccountItem
import com.tonapps.wallet.api.entity.AccountDetailsEntity
import com.tonapps.wallet.data.account.Wallet
Expand All @@ -27,7 +24,7 @@ class InitModelState(private val savedStateHandle: SavedStateHandle) {

constructor(parcel: Parcel) : this(
parcel.readBooleanCompat(),
parcel.readString()!!.publicKey()
parcel.readString()!!.publicKeyFromHex()
)

override fun writeToParcel(parcel: Parcel, flags: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package com.tonapps.blockchain.ton.extensions

import android.util.Base64
import com.tonapps.base64.decodeBase64
import com.tonapps.base64.encodeBase64
import org.ton.api.pub.PublicKeyEd25519
import org.ton.crypto.hex

fun String.publicKey(): PublicKeyEd25519 {
return try {
PublicKeyEd25519(hex(this))
} catch (e: Throwable) {
PublicKeyEd25519(decodeBase64())
fun String.publicKeyFromBase64(): PublicKeyEd25519 {
Base64.decode(this, Base64.DEFAULT).let {
return PublicKeyEd25519(it)
}
}

fun String.safePublicKey(): PublicKeyEd25519? {
return try {
publicKey()
} catch (e: Throwable) {
null
}
fun String.publicKeyFromHex(): PublicKeyEd25519 {
return PublicKeyEd25519(hex(this))
}

fun PublicKeyEd25519.base64(): String {
return key.toByteArray().encodeBase64()
return Base64.encodeToString(key.toByteArray(), Base64.DEFAULT)
}

fun PublicKeyEd25519.hex(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ fun ByteArray.toHex(): String = buildString(size * 2) {
append(Character.forDigit(b shr 4, 16))
append(Character.forDigit(b and 0x0F, 16))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,27 @@ package com.tonapps.extensions

import android.content.SharedPreferences
import android.os.Parcelable
import android.util.Base64
import androidx.core.content.edit
import com.tonapps.base64.decodeBase64
import com.tonapps.base64.encodeBase64

fun SharedPreferences.getByteArray(key: String): ByteArray? {
val value = getString(key, null)?.trim() ?: return null
val bytes = value.decodeBase64()
if (bytes.isEmpty()) {
return null
}
return bytes
}

/*
fun SharedPreferences.getByteArray(key: String): ByteArray? {
val value = run {
val value = getString(key, null)
if (value.isNullOrBlank()) {
return byteArrayOf(0)
return null
}
base64(value) ?: byteArrayOf(0)
}
if (value.isZero()) {
value.clear()
return null
Base64.decode(value, Base64.DEFAULT)
}
return value
}

fun SharedPreferences.Editor.putByteArray(key: String, value: ByteArray): SharedPreferences.Editor {
if (value.isZero()) {
fun SharedPreferences.Editor.putByteArray(key: String, value: ByteArray?) = apply {
val string = Base64.encodeToString(value, Base64.DEFAULT)
if (string == null) {
remove(key)
return this
} else {
val string = base64(value)
if (string == null) {
remove(key)
} else {
putString(key, string)
}
putString(key, string)
}
return this
}
*/

fun SharedPreferences.putByteArray(key: String, value: ByteArray?) {
if (value == null) {
remove(key)
} else {
edit {
putByteArray(key, value)
}
}
}

fun SharedPreferences.Editor.putByteArray(key: String, value: ByteArray?): SharedPreferences.Editor {
if (value == null) {
remove(key)
} else {
putString(key, value.encodeBase64())
}
return this
}

fun SharedPreferences.getIntArray(key: String): IntArray? {
Expand Down Expand Up @@ -136,7 +93,6 @@ fun SharedPreferences.getStringArray(key: String): Array<String> {
return string.split(",").toTypedArray()
}


fun SharedPreferences.putStringArray(key: String, value: Array<String>) {
putString(key, value.joinToString(","))
}
Expand Down

0 comments on commit b128f9d

Please sign in to comment.