Skip to main content

Export Key


Users can at any time request to "export" their keys into a private key (EOA) and stop using an MPC wallet.

warning

Once the private key is exported, the guarantees of MPC are lost.

info

Since this operation is a sensitive operations, we don't expose it directly in the SDK. Instead we provide an endpoint in the cloud node /v3/export-key that can be used to get the encrypted share of the cloud node. We provide a method in the client SDK to combine the client and server shares to recover the full private key.

It's the responsibility of the company using the SDK to expose this endpoint to the user in a secure way.

For e.g:

  • When the user requests to export the private key. The backend can request additional authentication from the user (e.g: 2FA) before exporting the private key.
  • Once the user is authenticated, the backend can get the encrypted share from the cloud node and pass it to the user.
  • The user can decrypt the share and recover the full private key using duoSession.export().

Get the encrypted share of the cloud node by making a POST request to the /v3/export-key endpoint.

warning

The /v3/export-key endpoint is not exposed directly in the SDK. Here for the sake of simplicity, we will use the getExportData method to make the request from the client. Do NOT expose this endpoint to the user directly in a production application.


Step 1 : Create Session


Step 2 : Generate Encryption key-pair


  • Generate Encryption/Decryption key-pair to export private-key encrypted with generated key-pair by calling generateEncryptionDecryptionKeyPair() which returns Result of Success with Pair of ByteArray (private-public) or Failure with exception. This method is available on SilentShard.ECDSA and SilentShard.EdDSA object.

    // In case of ECDSA
    val privatePublicKeyPair = SilentShard.ECDSA.generateEncryptionDecryptionKeyPair()
    // In case of EdDSA
    val privatePublicKeyPair = SilentShard.EdDSA.generateEncryptionDecryptionKeyPair()
    // Private Key
    privatePublicKeyPair.first
    // Public Key
    privatePublicKeyPair.second

Step 3 : Perform network request to the server


  • Get the encrypted share of the cloud node by making a POST request to the /v3/export-key endpoint with :
  1. key_id : You can get UrlSafeBase64 encoded key_id string by using :

    SilentShard.EdDSA.getKeyshareKeyId<String>(keyshare = keyshare, resultType = SilentShard.ResultType.UrlSafeBase64)
  2. public_key : second element from the pair generated in step 2

Step 4 : Collect all the parameters


We need four things to perform export. Now, let's collect them all.

  1. hostKeyshare: ByteArray : Keyshare to export.
  2. otherEncryptedKeyshare: ByteArray : We parse the response from the network request we do in step 3 and get the server's encrypted keyshare(base64 encoded) convert it to ByteArray
    exportResponse.encServerShare.decodeBase64Bytes()
  3. hostEncryptionKey: ByteArray : first element from the pair generated in step 2
  4. otherDecryptionKey: ByteArray : We parse the response from the network request we do in step 3 and get the server's public key to decrypt the server's encrypted keyshare and then convert it to ByteArray
    exportResponse.serverPublicKey.map { it.toByte() }.toByteArray()

Step 5 : Perform Export


  • Perform export-key post-request

  • Call duoSession.export() with all the required params (as explained above and in the following example) which returns Result of Success with Keyshare ByteArray or Failure with exception.

Example


Example.kt
suspend fun exportPrivateKey(keyshare: ByteArray, duoSession: DuoSession): ByteArray {
return withContext(Dispatchers.IO) {
//Generate Encryption KeyPair to export private-key encrypted with this pair's private-key.
//It is a pair. The First item of the pair is the private key, and the second element is public key.
val hostEncryptionKeyPair = SilentShard.ECDSA.generateEncryptionDecryptionKeyPair()

//Get Other party data from network
val exportResponse = getExportData(
url = buildString {
append(websocketConfig.postRequestUrl)
append("/v3")
append("/export-key")
},
keyId = SilentShard.ECDSA.getKeyshareKeyId<String>(
keyshare = keyshare, resultType = SilentShard.ResultType.UrlSafeBase64
).getOrThrow(),
publicKey = hostEncryptionKeyPair.getOrThrow().second
)
val serverEncryptedKeyshare = exportResponse.encServerShare.decodeBase64Bytes()
val serverPublicKey = exportResponse.serverPublicKey.map { it.toByte() }.toByteArray()

//Perform Private-Key export
duoSession.export(
hostKeyshare = keyshare,
otherEncryptedKeyshare = serverEncryptedKeyshare,
hostEncryptionKey = hostEncryptionKeyPair.getOrThrow().first,
otherDecryptionKey = serverPublicKey
).getOrThrow()
}
}

private fun getExportData(url: String, keyId: ByteArray, publicKey: ByteArray) = ExportRequest(
keyId = Base64.UrlSafe.encode(keyId), clientEncPubKey = Base64.encode(publicKey)
).let {
json.encodeToString(it)
}.let { request: String ->
sendPostRequest(
url, null, request
).decodeToString().let { response: String ->
json.decodeFromString<ExportResponse>(response)
}
}

private data class ExportRequest(
@SerialName("key_id") val keyId: String,
@SerialName("client_enc_pubkey") val clientEncPubKey: String,
)

private data class ExportResponse(
@SerialName("key_id") val keyId: String,
@SerialName("server_public_key") val serverPublicKey: List<Int>,
@SerialName("enc_server_share") val encServerShare: String,
)

private fun sendPostRequest(url: String, authHeader: String?, body: Any): ByteArray {
return runBlocking {
val response: HttpResponse = client.post(url) {
headers {
authHeader?.let {
header(HttpHeaders.Authorization, "Bearer $it")
}
}
when (body) {
is String -> {
contentType(ContentType.Application.Json)
}

is ByteArray -> {
contentType(ContentType.Application.OctetStream)
}
}
setBody(body)
}
response.readRawBytes()
}
}
  • The request body format is documented in the export-key endpoint.
  • key_id: The key ID of the keyshare. Unique identifier for a wallet, 32 byte base64Url encoded string.
  • client_enc_pubkey: The encryption public key of the client. Base64 encoded string.
  • ExportRequest: The request params for the export-key endpoint.
  • ExportResponse: The response from the export-key endpoint. It contains the encrypted share of the cloud node.

  • hostKeyshare: The client's share of the MPC wallet.
  • hostEncryptionKey: The client's encryption private key.
  • otherDecryptionKey: The server's encryption public key. (This is included in the response from the export-key endpoint)
  • otherEncryptedKeyshare: The encrypted server share. (This is included in the response from the export-key endpoint)