Skip to content

Commit

Permalink
Adding the create starknet account logic for Kotlin (#76)
Browse files Browse the repository at this point in the history
* Adding the create starknet account logic

* Fixing RPC connection issue

* Adding XML for network security config purposes
  • Loading branch information
Gerson2102 authored Sep 15, 2024
1 parent 630f49f commit 99ffc4c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
4 changes: 4 additions & 0 deletions wallet_app/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ android {
}
dependencies {

implementation("com.swmansion.starknet:starknet:0.12.1@aar"){
isTransitive = true
}

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
Expand Down
2 changes: 1 addition & 1 deletion wallet_app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

<uses-permission android:name="android.permission.INTERNET" />


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -14,6 +13,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Walletapp"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31">
<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package com.example.walletapp
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -26,6 +24,7 @@ import androidx.compose.material.Card
import androidx.compose.material.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -41,6 +40,14 @@ import com.example.walletapp.ui.theme.WalletappTheme
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.HiltAndroidApp

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.*
import com.swmansion.starknet.provider.rpc.JsonRpcProvider
import com.swmansion.starknet.signer.StarkCurveSigner
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -70,6 +77,12 @@ fun StarknetLogo (modifier: Modifier = Modifier) {
@Composable
fun CreateAccount( modifier: Modifier) {
val context = (LocalContext.current as Activity)
val scope = rememberCoroutineScope()

// Predefined values for account creation
val privateKey = Felt.fromHex("0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a") // Replace with an actual private key
val accountAddress = Felt.fromHex("0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca") // Replace with an actual address
val provider = JsonRpcProvider("http://10.0.2.2:5050")
Column(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -132,11 +145,34 @@ fun CreateAccount( modifier: Modifier) {
}
Spacer(modifier = Modifier.height(10.dp))


// New button for deploying Starknet account
Button(
onClick = { val i = Intent(context, WalletActivity::class.java)
context.startActivity(i) },
colors = ButtonDefaults.buttonColors(backgroundColor = Color("#EC796B".toColorInt())),
onClick = {
scope.launch {
try {
val signer = StarkCurveSigner(privateKey)
val chainId = provider.getChainId().sendAsync().await()
val account = StandardAccount(
address = accountAddress,
signer = signer,
provider = provider,
chainId = chainId,
cairoVersion = Felt.ONE,
)

// Here you would typically deploy the account
// For now, we'll just show a success message
withContext(Dispatchers.Main) {
Toast.makeText(context, "Account deployed successfully!", Toast.LENGTH_LONG).show()
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(context, "Error deploying account: ${e.message}", Toast.LENGTH_LONG).show()
}
}
}
},
colors = ButtonDefaults.buttonColors(backgroundColor = Color("#4CAF50".toColorInt())),
shape = RoundedCornerShape(10.dp),
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">0.0.0.0</domain>
</domain-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>

0 comments on commit 99ffc4c

Please sign in to comment.