import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import com.google.android.material.textfield.TextInputEditText
import com.turnkey.core.TurnkeyContext
import com.turnkey.types.V1AddressFormat
import com.turnkey.types.V1Curve
import com.turnkey.types.V1PathFormat
import com.turnkey.types.V1WalletAccountParams
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private val mnemonic = MutableStateFlow<String?>(null)
private val name = MutableStateFlow<String?>(null)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mnemonicInput = findViewById<TextInputEditText>(R.id.mnemonicInput)
mnemonicInput.doOnTextChanged { text, start, before, count ->
mnemonic.value = text.toString()
}
val nameInput = findViewById<TextInputEditText>(R.id.nameInput)
nameInput.doOnTextChanged { text, start, before, count ->
name.value = text.toString()
}
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
lifecycleScope.launch {
try {
val m = mnemonic.value ?: throw Exception("No mnemonic entered") // Your user's mnemonic string from StateFlow
val walletName = name.value ?: "Wallet-${System.currentTimeMillis()}"
// Optionally define accounts to derive upon import
val accounts = listOf(
V1WalletAccountParams(
addressFormat = V1AddressFormat.ADDRESS_FORMAT_ETHEREUM,
curve = V1Curve.CURVE_SECP256K1,
path = "m/44'/60'/0'/0/0",
pathFormat = V1PathFormat.PATH_FORMAT_BIP32
),
V1WalletAccountParams(
addressFormat = V1AddressFormat.ADDRESS_FORMAT_SOLANA,
curve = V1Curve.CURVE_ED25519,
path = "m/44'/501'/0'/0'",
pathFormat = V1PathFormat.PATH_FORMAT_BIP32
)
)
TurnkeyContext.importWallet(
walletName = walletName,
mnemonic = m,
accounts = accounts
)
} catch (t: Throwable) {
println(t)
}
}
}
}
}