import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.turnkey.core.TurnkeyContext
import com.turnkey.types.TSignTransactionBody
import com.turnkey.types.V1TransactionType
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
lifecycleScope.launch {
try {
val wallets = TurnkeyContext.wallets.value
val selectedWalletAccount = wallets?.firstOrNull()?.accounts?.firstOrNull()
?: throw Exception("No account found. Create a wallet and account before signing.")
val organizationId = TurnkeyContext.session.value?.organizationId
?: throw Exception("No session found")
val unsignedTransaction =
"0x..."; // Replace with your unsigned transaction data
val res = TurnkeyContext.client.signTransaction(
TSignTransactionBody(
organizationId = organizationId,
signWith = selectedWalletAccount.address,
unsignedTransaction = unsignedTransaction,
type = V1TransactionType.TRANSACTION_TYPE_ETHEREUM
)
)
val signedTransaction =
res.activity.result.signTransactionResult?.signedTransaction
?: throw Exception("Failed to sign transaction")
println(signedTransaction)
} catch (t: Throwable) {
println(t)
}
}
}
}
}