From ca9ac5a97ccc366b8714575f175858167555237b Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 18 Jan 2024 20:20:49 -0800 Subject: [PATCH 1/3] Drop quotes from CoinbaseWallet return values.. Apply Spotless --- .gitignore | 1 + .../carteraexample/ExampleInstrumentedTest.kt | 8 +- .../CarteraSampleApplication.kt | 4 +- .../dydx/carteraExample/MainActivity.kt | 5 +- .../carteraExample/ModalTransitionDialog.kt | 12 +- .../dydx/carteraExample/WalletList.kt | 68 ++++----- .../carteraExample/WalletListViewModel.kt | 27 ++-- .../WalletProvidersConfigUtil.kt | 8 +- .../dydx/carteraexample/ExampleUnitTest.kt | 5 +- build.gradle | 42 +++++- .../dydx/cartera/ExampleInstrumentedTest.kt | 8 +- .../exchange/dydx/cartera/CarteraConfig.kt | 54 ++++--- .../exchange/dydx/cartera/CarteraErrorCode.kt | 2 +- .../exchange/dydx/cartera/CarteraProvider.kt | 10 +- .../main/java/exchange/dydx/cartera/Utils.kt | 3 +- .../dydx/cartera/entities/ModelExtensions.kt | 14 +- .../dydx/cartera/entities/WalletModels.kt | 86 +++++------ .../EIP712DomainTypedDataProvider.kt | 2 +- .../dydx/cartera/typeddata/EIP712TypedData.kt | 2 +- .../dydx/cartera/typeddata/WalletTypedData.kt | 2 +- .../WalletTypedDataProviderProtocol.kt | 1 - .../cartera/walletprovider/WalletConsent.kt | 2 +- .../cartera/walletprovider/WalletError.kt | 9 +- .../walletprovider/WalletProviderProtocols.kt | 2 +- .../cartera/walletprovider/WalletStatus.kt | 2 +- .../providers/MagicLinkProvider.kt | 4 +- .../providers/WalletConnectUtils.kt | 2 +- .../providers/WalletConnectV1Provider.kt | 4 +- .../providers/WalletConnectV2Provider.kt | 142 +++++++++--------- .../providers/WalletSegueProvider.kt | 37 +++-- .../userconsent/SkippedWalletUserConsent.kt | 2 +- .../exchange/dydx/cartera/ExampleUnitTest.kt | 5 +- gradle.properties | 2 +- 33 files changed, 304 insertions(+), 273 deletions(-) diff --git a/.gitignore b/.gitignore index 4c027f7..275a7b0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .gradle .idea github.properties +build diff --git a/app/src/androidTest/java/exchange/dydx/carteraexample/ExampleInstrumentedTest.kt b/app/src/androidTest/java/exchange/dydx/carteraexample/ExampleInstrumentedTest.kt index 22c1842..6207a48 100644 --- a/app/src/androidTest/java/exchange/dydx/carteraexample/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/exchange/dydx/carteraexample/ExampleInstrumentedTest.kt @@ -1,13 +1,11 @@ package exchange.dydx.carteraexample -import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * @@ -21,4 +19,4 @@ class ExampleInstrumentedTest { val appContext = InstrumentationRegistry.getInstrumentation().targetContext assertEquals("exchange.dydx.carteraexample", appContext.packageName) } -} \ No newline at end of file +} diff --git a/app/src/main/java/exchange/dydx/carteraExample/CarteraSampleApplication.kt b/app/src/main/java/exchange/dydx/carteraExample/CarteraSampleApplication.kt index 42885a9..aef413d 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/CarteraSampleApplication.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/CarteraSampleApplication.kt @@ -2,8 +2,8 @@ package exchange.dydx.carteraexample import android.app.Application -class CarteraSampleApplication: Application() { +class CarteraSampleApplication : Application() { override fun onCreate() { super.onCreate() } -} \ No newline at end of file +} diff --git a/app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt b/app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt index 1bf5e2d..567260e 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt @@ -10,7 +10,6 @@ import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import exchange.dydx.cartera.CarteraConfig - class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -24,7 +23,7 @@ class MainActivity : ComponentActivity() { CarteraConfig.shared = CarteraConfig( walletProvidersConfig = WalletProvidersConfigUtil.getWalletProvidersConfig(), application = applicationContext as Application, - launcher = launcher + launcher = launcher, ) CarteraConfig.shared?.registerWallets(context = applicationContext) @@ -44,5 +43,3 @@ fun MyApp(content: @Composable () -> Unit) { } } } - - diff --git a/app/src/main/java/exchange/dydx/carteraExample/ModalTransitionDialog.kt b/app/src/main/java/exchange/dydx/carteraExample/ModalTransitionDialog.kt index d148ca3..1cddd60 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/ModalTransitionDialog.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/ModalTransitionDialog.kt @@ -30,7 +30,6 @@ object ModalTransitionDialog { dismissOnBackPress: Boolean = true, content: @Composable (ModalTransitionDialogHelper) -> Unit ) { - val onCloseSharedFlow: MutableSharedFlow = remember { MutableSharedFlow() } val coroutineScope: CoroutineScope = rememberCoroutineScope() val animateContentBackTrigger = remember { mutableStateOf(false) } @@ -38,7 +37,7 @@ object ModalTransitionDialog { LaunchedEffect(key1 = Unit) { launch { - //delay(DIALOG_BUILD_TIME) + // delay(DIALOG_BUILD_TIME) animateContentBackTrigger.value = true } launch { @@ -48,7 +47,7 @@ object ModalTransitionDialog { Dialog( onDismissRequest = { coroutineScope.launch { startDismissWithExitAnimation(animateContentBackTrigger, onDismissRequest) } }, - properties = DialogProperties(usePlatformDefaultWidth = false, dismissOnBackPress = dismissOnBackPress, dismissOnClickOutside = false) + properties = DialogProperties(usePlatformDefaultWidth = false, dismissOnBackPress = dismissOnBackPress, dismissOnClickOutside = false), ) { LaunchedEffect(key1 = Unit) { if (mutex.isLocked) mutex.unlock() @@ -96,14 +95,13 @@ object ModalTransitionDialog { visible = visible, enter = slideInVertically( animationSpec = tween(ANIMATION_TIME.toInt()), - initialOffsetY = { fullHeight -> fullHeight } + initialOffsetY = { fullHeight -> fullHeight }, ), exit = slideOutVertically( animationSpec = tween(ANIMATION_TIME.toInt()), - targetOffsetY = { fullHeight -> fullHeight } + targetOffsetY = { fullHeight -> fullHeight }, ), - content = content + content = content, ) } } - diff --git a/app/src/main/java/exchange/dydx/carteraExample/WalletList.kt b/app/src/main/java/exchange/dydx/carteraExample/WalletList.kt index df44c6a..c888270 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/WalletList.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/WalletList.kt @@ -98,7 +98,7 @@ object WalletList { walletListContent( viewState = state.value, coroutineScope = coroutineScope, - bottomSheetState = bottomSheetState + bottomSheetState = bottomSheetState, ) } @@ -143,14 +143,14 @@ object WalletList { } else { wallet.openPlayStore(context) } - } + }, ) { Text( walletName, modifier = Modifier .padding(16.dp) .weight(1f, false), - textAlign = TextAlign.Start + textAlign = TextAlign.Start, ) val subText: String = @@ -163,7 +163,7 @@ object WalletList { modifier = Modifier .padding(14.dp) .weight(1f, false), - textAlign = TextAlign.End + textAlign = TextAlign.End, ) } Divider() @@ -182,14 +182,14 @@ object WalletList { viewState.debugQRCodeAction?.let { it(WalletList.QrCodeAction.V2, viewState.useTestnet) } - } + }, ) { Text( "Debug QR Code", modifier = Modifier .padding(16.dp) .weight(1f, false), - textAlign = TextAlign.Start + textAlign = TextAlign.Start, ) Text( @@ -197,7 +197,7 @@ object WalletList { modifier = Modifier .padding(14.dp) .weight(1f, false), - textAlign = TextAlign.End + textAlign = TextAlign.End, ) } Divider() @@ -207,14 +207,14 @@ object WalletList { Row( horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier - .fillMaxWidth() + .fillMaxWidth(), ) { Text( "Goerli Testnet:", modifier = Modifier .padding(16.dp) .weight(1f, false), - textAlign = TextAlign.Start + textAlign = TextAlign.Start, ) Switch( @@ -223,8 +223,6 @@ object WalletList { ) } } - - } } @@ -237,21 +235,19 @@ object WalletList { Column( horizontalAlignment = Alignment.CenterHorizontally, - modifier = Modifier.fillMaxWidth() - ) - { + modifier = Modifier.fillMaxWidth(), + ) { Text("Select Wallet Action to Test:", style = MaterialTheme.typography.h5) TextButton( onClick = { viewState.walletAction?.invoke( WalletAction.Connect, viewState.selectedWallet, - viewState.useTestnet + viewState.useTestnet, ) }, - modifier = buttonModifier - ) - { + modifier = buttonModifier, + ) { Text("Connect", style = buttonTextStyle) } TextButton( @@ -259,12 +255,11 @@ object WalletList { viewState.walletAction?.invoke( WalletAction.SignMessage, viewState.selectedWallet, - viewState.useTestnet + viewState.useTestnet, ) }, - modifier = buttonModifier - ) - { + modifier = buttonModifier, + ) { Text("Sign Personal Message", style = buttonTextStyle) } TextButton( @@ -272,12 +267,11 @@ object WalletList { viewState.walletAction?.invoke( WalletAction.SignTypedData, viewState.selectedWallet, - viewState.useTestnet + viewState.useTestnet, ) }, - modifier = buttonModifier - ) - { + modifier = buttonModifier, + ) { Text("Sign TypedData", style = buttonTextStyle) } TextButton( @@ -285,22 +279,20 @@ object WalletList { viewState.walletAction?.invoke( WalletAction.SignTransaction, viewState.selectedWallet, - viewState.useTestnet + viewState.useTestnet, ) }, - modifier = buttonModifier - ) - { + modifier = buttonModifier, + ) { Text("Send Transaction", style = buttonTextStyle) } Spacer(modifier = Modifier.height(20.dp)) TextButton( onClick = { viewState.showBottomSheet = false - }, - modifier = buttonModifier - ) - { + }, + modifier = buttonModifier, + ) { Text("Cancel", style = buttonTextStyle) } } @@ -315,9 +307,8 @@ object WalletList { Column( modifier = Modifier .fillMaxSize() - .background(Color.White) + .background(Color.White), ) { - Spacer(modifier = Modifier.size(32.dp)) uri?.let { @@ -333,7 +324,7 @@ object WalletList { modifier = Modifier .padding(16.dp) .fillMaxWidth() - .fillMaxHeight(0.6f) + .fillMaxHeight(0.6f), ) Text(it, modifier = Modifier.padding(16.dp)) @@ -346,7 +337,7 @@ object WalletList { .fillMaxWidth() .align(Alignment.Center), colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.black)), - onClick = modalTransitionDialogHelper::triggerAnimatedClose + onClick = modalTransitionDialogHelper::triggerAnimatedClose, ) { Text(text = "Close it", color = Color.White) } @@ -355,4 +346,3 @@ object WalletList { } } } - diff --git a/app/src/main/java/exchange/dydx/carteraExample/WalletListViewModel.kt b/app/src/main/java/exchange/dydx/carteraExample/WalletListViewModel.kt index 9fcf153..4fcc45d 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/WalletListViewModel.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/WalletListViewModel.kt @@ -24,7 +24,7 @@ import java.math.BigInteger class WalletListViewModel( private val context: Context -): ViewModel(), WalletStatusDelegate { +) : ViewModel(), WalletStatusDelegate { var viewState: MutableState = mutableStateOf(WalletList.WalletListState(listOf())) @@ -38,7 +38,7 @@ class WalletListViewModel( viewModelScope.launch { viewState.value = WalletList.WalletListState( wallets = CarteraConfig.shared?.wallets ?: listOf(), - walletAction = { action: WalletList.WalletAction, wallet: Wallet?, useTestnet: Boolean -> + walletAction = { action: WalletList.WalletAction, wallet: Wallet?, useTestnet: Boolean -> val chainId: Int = if (useTestnet) 5 else 1 when (action) { WalletList.WalletAction.Connect -> { @@ -69,7 +69,7 @@ class WalletListViewModel( testQRCodeV2(chainId) } } - } + }, ) } } @@ -102,7 +102,8 @@ class WalletListViewModel( private fun testSignMessage(wallet: Wallet?, chainId: Int) { val request = WalletRequest(wallet = wallet, address = null, chainId = chainId, context = context) - provider.signMessage(request = request, + provider.signMessage( + request = request, message = "Test Message", connected = { info -> Log.d(tag(this@WalletListViewModel), "Connected to: ${info?.peerName ?: info?.address}") @@ -113,7 +114,7 @@ class WalletListViewModel( } else { toastMessage("Signature: $signature") } - } + }, ) } @@ -122,7 +123,8 @@ class WalletListViewModel( dydxSign.message = message(action = "Sample Action", chainId = chainId) val request = WalletRequest(wallet = wallet, address = null, chainId = chainId, context = context) - provider.sign(request = request, + provider.sign( + request = request, typedDataProvider = dydxSign, connected = { info -> toastMessage("Connected to: ${info?.peerName ?: info?.address}") @@ -133,7 +135,7 @@ class WalletListViewModel( } else { toastMessage("Signature: $signature") } - } + }, ) } @@ -146,18 +148,19 @@ class WalletListViewModel( val ethereumRequest = EthereumTransactionRequest( fromAddress = info?.address ?: "0x00", toAddress = "0x0000000000000000000000000000000000000000", - weiValue = BigInteger("1"), + weiValue = BigInteger("1"), data = "0x", nonce = null, gasPriceInWei = BigInteger("100000000"), maxFeePerGas = null, maxPriorityFeePerGas = null, gasLimit = BigInteger("21000"), - chainId = chainId.toString() + chainId = chainId.toString(), ) val request = WalletTransactionRequest(walletRequest = request, ethereum = ethereumRequest) - provider.send(request = request, + provider.send( + request = request, connected = { info -> toastMessage("Connected to: ${info?.peerName ?: info?.address}") }, @@ -167,7 +170,7 @@ class WalletListViewModel( } else { toastMessage("Transaction Hash: $txHash") } - } + }, ) } } @@ -207,4 +210,4 @@ class WalletListViewModel( private fun type(name: String, type: String): Map { return mapOf("name" to name, "type" to type) } -} \ No newline at end of file +} diff --git a/app/src/main/java/exchange/dydx/carteraExample/WalletProvidersConfigUtil.kt b/app/src/main/java/exchange/dydx/carteraExample/WalletProvidersConfigUtil.kt index 981fde1..f7fa48a 100644 --- a/app/src/main/java/exchange/dydx/carteraExample/WalletProvidersConfigUtil.kt +++ b/app/src/main/java/exchange/dydx/carteraExample/WalletProvidersConfigUtil.kt @@ -13,7 +13,7 @@ object WalletProvidersConfigUtil { "https://media.dydx.exchange/logos/dydx-x.png", "dydx:", "https://trade.dydx.exchange/", - "wss://api.stage.dydx.exchange/wc/" + "wss://api.stage.dydx.exchange/wc/", ) val walletConnectV2Config = WalletConnectV2Config( @@ -25,13 +25,13 @@ object WalletProvidersConfigUtil { ) val walletSegueConfig = WalletSegueConfig( - "https://trade.stage.dydx.exchange/walletsegueCarteraExample" + "https://trade.stage.dydx.exchange/walletsegueCarteraExample", ) return WalletProvidersConfig( walletConnectV1Config, walletConnectV2Config, - walletSegueConfig + walletSegueConfig, ) } -} \ No newline at end of file +} diff --git a/app/src/test/java/exchange/dydx/carteraexample/ExampleUnitTest.kt b/app/src/test/java/exchange/dydx/carteraexample/ExampleUnitTest.kt index a9882fc..4af20a4 100644 --- a/app/src/test/java/exchange/dydx/carteraexample/ExampleUnitTest.kt +++ b/app/src/test/java/exchange/dydx/carteraexample/ExampleUnitTest.kt @@ -1,9 +1,8 @@ package exchange.dydx.carteraexample +import org.junit.Assert.assertEquals import org.junit.Test -import org.junit.Assert.* - /** * Example local unit test, which will execute on the development machine (host). * @@ -14,4 +13,4 @@ class ExampleUnitTest { fun addition_isCorrect() { assertEquals(4, 2 + 2) } -} \ No newline at end of file +} diff --git a/build.gradle b/build.gradle index e7e6ff2..384c23e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,45 @@ +buildscript { + ext.ktlintVersion = '0.48.0' + + repositories { + google() + mavenCentral() + } +} + + // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '8.2.0' apply false - id 'com.android.library' version '8.2.0' apply false + id 'com.android.application' version '8.2.1' apply false + id 'com.android.library' version '8.2.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.21' apply false id 'com.google.dagger.hilt.android' version '2.41' apply false + id "com.diffplug.spotless" version "6.22.0" // apply false +} + +allprojects { + apply plugin: 'com.diffplug.spotless' + spotless { + kotlin { + target "**/*.kt" + targetExclude ".idea/" + + ktlint(ktlintVersion) + .userData(["android" : "true"]) + .editorConfigOverride([ + "ktlint_standard_enum-entry-name-case" : "disabled", + "ktlint_standard_trailing-comma-on-declaration-site": "disabled", + "ktlint_standard_no-semi": "disabled", + ]) + } + } + ext { + //dependencies shared between both v3 and v4 + } +} + + +tasks.named('check') { + dependsOn buildHealth + dependsOn spotlessApply } \ No newline at end of file diff --git a/cartera/src/androidTest/java/exchange/dydx/cartera/ExampleInstrumentedTest.kt b/cartera/src/androidTest/java/exchange/dydx/cartera/ExampleInstrumentedTest.kt index 1ccabcb..d037d42 100644 --- a/cartera/src/androidTest/java/exchange/dydx/cartera/ExampleInstrumentedTest.kt +++ b/cartera/src/androidTest/java/exchange/dydx/cartera/ExampleInstrumentedTest.kt @@ -1,13 +1,11 @@ package exchange.dydx.cartera -import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * @@ -21,4 +19,4 @@ class ExampleInstrumentedTest { val appContext = InstrumentationRegistry.getInstrumentation().targetContext assertEquals("exchange.dydx.cartera.test", appContext.packageName) } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/CarteraConfig.kt b/cartera/src/main/java/exchange/dydx/cartera/CarteraConfig.kt index 9f199cf..b753664 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/CarteraConfig.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/CarteraConfig.kt @@ -16,7 +16,6 @@ import exchange.dydx.cartera.walletprovider.providers.WalletConnectV2Provider import exchange.dydx.cartera.walletprovider.providers.WalletSegueProvider import java.lang.reflect.Type - sealed class WalletConnectionType(val rawValue: String) { object WalletConnect : WalletConnectionType("walletConnect") object WalletConnectV2 : WalletConnectionType("walletConnectV2") @@ -50,7 +49,6 @@ class CarteraConfig( shared?.registration?.get(WalletConnectionType.WalletSegue)?.provider?.let { provider -> val walletSegueProvider = provider as? WalletSegueProvider return walletSegueProvider?.handleResponse(url) ?: false - } return false } @@ -61,30 +59,30 @@ class CarteraConfig( val wallets: List get() = _wallets ?: emptyList() - init { - registration[WalletConnectionType.WalletConnect] = RegistrationConfig( - provider = WalletConnectV1Provider() - ) - if (walletProvidersConfig.walletConnectV2 != null) { - registration[WalletConnectionType.WalletConnectV2] = RegistrationConfig( - provider = WalletConnectV2Provider( - walletProvidersConfig.walletConnectV2, - application - ), - ) - } - if (walletProvidersConfig.walletSegue != null) { - registration[WalletConnectionType.WalletSegue] = RegistrationConfig( - provider = WalletSegueProvider( - walletProvidersConfig.walletSegue, - application, - launcher - ), - ) - } - registration[WalletConnectionType.MagicLink] = RegistrationConfig( - provider = MagicLinkProvider(), - ) + init { + registration[WalletConnectionType.WalletConnect] = RegistrationConfig( + provider = WalletConnectV1Provider(), + ) + if (walletProvidersConfig.walletConnectV2 != null) { + registration[WalletConnectionType.WalletConnectV2] = RegistrationConfig( + provider = WalletConnectV2Provider( + walletProvidersConfig.walletConnectV2, + application, + ), + ) + } + if (walletProvidersConfig.walletSegue != null) { + registration[WalletConnectionType.WalletSegue] = RegistrationConfig( + provider = WalletSegueProvider( + walletProvidersConfig.walletSegue, + application, + launcher, + ), + ) + } + registration[WalletConnectionType.MagicLink] = RegistrationConfig( + provider = MagicLinkProvider(), + ) } fun updateConfig(walletProvidersConfig: WalletProvidersConfig) { @@ -92,7 +90,7 @@ class CarteraConfig( registration[WalletConnectionType.WalletConnectV2] = RegistrationConfig( provider = WalletConnectV2Provider( walletProvidersConfig.walletConnectV2, - application + application, ), ) } @@ -101,7 +99,7 @@ class CarteraConfig( provider = WalletSegueProvider( walletProvidersConfig.walletSegue, application, - launcher + launcher, ), ) } diff --git a/cartera/src/main/java/exchange/dydx/cartera/CarteraErrorCode.kt b/cartera/src/main/java/exchange/dydx/cartera/CarteraErrorCode.kt index 96cb832..859934b 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/CarteraErrorCode.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/CarteraErrorCode.kt @@ -33,4 +33,4 @@ enum class CarteraErrorCode(val rawValue: Int) { ADD_CHAIN_FAILED -> "Add or switch chain failed" } } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/CarteraProvider.kt b/cartera/src/main/java/exchange/dydx/cartera/CarteraProvider.kt index 72fed6c..1e19d9f 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/CarteraProvider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/CarteraProvider.kt @@ -17,10 +17,10 @@ import exchange.dydx.cartera.walletprovider.WalletUserConsentProtocol import exchange.dydx.cartera.walletprovider.WalletUserConsentStatus import exchange.dydx.cartera.walletprovider.userconsent.SkippedWalletUserConsent -class CarteraProvider(private val context: Context): WalletOperationProviderProtocol { +class CarteraProvider(private val context: Context) : WalletOperationProviderProtocol { private var currentRequestHandler: WalletOperationProviderProtocol? = null - private val debugQrCodeProvider = CarteraConfig.shared?.getProvider(WalletConnectionType.WalletConnectV2) + private val debugQrCodeProvider = CarteraConfig.shared?.getProvider(WalletConnectionType.WalletConnectV2) fun startDebugLink(chainId: Int, completion: WalletConnectCompletion) { updateCurrentHandler(WalletRequest(null, null, chainId, context)) @@ -68,9 +68,9 @@ class CarteraProvider(private val context: Context): WalletOperationProviderProt when (status) { WalletUserConsentStatus.CONSENTED -> currentRequestHandler?.send(request, connected, completion) WalletUserConsentStatus.REJECTED -> { - val error = WalletError(CarteraErrorCode.USER_CANCELED, "User canceled") - completion(null, error) - } + val error = WalletError(CarteraErrorCode.USER_CANCELED, "User canceled") + completion(null, error) + } } } } diff --git a/cartera/src/main/java/exchange/dydx/cartera/Utils.kt b/cartera/src/main/java/exchange/dydx/cartera/Utils.kt index cd2c510..67e1b7b 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/Utils.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/Utils.kt @@ -19,7 +19,8 @@ object Utils { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong())) } else { - @Suppress("DEPRECATION") getPackageInfo(packageName, flags) + @Suppress("DEPRECATION") + getPackageInfo(packageName, flags) } } diff --git a/cartera/src/main/java/exchange/dydx/cartera/entities/ModelExtensions.kt b/cartera/src/main/java/exchange/dydx/cartera/entities/ModelExtensions.kt index b7d8d36..2d576a1 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/entities/ModelExtensions.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/entities/ModelExtensions.kt @@ -15,16 +15,20 @@ fun Wallet.installed(context: Context): Boolean { return false } -fun Wallet.openPlayStore(context: Context): Unit { +fun Wallet.openPlayStore(context: Context) { config?.androidPackage?.let { androidPackage -> try { - startActivity(context, + startActivity( + context, Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$androidPackage")), - null) + null, + ) } catch (e: ActivityNotFoundException) { - startActivity(context, + startActivity( + context, Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$androidPackage")), - null) + null, + ) } } } diff --git a/cartera/src/main/java/exchange/dydx/cartera/entities/WalletModels.kt b/cartera/src/main/java/exchange/dydx/cartera/entities/WalletModels.kt index 44c6d7c..d2c3165 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/entities/WalletModels.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/entities/WalletModels.kt @@ -2,63 +2,63 @@ package exchange.dydx.cartera.entities import com.google.gson.annotations.SerializedName -data class Wallet ( - @SerializedName("id" ) var id : String? = null, - @SerializedName("name" ) var name : String? = null, - @SerializedName("description" ) var description : String? = null, - @SerializedName("homepage" ) var homepage : String? = null, - @SerializedName("chains" ) var chains : ArrayList = arrayListOf(), - @SerializedName("versions" ) var versions : ArrayList = arrayListOf(), - @SerializedName("app" ) var app : WalletApp? = WalletApp(), - @SerializedName("mobile" ) var mobile : WalletMobile? = WalletMobile(), - @SerializedName("desktop" ) var desktop : WalletDesktop? = WalletDesktop(), - @SerializedName("metadata" ) var metadata : WalletMetadata? = WalletMetadata(), - @SerializedName("config" ) var config : WalletConfig? = WalletConfig(), - @SerializedName("userFields" ) var userFields : Map? = null +data class Wallet( + @SerializedName("id") var id: String? = null, + @SerializedName("name") var name: String? = null, + @SerializedName("description") var description: String? = null, + @SerializedName("homepage") var homepage: String? = null, + @SerializedName("chains") var chains: ArrayList = arrayListOf(), + @SerializedName("versions") var versions: ArrayList = arrayListOf(), + @SerializedName("app") var app: WalletApp? = WalletApp(), + @SerializedName("mobile") var mobile: WalletMobile? = WalletMobile(), + @SerializedName("desktop") var desktop: WalletDesktop? = WalletDesktop(), + @SerializedName("metadata") var metadata: WalletMetadata? = WalletMetadata(), + @SerializedName("config") var config: WalletConfig? = WalletConfig(), + @SerializedName("userFields") var userFields: Map? = null ) -data class WalletApp ( - @SerializedName("browser" ) var browser : String? = null, - @SerializedName("ios" ) var ios : String? = null, - @SerializedName("android" ) var android : String? = null, - @SerializedName("mac" ) var mac : String? = null, - @SerializedName("windows" ) var windows : String? = null, - @SerializedName("linux" ) var linux : String? = null +data class WalletApp( + @SerializedName("browser") var browser: String? = null, + @SerializedName("ios") var ios: String? = null, + @SerializedName("android") var android: String? = null, + @SerializedName("mac") var mac: String? = null, + @SerializedName("windows") var windows: String? = null, + @SerializedName("linux") var linux: String? = null ) -data class WalletMobile ( - @SerializedName("native" ) var native : String? = null, - @SerializedName("universal" ) var universal : String? = null +data class WalletMobile( + @SerializedName("native") var native: String? = null, + @SerializedName("universal") var universal: String? = null ) -data class WalletDesktop ( - @SerializedName("native" ) var native : String? = null, - @SerializedName("universal" ) var universal : String? = null +data class WalletDesktop( + @SerializedName("native") var native: String? = null, + @SerializedName("universal") var universal: String? = null ) -data class WalletColors ( - @SerializedName("primary" ) var primary : String? = null, - @SerializedName("secondary" ) var secondary : String? = null +data class WalletColors( + @SerializedName("primary") var primary: String? = null, + @SerializedName("secondary") var secondary: String? = null ) -data class WalletMetadata ( - @SerializedName("shortName" ) var shortName : String? = null, - @SerializedName("colors" ) var colors : WalletColors? = WalletColors() +data class WalletMetadata( + @SerializedName("shortName") var shortName: String? = null, + @SerializedName("colors") var colors: WalletColors? = WalletColors() ) -data class WalletConnections ( - @SerializedName("type" ) var type : String? = null, - @SerializedName("native" ) var native : String? = null, - @SerializedName("universal" ) var universal : String? = null, +data class WalletConnections( + @SerializedName("type") var type: String? = null, + @SerializedName("native") var native: String? = null, + @SerializedName("universal") var universal: String? = null, ) -data class WalletConfig ( - @SerializedName("comment" ) var comment : String? = null, - @SerializedName("iosMinVersion" ) var iosMinVersion : String? = null, - @SerializedName("encoding" ) var encoding : String? = null, - @SerializedName("androidPackage" ) var androidPackage : String? = null, - @SerializedName("imageUrl" ) var imageUrl : String? = null, - @SerializedName("connections" ) var connections : ArrayList = arrayListOf() +data class WalletConfig( + @SerializedName("comment") var comment: String? = null, + @SerializedName("iosMinVersion") var iosMinVersion: String? = null, + @SerializedName("encoding") var encoding: String? = null, + @SerializedName("androidPackage") var androidPackage: String? = null, + @SerializedName("imageUrl") var imageUrl: String? = null, + @SerializedName("connections") var connections: ArrayList = arrayListOf() ) val Wallet.universal: String? diff --git a/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712DomainTypedDataProvider.kt b/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712DomainTypedDataProvider.kt index 36551fb..310f1fa 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712DomainTypedDataProvider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712DomainTypedDataProvider.kt @@ -42,4 +42,4 @@ class EIP712DomainTypedDataProvider( private val isValid: Boolean get() = (eip712?.isValid == true) && (message?.isValid == true) -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712TypedData.kt b/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712TypedData.kt index eecb40a..ac8643d 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712TypedData.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/typeddata/EIP712TypedData.kt @@ -19,4 +19,4 @@ class EIP712TypedData(name: String, chainId: Int, version: String?) : WalletType this.definitions = definitions this.data = data } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedData.kt b/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedData.kt index aad71a0..48f21b4 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedData.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedData.kt @@ -21,4 +21,4 @@ open class WalletTypedData(typeName: String) { fun type(name: String, type: String): Map { return mapOf("name" to name, "type" to type) } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedDataProviderProtocol.kt b/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedDataProviderProtocol.kt index c867af6..a9f4213 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedDataProviderProtocol.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/typeddata/WalletTypedDataProviderProtocol.kt @@ -11,7 +11,6 @@ fun WalletTypedDataProviderProtocol.type(name: String, type: String): Map diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletConsent.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletConsent.kt index f43b0b3..70f9caa 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletConsent.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletConsent.kt @@ -9,4 +9,4 @@ typealias WalletUserConsentCompletion = (consentStatus: WalletUserConsentStatus) interface WalletUserConsentProtocol { fun showTransactionConsent(request: WalletTransactionRequest, completion: WalletUserConsentCompletion?) -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletError.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletError.kt index 4f8fe1c..5267601 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletError.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletError.kt @@ -2,7 +2,8 @@ package exchange.dydx.cartera.walletprovider import exchange.dydx.cartera.CarteraErrorCode -data class WalletError(val code: CarteraErrorCode, - val title: String? = null, - val message: String? = null) { -} \ No newline at end of file +data class WalletError( + val code: CarteraErrorCode, + val title: String? = null, + val message: String? = null +) diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletProviderProtocols.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletProviderProtocols.kt index 96ef023..b110fd6 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletProviderProtocols.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletProviderProtocols.kt @@ -51,4 +51,4 @@ interface WalletUserConsentOperationProtocol : WalletOperationProtocol { var userConsentDelegate: WalletUserConsentProtocol? } -interface WalletOperationProviderProtocol : WalletStatusProviding, WalletUserConsentOperationProtocol \ No newline at end of file +interface WalletOperationProviderProtocol : WalletStatusProviding, WalletUserConsentOperationProtocol diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletStatus.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletStatus.kt index b81d8dc..abd2fe2 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletStatus.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/WalletStatus.kt @@ -36,4 +36,4 @@ data class WalletStatusImp( override var connectedWallet: WalletInfo? = null, override var state: WalletState = WalletState.IDLE, override var connectionDeeplink: String? = null, -) : WalletStatusProtocol \ No newline at end of file +) : WalletStatusProtocol diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/MagicLinkProvider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/MagicLinkProvider.kt index 6755b68..91245fb 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/MagicLinkProvider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/MagicLinkProvider.kt @@ -13,7 +13,7 @@ import exchange.dydx.cartera.walletprovider.WalletStatusProtocol import exchange.dydx.cartera.walletprovider.WalletTransactionRequest import exchange.dydx.cartera.walletprovider.WalletUserConsentProtocol -class MagicLinkProvider: WalletOperationProviderProtocol { +class MagicLinkProvider : WalletOperationProviderProtocol { private var _walletStatus = WalletStatusImp() set(value) { field = value @@ -65,4 +65,4 @@ class MagicLinkProvider: WalletOperationProviderProtocol { ) { TODO("Not yet implemented") } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectUtils.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectUtils.kt index 763329f..5f53dd5 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectUtils.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectUtils.kt @@ -87,4 +87,4 @@ object WalletConnectUtils { return deeplink } } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV1Provider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV1Provider.kt index b66077d..15a3ca3 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV1Provider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV1Provider.kt @@ -13,7 +13,7 @@ import exchange.dydx.cartera.walletprovider.WalletStatusProtocol import exchange.dydx.cartera.walletprovider.WalletTransactionRequest import exchange.dydx.cartera.walletprovider.WalletUserConsentProtocol -class WalletConnectV1Provider: WalletOperationProviderProtocol { +class WalletConnectV1Provider : WalletOperationProviderProtocol { private var _walletStatus = WalletStatusImp() set(value) { field = value @@ -66,4 +66,4 @@ class WalletConnectV1Provider: WalletOperationProviderProtocol { ) { TODO("Not yet implemented") } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt index 77914d4..18a2eb7 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt @@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit class WalletConnectV2Provider( private val walletConnectV2Config: WalletConnectV2Config?, private val application: Application - ): WalletOperationProviderProtocol { +) : WalletOperationProviderProtocol { private var _walletStatus = WalletStatusImp() set(value) { field = value @@ -57,8 +57,8 @@ class WalletConnectV2Provider( override var walletStatusDelegate: WalletStatusDelegate? = null override var userConsentDelegate: WalletUserConsentProtocol? = null - private var connectCompletions : MutableList = mutableListOf() - private var operationCompletions: MutableMap = mutableMapOf() + private var connectCompletions: MutableList = mutableListOf() + private var operationCompletions: MutableMap = mutableMapOf() private var requestingWallet: WalletRequest? = null private var currentSession: Sign.Model.ApprovedSession? = null @@ -73,14 +73,15 @@ class WalletConnectV2Provider( CoroutineScope(Dispatchers.Main).launch { if (requestingWallet?.chainId != null && approvedSession.chainId() != null && - requestingWallet?.chainId != approvedSession.chainId()) { + requestingWallet?.chainId != approvedSession.chainId() + ) { for (connectCompletion in connectCompletions) { connectCompletion.invoke( null, WalletError( code = CarteraErrorCode.WALLET_MISMATCH, - message = CarteraErrorCode.WALLET_MISMATCH.message - ) + message = CarteraErrorCode.WALLET_MISMATCH.message, + ), ) } connectCompletions.clear() @@ -95,7 +96,7 @@ class WalletConnectV2Provider( for (connectCompletion in connectCompletions) { connectCompletion.invoke( _walletStatus.connectedWallet, - null + null, ) } connectCompletions.clear() @@ -119,8 +120,8 @@ class WalletConnectV2Provider( null, WalletError( code = CarteraErrorCode.REFUSED_BY_WALLET, - message = rejectedSession.reason - ) + message = rejectedSession.reason, + ), ) } connectCompletions.clear() @@ -166,7 +167,7 @@ class WalletConnectV2Provider( response.result as Sign.Model.JsonRpcResponse.JsonRpcResult completion.invoke( result.result, - null + null, ) } @@ -177,8 +178,8 @@ class WalletConnectV2Provider( null, WalletError( code = CarteraErrorCode.UNEXPECTED_RESPONSE, - message = error.message - ) + message = error.message, + ), ) } } @@ -189,7 +190,7 @@ class WalletConnectV2Provider( } override fun onConnectionStateChange(state: Sign.Model.ConnectionState) { - //Triggered whenever the connection state is changed + // Triggered whenever the connection state is changed Log.d(tag(this@WalletConnectV2Provider), "onConnectionStateChange: $state") } @@ -201,43 +202,43 @@ class WalletConnectV2Provider( } init { - walletConnectV2Config?.let { walletConnectV2Config -> - // Reference: https://docs.walletconnect.com/2.0/android/sign/dapp-usage - val projectId = walletConnectV2Config.projectId - val relayUrl = "relay.walletconnect.com" - val serverUrl = "wss://$relayUrl?projectId=$projectId" - val connectionType = ConnectionType.AUTOMATIC // or ConnectionType.MANUAL - - val metadata = Core.Model.AppMetaData( - name = walletConnectV2Config.clientName, - description = walletConnectV2Config.clientDescription, - url = walletConnectV2Config.clientUrl, - icons = walletConnectV2Config.iconUrls, - redirect = "exchange.dydx.carteraexample" - ) - - CoreClient.initialize( - metaData = metadata, - relayServerUrl = serverUrl, - connectionType = connectionType, - application = application, - onError = { error -> - Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) - } - ) - - val init = Sign.Params.Init(core = CoreClient) - - SignClient.initialize(init) { error -> - Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) - } - - SignClient.setDappDelegate(dappDelegate) - } + walletConnectV2Config?.let { walletConnectV2Config -> + // Reference: https://docs.walletconnect.com/2.0/android/sign/dapp-usage + val projectId = walletConnectV2Config.projectId + val relayUrl = "relay.walletconnect.com" + val serverUrl = "wss://$relayUrl?projectId=$projectId" + val connectionType = ConnectionType.AUTOMATIC // or ConnectionType.MANUAL + + val metadata = Core.Model.AppMetaData( + name = walletConnectV2Config.clientName, + description = walletConnectV2Config.clientDescription, + url = walletConnectV2Config.clientUrl, + icons = walletConnectV2Config.iconUrls, + redirect = "exchange.dydx.carteraexample", + ) + + CoreClient.initialize( + metaData = metadata, + relayServerUrl = serverUrl, + connectionType = connectionType, + application = application, + onError = { error -> + Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) + }, + ) + + val init = Sign.Params.Init(core = CoreClient) + + SignClient.initialize(init) { error -> + Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) + } + + SignClient.setDappDelegate(dappDelegate) + } } override fun connect(request: WalletRequest, completion: WalletConnectCompletion) { - if ( _walletStatus.state == WalletState.CONNECTED_TO_WALLET) { + if (_walletStatus.state == WalletState.CONNECTED_TO_WALLET) { completion(walletStatus?.connectedWallet, null) } else { requestingWallet = request @@ -279,7 +280,8 @@ class WalletConnectV2Provider( } override fun disconnect() { - currentPairing?.let { it + currentPairing?.let { + it CoreClient.Pairing.disconnect(Core.Params.Disconnect(it.topic)) { error -> Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) } @@ -305,12 +307,12 @@ class WalletConnectV2Provider( val account = currentSession?.account() val namespace = currentSession?.namespace() val chainId = currentSession?.chainId() - return if (sessionTopic!= null && account != null && namespace != null && chainId != null) { + return if (sessionTopic != null && account != null && namespace != null && chainId != null) { return Sign.Params.Request( sessionTopic = sessionTopic, method = "personal_sign", params = "[\"${message}\", \"${account}\"]", - chainId = "${namespace}:${chainId}" + chainId = "$namespace:$chainId", ) } else { return null @@ -338,7 +340,7 @@ class WalletConnectV2Provider( sessionTopic = sessionTopic, method = "eth_signTypedData", params = "[\"${account}\", \"${message}\"]", - chainId = "${namespace}:${chainId}" + chainId = "$namespace:$chainId", ) } else { null @@ -359,12 +361,12 @@ class WalletConnectV2Provider( val namespace = currentSession?.namespace() val chainId = currentSession?.chainId() val message = request.ethereum?.toJsonRequest() - return if (sessionTopic!= null && account != null && namespace != null && chainId != null && message != null) { + return if (sessionTopic != null && account != null && namespace != null && chainId != null && message != null) { Sign.Params.Request( sessionTopic = sessionTopic, method = "eth_sendTransaction", - params = "[${message}]", - chainId = "${namespace}:${chainId}" + params = "[$message]", + chainId = "$namespace:$chainId", ) } else { null @@ -385,7 +387,7 @@ class WalletConnectV2Provider( private fun doConnect(request: WalletRequest, completion: (pairing: Core.Model.Pairing?, error: WalletError?) -> Unit) { val namespace: String = "eip155" /*Namespace identifier, see for reference: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md#syntax*/ - val chain: String = if (request.chainId != null) { + val chain: String = if (request.chainId != null) { "eip155:${request.chainId}" } else { "eip155:5" @@ -395,11 +397,11 @@ class WalletConnectV2Provider( "personal_sign", "eth_sendTransaction", "eth_signTypedData", - // "wallet_addEthereumChain" + // "wallet_addEthereumChain" ) - val events: List = listOf( + val events: List = listOf( "accountsChanged", - "chainChanged" + "chainChanged", ) val proposal = Sign.Model.Namespace.Proposal(chains, methods, events) val requiredNamespaces: Map = mapOf(namespace to proposal) /*Required namespaces to setup a session*/ @@ -426,7 +428,7 @@ class WalletConnectV2Provider( namespaces = requiredNamespaces, optionalNamespaces = optionalNamespaces, properties = properties, - pairing = pairing + pairing = pairing, ) SignClient.connect( @@ -439,9 +441,9 @@ class WalletConnectV2Provider( Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) completion( null, - WalletError(CarteraErrorCode.CONNECTION_FAILED, "SignClient.connect error", error.throwable.stackTraceToString()) + WalletError(CarteraErrorCode.CONNECTION_FAILED, "SignClient.connect error", error.throwable.stackTraceToString()), ) - } + }, ) openPeerDeeplink(request, pairing) } @@ -480,7 +482,7 @@ class WalletConnectV2Provider( requestParams: Sign.Params.Request, completion: WalletOperationCompletion ) { - SignClient.request( + SignClient.request( request = requestParams, onSuccess = { request: Sign.Model.SentRequest -> Log.d(tag(this@WalletConnectV2Provider), "Wallet request made.") @@ -493,10 +495,10 @@ class WalletConnectV2Provider( WalletError( CarteraErrorCode.CONNECTION_FAILED, "SignClient.request error", - error.throwable.stackTraceToString() - ) + error.throwable.stackTraceToString(), + ), ) - } + }, ) openPeerDeeplink(request, currentPairing) @@ -507,8 +509,8 @@ class WalletConnectV2Provider( address = "address", chainId = 0, wallet = wallet, - peerName = pairing.peerAppMetaData?.name, - peerImageUrl = pairing.peerAppMetaData?.icons?.firstOrNull() + peerName = pairing.peerAppMetaData?.name, + peerImageUrl = pairing.peerAppMetaData?.icons?.firstOrNull(), ) } @@ -517,8 +519,8 @@ class WalletConnectV2Provider( address = session.account(), chainId = session.chainId(), wallet = wallet, - peerName = session.metaData?.name, - peerImageUrl = session.metaData?.icons?.firstOrNull() + peerName = session.metaData?.name, + peerImageUrl = session.metaData?.icons?.firstOrNull(), ) } @@ -536,7 +538,7 @@ class WalletConnectV2Provider( wallet = request.wallet, deeplink = pairing?.uri, type = WalletConnectionType.WalletConnectV2, - context = request.context + context = request.context, ) val deeplinkPairingUri = url?.toURI()?.toString() if (deeplinkPairingUri != null) { diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletSegueProvider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletSegueProvider.kt index 3ca4fe9..a84f6d4 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletSegueProvider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletSegueProvider.kt @@ -36,7 +36,7 @@ class WalletSegueProvider( private val walletSegueConfig: WalletSegueConfig?, private val application: Application, private val launcher: ActivityResultLauncher? - ): WalletOperationProviderProtocol { +) : WalletOperationProviderProtocol { private var _walletStatus = WalletStatusImp() set(value) { @@ -58,7 +58,7 @@ class WalletSegueProvider( client = CoinbaseWalletSDK( appContext = application.applicationContext, domain = Uri.parse(walletSegueCallbackUrl), - openIntent = { intent -> launcher?.launch(intent) } + openIntent = { intent -> launcher?.launch(intent) }, ) } } @@ -72,10 +72,9 @@ class WalletSegueProvider( when (_walletStatus.state) { WalletState.IDLE -> { - val requestAccount = Web3JsonRPC.RequestAccounts().action() client?.initiateHandshake( - initialActions = listOf(requestAccount) + initialActions = listOf(requestAccount), ) { result: Result>, account: Account? -> result.onSuccess { actionResults: List -> if (account != null) { @@ -91,8 +90,8 @@ class WalletSegueProvider( WalletError( code = CarteraErrorCode.NETWORK_MISMATCH, title = errorTitle, - message = errorMessage - ) + message = errorMessage, + ), ) } else if (expected.address != null && expected.address?.lowercase() != account.address.lowercase()) { val errorTitle = "Wallet Mismatch" @@ -103,20 +102,19 @@ class WalletSegueProvider( WalletError( code = CarteraErrorCode.WALLET_MISMATCH, title = errorTitle, - message = errorMessage - ) + message = errorMessage, + ), ) } else { this?.account = account this?._walletStatus?.connectedWallet = WalletInfo( address = account.address, chainId = expected.chainId, - wallet = wallet + wallet = wallet, ) this?._walletStatus?.state = WalletState.CONNECTED_TO_WALLET completion(this?._walletStatus?.connectedWallet, null) } - } else { completion(null, WalletError(CarteraErrorCode.WALLET_CONTAINS_NO_ACCOUNT, CarteraErrorCode.WALLET_CONTAINS_NO_ACCOUNT.message)) } @@ -127,7 +125,7 @@ class WalletSegueProvider( } } WalletState.LISTENING -> { - Log.e(tag(this@WalletSegueProvider), "Invalid state") + Log.e(tag(this@WalletSegueProvider), "Invalid state") } WalletState.CONNECTED_TO_SERVER -> { completion(_walletStatus.connectedWallet, null) @@ -181,7 +179,7 @@ class WalletSegueProvider( request.ethereum.maxFeePerGas?.toHexString(), request.ethereum.maxPriorityFeePerGas?.toHexString(), request.ethereum.gasLimit?.toHexString(), - request.ethereum.chainId + request.ethereum.chainId, ).action() reallyMakeRequest(sendAction) { result, error -> if (error != null) { @@ -192,7 +190,7 @@ class WalletSegueProvider( } else { completion( null, - WalletError(CarteraErrorCode.INVALID_INPUT, CarteraErrorCode.INVALID_INPUT.message) + WalletError(CarteraErrorCode.INVALID_INPUT, CarteraErrorCode.INVALID_INPUT.message), ) } } @@ -237,11 +235,18 @@ class WalletSegueProvider( ) { val request = RequestContent.Request(actions = listOf(action), account = null) client?.makeRequest( - request = request + request = request, ) { result: Result> -> result.onSuccess { actionResults: List -> when (val actionResult = actionResults.firstOrNull()) { - is ActionResult.Result -> completion(actionResult.value, null) + is ActionResult.Result -> { + // the hash returned has quotes around it, so we remove them + var value = actionResult.value + if (value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length - 1) + } + completion(value, null) + } is ActionResult.Error -> completion(null, WalletError(CarteraErrorCode.REFUSED_BY_WALLET, actionResult.message)) null -> completion(null, WalletError(CarteraErrorCode.REFUSED_BY_WALLET, "No result")) } @@ -251,4 +256,4 @@ class WalletSegueProvider( } } } -} \ No newline at end of file +} diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/userconsent/SkippedWalletUserConsent.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/userconsent/SkippedWalletUserConsent.kt index 5f4c52b..57d853e 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/userconsent/SkippedWalletUserConsent.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/userconsent/SkippedWalletUserConsent.kt @@ -9,4 +9,4 @@ class SkippedWalletUserConsent : WalletUserConsentProtocol { override fun showTransactionConsent(request: WalletTransactionRequest, completion: WalletUserConsentCompletion?) { completion?.invoke(WalletUserConsentStatus.CONSENTED) } -} \ No newline at end of file +} diff --git a/cartera/src/test/java/exchange/dydx/cartera/ExampleUnitTest.kt b/cartera/src/test/java/exchange/dydx/cartera/ExampleUnitTest.kt index 256f3c9..b3542c2 100644 --- a/cartera/src/test/java/exchange/dydx/cartera/ExampleUnitTest.kt +++ b/cartera/src/test/java/exchange/dydx/cartera/ExampleUnitTest.kt @@ -1,9 +1,8 @@ package exchange.dydx.cartera +import org.junit.Assert.assertEquals import org.junit.Test -import org.junit.Assert.* - /** * Example local unit test, which will execute on the development machine (host). * @@ -14,4 +13,4 @@ class ExampleUnitTest { fun addition_isCorrect() { assertEquals(4, 2 + 2) } -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index 2a4eb8e..7208226 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,4 +26,4 @@ android.nonTransitiveRClass=true LIBRARY_GROUP=dydxprotocol LIBRARY_ARTIFACT_ID=cartera-android -LIBRARY_VERSION_NAME=0.0.9 \ No newline at end of file +LIBRARY_VERSION_NAME=0.0.10 \ No newline at end of file From 6d357817720981262998199bc7296bec5e39f4eb Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 18 Jan 2024 20:23:11 -0800 Subject: [PATCH 2/3] Add SpotlessCheck Github Action --- .github/workflow/SpotlessCheck.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflow/SpotlessCheck.yml diff --git a/.github/workflow/SpotlessCheck.yml b/.github/workflow/SpotlessCheck.yml new file mode 100644 index 0000000..09aece0 --- /dev/null +++ b/.github/workflow/SpotlessCheck.yml @@ -0,0 +1,24 @@ +name: SpotlessCheck + +on: + pull_request: + paths: + - '**/*.kt' + +jobs: + spotlessCheck: + runs-on: macos-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'adopt' + + - name: Lint Kotlin Files + run: | + ./gradlew spotlessCheck From 856c1db71e9b7934b730504c4f079422aae262b2 Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 18 Jan 2024 20:26:49 -0800 Subject: [PATCH 3/3] Renaming --- .github/{workflow => workflows}/SpotlessCheck.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{workflow => workflows}/SpotlessCheck.yml (100%) diff --git a/.github/workflow/SpotlessCheck.yml b/.github/workflows/SpotlessCheck.yml similarity index 100% rename from .github/workflow/SpotlessCheck.yml rename to .github/workflows/SpotlessCheck.yml