make network and path configurable

This commit is contained in:
Michele Balistreri 2019-11-25 13:45:13 +03:00
parent 5ce532c3c7
commit 5c05b587b6
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
10 changed files with 218 additions and 7 deletions

View File

@ -17,7 +17,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.ChangePairingPasswordActivity"></activity>
<activity android:name=".ui.SettingsActivity" />
<activity android:name=".ui.ChangePairingPasswordActivity" />
<activity android:name=".ui.ChangePUKActivity" />
<activity android:name=".ui.ShowMnemonicActivity" />
<activity android:name=".ui.LoadKeyActivity" />

View File

@ -19,6 +19,9 @@ object Registry {
lateinit var pairingManager: PairingManager
private set
lateinit var settingsManager: SettingsManager
private set
lateinit var mainActivity: Activity
private set
@ -54,6 +57,7 @@ object Registry {
pairingManager = PairingManager(activity)
pinCache = PINCache()
settingsManager = SettingsManager(activity)
nfcAdapter = NfcAdapter.getDefaultAdapter(activity)
scriptExecutor = CardScriptExecutor(listener)
@ -62,8 +66,7 @@ object Registry {
cardManager.setCardListener(scriptExecutor)
cardManager.start()
//TODO: should be configurable
ethereumRPC = EthereumRPC(RPC_ENDPOINT)
walletConnect = WalletConnect(BIP32_PATH, CHAIN_ID)
ethereumRPC = EthereumRPC(settingsManager.rpcEndpoint)
walletConnect = WalletConnect(settingsManager.bip32Path, settingsManager.chainID)
}
}

View File

@ -33,6 +33,20 @@ const val REQ_LOADKEY = 0x03
const val CACHE_VALIDITY = 15 * 60 * 1000
const val RPC_ENDPOINT = "https://goerli.infura.io/v3/27efcb33f94e4bd0866d1aadf8e1a12d"
const val CHAIN_ID = 5L
const val BIP32_PATH = "m/44'/60'/0'/0"
const val SETTINGS_CHAIN_ID = "chainID"
const val SETTINGS_BIP32_PATH = "bip32Path"
const val INFURA_API_KEY = "27efcb33f94e4bd0866d1aadf8e1a12d"
const val RPC_ENDPOINT_TEMPLATE = "https://%s.infura.io/v3/${INFURA_API_KEY}"
const val CHAIN_ID_MAINNET = 1L
const val CHAIN_ID_ROPSTEN = 3L
const val CHAIN_ID_RINKEBY = 4L
const val CHAIN_ID_GOERLI = 5L
const val CHAIN_ID_KOVAN = 42L
val CHAIN_ID_TO_SHORTNAME = mapOf(CHAIN_ID_MAINNET to "mainnet", CHAIN_ID_ROPSTEN to "ropsten", CHAIN_ID_RINKEBY to "rinkeby", CHAIN_ID_GOERLI to "goerli", CHAIN_ID_KOVAN to "kovan")
val CHAIN_IDS = listOf(CHAIN_ID_MAINNET, CHAIN_ID_ROPSTEN, CHAIN_ID_RINKEBY, CHAIN_ID_GOERLI, CHAIN_ID_KOVAN)
const val DEFAULT_CHAIN_ID = CHAIN_ID_MAINNET
const val DEFAULT_BIP32_PATH = "m/44'/60'/0'/0"

View File

@ -0,0 +1,37 @@
package im.status.keycard.connect.data
import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
class SettingsManager(context: Context) {
private val sharedPreferences: SharedPreferences
init {
/** encrypted settings used for privacy **/
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
sharedPreferences = EncryptedSharedPreferences.create("settings", masterKeyAlias, context, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)
}
val rpcEndpoint
get() = String.format(RPC_ENDPOINT_TEMPLATE, CHAIN_ID_TO_SHORTNAME.getValue(sharedPreferences.getLong(SETTINGS_CHAIN_ID, DEFAULT_CHAIN_ID)))
var chainID
get() = sharedPreferences.getLong(SETTINGS_CHAIN_ID, DEFAULT_CHAIN_ID)
set(chainID) {
sharedPreferences.edit().apply {
putLong(SETTINGS_CHAIN_ID, chainID)
apply()
}
}
var bip32Path
get() = sharedPreferences.getString(SETTINGS_BIP32_PATH, DEFAULT_BIP32_PATH)!!
set(bip32Path) {
sharedPreferences.edit().apply {
putString(SETTINGS_BIP32_PATH, bip32Path)
apply()
}
}
}

View File

@ -106,6 +106,10 @@ class MainActivity : AppCompatActivity(), ScriptListener {
Registry.scriptExecutor.runScript(scriptWithAuthentication().plus(RemoveKeyCommand()))
}
fun settings(@Suppress("UNUSED_PARAMETER") view: View) {
startCommand(SettingsActivity::class)
}
private fun loadKeyHandler(resultCode: Int, data: Intent?) {
if (resultCode != Activity.RESULT_OK || data == null) return

View File

@ -0,0 +1,49 @@
package im.status.keycard.connect.ui
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import im.status.keycard.connect.R
import im.status.keycard.connect.Registry
import im.status.keycard.connect.data.CHAIN_IDS
class SettingsActivity : AppCompatActivity() {
lateinit var networkSpinner : Spinner
lateinit var walletPath : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
networkSpinner = findViewById(R.id.networkSpinner)
ArrayAdapter.createFromResource(this, R.array.networks, android.R.layout.simple_spinner_item).also {
it.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
networkSpinner.adapter = it
}
networkSpinner.setSelection(CHAIN_IDS.indexOf(Registry.settingsManager.chainID))
walletPath = findViewById(R.id.walletPathText)
walletPath.setText(Registry.settingsManager.bip32Path)
}
fun ok(@Suppress("UNUSED_PARAMETER") view: View) {
val chainID = CHAIN_IDS[networkSpinner.selectedItemPosition]
Registry.settingsManager.chainID = chainID
Registry.ethereumRPC.changeEndpoint(Registry.settingsManager.rpcEndpoint)
Registry.walletConnect.chainID = chainID
val bip32Path = walletPath.text.toString()
Registry.settingsManager.bip32Path = bip32Path
Registry.walletConnect.bip32Path = bip32Path
finish()
}
fun cancel(@Suppress("UNUSED_PARAMETER") view: View) {
finish()
}
}

View File

@ -95,4 +95,15 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/changeKeyButton" />
<Button
android:id="@+id/settingsButton"
android:layout_width="236dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="settings"
android:text="@string/settings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/removeKey" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.SettingsActivity">
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:onClick="ok"
android:text="@android:string/ok"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/walletPathText" />
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:onClick="cancel"
android:text="@android:string/cancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/walletPathText" />
<TextView
android:id="@+id/networkLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="@string/network"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/networkSpinner"
android:layout_width="0dp"
android:layout_height="26dp"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/networkLabel" />
<TextView
android:id="@+id/walletPathLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/wallet_path_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/networkSpinner" />
<EditText
android:id="@+id/walletPathText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="@string/wallet_path_hint"
android:inputType="textPersonName"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/walletPathLabel"
tools:text="m/44'/60'/0'/0" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="networks">
<item>Ethereum Mainnet</item>
<item>Ropsten PoW Testnet</item>
<item>Rinkeby PoA Testnet</item>
<item>Görli PoA Testnet</item>
<item>Kovan PoA Testnet</item>
</string-array>
</resources>

View File

@ -33,4 +33,8 @@
<string name="remove_key">Remove key</string>
<string name="change_puk_prompt">New PUK</string>
<string name="change_pairing_password_prompt">New pairing password</string>
<string name="network">Network</string>
<string name="wallet_path_label">Wallet Path</string>
<string name="wallet_path_hint">m/44\'/...</string>
<string name="settings">Settings</string>
</resources>