-
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.
Add JSON serialization support for SolanaPublicKey (#30)
* add json serialization support for SolanaPublicKey * fix transaction encoding
- Loading branch information
1 parent
f0f33e8
commit e5a9205
Showing
2 changed files
with
134 additions
and
6 deletions.
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
99 changes: 99 additions & 0 deletions
99
solana/src/commonTest/kotlin/com/solana/serialization/SolanaPublicKeySerializerTests.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,99 @@ | ||
package com.solana.serialization | ||
|
||
import com.funkatronics.kborsh.Borsh | ||
import com.solana.publickey.SolanaPublicKey | ||
import com.solana.publickey.SolanaPublicKeySerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class SolanaPublicKeySerializerTests { | ||
|
||
@Test | ||
fun `Json serializes as base58 string`() { | ||
// given | ||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
|
||
// when | ||
val serialized = Json.encodeToString(SolanaPublicKeySerializer, publicKey) | ||
|
||
// then | ||
assertEquals("\"$publicKeyBase58\"", serialized) | ||
} | ||
|
||
@Test | ||
fun `Json deserializes from base58 string`() { | ||
// given | ||
@Serializable | ||
data class TestStruct(val owner: SolanaPublicKey) | ||
|
||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
val json = """ | ||
{ | ||
"owner": "$publicKeyBase58" | ||
} | ||
""".trimIndent() | ||
|
||
// when | ||
val deserialized = Json.decodeFromString<TestStruct>(json) | ||
|
||
// then | ||
assertEquals(publicKey, deserialized.owner) | ||
} | ||
|
||
@Test | ||
fun `Borsh serializes as base58 string`() { | ||
// given | ||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
|
||
// when | ||
val serialized = Borsh.encodeToByteArray(SolanaPublicKeySerializer, publicKey) | ||
|
||
// then | ||
assertContentEquals(publicKey.bytes, serialized) | ||
} | ||
|
||
@Test | ||
fun `Borsh deserializes from base58 string`() { | ||
// given | ||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
|
||
// when | ||
val deserialized = Borsh.decodeFromByteArray(SolanaPublicKeySerializer, publicKey.bytes) | ||
|
||
// then | ||
assertEquals(publicKey, deserialized) | ||
} | ||
|
||
@Test | ||
fun `Transaction encoder encodes public key bytes`() { | ||
// given | ||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
|
||
// when | ||
val serialized = TransactionFormat.encodeToByteArray(SolanaPublicKeySerializer, publicKey) | ||
|
||
// then | ||
assertContentEquals(publicKey.bytes, serialized) | ||
} | ||
|
||
@Test | ||
fun `Transaction encoder decodes public key bytes`() { | ||
// given | ||
val publicKeyBase58 = "11111111111111111111111111111111" | ||
val publicKey = SolanaPublicKey.from(publicKeyBase58) | ||
|
||
// when | ||
val deserialized = TransactionFormat.decodeFromByteArray(SolanaPublicKeySerializer, publicKey.bytes) | ||
|
||
// then | ||
assertEquals(publicKey, deserialized) | ||
} | ||
} |