Skip to content

Commit 55ef0e0

Browse files
authored
Implemented Wallet SDK (#139)
* implemented basic functions * implemented and tested the deployAccount function * implemented and tested the deployAccount function * implemented and tested the balance function * implemented and tested the TransferFunds function * removed unused imports * updated return parameters and other fixes
1 parent 815e676 commit 55ef0e0

File tree

8 files changed

+414
-7
lines changed

8 files changed

+414
-7
lines changed

wallet-sdk/app/build.gradle.kts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import java.util.Properties
2+
13
plugins {
24
alias(libs.plugins.android.application)
35
alias(libs.plugins.jetbrains.kotlin.android)
6+
id("kotlin-kapt")
47
}
58

69
android {
710
namespace = "com.snphone.snwalletsdk"
8-
compileSdk = 34
11+
compileSdk = 35
12+
13+
buildFeatures {
14+
buildConfig = true
15+
}
916

1017
defaultConfig {
1118
applicationId = "com.snphone.snwalletsdk"
@@ -15,6 +22,14 @@ android {
1522
versionName = "1.0"
1623

1724
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
25+
26+
val properties = Properties()
27+
properties.load(project.rootProject.file("local.properties").inputStream())
28+
buildConfigField("String", "RPC_URL", "\"${properties.getProperty("RPC_URL")}\"")
29+
buildConfigField("String", "publicAddress", "\"${properties.getProperty("publicAddress")}\"")
30+
buildConfigField("String", "privateKey", "\"${properties.getProperty("privateKey")}\"")
31+
buildConfigField("String", "recepientAddress", "\"${properties.getProperty("recepientAddress")}\"")
32+
1833
}
1934

2035
buildTypes {
@@ -27,20 +42,38 @@ android {
2742
}
2843
}
2944
compileOptions {
30-
sourceCompatibility = JavaVersion.VERSION_1_8
31-
targetCompatibility = JavaVersion.VERSION_1_8
45+
sourceCompatibility = JavaVersion.VERSION_17
46+
targetCompatibility = JavaVersion.VERSION_17
3247
}
3348
kotlinOptions {
34-
jvmTarget = "1.8"
49+
jvmTarget = "17"
50+
}
51+
composeOptions {
52+
kotlinCompilerExtensionVersion = "1.5.7"
3553
}
3654
}
3755

3856
dependencies {
3957

58+
implementation("com.swmansion.starknet:starknet:0.13.0@aar"){
59+
isTransitive = true
60+
}
61+
62+
//Room
63+
implementation(libs.androidx.room.runtime)
64+
implementation(libs.androidx.activity)
65+
implementation(libs.androidx.constraintlayout)
66+
kapt(libs.androidx.room.compiler)
67+
implementation(libs.androidx.room.ktx)
68+
69+
//crypto-security
70+
implementation(libs.androidx.security.crypto.v110alpha06)
71+
4072
implementation(libs.androidx.core.ktx)
4173
implementation(libs.androidx.appcompat)
4274
implementation(libs.material)
4375
testImplementation(libs.junit)
4476
androidTestImplementation(libs.androidx.junit)
4577
androidTestImplementation(libs.androidx.espresso.core)
78+
testImplementation(libs.kotlinx.coroutines.test)
4679
}

wallet-sdk/app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
57
<application
68
android:allowBackup="true"
79
android:dataExtractionRules="@xml/data_extraction_rules"
@@ -11,6 +13,16 @@
1113
android:roundIcon="@mipmap/ic_launcher_round"
1214
android:supportsRtl="true"
1315
android:theme="@style/Theme.SNWalletSDK"
14-
tools:targetApi="31" />
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
1527

1628
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.snphone.snwalletsdk
2+
3+
import android.content.Context
4+
import android.os.Bundle
5+
import android.util.Log
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.lifecycle.lifecycleScope
9+
import com.snphone.snwalletsdk.utils.StarknetClient
10+
import com.swmansion.starknet.account.StandardAccount
11+
import com.swmansion.starknet.data.types.Felt
12+
import com.swmansion.starknet.data.types.StarknetChainId
13+
import com.swmansion.starknet.extensions.toFelt
14+
import com.swmansion.starknet.provider.rpc.JsonRpcProvider
15+
import kotlinx.coroutines.Dispatchers
16+
import kotlinx.coroutines.launch
17+
18+
class MainActivity : AppCompatActivity() {
19+
20+
init {
21+
instance = this
22+
}
23+
24+
companion object {
25+
private var instance: MainActivity? = null
26+
27+
fun applicationContext() : Context {
28+
return instance!!.applicationContext
29+
}
30+
}
31+
32+
override fun onCreate(savedInstanceState: Bundle?) {
33+
super.onCreate(savedInstanceState)
34+
enableEdgeToEdge()
35+
instance = this
36+
setContentView(R.layout.activity_main)
37+
38+
val starknetClient = StarknetClient(BuildConfig.RPC_URL)
39+
lifecycleScope.launch(Dispatchers.IO) {
40+
41+
//Deploy account
42+
//val (privateKey, accountAddress) = deployAccount()
43+
44+
//GetBalance
45+
//val balance = getBalance(accountAddress)
46+
val addressUser = BuildConfig.publicAddress.toFelt
47+
val privateKey = BuildConfig.privateKey.toFelt
48+
49+
val account = StandardAccount(
50+
address = addressUser,
51+
privateKey = privateKey,
52+
provider = JsonRpcProvider(BuildConfig.RPC_URL),
53+
chainId = StarknetChainId.SEPOLIA,
54+
)
55+
val recipientAddress = Felt.fromHex(BuildConfig.recepientAddress)
56+
try {
57+
val amount = StarknetClient.toUint256Amount(1.toString())
58+
val address = starknetClient.transferFunds(account,recipientAddress,amount)
59+
Log.d("MainActivity", "transaction id: $address")
60+
61+
}catch (e: Exception){
62+
Log.d("MainActivity", "Error in amount: $e")
63+
64+
}
65+
}
66+
}
67+
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
package com.snphone.snwalletsdk
22

3-
class SNWalletSDK {
3+
import android.content.Context
4+
import android.os.Bundle
5+
import androidx.activity.ComponentActivity
6+
import androidx.lifecycle.lifecycleScope
7+
import com.snphone.snwalletsdk.utils.StarknetClient
8+
import kotlinx.coroutines.launch
9+
10+
class SNWalletSDK : ComponentActivity() {
11+
init {
12+
instance = this
13+
}
14+
15+
companion object {
16+
private var instance: SNWalletSDK? = null
17+
18+
fun applicationContext() : Context {
19+
return instance!!.applicationContext
20+
}
21+
}
22+
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
instance = this
26+
27+
val starknetClient = StarknetClient(BuildConfig.RPC_URL)
28+
lifecycleScope.launch {
29+
starknetClient.deployAccount()
30+
}
31+
}
432
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.snphone.snwalletsdk.utils
2+
3+
import android.util.Log
4+
import androidx.security.crypto.EncryptedSharedPreferences
5+
import androidx.security.crypto.MasterKey
6+
import com.snphone.snwalletsdk.MainActivity
7+
import com.snphone.snwalletsdk.SNWalletSDK
8+
import java.io.IOException
9+
import java.security.GeneralSecurityException
10+
11+
class Keystore() {
12+
13+
private val tag = "Keystore"
14+
private val context = MainActivity.applicationContext()
15+
fun storeData(message: String) {
16+
try {
17+
val masterKey = MasterKey.Builder(context)
18+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
19+
.build()
20+
21+
val sharedPreferences = EncryptedSharedPreferences.create(
22+
context,
23+
"my_encrypted_prefs",
24+
masterKey,
25+
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
26+
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
27+
)
28+
29+
val editor = sharedPreferences.edit()
30+
editor.putString("key", message)
31+
editor.apply()
32+
33+
} catch (e: GeneralSecurityException) {
34+
Log.e(tag, "Security exception while storing data: ${e.message}", e)
35+
} catch (e: IOException) {
36+
Log.e(tag, "I/O exception while storing data: ${e.message}", e)
37+
}
38+
}
39+
40+
fun retrieveData(): String {
41+
try {
42+
val masterKey = MasterKey.Builder(context)
43+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
44+
.build()
45+
46+
val sharedPreferences = EncryptedSharedPreferences.create(
47+
context,
48+
"my_encrypted_prefs",
49+
masterKey,
50+
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
51+
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
52+
)
53+
54+
return sharedPreferences.getString("key", "") ?: ""
55+
56+
} catch (e: GeneralSecurityException) {
57+
Log.e(tag, "Security exception while retrieving data: ${e.message}", e)
58+
return ""
59+
} catch (e: IOException) {
60+
Log.e(tag, "I/O exception while retrieving data: ${e.message}", e)
61+
return ""
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)