Skip to content

Commit

Permalink
Implemented Wallet SDK (#139)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
maxslimb authored Jan 8, 2025
1 parent 815e676 commit 55ef0e0
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 7 deletions.
41 changes: 37 additions & 4 deletions wallet-sdk/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import java.util.Properties

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
id("kotlin-kapt")
}

android {
namespace = "com.snphone.snwalletsdk"
compileSdk = 34
compileSdk = 35

buildFeatures {
buildConfig = true
}

defaultConfig {
applicationId = "com.snphone.snwalletsdk"
Expand All @@ -15,6 +22,14 @@ android {
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

val properties = Properties()
properties.load(project.rootProject.file("local.properties").inputStream())
buildConfigField("String", "RPC_URL", "\"${properties.getProperty("RPC_URL")}\"")
buildConfigField("String", "publicAddress", "\"${properties.getProperty("publicAddress")}\"")
buildConfigField("String", "privateKey", "\"${properties.getProperty("privateKey")}\"")
buildConfigField("String", "recepientAddress", "\"${properties.getProperty("recepientAddress")}\"")

}

buildTypes {
Expand All @@ -27,20 +42,38 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.7"
}
}

dependencies {

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

//Room
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
kapt(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)

//crypto-security
implementation(libs.androidx.security.crypto.v110alpha06)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
testImplementation(libs.kotlinx.coroutines.test)
}
14 changes: 13 additions & 1 deletion wallet-sdk/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -11,6 +13,16 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SNWalletSDK"
tools:targetApi="31" />
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.snphone.snwalletsdk

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.snphone.snwalletsdk.utils.StarknetClient
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.Felt
import com.swmansion.starknet.data.types.StarknetChainId
import com.swmansion.starknet.extensions.toFelt
import com.swmansion.starknet.provider.rpc.JsonRpcProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

init {
instance = this
}

companion object {
private var instance: MainActivity? = null

fun applicationContext() : Context {
return instance!!.applicationContext
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
instance = this
setContentView(R.layout.activity_main)

val starknetClient = StarknetClient(BuildConfig.RPC_URL)
lifecycleScope.launch(Dispatchers.IO) {

//Deploy account
//val (privateKey, accountAddress) = deployAccount()

//GetBalance
//val balance = getBalance(accountAddress)
val addressUser = BuildConfig.publicAddress.toFelt
val privateKey = BuildConfig.privateKey.toFelt

val account = StandardAccount(
address = addressUser,
privateKey = privateKey,
provider = JsonRpcProvider(BuildConfig.RPC_URL),
chainId = StarknetChainId.SEPOLIA,
)
val recipientAddress = Felt.fromHex(BuildConfig.recepientAddress)
try {
val amount = StarknetClient.toUint256Amount(1.toString())
val address = starknetClient.transferFunds(account,recipientAddress,amount)
Log.d("MainActivity", "transaction id: $address")

}catch (e: Exception){
Log.d("MainActivity", "Error in amount: $e")

}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
package com.snphone.snwalletsdk

class SNWalletSDK {
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.snphone.snwalletsdk.utils.StarknetClient
import kotlinx.coroutines.launch

class SNWalletSDK : ComponentActivity() {
init {
instance = this
}

companion object {
private var instance: SNWalletSDK? = null

fun applicationContext() : Context {
return instance!!.applicationContext
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
instance = this

val starknetClient = StarknetClient(BuildConfig.RPC_URL)
lifecycleScope.launch {
starknetClient.deployAccount()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.snphone.snwalletsdk.utils

import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.snphone.snwalletsdk.MainActivity
import com.snphone.snwalletsdk.SNWalletSDK
import java.io.IOException
import java.security.GeneralSecurityException

class Keystore() {

private val tag = "Keystore"
private val context = MainActivity.applicationContext()
fun storeData(message: String) {
try {
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()

val sharedPreferences = EncryptedSharedPreferences.create(
context,
"my_encrypted_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)

val editor = sharedPreferences.edit()
editor.putString("key", message)
editor.apply()

} catch (e: GeneralSecurityException) {
Log.e(tag, "Security exception while storing data: ${e.message}", e)
} catch (e: IOException) {
Log.e(tag, "I/O exception while storing data: ${e.message}", e)
}
}

fun retrieveData(): String {
try {
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()

val sharedPreferences = EncryptedSharedPreferences.create(
context,
"my_encrypted_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)

return sharedPreferences.getString("key", "") ?: ""

} catch (e: GeneralSecurityException) {
Log.e(tag, "Security exception while retrieving data: ${e.message}", e)
return ""
} catch (e: IOException) {
Log.e(tag, "I/O exception while retrieving data: ${e.message}", e)
return ""
}
}
}
Loading

0 comments on commit 55ef0e0

Please sign in to comment.