From 05907ae908cc6b1da3b150ca828c4b251ec53ee1 Mon Sep 17 00:00:00 2001 From: Guillermo Orellana Date: Mon, 18 Mar 2024 17:41:55 +0100 Subject: [PATCH] add nsdata utility functions --- .../solana/domain/core/Transaction.kt | 4 +-- .../solana/domain/core/Transaction.ios.kt | 7 +++++ tweetnacl-multiplatform/build.gradle.kts | 2 +- .../avianlabs/solana/tweetnacl/TweetNaCl.kt | 1 + .../solana/tweetnacl/ed25519/NSData.ios.kt | 28 +++++++++++++++++++ .../solana/tweetnacl/ed25519/PublicKey.ios.kt | 24 ++-------------- 6 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 solana-kotlin/src/iosMain/kotlin/net/avianlabs/solana/domain/core/Transaction.ios.kt create mode 100644 tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/NSData.ios.kt diff --git a/solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/Transaction.kt b/solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/Transaction.kt index 394f72d..1da293c 100644 --- a/solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/Transaction.kt +++ b/solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/Transaction.kt @@ -53,7 +53,7 @@ public class Transaction( val signaturesSize = signatures.size val signaturesLength = ShortvecEncoding.encodeLength(signaturesSize) val bufferSize = - signaturesLength.size + signaturesSize * SIGNATURE_LENGTH + serializedMessage.size + signaturesLength.size + signaturesSize * TweetNaCl.Signature.SIGNATURE_BYTES + serializedMessage.size val out = Buffer() out.write(signaturesLength) for (signature in signatures) { @@ -72,5 +72,3 @@ public class Transaction( } } - -private const val SIGNATURE_LENGTH = 64 diff --git a/solana-kotlin/src/iosMain/kotlin/net/avianlabs/solana/domain/core/Transaction.ios.kt b/solana-kotlin/src/iosMain/kotlin/net/avianlabs/solana/domain/core/Transaction.ios.kt new file mode 100644 index 0000000..12e7afd --- /dev/null +++ b/solana-kotlin/src/iosMain/kotlin/net/avianlabs/solana/domain/core/Transaction.ios.kt @@ -0,0 +1,7 @@ +package net.avianlabs.solana.domain.core + +import kotlinx.cinterop.* +import net.avianlabs.solana.tweetnacl.ed25519.toNSData +import platform.Foundation.NSData + +public fun Transaction.serializeData(): NSData = serialize().toNSData() diff --git a/tweetnacl-multiplatform/build.gradle.kts b/tweetnacl-multiplatform/build.gradle.kts index fca93cb..34f4c7e 100644 --- a/tweetnacl-multiplatform/build.gradle.kts +++ b/tweetnacl-multiplatform/build.gradle.kts @@ -76,7 +76,7 @@ kotlin { val main by compilations.getting main.cinterops { - create("tweetnacl") { + create("TweetNaCl") { header(file("vendor/tweetnacl/tweetnacl.h")) packageName("net.avianlabs.solana.tweetnacl") } diff --git a/tweetnacl-multiplatform/src/commonMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt b/tweetnacl-multiplatform/src/commonMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt index 931ce9d..4337288 100644 --- a/tweetnacl-multiplatform/src/commonMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt +++ b/tweetnacl-multiplatform/src/commonMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt @@ -34,6 +34,7 @@ public interface TweetNaCl { public companion object : Signature { public const val SECRET_KEY_BYTES: Int = 64 public const val PUBLIC_KEY_BYTES: Int = 32 + public const val SIGNATURE_BYTES: Int = 64 override fun sign(message: ByteArray, secretKey: ByteArray): ByteArray = signInternal(message, secretKey) diff --git a/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/NSData.ios.kt b/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/NSData.ios.kt new file mode 100644 index 0000000..b8ba6d5 --- /dev/null +++ b/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/NSData.ios.kt @@ -0,0 +1,28 @@ +@file:OptIn( + ExperimentalForeignApi::class, + BetaInteropApi::class, +) + +package net.avianlabs.solana.tweetnacl.ed25519 + +import kotlinx.cinterop.* +import platform.Foundation.NSData +import platform.Foundation.create +import platform.posix.memcpy + +public fun NSData.toKotlinByteArray(): ByteArray = ByteArray(length.toInt()).apply { + usePinned { pinned: Pinned -> + memcpy( + __dst = pinned.addressOf(0), + __src = this@toKotlinByteArray.bytes, + __n = this@toKotlinByteArray.length, + ) + } +} + +public fun ByteArray.toNSData(): NSData = memScoped { + NSData.create( + bytes = allocArrayOf(this@toNSData), + length = size.toULong(), + ) +} \ No newline at end of file diff --git a/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/PublicKey.ios.kt b/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/PublicKey.ios.kt index 13df2e3..ea38762 100644 --- a/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/PublicKey.ios.kt +++ b/tweetnacl-multiplatform/src/iosMain/kotlin/net/avianlabs/solana/tweetnacl/ed25519/PublicKey.ios.kt @@ -1,31 +1,11 @@ -@file:OptIn( - ExperimentalForeignApi::class, - BetaInteropApi::class, -) - package net.avianlabs.solana.tweetnacl.ed25519 import kotlinx.cinterop.* import platform.Foundation.NSData -import platform.Foundation.create -import platform.posix.memcpy public fun PublicKey(data: NSData): PublicKey = PublicKey( - bytes = ByteArray(data.length.toInt()).apply { - usePinned { pinned -> - memcpy( - __dst = pinned.addressOf(0), - __src = data.bytes, - __n = data.length, - ) - } - } + bytes = data.toKotlinByteArray(), ) public val PublicKey.data: NSData - get() = memScoped { - NSData.create( - bytes = allocArrayOf(bytes), - length = bytes.size.toULong(), - ) - } + get() = bytes.toNSData()