-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
solana/src/commonMain/kotlin/com/solana/serialization/AnchorInstructionSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.solana.serialization | ||
|
||
import com.funkatronics.hash.Sha256 | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.serializer | ||
|
||
open class DiscriminatorSerializer<T>(val discriminator: ByteArray, serializer: KSerializer<T>) | ||
: KSerializer<T> { | ||
|
||
private val accountSerializer = serializer | ||
override val descriptor: SerialDescriptor = accountSerializer.descriptor | ||
|
||
override fun serialize(encoder: Encoder, value: T) { | ||
discriminator.forEach { encoder.encodeByte(it) } | ||
accountSerializer.serialize(encoder, value) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): T { | ||
ByteArray(discriminator.size).map { decoder.decodeByte() } | ||
return accountSerializer.deserialize(decoder) | ||
} | ||
} | ||
|
||
open class AnchorDiscriminatorSerializer<T>(namespace: String, ixName: String, | ||
serializer: KSerializer<T>) | ||
: DiscriminatorSerializer<T>(buildDiscriminator(namespace, ixName), serializer) { | ||
companion object { | ||
private fun buildDiscriminator(namespace: String, ixName: String) = | ||
Sha256.hash("$namespace:$ixName".encodeToByteArray()).sliceArray(0 until 8) | ||
} | ||
} | ||
|
||
class AnchorInstructionSerializer<T>(namespace: String, ixName: String, serializer: KSerializer<T>) | ||
: AnchorDiscriminatorSerializer<T>(namespace, ixName, serializer) { | ||
constructor(ixName: String, serializer: KSerializer<T>) : this("global", ixName, serializer) | ||
} | ||
|
||
inline fun <reified A> AnchorInstructionSerializer(namespace: String, ixName: String) = | ||
AnchorInstructionSerializer<A>(namespace, ixName, serializer()) | ||
|
||
inline fun <reified A> AnchorInstructionSerializer(ixName: String) = | ||
AnchorInstructionSerializer<A>(ixName, serializer()) |
60 changes: 60 additions & 0 deletions
60
solana/src/commonTest/kotlin/com/solana/serialization/AnchorDiscriminatorSerializerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.solana.serialization | ||
|
||
import com.funkatronics.hash.Sha256 | ||
import com.funkatronics.kborsh.Borsh | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToByteArray | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class AnchorDiscriminatorSerializerTests { | ||
|
||
@Test | ||
fun `discriminator is first 8 bytes of identifier hash`() { | ||
// given | ||
val namespace = "test" | ||
val ixName = "testInstruction" | ||
val data = "data" | ||
val expectedDiscriminator = Sha256.hash( | ||
"$namespace:$ixName".encodeToByteArray() | ||
).sliceArray(0..7) | ||
|
||
// when | ||
val serialized = Borsh.encodeToByteArray(AnchorInstructionSerializer(namespace, ixName), data) | ||
|
||
// then | ||
assertContentEquals(expectedDiscriminator, serialized.sliceArray(0..7)) | ||
} | ||
|
||
@Test | ||
fun `data is serialized after 8 byte identifier hash`() { | ||
// given | ||
val ixName = "testInstruction" | ||
val data = "data" | ||
val expectedEncodedData = Borsh.encodeToByteArray(data) | ||
|
||
// when | ||
val serialized = Borsh.encodeToByteArray(AnchorInstructionSerializer(ixName), data) | ||
|
||
// then | ||
assertContentEquals(expectedEncodedData, serialized.sliceArray(8 until serialized.size)) | ||
} | ||
|
||
@Test | ||
fun `serialize and deserialize data struct`() { | ||
// given | ||
@Serializable data class TestData(val name: String, val number: Int, val boolean: Boolean) | ||
val ixName = "testInstruction" | ||
val data = TestData("testName", 12345678, true) | ||
val expectedEncodedData = Borsh.encodeToByteArray(data) | ||
|
||
// when | ||
val serialized = Borsh.encodeToByteArray(AnchorInstructionSerializer(ixName), data) | ||
val deserialized: TestData = Borsh.decodeFromByteArray(AnchorInstructionSerializer(ixName), serialized) | ||
|
||
// then | ||
assertContentEquals(expectedEncodedData, serialized.sliceArray(8 until serialized.size)) | ||
assertEquals(data, deserialized) | ||
} | ||
} |