Skip to content

Commit

Permalink
Merge pull request #933 from tari-project/master
Browse files Browse the repository at this point in the history
feat: master -> dev
  • Loading branch information
alexandrVakhtinTari committed Jul 17, 2023
2 parents b1520dd + 2cea84a commit 592c7b6
Show file tree
Hide file tree
Showing 69 changed files with 928 additions and 300 deletions.
Binary file added app/regular/release/app-regular-release.aab
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,41 @@ class DeepLinkTest {

private val networkRepository: NetworkRepository = NetworkRepositoryMock()
private val deeplinkHandler: DeeplinkHandler = DeeplinkHandler(networkRepository)
private val currentNetwork = Network.NEXTNET
private val currentNetwork = Network.STAGENET

@Test
fun assertNetwork() {
val nullLink = "tari://mainnet/${DeepLink.Send.sendCommand}?${DeepLink.Send.publicKeyKey}=$PUBLIC_KEY"
val nullResult = deeplinkHandler.handle(nullLink) as? DeepLink.Send
val nullLink = "tari://mainnet/${DeepLink.Send.sendCommand}?${DeepLink.Send.tariAddressKey}=$PUBLIC_KEY"
val nullResult = deeplinkHandler.handle(nullLink) as? DeepLink.ContactlessPayment
assertNull(nullResult)

val notNullLink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.publicKeyKey}=$PUBLIC_KEY"
val notNullResult = deeplinkHandler.handle(notNullLink) as? DeepLink.Send
val notNullLink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.tariAddressKey}=$PUBLIC_KEY"
val notNullResult = deeplinkHandler.handle(notNullLink) as? DeepLink.ContactlessPayment
assertNotNull(notNullResult)
}

@Test
fun assertNode() {
val deeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.noteKey}=hey"
val result = deeplinkHandler.handle(deeplink) as? DeepLink.Send
val result = deeplinkHandler.handle(deeplink) as? DeepLink.ContactlessPayment
assertEquals(result!!.note, "hey")

val cyrillicDeeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.noteKey}=привет"
val cyrillicResult = deeplinkHandler.handle(cyrillicDeeplink) as? DeepLink.Send
val cyrillicResult = deeplinkHandler.handle(cyrillicDeeplink) as? DeepLink.ContactlessPayment
assertEquals(cyrillicResult!!.note, "привет")
}

@Test
fun assertPubkey() {
val deeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.publicKeyKey}=$PUBLIC_KEY"
val result = deeplinkHandler.handle(deeplink) as? DeepLink.Send
val deeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.tariAddressKey}=$PUBLIC_KEY"
val result = deeplinkHandler.handle(deeplink) as? DeepLink.ContactlessPayment
assertEquals(result!!.walletAddress, PUBLIC_KEY)
}

@Test
fun assertAmount() {
val deeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.amountKey}=12345678"
val result = deeplinkHandler.handle(deeplink) as? DeepLink.Send
val result = deeplinkHandler.handle(deeplink) as? DeepLink.ContactlessPayment
assertEquals(result!!.amount!!.tariValue.toDouble(), 12.345678, 0.1)
}

Expand All @@ -99,8 +99,8 @@ class DeepLinkTest {

@Test
fun assertFullDataDeeplinks() {
val sendDeeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.amountKey}=12345678&${DeepLink.Send.noteKey}=hey&${DeepLink.Send.publicKeyKey}=$PUBLIC_KEY"
val result = deeplinkHandler.handle(sendDeeplink) as? DeepLink.Send
val sendDeeplink = "tari://${currentNetwork.uriComponent}/${DeepLink.Send.sendCommand}?${DeepLink.Send.amountKey}=12345678&${DeepLink.Send.noteKey}=hey&${DeepLink.Send.tariAddressKey}=$PUBLIC_KEY"
val result = deeplinkHandler.handle(sendDeeplink) as? DeepLink.ContactlessPayment
assertEquals(result!!.note, "hey")
assertEquals(result.walletAddress, PUBLIC_KEY)
assertEquals(result.amount!!.tariValue.toDouble(), 12.345678, 0.1)
Expand All @@ -119,7 +119,7 @@ class DeepLinkTest {
}

class NetworkRepositoryMock : NetworkRepository {
private val network: Network = Network.NEXTNET
private val network: Network = Network.STAGENET

override var supportedNetworks: List<Network> = listOf(network)
override var currentNetwork: TariNetwork? = TariNetwork(network, "")
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@
android:host="nextnet"
android:scheme="tari" />

<data
android:host="stagenet"
android:scheme="tari" />

<data
android:host="esmeralda"
android:scheme="tari" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ enum class Network(val uriComponent: String, val displayName: String) {

ESMERALDA("esmeralda", "ESMERALDA"),

NEXTNET("nextnet", "NextNet");
NEXTNET("nextnet", "NextNet"),

STAGENET("stagenet", "StageNet");

companion object {
fun from(uriComponent: String): Network {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class WalletManager(
// stop tor proxy
EventBus.torProxyState.unsubscribe(this)
torManager.shutdown()
EventBus.walletState.post(WalletState.NotReady)
}

@SuppressLint("CheckResult")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ class BaseNodes(

@Suppress("UNUSED_EXPRESSION")
private fun getBaseNodeResource(network: Network): Int = when(network) {
else -> R.raw.nextnet_base_nodes
else -> R.raw.stagenet_base_nodes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ sealed class DeepLink {
}
}

class Send(val walletAddress: String = "", val amount: MicroTari? = null, val note: String = "") : DeepLink() {
class Send(val walletAddressHex: String = "", val amount: MicroTari? = null, val note: String = "") : DeepLink() {

constructor(params: Map<String, String>) : this(
params[publicKeyKey].orEmpty(),
params[tariAddressKey].orEmpty(),
params[amountKey]?.let { if (it.isEmpty()) null else MicroTari(BigInteger(it)) },
params[noteKey].orEmpty()
)

override fun getParams(): Map<String, String> = hashMapOf<String, String>().apply {
put(publicKeyKey, walletAddress)
put(tariAddressKey, walletAddressHex)
put(amountKey, amount?.formattedValue.orEmpty())
put(noteKey, note)
}
Expand All @@ -111,13 +111,34 @@ sealed class DeepLink {

companion object {
const val sendCommand = "transactions/send"
const val publicKeyKey = "publicKey"
const val walletAddressKey = "walletAddressKey"
const val tariAddressKey = "tariAddress"
const val amountKey = "amount"
const val noteKey = "note"
}
}


class UserProfile(val tariAddressHex: String = "", val alias: String = "") : DeepLink() {

constructor(params: Map<String, String>) : this(
params[walletAddressKey].orEmpty(),
params[aliasKey].orEmpty()
)

override fun getParams(): Map<String, String> = hashMapOf<String, String>().apply {
put(walletAddressKey, tariAddressHex)
put(aliasKey, alias)
}

override fun getCommand(): String = profileCommand

companion object {
const val profileCommand = "profile"
const val walletAddressKey = "tariAddress"
const val aliasKey = "alias"
}
}

class AddBaseNode(val name: String = "", val peer: String = "") : DeepLink() {

constructor(params: Map<String, String>) : this(
Expand Down Expand Up @@ -145,6 +166,7 @@ sealed class DeepLink {
Contacts.contactsCommand -> Contacts(params)
Send.sendCommand -> Send(params)
AddBaseNode.addNodeCommand -> AddBaseNode(params)
UserProfile.profileCommand -> UserProfile(params)
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DeeplinkFormatter(private val networkRepository: NetworkRepository) {

fun toDeeplink(deepLink: DeepLink): String {
val fullPart = Uri.Builder()
.scheme("tari")
.scheme(scheme)
.authority(networkRepository.currentNetwork!!.network.uriComponent)
.appendPath(deepLink.getCommand())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.tari.android.wallet.ui.dialog.modular.modules.head.HeadModule
import com.tari.android.wallet.ui.fragment.contact_book.data.ContactsRepository
import com.tari.android.wallet.ui.fragment.contact_book.data.contacts.ContactDto
import com.tari.android.wallet.ui.fragment.contact_book.data.contacts.FFIContactDto
import com.tari.android.wallet.ui.fragment.home.navigation.Navigation
import javax.inject.Inject

class DeeplinkViewModel : CommonViewModel() {
Expand All @@ -33,11 +34,27 @@ class DeeplinkViewModel : CommonViewModel() {
@Inject
lateinit var contactRepository: ContactsRepository

@Inject
lateinit var deeplinkHandler: DeeplinkHandler

init {
component.inject(this)
}

fun executeAction(context: Context, deeplink: DeepLink.AddBaseNode) {
fun tryToHandle(context:Context, qrData: String) {
deeplinkHandler.handle(qrData)?.let { execute(context, it) }
}

fun execute(context: Context, deeplink: DeepLink) {
when (deeplink) {
is DeepLink.AddBaseNode -> addBaseNode(context, deeplink)
is DeepLink.Contacts -> addContacts(deeplink.contacts)
is DeepLink.Send -> send(deeplink)
is DeepLink.UserProfile -> addContacts(listOf(DeepLink.Contacts.DeeplinkContact(deeplink.alias, deeplink.tariAddressHex)))
}
}

fun addBaseNode(context: Context, deeplink: DeepLink.AddBaseNode) {
val baseNode = BaseNodeDto.fromDeeplink(deeplink)
val args = ConfirmDialogArgs(
resourceManager.getString(R.string.home_custom_base_node_title),
Expand All @@ -62,17 +79,27 @@ class DeeplinkViewModel : CommonViewModel() {
val args = ModularDialogArgs(
DialogArgs(), listOf(
HeadModule(resourceManager.getString(R.string.contact_deeplink_title)),
BodyModule(resourceManager.getString(R.string.contact_deeplink_message, contactDtos.size.toString()) + ". " + names),
BodyModule(resourceManager.getString(R.string.contact_deeplink_message, contactDtos.size.toString()) + ". " + names),
ButtonModule(resourceManager.getString(R.string.common_confirm), ButtonStyle.Normal) {
contactDtos.forEach { contactRepository.addContact(it) }
_dismissDialog.postValue(Unit)
dismissDialog.postValue(Unit)
},
ButtonModule(resourceManager.getString(R.string.common_cancel), ButtonStyle.Close)
)
)
modularDialog.postValue(args)
}

fun send(deeplink: DeepLink.Send) {
val contactDto = runCatching {
val ffiWalletAddress = FFITariWalletAddress(HexString(deeplink.walletAddressHex))
val tariWalletAddress = TariWalletAddress(ffiWalletAddress.toString(), ffiWalletAddress.getEmojiId())
ContactDto(FFIContactDto(tariWalletAddress, ""))
}.getOrNull() ?: return

navigation.postValue(Navigation.TxListNavigation.ToSendTariToUser(contactDto))
}

private fun addBaseNode(baseNodeDto: BaseNodeDto) {
baseNodeRepository.addUserBaseNode(baseNodeDto)
baseNodes.setBaseNode(baseNodeDto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class StagedWalletSecurityManager : CommonViewModel() {
}

private fun openStage1() {
_dismissDialog.postValue(Unit)
dismissDialog.postValue(Unit)
tariNavigator?.let {
it.toAllSettings()
it.toBackupSettings(false)
Expand All @@ -139,7 +139,7 @@ class StagedWalletSecurityManager : CommonViewModel() {
}

private fun openStage1B() {
_dismissDialog.postValue(Unit)
dismissDialog.postValue(Unit)
tariNavigator?.let {
it.toAllSettings()
it.toBackupSettings(true)
Expand All @@ -157,7 +157,7 @@ class StagedWalletSecurityManager : CommonViewModel() {
}

private fun openStage2() {
_dismissDialog.postValue(Unit)
dismissDialog.postValue(Unit)
tariNavigator?.let {
it.toAllSettings()
it.toBackupSettings(false)
Expand All @@ -176,7 +176,7 @@ class StagedWalletSecurityManager : CommonViewModel() {
}

private fun openStage3() {
_dismissDialog.postValue(Unit)
dismissDialog.postValue(Unit)
//todo for future
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import com.tari.android.wallet.data.sharedPrefs.delegates.SharedPrefGsonDelegate

class NetworkRepositoryImpl(sharedPrefs: SharedPreferences) : NetworkRepository {

override var supportedNetworks: List<Network> = listOf(Network.NEXTNET)
override var supportedNetworks: List<Network> = listOf(Network.STAGENET)

override var recommendedNetworks: List<Network> = listOf(Network.NEXTNET)
override var recommendedNetworks: List<Network> = listOf(Network.STAGENET)

override var currentNetwork by SharedPrefGsonDelegate(sharedPrefs, SimpleRepository(this), Keys.currentNetwork, TariNetwork::class.java)

init {
try {
currentNetwork!!.network.displayName
} catch (e: Throwable) {
currentNetwork = getNextnet()
currentNetwork = getStagenet()
}
}

override var ffiNetwork: Network? by SharedPrefGsonDelegate(sharedPrefs, SimpleRepository(this), formatKey(Keys.ffiNetwork), Network::class.java)

override var incompatibleNetworkShown by SharedPrefBooleanDelegate(sharedPrefs, SimpleRepository(this), formatKey(Keys.networkIncompatible), false)

override fun getAllNetworks(): List<TariNetwork> = listOf(getNextnet())
override fun getAllNetworks(): List<TariNetwork> = listOf(getStagenet())

object Keys {
const val currentNetwork = "tari_current_network"
Expand All @@ -38,6 +38,6 @@ class NetworkRepositoryImpl(sharedPrefs: SharedPreferences) : NetworkRepository
private const val mainNetThicker = "XTR"
private const val testNetThicker = "tXTR"

fun getNextnet(): TariNetwork = TariNetwork(Network.NEXTNET, testNetThicker)
fun getStagenet(): TariNetwork = TariNetwork(Network.STAGENET, testNetThicker)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BackupManager @Inject constructor(
private val logger
get() = Logger.t(BackupManager::class.simpleName)

var currentOption: BackupOptions? = null
var currentOption: BackupOptions? = BackupOptions.Dropbox

private val coroutineContext = Job()
private var localScope = CoroutineScope(coroutineContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ abstract class TariBluetoothAdapter() : CommonViewModel() {
field = value
onContextSet()
}
protected var bluetoothAdapter: BluetoothAdapter? = null
protected var bluetoothManager: BluetoothManager? = null
protected val bluetoothAdapter: BluetoothAdapter?
get() = bluetoothManager?.adapter
protected val bluetoothManager: BluetoothManager?
get() = fragappCompatActivity!!.getSystemService(BluetoothManager::class.java)

open fun onContextSet() { }

Expand Down Expand Up @@ -58,9 +60,6 @@ abstract class TariBluetoothAdapter() : CommonViewModel() {


fun init(fragment: AppCompatActivity) {
bluetoothManager = fragment.getSystemService(BluetoothManager::class.java)
bluetoothAdapter = bluetoothManager!!.adapter

this.fragappCompatActivity = fragment
}

Expand All @@ -74,15 +73,15 @@ abstract class TariBluetoothAdapter() : CommonViewModel() {
}
}

protected fun runWithPermissions(permission: String, action: () -> Unit) {
protected fun runWithPermissions(permission: String, silently: Boolean = false, action: () -> Unit) {
try {
if (ActivityCompat.checkSelfPermission(fragappCompatActivity!!, permission) != PackageManager.PERMISSION_GRANTED) {
doOnRequiredPermissions.invoke(bluetoothPermissions, action)
if (!silently) doOnRequiredPermissions.invoke(listOf(permission), action)
} else {
action()
}
} catch (e: SecurityException) {
doOnRequiredPermissions.invoke(bluetoothPermissions, action)
if (!silently) doOnRequiredPermissions.invoke(listOf(permission), action)
}
}

Expand All @@ -107,5 +106,8 @@ abstract class TariBluetoothAdapter() : CommonViewModel() {

const val SERVICE_UUID = "0DABCA14-0688-458D-89D3-367A3D969537"
const val CHARACTERISTIC_UUID = "999CB541-8D4C-4075-BFF3-43AB74DE8C9B"
const val TRANSACTION_DATA_UUID = "4567F76F-2577-4EA4-9220-AFCCCAA89B59"

const val chunkSize = 150
}
}
Loading

0 comments on commit 592c7b6

Please sign in to comment.