From 62b3d0d4a40c262bc7ff3680d93713015a84a8cb Mon Sep 17 00:00:00 2001 From: Andrew Watson Date: Tue, 17 Oct 2023 13:50:51 -0700 Subject: [PATCH] Add Blockchain object support --- .../clientlib/MobileWalletAdapter.kt | 29 +++++++++++++++++-- .../clientlib/TransactionParams.kt | 20 +++++++++++++ .../viewmodel/SampleViewModel.kt | 1 + 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/MobileWalletAdapter.kt b/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/MobileWalletAdapter.kt index cb5661c4d..0baa77bae 100644 --- a/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/MobileWalletAdapter.kt +++ b/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/MobileWalletAdapter.kt @@ -32,7 +32,7 @@ class MobileWalletAdapter( * Specify the RPC cluster used for all operations. Note: changing at runtime will invalidate * the auth token and reauthorization will be required */ - var rpcCluster: RpcCluster = RpcCluster.Devnet + var blockchain: Blockchain = Solana.Devnet set(value) { if (value != field) { authToken = null @@ -41,6 +41,29 @@ class MobileWalletAdapter( field = value } + @Deprecated( + "RpcCluster provides only Solana clusters; use the Blockchain object for full multi-chain support.", + replaceWith = ReplaceWith("Set `blockchain` property moving forward."), + DeprecationLevel.WARNING + ) + var rpcCluster: RpcCluster = RpcCluster.Devnet + set(value) { + when (value) { + RpcCluster.MainnetBeta -> { + blockchain = Solana.Mainnet + } + RpcCluster.Devnet -> { + blockchain = Solana.Devnet + } + RpcCluster.Testnet -> { + blockchain = Solana.Testnet + } + else -> { } + } + + field = value + } + init { connectionIdentity?.let { identityState = IdentityState.Provided(it) @@ -102,12 +125,12 @@ class MobileWalletAdapter( with (id.appIdentity) { if (protocolVersion == SessionProperties.ProtocolVersion.V1) { //TODO: Full MWA 2.0 support has feature & multi-address params. Will be implemented in a future minor release. - adapterOperations.authorize(identityUri, iconUri, identityName, "SOMECHAIN", authToken, null, null) + adapterOperations.authorize(identityUri, iconUri, identityName, blockchain.fullName, authToken, null, null) } else { authToken?.let { token -> adapterOperations.reauthorize(identityUri, iconUri, identityName, token) } ?: run { - adapterOperations.authorize(identityUri, iconUri, identityName, rpcCluster) + adapterOperations.authorize(identityUri, iconUri, identityName, RpcCluster.Custom(blockchain.cluster)) }.also { authToken = it.authToken } diff --git a/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/TransactionParams.kt b/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/TransactionParams.kt index 08f31b0af..2cc1e4210 100644 --- a/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/TransactionParams.kt +++ b/android/clientlib-ktx/src/main/java/com/solana/mobilewalletadapter/clientlib/TransactionParams.kt @@ -2,10 +2,30 @@ package com.solana.mobilewalletadapter.clientlib import com.solana.mobilewalletadapter.common.ProtocolContract +@Deprecated( + "RpcCluster is deprecated as of MWA 2.0", + replaceWith = ReplaceWith("Use the Blockchain object for multi-chain support"), + DeprecationLevel.WARNING +) sealed class RpcCluster(val name: String) { object MainnetBeta : RpcCluster(ProtocolContract.CLUSTER_MAINNET_BETA) object Testnet : RpcCluster(ProtocolContract.CLUSTER_TESTNET) object Devnet : RpcCluster(ProtocolContract.CLUSTER_DEVNET) + class Custom(name: String) : RpcCluster(name) +} + +sealed class Blockchain( + val name: String, + val cluster: String +) { + val fullName + get() = "$name:$cluster" +} + +sealed class Solana { + object Mainnet: Blockchain("solana", ProtocolContract.CLUSTER_MAINNET_BETA) + object Testnet: Blockchain("solana", ProtocolContract.CLUSTER_TESTNET) + object Devnet: Blockchain("solana", ProtocolContract.CLUSTER_DEVNET) } open class TransactionParams( diff --git a/examples/example-clientlib-ktx-app/app/src/main/java/com/solanamobile/ktxclientsample/viewmodel/SampleViewModel.kt b/examples/example-clientlib-ktx-app/app/src/main/java/com/solanamobile/ktxclientsample/viewmodel/SampleViewModel.kt index 7b4395dd7..124dc7d00 100644 --- a/examples/example-clientlib-ktx-app/app/src/main/java/com/solanamobile/ktxclientsample/viewmodel/SampleViewModel.kt +++ b/examples/example-clientlib-ktx-app/app/src/main/java/com/solanamobile/ktxclientsample/viewmodel/SampleViewModel.kt @@ -7,6 +7,7 @@ import com.solana.core.SerializeConfig import com.solana.core.Transaction import com.solana.mobilewalletadapter.clientlib.ActivityResultSender import com.solana.mobilewalletadapter.clientlib.MobileWalletAdapter +import com.solana.mobilewalletadapter.clientlib.RpcCluster import com.solana.mobilewalletadapter.clientlib.TransactionResult import com.solana.programs.MemoProgram import com.solanamobile.ktxclientsample.usecase.Connected