Skip to content

Commit

Permalink
make some parameters of MsgExecuteContract + MsgInstantiateContract vars
Browse files Browse the repository at this point in the history
this is useful for dev because for example this allows you to create instances MsgInstantiateContract before storing the code. and injecting it after.

Also throw errors for null or blank codeHash or codeIds. Instead of logging warnings to prevent user error
  • Loading branch information
luca992 committed Sep 30, 2022
1 parent 3d2f94b commit 5790a20
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
5 changes: 5 additions & 0 deletions secretk/src/commonMain/kotlin/io/eqoty/secretk/types/Msg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ interface Msg<M : MsgProto> {
}

interface EncryptedMsg<M : MsgProto> : Msg<M> {

fun getMissingParameterWarning(method: String, parameter: String): String =
"""${method} was used without the "codeHash" parameter.
|This is discouraged and will result in much slower execution times for your app.""".trimMargin()

@Suppress("INAPPLICABLE_JVM_NAME")
@JvmName("toProtoNullable")
override suspend fun toProto(utils: EncryptionUtils?): ProtoMsg<M> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import io.eqoty.secretk.types.proto.MsgExecuteContractProto
import io.eqoty.secretk.types.proto.ProtoMsg
import io.eqoty.secretk.utils.EncryptionUtils
import io.eqoty.secretk.utils.addressToBytes
import io.eqoty.secretk.utils.getMissingCodeHashWarning
import io.ktor.util.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject

class MsgExecuteContract(
val sender: String,
var sender: String,
/** The contract's address */
val contractAddress: String,
var contractAddress: String,
/** The input message */
val msg: String,
/** Funds to send to the contract */
val sentFunds: List<Coin> = emptyList(),
var sentFunds: List<Coin> = emptyList(),
/** The SHA256 hash value of the contract's WASM bytecode, represented as case-insensitive 64
* character hex String.
* This is used to make sure only the contract that's being invoked can decrypt the query data.
Expand All @@ -37,26 +36,24 @@ class MsgExecuteContract(
var codeHash: String? = codeHash
set(value) {
if (!value.isNullOrBlank()) {
warnCodeHash = false
field = value.replace("0x", "").lowercase()
} else {
Logger.w { getMissingCodeHashWarning("MsgExecuteContract") }
Logger.w { getMissingParameterWarning("MsgExecuteContract", "codeHash") }
field = null
}
}

private var warnCodeHash: Boolean = true

init {
// set isn't triggered otherwise
this.codeHash = codeHash
}

override suspend fun toProto(utils: EncryptionUtils): ProtoMsg<MsgExecuteContractProto> {
if (warnCodeHash) {
Logger.w { getMissingCodeHashWarning("MsgExecuteContract") }
if (codeHash.isNullOrBlank()) {
throw RuntimeException(getMissingParameterWarning("MsgExecuteContract", "codeHash"))
}


if (msgEncrypted == null) {
// The encryption uses a random nonce
// toProto() & toAmino() are called multiple times during signing
Expand All @@ -81,8 +78,8 @@ class MsgExecuteContract(
}

override suspend fun toAmino(utils: EncryptionUtils): MsgAmino {
if (warnCodeHash) {
Logger.w { getMissingCodeHashWarning("MsgExecuteContract") }
if (codeHash.isNullOrBlank()) {
throw RuntimeException(getMissingParameterWarning("MsgExecuteContract", "codeHash"))
}

if (msgEncrypted == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package io.eqoty.secretk.types

import co.touchlab.kermit.Logger
import io.eqoty.secretk.types.proto.MsgExecuteContractProto
import io.eqoty.secretk.types.proto.MsgInstantiateContractProto
import io.eqoty.secretk.types.proto.ProtoMsg
import io.eqoty.secretk.utils.EncryptionUtils
import io.eqoty.secretk.utils.addressToBytes
import io.eqoty.secretk.utils.getMissingCodeHashWarning
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject

class MsgInstantiateContract(
val sender: String,
var sender: String,
/** The id of the contract's WASM code */
val codeId: Int,
var codeId: Int?,
/** A unique label across all contracts */
val label: String,
var label: String,
/** The input message to the contract's constructor */
val initMsg: String,
/** Funds to send to the contract */
val initFunds: List<Coin> = emptyList(),
var initFunds: List<Coin> = emptyList(),
/** The SHA256 hash value of the contract's WASM bytecode, represented as case-insensitive 64
* character hex string.
* This is used to make sure only the contract that's being invoked can decrypt the query data.
Expand All @@ -38,24 +36,23 @@ class MsgInstantiateContract(
var codeHash: String? = codeHash
set(value) {
if (!value.isNullOrBlank()) {
warnCodeHash = false
field = value.replace("0x", "").lowercase()
} else {
Logger.w { getMissingCodeHashWarning("MsgInstantiateContract") }
field = null
}
}

private var warnCodeHash: Boolean = true

init {
// set isn't triggered otherwise
this.codeHash = codeHash
}

override suspend fun toProto(utils: EncryptionUtils): ProtoMsg<MsgInstantiateContractProto> {
if (warnCodeHash) {
Logger.w { getMissingCodeHashWarning("MsgInstantiateContract") }
if (codeHash.isNullOrBlank()) {
throw RuntimeException(getMissingParameterWarning("MsgInstantiateContract", "codeHash"))
}
if (codeId == null) {
throw RuntimeException(getMissingParameterWarning("MsgInstantiateContract", "codeId"))
}

if (initMsgEncrypted == null) {
Expand All @@ -67,7 +64,7 @@ class MsgInstantiateContract(

val msgContent = MsgInstantiateContractProto(
sender = addressToBytes(sender),
codeId = codeId,
codeId = codeId!!,
label = label,
initMsg = initMsgEncrypted!!.toByteArray(),
initFunds = initFunds.map { it.toProto() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package io.eqoty.secretk.utils
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import io.eqoty.secretk.utils.bech32.Bech32

fun getMissingCodeHashWarning(method: String): String {
return "${method} was used without the \"codeHash\" parameter. This is discouraged and will result in much slower execution times for your app."
}

fun addressToBytes(address: String): ByteArray {
return Bech32.decode(address).data
}
Expand Down

0 comments on commit 5790a20

Please sign in to comment.