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

@ -40,16 +40,19 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0-beta01'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.skyfishjy.ripplebackground:library:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation "androidx.lifecycle:lifecycle-runtime:2.1.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.1.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.lifecycle:lifecycle-runtime:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"
implementation 'at.favre.lib:armadillo:0.9.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
}

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].
*/