diff --git a/core/src/commonMain/kotlin/com/funkatronics/serialization/ByteStringSerializer.kt b/core/src/commonMain/kotlin/com/funkatronics/serialization/ByteStringSerializer.kt index 951ac07..213d4a6 100644 --- a/core/src/commonMain/kotlin/com/funkatronics/serialization/ByteStringSerializer.kt +++ b/core/src/commonMain/kotlin/com/funkatronics/serialization/ByteStringSerializer.kt @@ -15,6 +15,8 @@ open class ByteStringSerializer(val length: Int) : KSerializer { } override fun serialize(encoder: Encoder, value: ByteArray) { + if (value.isEmpty()) ByteArray(length).forEach { encoder.encodeByte(it) } + check(value.size == length) { "Cannot serialize, provided byte array has incorrect size { ${value.size} }" } value.forEach { encoder.encodeByte(it) } } } \ No newline at end of file diff --git a/solana/src/commonMain/kotlin/com/funkatronics/transaction/Message.kt b/solana/src/commonMain/kotlin/com/funkatronics/transaction/Message.kt index 681b26e..66947f0 100644 --- a/solana/src/commonMain/kotlin/com/funkatronics/transaction/Message.kt +++ b/solana/src/commonMain/kotlin/com/funkatronics/transaction/Message.kt @@ -17,6 +17,13 @@ val Blockhash.blockhash get() = this.bytes sealed class Message { + abstract val signatureCount: UByte + abstract val readOnlyAccounts: UByte + abstract val readOnlyNonSigners: UByte + abstract val accounts: List + abstract val blockhash: Blockhash + abstract val instructions: List + companion object { fun from(bytes: ByteArray) = TransactionFormat.decodeFromByteArray(MessageSerializer, bytes) } @@ -82,23 +89,23 @@ sealed class Message { @Serializable data class LegacyMessage( - val signatureCount: UByte, - val readOnlyAccounts: UByte, - val readOnlyNonSigners: UByte, - val accounts: List, - val blockhash: Blockhash, - val instructions: List + override val signatureCount: UByte, + override val readOnlyAccounts: UByte, + override val readOnlyNonSigners: UByte, + override val accounts: List, + override val blockhash: Blockhash, + override val instructions: List ) : Message() @Serializable data class VersionedMessage( @Transient val version: Byte = 0, - val signatureCount: UByte, - val readOnlyAccounts: UByte, - val readOnlyNonSigners: UByte, - val accounts: List, - val blockhash: Blockhash, - val instructions: List, + override val signatureCount: UByte, + override val readOnlyAccounts: UByte, + override val readOnlyNonSigners: UByte, + override val accounts: List, + override val blockhash: Blockhash, + override val instructions: List, val addressTableLookups: List ) : Message() @@ -143,4 +150,6 @@ object MessageSerializer : KSerializer { is VersionedMessage -> encoder.encodeSerializableValue(VersionedMessage.serializer(), value) } } -} \ No newline at end of file +} + +fun Message.toUnsignedTransaction(): Transaction = Transaction(this) \ No newline at end of file diff --git a/solana/src/commonMain/kotlin/com/funkatronics/transaction/Transaction.kt b/solana/src/commonMain/kotlin/com/funkatronics/transaction/Transaction.kt index 3529032..5dd643c 100644 --- a/solana/src/commonMain/kotlin/com/funkatronics/transaction/Transaction.kt +++ b/solana/src/commonMain/kotlin/com/funkatronics/transaction/Transaction.kt @@ -12,6 +12,8 @@ data class Transaction( @Serializable(with = MessageSerializer::class) val message: Message ) { + constructor(message: Message): this(buildList(message.signatureCount.toInt()) { ByteArray(size) }, message) + companion object { fun from(bytes: ByteArray) = TransactionFormat.decodeFromByteArray(serializer(), bytes) }