Skip to content

Commit

Permalink
add nsdata utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wiyarmir committed Mar 18, 2024
1 parent e2d567d commit 05907ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -72,5 +72,3 @@ public class Transaction(
}

}

private const val SIGNATURE_LENGTH = 64
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tweetnacl-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ByteArray> ->
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(),
)
}
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 05907ae

Please sign in to comment.