mirror of
https://github.com/status-im/keycard-connect.git
synced 2025-01-10 02:55:58 +00:00
make connection work
This commit is contained in:
parent
4fd3dc530e
commit
fc451c142f
@ -0,0 +1,40 @@
|
||||
package im.status.keycard.connect.card
|
||||
|
||||
import im.status.keycard.applet.BIP32KeyPair
|
||||
import im.status.keycard.applet.KeyPath
|
||||
import im.status.keycard.applet.KeycardCommandSet
|
||||
import im.status.keycard.io.APDUResponse
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
|
||||
class ExportKeyCommand(private val listener: Listener, private val path: String? = null, private val makeCurrent: Boolean = true, private val publicOnly: Boolean = true) : CardCommand {
|
||||
interface Listener {
|
||||
fun onResponse(keyPair: BIP32KeyPair)
|
||||
}
|
||||
|
||||
override fun run(context: CardScriptExecutor.ScriptContext): CardCommand.Result {
|
||||
try {
|
||||
var response: APDUResponse? = null
|
||||
|
||||
if (path != null) {
|
||||
val currentPath = KeyPath(context.cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_KEY_PATH).checkOK().data)
|
||||
|
||||
if (path != currentPath.toString()) {
|
||||
response = context.cmdSet.exportKey(path, makeCurrent, publicOnly).checkOK()
|
||||
}
|
||||
}
|
||||
|
||||
if (response == null) {
|
||||
response = context.cmdSet.exportCurrentKey(publicOnly).checkOK()
|
||||
}
|
||||
|
||||
listener.onResponse(BIP32KeyPair.fromTLV(response?.data))
|
||||
} catch(e: IOException) {
|
||||
return CardCommand.Result.RETRY
|
||||
} catch (e: Exception) {
|
||||
return CardCommand.Result.CANCEL
|
||||
}
|
||||
|
||||
return CardCommand.Result.OK
|
||||
}
|
||||
}
|
@ -4,3 +4,4 @@ fun scriptWithSecureChannel(): List<CardCommand> = listOf(SelectCommand(), InitC
|
||||
fun scriptWithAuthentication(): List<CardCommand> = scriptWithSecureChannel().plus(VerifyPINCommand())
|
||||
fun cardCheckupScript(): List<CardCommand> = scriptWithSecureChannel().plus(CheckMasterKeyCommand()).plus(VerifyPINCommand()).plus(LoadKeyCommand())
|
||||
|
||||
fun ByteArray.toHexString() = asUByteArray().joinToString("") { it.toString(16).padStart(2, '0') }
|
||||
|
@ -2,7 +2,11 @@ package im.status.keycard.connect.walletconnect
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import im.status.keycard.applet.BIP32KeyPair
|
||||
import im.status.keycard.connect.Registry
|
||||
import im.status.keycard.connect.card.ExportKeyCommand
|
||||
import im.status.keycard.connect.card.scriptWithAuthentication
|
||||
import im.status.keycard.connect.card.toHexString
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.OkHttpClient
|
||||
@ -11,24 +15,42 @@ import org.walletconnect.Session.Config.Companion.fromWCUri
|
||||
import org.walletconnect.impls.*
|
||||
import java.io.File
|
||||
|
||||
class SessionManager {
|
||||
class SessionManager : ExportKeyCommand.Listener {
|
||||
//TODO: Provide settings for these two
|
||||
private val bip39Path = "m/44'/60'/0'/0"
|
||||
private val chainID: Long = 1
|
||||
|
||||
private val scope = MainScope()
|
||||
private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
private val okHttpClient = OkHttpClient()
|
||||
private val sessionStore = FileWCSessionStore(File(Registry.mainActivity.filesDir, "wcSessions.json").apply { createNewFile() }, moshi)
|
||||
private var session: WCSession? = null
|
||||
|
||||
object SessionCB : Session.Callback {
|
||||
private val sessionCB = object : Session.Callback {
|
||||
override fun onStatus(status: Session.Status) {
|
||||
println(status)
|
||||
when (status) {
|
||||
is Session.Status.Error -> println("WalletConnect Error")
|
||||
is Session.Status.Approved -> println("WalletConnect Approved")
|
||||
is Session.Status.Connected -> println("WalletConnect Connected")
|
||||
is Session.Status.Disconnected -> println("WalletConnect Disconnected")
|
||||
is Session.Status.Closed -> session = null
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: Session.MethodCall) {
|
||||
println(call)
|
||||
scope.launch {
|
||||
when (call) {
|
||||
is Session.MethodCall.SessionRequest -> Registry.scriptExecutor.runScript(scriptWithAuthentication().plus(ExportKeyCommand(Registry.walletConnect, bip39Path)))
|
||||
is Session.MethodCall.SignMessage -> session?.rejectRequest(call.id, 1L, "Not implemented yet")
|
||||
is Session.MethodCall.SendTransaction -> session?.rejectRequest(call.id, 1L, "Not implemented yet")
|
||||
is Session.MethodCall.Custom -> session?.rejectRequest(call.id, 1L, "Not implemented yet")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun connect(uri: String) {
|
||||
|
||||
scope.launch {
|
||||
session = WCSession(
|
||||
fromWCUri(uri),
|
||||
@ -38,7 +60,15 @@ class SessionManager {
|
||||
Session.PeerMeta(name = "Keycard Connect")
|
||||
)
|
||||
|
||||
session?.addCallback(SessionCB)
|
||||
session?.addCallback(sessionCB)
|
||||
session?.init()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(keyPair: BIP32KeyPair) {
|
||||
scope.launch {
|
||||
val addr = "0x${keyPair.toEthereumAddress().toHexString()}"
|
||||
session?.approve(listOf(addr), chainID)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user