Skip to content

Commit

Permalink
Merge pull request #9 from solana-mobile/grrrrrrr
Browse files Browse the repository at this point in the history
fix list build + tests
  • Loading branch information
Funkatronics authored Jan 12, 2024
2 parents 78237e6 + e31f5fb commit 1261cca
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class Transaction(
) {

constructor(message: Message):
this(buildList(message.signatureCount.toInt()) { ByteArray(SIGNATURE_LENGTH_BYTES) }, message)
this(List(message.signatureCount.toInt()) { ByteArray(SIGNATURE_LENGTH_BYTES) }, message)

companion object {
const val SIGNATURE_LENGTH_BYTES = 64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.solana.publickey.SolanaPublicKey
import com.solana.util.asVarint
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class MessageTests {

Expand Down Expand Up @@ -83,4 +84,69 @@ class MessageTests {
// then
assertContentEquals(memoInstructionTemplate, serializedMessage)
}

@Test
fun testMessageToUnsignedTransaction() {
// given
val account = SolanaPublicKey(Base64.decode("XJy50755nz75BGthIrxe7XIQ9WkcMxgIOCmqEM30qq4"))
val programId = SolanaPublicKey.from("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr")
val blockhash = Blockhash(ByteArray(32))
val data = "hello world ".encodeToByteArray()
val expectedSignatureCount = 1
val expectedSignature = ByteArray(Transaction.SIGNATURE_LENGTH_BYTES)

val memoInstruction = TransactionInstruction(
programId,
listOf(AccountMeta(account, true, true)),
data
)

// when
val transaction = Message.Builder()
.addInstruction(memoInstruction)
.setRecentBlockhash(blockhash)
.build()
.toUnsignedTransaction()

// then
assertEquals(expectedSignatureCount, transaction.signatures.size)
assertContentEquals(expectedSignature, transaction.signatures.first())
}

@Test
fun testMessageToUnsignedTransactionMultipleSignatures() {
// given
val account1 = SolanaPublicKey(Base64.decode("XJy40744nz74BGthIrxe7XIQ9WkcMxgIOCmqEM30qq4"))
val account2 = SolanaPublicKey(Base64.decode("YJy50755nz75BGthIrxe7XIQ9WkcMxgIOCmqEM30qq5"))
val programId = SolanaPublicKey.from("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr")
val blockhash = Blockhash(ByteArray(32))
val data = "hello world ".encodeToByteArray()
val expectedSignatureCount = 2
val expectedSignature = ByteArray(Transaction.SIGNATURE_LENGTH_BYTES)

val memoInstruction1 = TransactionInstruction(
programId,
listOf(AccountMeta(account1, true, true)),
data
)

val memoInstruction2 = TransactionInstruction(
programId,
listOf(AccountMeta(account2, true, true)),
data
)

// when
val transaction = Message.Builder()
.addInstruction(memoInstruction1)
.addInstruction(memoInstruction2)
.setRecentBlockhash(blockhash)
.build()
.toUnsignedTransaction()

// then
assertEquals(expectedSignatureCount, transaction.signatures.size)
assertContentEquals(expectedSignature, transaction.signatures[0])
assertContentEquals(expectedSignature, transaction.signatures[1])
}
}

0 comments on commit 1261cca

Please sign in to comment.