Skip to content

Commit

Permalink
decrypt errors for execute and simulate
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Feb 18, 2023
1 parent ac4de61 commit 615cbaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
36 changes: 20 additions & 16 deletions secretk/src/commonMain/kotlin/io/eqoty/secretk/client/RestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,30 @@ internal class RestClient(
throw Error(responseData.message)
}
}
} catch (err: Throwable) {
val message = err.message ?: throw err
val errorMessageRgx = Regex("""encrypted: (.+?): (?:instantiate|execute|query|reply to) contract failed""")
val matches = errorMessageRgx.findAll(message).toList()
if (matches.isEmpty() || matches.first().groupValues.size < 2) {
throw err
}
val decodedError: Error = try {
val errorCipherB64 = matches.first().groupValues[1]
val errorCipherBz = errorCipherB64.decodeBase64()!!.toUByteArray()
val errorPlainBz = enigmautils.decrypt(errorCipherBz, nonce).decodeToString()
Error(errorPlainBz)
} catch (decryptionError: Throwable) {
Error("Failed to decrypt the following error message: ${err.message}. Due to decryptionError: $decryptionError")
}
throw decodedError
} catch (t: Throwable) {
throw decrypt(t, nonce)
}
return response
}

suspend fun decrypt(t: Throwable, nonce: UByteArray): Throwable {
val message = t.message ?: return t
val errorMessageRgx = Regex("""encrypted: (.+?): (?:instantiate|execute|query|reply to) contract failed""")
val matches = errorMessageRgx.findAll(message).toList()
if (matches.isEmpty() || matches.first().groupValues.size < 2) {
return t
}
val decodedError: Error = try {
val errorCipherB64 = matches.first().groupValues[1]
val errorCipherBz = errorCipherB64.decodeBase64()!!.toUByteArray()
val errorPlainBz = enigmautils.decrypt(errorCipherBz, nonce).decodeToString()
Error(errorPlainBz)
} catch (decryptionError: Throwable) {
Error("Failed to decrypt the following error message: ${t.message}. Due to decryptionError: $decryptionError")
}
return decodedError
}

suspend fun decryptDataField(msg: MsgProto, nonce: UByteArray?): UByteArray {
val dataField = when (msg) {
is MsgExecuteContractResponseProto -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ private constructor(
val simulateTxResponse = try {
postSimulateTx(txRawBytes)
} catch (t: Throwable) {
throw t
val nonces = msgs.map { msg ->
msg.populateCodeHash()
msg.toProto(this.restClient.enigmautils).value
}.map { anyProto ->
extractMessageNonceIfNeeded(anyProto)
}
throw restClient.decrypt(t, nonces[0]!!)
}
return simulateTxResponse.gasInfo!!
}
Expand All @@ -108,33 +114,16 @@ private constructor(
val txRawBytes = ProtoBuf.encodeToByteArray(txRawProto).toUByteArray()
val txResponse = try {
postTx(txRawBytes)
} catch (err: Throwable) {
// try {
// const errorMessageRgx = /failed to execute message; message index: 0: encrypted: (.+?): (?:instantiate|execute|query) contract failed/g;
// // console.log(`Got error message: ${err.message}`);
//
// const rgxMatches = errorMessageRgx.exec(err.message);
// if (rgxMatches == null || rgxMatches.length != 2) {
// throw err;
// }
//
// const errorCipherB64 = rgxMatches[1];
//
// // console.log(`Got error message: ${errorCipherB64}`);
//
// const errorCipherBz = Encoding.fromBase64(errorCipherB64);
//
// const errorPlainBz = await this.restClient.enigmautils.decrypt(errorCipherBz, encryptionNonce);
//
// err.message = err.message.replace(errorCipherB64, Encoding.fromUtf8(errorPlainBz));
// } catch (decryptionError) {
// throw new Error(
// `Failed to decrypt the following error message: ${err.message}. Decryption error of the error message: ${decryptionError.message}`,
// );
// }

throw err
} catch (t: Throwable) {
val nonces = msgs.map { msg ->
msg.populateCodeHash()
msg.toProto(this.restClient.enigmautils).value
}.map { anyProto ->
extractMessageNonceIfNeeded(anyProto)
}
throw restClient.decrypt(t, nonces[0]!!)
}

txResponse.data = if (this.restClient.broadcastMode == BroadcastMode.Block) {
// inject tx here to standardize decoding tx responses. Since txsQuery responses (not implemented yet)
// will actually have a tx value populated.
Expand Down

0 comments on commit 615cbaa

Please sign in to comment.