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.TSignRawPayloadBody
import com.turnkey.types.V1HashFunction
import com.turnkey.types.V1PayloadEncoding
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 message = "Hello, Turnkey!"
val payload = message.toByteArray().toHexString()
val response = TurnkeyContext.client.signRawPayload(
TSignRawPayloadBody(
organizationId = TurnkeyContext.session.value?.organizationId
?: throw Exception("No active session"),
signWith = selectedWalletAccount.address,
payload = payload,
encoding = V1PayloadEncoding.PAYLOAD_ENCODING_HEXADECIMAL,
hashFunction = V1HashFunction.HASH_FUNCTION_NOT_APPLICABLE
)
)
val (r, s, v) = response.activity.result.signRawPayloadResult!!
println("Message signed: r=$r, s=$s, v=$v")
} catch (e: Exception) {
println("Error signing message: ${e.message}")
}
}
}
}
}