Skip to content

Commit

Permalink
feat: sign messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhiraj40 committed Dec 27, 2023
1 parent 4d746eb commit 0a5c4ef
Show file tree
Hide file tree
Showing 26 changed files with 626 additions and 15 deletions.
16 changes: 16 additions & 0 deletions solwave/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Properties

plugins {
id("maven-publish")
id("com.android.library")
Expand All @@ -18,6 +20,19 @@ android {
vectorDrawables {
useSupportLibrary = true
}

val localProperties = Properties()
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localProperties.load(localPropertiesFile.inputStream())
}

buildConfigField("String", "BASE_URL", "\"${localProperties.getProperty("BASE_URL")}\"")
buildConfigField("String", "INITIATE_LOGIN", "\"${localProperties.getProperty("INITIATE_LOGIN")}\"")
buildConfigField("String", "INITIATE_CREATION", "\"${localProperties.getProperty("INITIATE_CREATION")}\"")
buildConfigField("String", "INITIATE_TRANSACTION", "\"${localProperties.getProperty("INITIATE_TRANSACTION")}\"")
buildConfigField("String", "SIMULATE_TRANSACTION", "\"${localProperties.getProperty("SIMULATE_TRANSACTION")}\"")
buildConfigField("String", "SIGN_MESSAGE", "\"${localProperties.getProperty("SIGN_MESSAGE")}\"")
}

buildTypes {
Expand All @@ -38,6 +53,7 @@ android {
}
buildFeatures {
compose = true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
Expand Down
36 changes: 36 additions & 0 deletions solwave/src/main/java/com/saganize/solwave/Solwave.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.saganize.solwave

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.annotation.Keep
import com.google.firebase.FirebaseApp
import com.saganize.solwave.core.di.TAG
import com.saganize.solwave.core.models.CompleteEvents
import com.saganize.solwave.core.models.EventKeys
import com.saganize.solwave.core.models.FIREBASE_NOT_INITIALIZED
Expand Down Expand Up @@ -38,6 +40,25 @@ class Solwave(
context.startActivity(intent)
}

fun signMessage(
message: String,
onSuccess: (result: String) -> Unit,
onFailure: (error: SolwaveErrors) -> Unit,
) {
if (validateMessage(message).not()) {
onFailure(SolwaveErrors.InvalidSigningMessage)
return
}
val intent = createIntent(
StartEvents.SIGN_MESSAGE,
message = message,
onSuccess = onSuccess,
onFailure = onFailure,
)

context.startActivity(intent)
}

fun performTransaction(
transaction: Transaction,
onSuccess: (result: String) -> Unit,
Expand All @@ -62,6 +83,7 @@ class Solwave(

private fun createIntent(
startEvent: StartEvents,
message: String? = null,
serializedTransaction: String? = null,
onSuccess: (result: String) -> Unit,
onFailure: (error: SolwaveErrors) -> Unit,
Expand All @@ -72,6 +94,7 @@ class Solwave(
putExtra(EventKeys.START.key, startEvent.event)
putExtra(EventKeys.API_KEY.key, apiKey)
// putExtra(EventKeys.EVENT.key, CompleteEvents(onSuccess, onFailure))
message?.let { putExtra(EventKeys.MESSAGE.key, it) }
serializedTransaction?.let { putExtra(EventKeys.TRANSACTION.key, it) }
}
return intent
Expand All @@ -84,4 +107,17 @@ class Solwave(
throw IllegalStateException(FIREBASE_NOT_INITIALIZED)
}
}

private fun validateMessage(message: String): Boolean {
if ((message.length <= SIGNING_MESSAGE_MAX_LENGTH).not()) {
Log.e(TAG, "Message length is greater than $SIGNING_MESSAGE_MAX_LENGTH")
return false
}

return true
}

companion object {
const val SIGNING_MESSAGE_MAX_LENGTH = 300
}
}
8 changes: 8 additions & 0 deletions solwave/src/main/java/com/saganize/solwave/SolwaveActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class SolwaveActivity : ComponentActivity() {
val apiKey = intent.getStringExtra(EventKeys.API_KEY.key)
?: throw IllegalStateException(SolwaveErrors.NoApiKeyPassedMessage.message)

val message = if (start == StartEvents.SIGN_MESSAGE.event) {
intent.getStringExtra(EventKeys.MESSAGE.key)
?: throw IllegalStateException(SolwaveErrors.NoMessagePassedMessage.message)
} else {
""
}

val transactionString = if (start == StartEvents.PAY.event) {
intent.getStringExtra(EventKeys.TRANSACTION.key)
?: throw IllegalStateException(SolwaveErrors.NoTransactionPassedMessage.message)
Expand All @@ -61,6 +68,7 @@ class SolwaveActivity : ComponentActivity() {
SolwaveViewModel(
module.usecases,
start,
message = message,
transactionString.toTransaction(),
apiKey,
completeEvents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import com.saganize.solwave.data.remote.SolwaveAPI
import com.saganize.solwave.domain.repository.ApiRepository
import com.saganize.solwave.domain.repository.DataStoreRepository
import com.saganize.solwave.domain.repository.DatabaseRepository
import com.saganize.solwave.domain.usecases.GenerateSignMessage
import com.saganize.solwave.domain.usecases.GetBalance
import com.saganize.solwave.domain.usecases.GetEncryptionKeyPair
import com.saganize.solwave.domain.usecases.GetLatestBlockHash
import com.saganize.solwave.domain.usecases.GetWallets
import com.saganize.solwave.domain.usecases.GetWalletsPreference
import com.saganize.solwave.domain.usecases.InitiateCreateUser
import com.saganize.solwave.domain.usecases.InitiateLogin
import com.saganize.solwave.domain.usecases.InitiateSignMessage
import com.saganize.solwave.domain.usecases.InitiateTransaction
import com.saganize.solwave.domain.usecases.SaveEncryptionKeyPair
import com.saganize.solwave.domain.usecases.SaveWallet
Expand Down Expand Up @@ -84,6 +86,8 @@ class SolwaveAppModuleImpl(
InitiateCreateUser(apiRepository),
InitiateLogin(apiRepository),
InitiateTransaction(apiRepository),
InitiateSignMessage(apiRepository),
GenerateSignMessage(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ sealed class SolwaveAuthNavEvents {
data class InitiateTransaction(
val context: Context,
val user: Transaction?,
) :
SolwaveAuthNavEvents()
) : SolwaveAuthNavEvents()

data class InitiateSignMessage(
val context: Context,
val publicKey: String,
) : SolwaveAuthNavEvents()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ sealed class SolwaveEvents {

data class PayUsingWallet(val openDeepLink: () -> Unit = {}) : SolwaveEvents()
data class DecryptTransactionResult(val data: String, val nonce: String) : SolwaveEvents()

data class DecryptSignedMessageResult(val data: String, val nonce: String) : SolwaveEvents()

data class MessageSigned(val signature: String, val messageBytes: String) : SolwaveEvents()
data class TransactionDone(val id: String) : SolwaveEvents()
data class Error(
val title: String? = null,
val error: SolwaveErrors,
val closeWebView: Boolean = false,
) : SolwaveEvents()

data class TransactionFailed(val error: SolwaveErrors) : SolwaveEvents()

data class SignMessageUsingWallet(
val message: String,
val openDeepLink: () -> Unit = {},
) : SolwaveEvents()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.saganize.solwave.core.models

enum class DisplayFormat(val format: String) {
UTF8("utf8"),
HEX("hex"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package com.saganize.solwave.core.models
enum class EventKeys(val key: String) {
START("start"),
API_KEY("apiKey"),
MESSAGE("message"),
TRANSACTION("transaction"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sealed class Screens {
object SignupScreen : Screens()
object WalletScreen : Screens()
data class PayScreen(val transactionParams: TransactionParams) : Screens()
data class SignMessageScreen(val message: String) : Screens()
object NoFundsScreen : Screens()
object TransactionDoneScreen : Screens()
object TransactionFailedScreen : Screens()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.saganize.solwave.core.models

import com.saganize.solwave.Solwave

enum class SolwaveErrors(var message: String) {
NoInternetConnectionMessage(NO_INTERNET),
GenericErrorMsg(GENERIC_ERROR),
Expand All @@ -10,10 +12,13 @@ enum class SolwaveErrors(var message: String) {
InitLoginErrorMessage(INIT_LOGIN_USER_ERROR),
WebviewErrorMessage(WEBVIEW_ERROR),
InitiateTransactionErrorMessage(INITIATE_TRANSACTION_ERROR),
InitiateSignMessageErrorMessage(INITIATE_SIGN_MESSAGE_ERROR),
InvalidSigningMessage(INVALID_SIGNING_MESSAGE_LENGTH),
InvalidTransactionMessage(INVALID_TRANSACTION),
NoStartEventMessage(NO_START_EVENT),
NoApiKeyPassedMessage(NO_API_KEY_PASSED),
NoTransactionPassedMessage(NO_TRANSACTION_PASSED),
NoMessagePassedMessage(NO_MESSAGE_PASSED),
NoWalletSelectedMessage(NO_WALLET_SELECTED),
;

Expand Down Expand Up @@ -43,12 +48,18 @@ const val INIT_LOGIN_USER_ERROR =
"Unable to login user at the moment. Please try again later."
const val INITIATE_TRANSACTION_ERROR =
"Unable to initiate transaction at the moment. Please try again later."
const val INITIATE_SIGN_MESSAGE_ERROR =
"Unable to initiate sign message at the moment. Please try again later."
const val INVALID_SIGNING_MESSAGE_LENGTH =
"Message length is greater than ${Solwave.SIGNING_MESSAGE_MAX_LENGTH}"
const val WEBVIEW_ERROR = GENERIC_ERROR

const val NO_START_EVENT = "No start event found. Illegal state."
const val NO_API_KEY_PASSED = "No API key passed. Please pass API key to use the library."
const val NO_TRANSACTION_PASSED =
"No transaction passed. Please pass transaction to use the function."
const val NO_MESSAGE_PASSED =
"No message passed. Please pass message to use the function."

const val INVALID_TRANSACTION = "Invalid transaction. Please pass a valid transaction."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.saganize.solwave.core.models
enum class StartEvents(val event: String) {
PAY("pay"),
SELECT("select"),
SIGN_MESSAGE("signMessage"),
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.saganize.solwave.core.util

import com.saganize.solwave.BuildConfig

object BackendEndpoints {
// RETROFIT requires trailing slash :/
const val BASE_URL: String = "***REMOVED***"
const val BASE_URL: String = BuildConfig.BASE_URL

object Auth {
const val INITIATE_LOGIN = "***REMOVED***"
const val INITIATE_CREATION = "***REMOVED***"
const val INITIATE_LOGIN = BuildConfig.INITIATE_LOGIN
const val INITIATE_CREATION = BuildConfig.INITIATE_CREATION
}

object Transaction {
const val INITIATE_TRANSACTION = "***REMOVED***"
const val SIMULATE_TRANSACTION = "***REMOVED***"
const val INITIATE_TRANSACTION = BuildConfig.INITIATE_TRANSACTION
const val SIMULATE_TRANSACTION = BuildConfig.SIMULATE_TRANSACTION
const val SIGN_MESSAGE = BuildConfig.SIGN_MESSAGE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.saganize.solwave.data.remote

import com.saganize.solwave.data.remote.model.NetworkResponse
import com.saganize.solwave.data.remote.model.requests.InitiateAuthRequest
import com.saganize.solwave.data.remote.model.requests.InitiateSignMessageRequest
import com.saganize.solwave.data.remote.model.requests.InitiateTransactionRequest
import com.saganize.solwave.data.remote.model.requests.SimulateTransactionRequest
import com.saganize.solwave.data.remote.model.response.InitiateAuthResponse
import com.saganize.solwave.data.remote.model.response.InitiateSignMessageResponse
import com.saganize.solwave.data.remote.model.response.InitiateTransactionResponse
import com.saganize.solwave.data.remote.model.response.SimulateTransactionResponse
import com.saganize.solwave.domain.repository.ApiRepository
Expand Down Expand Up @@ -46,4 +48,12 @@ class ApiRepositoryImpl(
api = apiKey,
)
}

override suspend fun initiateSignMessage(requestBody: InitiateSignMessageRequest):
Response<NetworkResponse<List<InitiateSignMessageResponse>>> {
return api.initiateSignMessage(
requestBody = requestBody,
api = apiKey,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import androidx.annotation.Keep
import com.saganize.solwave.core.util.BackendEndpoints
import com.saganize.solwave.data.remote.model.NetworkResponse
import com.saganize.solwave.data.remote.model.requests.InitiateAuthRequest
import com.saganize.solwave.data.remote.model.requests.InitiateSignMessageRequest
import com.saganize.solwave.data.remote.model.requests.InitiateTransactionRequest
import com.saganize.solwave.data.remote.model.requests.SimulateTransactionRequest
import com.saganize.solwave.data.remote.model.response.InitiateAuthResponse
import com.saganize.solwave.data.remote.model.response.InitiateSignMessageResponse
import com.saganize.solwave.data.remote.model.response.InitiateTransactionResponse
import com.saganize.solwave.data.remote.model.response.SimulateTransactionResponse
import retrofit2.Response
Expand Down Expand Up @@ -40,4 +42,10 @@ interface SolwaveAPI {
@Header("api") api: String,
@Body requestBody: SimulateTransactionRequest,
): Response<NetworkResponse<List<SimulateTransactionResponse>>>

@POST(BackendEndpoints.Transaction.SIGN_MESSAGE)
suspend fun initiateSignMessage(
@Header("api") api: String,
@Body requestBody: InitiateSignMessageRequest,
): Response<NetworkResponse<List<InitiateSignMessageResponse>>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.saganize.solwave.data.remote.model.requests

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class InitiateSignMessageRequest(
@SerialName("publicKey")
val publicKey: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.saganize.solwave.data.remote.model.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class InitiateSignMessageResponse(
@SerialName("idempotencyId")
val idempotencyId: String,
@SerialName("authToken")
val authToken: String,
@SerialName("rsaPublicKey")
val rsaPublicKey: String,
@SerialName("url")
val url: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.saganize.solwave.domain.repository

import com.saganize.solwave.data.remote.model.NetworkResponse
import com.saganize.solwave.data.remote.model.requests.InitiateAuthRequest
import com.saganize.solwave.data.remote.model.requests.InitiateSignMessageRequest
import com.saganize.solwave.data.remote.model.requests.InitiateTransactionRequest
import com.saganize.solwave.data.remote.model.requests.SimulateTransactionRequest
import com.saganize.solwave.data.remote.model.response.InitiateAuthResponse
import com.saganize.solwave.data.remote.model.response.InitiateSignMessageResponse
import com.saganize.solwave.data.remote.model.response.InitiateTransactionResponse
import com.saganize.solwave.data.remote.model.response.SimulateTransactionResponse
import retrofit2.Response
Expand All @@ -22,4 +24,7 @@ interface ApiRepository {

suspend fun simulateTransaction(@Body requestBody: SimulateTransactionRequest):
Response<NetworkResponse<List<SimulateTransactionResponse>>>

suspend fun initiateSignMessage(@Body requestBody: InitiateSignMessageRequest):
Response<NetworkResponse<List<InitiateSignMessageResponse>>>
}
Loading

0 comments on commit 0a5c4ef

Please sign in to comment.