Updated on 2026-08-14

This commit is contained in:
Tangem 2020-02-05 19:07:31 +03:00
parent 7c70545ae7
commit eb468efb99
9 changed files with 116 additions and 23 deletions

View file

@ -0,0 +1,56 @@
package com.tangem.tangem_sdk_new
import android.app.Application
import android.content.SharedPreferences
import at.favre.lib.armadillo.Armadillo
import com.tangem.common.KeyPair
import com.tangem.common.TerminalKeysService
import com.tangem.common.extensions.hexToBytes
import com.tangem.common.extensions.toHexString
import com.tangem.crypto.CryptoUtils
/**
* Service for managing Terminal keypair, used for Linked Terminal feature.
* Needs to be provided to [com.tangem.CardManager] by calling [com.tangem.CardManager.setTerminalKeysService]
* Linked Terminal feature can be disabled manually by editing [com.tangem.Config].
* @param applicationContext is required to retrieve an instance of [SharedPreferences]
*/
class TerminalKeysStorage(applicationContext: Application): TerminalKeysService {
private val preferences: SharedPreferences by lazy {
Armadillo.create(applicationContext, PREFERENCES_KEY)
.encryptionFingerprint(applicationContext)
.build()
}
/**
* Retrieves generated keys from encrypted shared preferences if the keys exist.
* Generates new and stores them in encrypted shared preferences otherwise
*/
override fun getKeys(): KeyPair {
val privateKey = preferences.getString(TERMINAL_PRIVATE_KEY, null)?.hexToBytes()
val publicKey = preferences.getString(TERMINAL_PUBLIC_KEY, null)?.hexToBytes()
return if (privateKey == null || publicKey == null) {
generateAndSaveKeys()
} else {
KeyPair(publicKey, privateKey)
}
}
private fun generateAndSaveKeys(): KeyPair {
val privateKey = CryptoUtils.generateRandomBytes(32)
val publicKey = CryptoUtils.generatePublicKey(privateKey)
preferences.edit().putString(TERMINAL_PRIVATE_KEY, privateKey.toHexString()).apply()
preferences.edit().putString(TERMINAL_PUBLIC_KEY, publicKey.toHexString()).apply()
return KeyPair(publicKey, privateKey)
}
companion object {
const val PREFERENCES_KEY = "myPrefs"
const val TERMINAL_PRIVATE_KEY = "terminalPrivateKey"
const val TERMINAL_PUBLIC_KEY = "terminalPublicKey"
}
}

View file

@ -13,7 +13,7 @@ import android.os.Bundle
import com.tangem.Log
/**
* Helps use of NFC, leveraging Android NFC functionality.
* Helps use NFC, leveraging Android NFC functionality.
* Launches [NfcAdapter], manages it with [Activity] lifecycle,
* enables and disables Nfc Reading Mode, receives NFC [Tag].
*/