Skip to content

Commit

Permalink
Session management
Browse files Browse the repository at this point in the history
  • Loading branch information
elefantel committed Jul 18, 2024
1 parent 480ddde commit 9d96d1a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import java.lang.ref.WeakReference

private const val METAMASK_DEEPLINK = "https://metamask.app.link"
private const val METAMASK_BIND_DEEPLINK = "$METAMASK_DEEPLINK/bind"
private const val DEFAULT_SESSION_DURATION: Long = 30 * 24 * 3600 // 30 days default

class Ethereum (
private val context: Context,
Expand Down Expand Up @@ -58,12 +57,6 @@ class Ethereum (
communicationClient?.enableDebug = value
}

companion object {
const val ACCOUNT_KEY = "account_key"
const val CHAIN_ID_KEY = "chain_id_key"
const val SESSION_FILE = "session_file"
}

init {
updateSessionDuration()
initializeEthereumState()
Expand All @@ -72,8 +65,8 @@ class Ethereum (
private fun initializeEthereumState() {
coroutineScope.launch(Dispatchers.IO) {
try {
val account = storage.getValue(key = ACCOUNT_KEY, file = SESSION_FILE)
val chainId = storage.getValue(key = CHAIN_ID_KEY, file = SESSION_FILE)
val account = storage.getValue(key = SessionManager.SESSION_ACCOUNT_KEY, file = SessionManager.SESSION_CONFIG_FILE)
val chainId = storage.getValue(key = SessionManager.SESSION_CHAIN_ID_KEY, file = SessionManager.SESSION_CONFIG_FILE)
_ethereumState.postValue(
currentEthereumState.copy(
selectedAddress = account ?: "",
Expand All @@ -90,7 +83,7 @@ class Ethereum (
this.enableDebug = enable
}

private var sessionDuration: Long = DEFAULT_SESSION_DURATION
private var sessionDuration: Long = SessionManager.DEFAULT_SESSION_DURATION

override fun updateAccount(account: String) {
logger.log("Ethereum:: Selected account changed: $account")
Expand All @@ -101,7 +94,7 @@ class Ethereum (
)
)
if (account.isNotEmpty()) {
storage.putValue(account, key = ACCOUNT_KEY, SESSION_FILE)
storage.putValue(account, key = SessionManager.SESSION_ACCOUNT_KEY, SessionManager.SESSION_CONFIG_FILE)
}
}

Expand All @@ -114,20 +107,20 @@ class Ethereum (
)
)
if (newChainId.isNotEmpty()) {
storage.putValue(newChainId, key = CHAIN_ID_KEY, SESSION_FILE)
storage.putValue(newChainId, key = SessionManager.SESSION_CHAIN_ID_KEY, SessionManager.SESSION_CONFIG_FILE)
}
}

// Set session duration in seconds
fun updateSessionDuration(duration: Long = DEFAULT_SESSION_DURATION) = apply {
fun updateSessionDuration(duration: Long = SessionManager.DEFAULT_SESSION_DURATION) = apply {
sessionDuration = duration
communicationClient?.updateSessionDuration(duration)
}

// Clear persisted session. Subsequent MetaMask connection request will need approval
fun clearSession() {
disconnect(true)
storage.clear(SESSION_FILE)
storage.clear(SessionManager.SESSION_CONFIG_FILE)
}

fun connect(callback: ((Result) -> Unit)? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ class SessionManager(
private var sessionDuration: Long = 30 * 24 * 3600, // 30 days default
private val logger: Logger = DefaultLogger
) {
private val sessionConfigKey: String = "SESSION_CONFIG_KEY"
private val sessionConfigFile: String = "SESSION_CONFIG_FILE"

var sessionId: String = ""

var onInitialized: () -> Unit = {}
private val coroutineScope = CoroutineScope(Dispatchers.IO)

companion object {
const val SESSION_CONFIG_KEY = "SESSION_CONFIG_KEY"
const val SESSION_CONFIG_FILE = "SESSION_CONFIG_FILE"
const val SESSION_ACCOUNT_KEY = "SESSION_ACCOUNT_KEY"
const val SESSION_CHAIN_ID_KEY = "SESSION_CHAIN_ID_KEY"
const val DEFAULT_SESSION_DURATION: Long = 30 * 24 * 3600 // 30 days default
}

init {
coroutineScope.launch {
val id = getSessionConfig().sessionId
Expand All @@ -39,11 +44,11 @@ class SessionManager(

suspend fun getSessionConfig(reset: Boolean = false): SessionConfig {
if (reset) {
store.clearValue(sessionConfigKey, sessionConfigFile)
store.clearValue(SESSION_CONFIG_KEY, SESSION_CONFIG_FILE)
return makeNewSessionConfig()
}

val sessionConfigJson = store.getValue(sessionConfigKey, sessionConfigFile)
val sessionConfigJson = store.getValue(SESSION_CONFIG_KEY, SESSION_CONFIG_FILE)
?: return makeNewSessionConfig()

val type: Type = object : TypeToken<SessionConfig>() {}.type
Expand All @@ -64,19 +69,20 @@ class SessionManager(

fun saveSessionConfig(sessionConfig: SessionConfig) {
val sessionConfigJson = Gson().toJson(sessionConfig)
store.putValue(sessionConfigJson, sessionConfigKey, sessionConfigFile)
store.putValue(sessionConfigJson, SESSION_CONFIG_KEY, SESSION_CONFIG_FILE)
}

fun clearSession(onComplete: () -> Unit) {
coroutineScope.launch {
store.clearValue(sessionConfigKey, sessionConfigFile)
store.clearValue(SESSION_CONFIG_KEY, SESSION_CONFIG_FILE)
makeNewSessionConfig()
sessionId = getSessionConfig().sessionId
onComplete()
}
}

fun makeNewSessionConfig(): SessionConfig {
store.clear(SESSION_CONFIG_FILE)
val sessionId = TimeStampGenerator.timestamp()
val expiryDate = System.currentTimeMillis() + sessionDuration * 1000
val sessionConfig = SessionConfig(sessionId, expiryDate)
Expand Down

0 comments on commit 9d96d1a

Please sign in to comment.