Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RN Lib: Async-Await API #212

Merged
merged 10 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.solanamobile.seedvault.model

import android.net.Uri
import android.util.Base64
import com.solanamobile.seedvault.SigningRequest
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
Expand All @@ -11,7 +12,7 @@ import kotlinx.serialization.encoding.Encoder

@Serializable
data class SigningRequestSurrogate(
val payload: ByteArray,
val payload: String,
val requestedSignatures: List<@Serializable(with = UriAsStringSerializer::class) Uri>
)

Expand All @@ -20,13 +21,14 @@ object SigningRequestSerializer : KSerializer<SigningRequest> {

override fun serialize(encoder: Encoder, value: SigningRequest) {
encoder.encodeSerializableValue(SigningRequestSurrogate.serializer(),
SigningRequestSurrogate(value.payload, value.requestedSignatures)
SigningRequestSurrogate(Base64.encodeToString(value.payload, Base64.NO_WRAP), value.requestedSignatures)
)
}

override fun deserialize(decoder: Decoder): SigningRequest =
decoder.decodeSerializableValue(SigningRequestSurrogate.serializer()).let {
SigningRequest(it.payload, ArrayList(it.requestedSignatures))
val payload = Base64.decode(it.payload, Base64.DEFAULT)
SigningRequest(payload, ArrayList(it.requestedSignatures))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
package com.solanamobile.seedvault.reactnative

import com.facebook.react.bridge.*
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.add
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.double
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.int
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.put

// Converts a React ReadableArray into a Kotlin ByteArray.
// Expects ReadableArray to be an Array of ints, where each int represents a byte.
internal fun ReadableArray.toByteArray(): ByteArray =
ByteArray(size()) { index ->
getInt(index).toByte()
}
ByteArray(size()) { index ->
getInt(index).toByte()
}

// Converts a Kotlin ByteArray into a React ReadableArray of ints.
internal fun ByteArray.toWritableArray(): ReadableArray =
Arguments.createArray().apply {
forEach {
this.pushInt(it.toInt())
}
}
}

internal fun ReadableMap.toJson(): JsonObject = buildJsonObject {
keySetIterator().let { iterator ->
while (iterator.hasNextKey()) {
val key = iterator.nextKey()
when (getType(key)) {
ReadableType.Array -> put(key, getArray(key)!!.toJson())
ReadableType.Boolean -> put(key, getBoolean(key))
ReadableType.Map -> put(key, getMap(key)!!.toJson())
ReadableType.Null -> put(key, JsonNull)
ReadableType.Number -> put(key, getDouble(key))
ReadableType.String -> put(key, getString(key))
}
}
}
}

internal fun ReadableArray.toJson(): JsonArray = buildJsonArray {
for (i in 0 until size()) {
when (getType(i)) {
ReadableType.Array -> add(getArray(i)!!.toJson())
ReadableType.Boolean -> add(getBoolean(i))
ReadableType.Map -> add(getMap(i)!!.toJson())
ReadableType.Null -> {}
ReadableType.Number -> add(getDouble(i))
ReadableType.String -> add(getString(i))
}
}
}

internal fun JsonObject.toReadableMap(): ReadableMap {
val map: WritableMap = WritableNativeMap()
val iterator = keys.iterator()
while (iterator.hasNext()) {
val key = iterator.next()
when (val value = this[key]) {
is JsonPrimitive -> when {
value.booleanOrNull != null -> map.putBoolean(key, value.boolean)
value.doubleOrNull != null -> map.putDouble(key, value.double)
value.intOrNull != null -> map.putInt(key, value.int)
value.isString -> map.putString(key, value.content)
}
is JsonArray -> map.putArray(key, value.toReadableArray())
is JsonObject -> map.putMap(key, value.toReadableMap())
else -> map.putString(key, value.toString())
}
}
return map
}

internal fun JsonArray.toReadableArray(): ReadableArray {
val array: WritableArray = WritableNativeArray()
for (i in 0 until size) {
when (val value = this[i]) {
is JsonPrimitive -> when {
value.booleanOrNull != null -> array.pushBoolean(value.boolean)
value.doubleOrNull != null -> array.pushDouble(value.double)
value.intOrNull != null -> array.pushInt(value.int)
value.isString -> array.pushString(value.toString())
}
is JsonArray -> array.pushArray(value.toReadableArray())
is JsonObject -> array.pushMap(value.toReadableMap())
else -> array.pushString(value.toString())
}
}
return array
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.solanamobile.seedvault.reactnative

import android.net.Uri
import com.facebook.react.bridge.*
import com.solanamobile.seedvault.*
import kotlinx.serialization.Serializable

interface RNWritable {
fun toWritableMap(): WritableMap
}

data class Account(
@WalletContractV1.AccountId val id: Long,
val name: String,
val derivationPath: Uri,
val publicKeyEncoded: String
) : RNWritable {
override fun toWritableMap() = Arguments.createMap().apply {
putString("id", "$id")
putString("name", name)
putString("derivationPath", "$derivationPath")
putString("publicKeyEncoded", publicKeyEncoded)
}
}

data class Seed(
@WalletContractV1.AuthToken val authToken: Long,
val name: String,
@WalletContractV1.Purpose val purpose: Int,
// val accounts: List<Account> = listOf()
) : RNWritable {
override fun toWritableMap() = Arguments.createMap().apply {
putString("authToken", "$authToken")
putString("name", name)
putInt("purpose", purpose)
}
}

fun List<RNWritable>.toWritableArray() = Arguments.createArray().apply {
forEach { writable ->
pushMap(writable.toWritableMap())
}
}

@Serializable
sealed interface SeedVaultEvent {
sealed class SeedEvent(val authToken: Long) : SeedVaultEvent
class SeedAuthorized(authToken: Long) : SeedEvent(authToken)
class NewSeedCreated(authToken: Long) : SeedEvent(authToken)
class ExistingSeedImported(authToken: Long) : SeedEvent(authToken)

data class PayloadsSigned(val result: List<SigningResponse>) : SeedVaultEvent

data class PublicKeysEvent(val result: List<PublicKeyResponse>) : SeedVaultEvent
}

internal fun SeedVaultEvent.toWritableMap() : WritableMap = Arguments.createMap().apply {
putString("__type", this@toWritableMap::class.simpleName)
when (this@toWritableMap) {
is SeedVaultEvent.SeedEvent -> {
putString("authToken", authToken.toString())
}
is SeedVaultEvent.PayloadsSigned -> {
putArray("result", Arguments.makeNativeArray(result.map { response ->
Arguments.createMap().apply {
putArray("signatures", Arguments.makeNativeArray(response.signatures.map { it.toWritableArray() }))
putArray("resolvedDerivationPaths", Arguments.makeNativeArray(response.resolvedDerivationPaths.map { it.toString() }))
}
}))
}
is SeedVaultEvent.PublicKeysEvent -> {
putArray("result", Arguments.makeNativeArray(result.map { response ->
Arguments.createMap().apply {
putArray("publicKey", response.publicKey.toWritableArray())
putString("publicKeyEncoded", response.publicKeyEncoded)
putString("resolvedDerviationPath", response.resolvedDerivationPath.toString())
}
}))
}
}
}
Loading