Skip to content

Commit

Permalink
handle empty sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Jan 8, 2024
1 parent 1773dee commit 8974359
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ open class ByteStringSerializer(val length: Int) : KSerializer<ByteArray> {
}

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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SolanaPublicKey>
abstract val blockhash: Blockhash
abstract val instructions: List<Instruction>

companion object {
fun from(bytes: ByteArray) = TransactionFormat.decodeFromByteArray(MessageSerializer, bytes)
}
Expand Down Expand Up @@ -82,23 +89,23 @@ sealed class Message {

@Serializable
data class LegacyMessage(
val signatureCount: UByte,
val readOnlyAccounts: UByte,
val readOnlyNonSigners: UByte,
val accounts: List<SolanaPublicKey>,
val blockhash: Blockhash,
val instructions: List<Instruction>
override val signatureCount: UByte,
override val readOnlyAccounts: UByte,
override val readOnlyNonSigners: UByte,
override val accounts: List<SolanaPublicKey>,
override val blockhash: Blockhash,
override val instructions: List<Instruction>
) : Message()

@Serializable
data class VersionedMessage(
@Transient val version: Byte = 0,
val signatureCount: UByte,
val readOnlyAccounts: UByte,
val readOnlyNonSigners: UByte,
val accounts: List<SolanaPublicKey>,
val blockhash: Blockhash,
val instructions: List<Instruction>,
override val signatureCount: UByte,
override val readOnlyAccounts: UByte,
override val readOnlyNonSigners: UByte,
override val accounts: List<SolanaPublicKey>,
override val blockhash: Blockhash,
override val instructions: List<Instruction>,
val addressTableLookups: List<AddressTableLookup>
) : Message()

Expand Down Expand Up @@ -143,4 +150,6 @@ object MessageSerializer : KSerializer<Message> {
is VersionedMessage -> encoder.encodeSerializableValue(VersionedMessage.serializer(), value)
}
}
}
}

fun Message.toUnsignedTransaction(): Transaction = Transaction(this)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8974359

Please sign in to comment.