@solana-mobile/seed-vault-lib 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +12 -12
- package/README.md +10 -2
- package/android/gradlew.bat +89 -89
- package/android/src/main/AndroidManifest.xml +1 -1
- package/android/src/main/java/com/solanamobile/seedvault/model/SigningRequest.kt +44 -44
- package/android/src/main/java/com/solanamobile/seedvault/reactnative/Extensions.kt +98 -98
- package/android/src/main/java/com/solanamobile/seedvault/reactnative/SeedVaultLibReactNativePackage.kt +15 -15
- package/android/src/main/java/com/solanamobile/seedvault/reactnative/SerializationUtils.kt +80 -80
- package/android/src/main/java/com/solanamobile/seedvault/reactnative/SolanaMobileSeedVaultLibModule.kt +34 -13
- package/lib/index.d.ts +162 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +61 -0
- package/lib/index.js.map +1 -0
- package/package.json +38 -43
- package/src/index.ts +3 -0
- package/src/seedVaultEvent.ts +111 -0
- package/src/types.ts +113 -0
- package/src/useSeedVault.ts +100 -0
- package/lib/esm/index.js +0 -116
- package/lib/esm/index.native.js +0 -116
- package/lib/esm/package.json +0 -3
- package/lib/types/index.d.ts +0 -172
- package/lib/types/index.d.ts.map +0 -1
- package/lib/types/index.native.d.ts +0 -172
- package/lib/types/index.native.d.ts.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
Copyright 2022 Solana Mobile Inc.
|
|
2
|
-
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
|
|
7
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
|
|
9
|
-
Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
See the License for the specific language governing permissions and
|
|
1
|
+
Copyright 2022 Solana Mobile Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
13
|
limitations under the License.
|
package/README.md
CHANGED
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
A React Native wrapper of the Seed Vault SDK.
|
|
4
4
|
|
|
5
|
+
## Community Maintained
|
|
6
|
+
|
|
7
|
+
This React Native library is a community-maintained wrapper around the [Seed Vault SDK](../../../README.md). It is not actively maintained or production-tested by Solana Mobile.
|
|
8
|
+
|
|
9
|
+
Contributions are welcome and encouraged! If you encounter bugs or need new features, please open a PR — this library improves when the community that depends on it helps build it.
|
|
10
|
+
|
|
11
|
+
If you need a production-grade integration, we recommend using the native Java/Kotlin Seed Vault SDK directly.
|
|
12
|
+
|
|
5
13
|
# Usage
|
|
6
14
|
|
|
7
15
|
### Check if Seed Vault is Available
|
|
8
|
-
We first need to check if the seed vault service is available on the device. Currently only
|
|
16
|
+
We first need to check if the seed vault service is available on the device. Currently only devices from Solana Mobile implement a seed vault.
|
|
9
17
|
```javascript
|
|
10
18
|
const allowSimulated = false; // use true to allow simulated seed vault (for dev/testing)
|
|
11
19
|
const seedVaultAvailable = await SolanaMobileSeedVaultLib.isSeedVaultAvailable(allowSimulated);
|
|
@@ -39,7 +47,7 @@ if (permissionResult === PermissionsAndroid.RESULTS.GRANTED) {
|
|
|
39
47
|
}
|
|
40
48
|
```
|
|
41
49
|
|
|
42
|
-
Read more about requesting Android Permission in React
|
|
50
|
+
Read more about requesting Android Permission in React Native [here](https://reactnative.dev/docs/permissionsandroid).
|
|
43
51
|
|
|
44
52
|
### Authorize a Seed
|
|
45
53
|
Before our app can access any seeds in the seed vault, we must first request authorization for our app to use a seed from the user.
|
package/android/gradlew.bat
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
@rem
|
|
2
|
-
@rem Copyright 2015 the original author or authors.
|
|
3
|
-
@rem
|
|
4
|
-
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
@rem you may not use this file except in compliance with the License.
|
|
6
|
-
@rem You may obtain a copy of the License at
|
|
7
|
-
@rem
|
|
8
|
-
@rem https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
@rem
|
|
10
|
-
@rem Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
@rem See the License for the specific language governing permissions and
|
|
14
|
-
@rem limitations under the License.
|
|
15
|
-
@rem
|
|
16
|
-
|
|
17
|
-
@if "%DEBUG%" == "" @echo off
|
|
18
|
-
@rem ##########################################################################
|
|
19
|
-
@rem
|
|
20
|
-
@rem Gradle startup script for Windows
|
|
21
|
-
@rem
|
|
22
|
-
@rem ##########################################################################
|
|
23
|
-
|
|
24
|
-
@rem Set local scope for the variables with windows NT shell
|
|
25
|
-
if "%OS%"=="Windows_NT" setlocal
|
|
26
|
-
|
|
27
|
-
set DIRNAME=%~dp0
|
|
28
|
-
if "%DIRNAME%" == "" set DIRNAME=.
|
|
29
|
-
set APP_BASE_NAME=%~n0
|
|
30
|
-
set APP_HOME=%DIRNAME%
|
|
31
|
-
|
|
32
|
-
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
|
33
|
-
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
|
34
|
-
|
|
35
|
-
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
36
|
-
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
|
37
|
-
|
|
38
|
-
@rem Find java.exe
|
|
39
|
-
if defined JAVA_HOME goto findJavaFromJavaHome
|
|
40
|
-
|
|
41
|
-
set JAVA_EXE=java.exe
|
|
42
|
-
%JAVA_EXE% -version >NUL 2>&1
|
|
43
|
-
if "%ERRORLEVEL%" == "0" goto execute
|
|
44
|
-
|
|
45
|
-
echo.
|
|
46
|
-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
47
|
-
echo.
|
|
48
|
-
echo Please set the JAVA_HOME variable in your environment to match the
|
|
49
|
-
echo location of your Java installation.
|
|
50
|
-
|
|
51
|
-
goto fail
|
|
52
|
-
|
|
53
|
-
:findJavaFromJavaHome
|
|
54
|
-
set JAVA_HOME=%JAVA_HOME:"=%
|
|
55
|
-
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|
56
|
-
|
|
57
|
-
if exist "%JAVA_EXE%" goto execute
|
|
58
|
-
|
|
59
|
-
echo.
|
|
60
|
-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
|
61
|
-
echo.
|
|
62
|
-
echo Please set the JAVA_HOME variable in your environment to match the
|
|
63
|
-
echo location of your Java installation.
|
|
64
|
-
|
|
65
|
-
goto fail
|
|
66
|
-
|
|
67
|
-
:execute
|
|
68
|
-
@rem Setup the command line
|
|
69
|
-
|
|
70
|
-
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
@rem Execute Gradle
|
|
74
|
-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
|
75
|
-
|
|
76
|
-
:end
|
|
77
|
-
@rem End local scope for the variables with windows NT shell
|
|
78
|
-
if "%ERRORLEVEL%"=="0" goto mainEnd
|
|
79
|
-
|
|
80
|
-
:fail
|
|
81
|
-
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
|
82
|
-
rem the _cmd.exe /c_ return code!
|
|
83
|
-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
|
84
|
-
exit /b 1
|
|
85
|
-
|
|
86
|
-
:mainEnd
|
|
87
|
-
if "%OS%"=="Windows_NT" endlocal
|
|
88
|
-
|
|
89
|
-
:omega
|
|
1
|
+
@rem
|
|
2
|
+
@rem Copyright 2015 the original author or authors.
|
|
3
|
+
@rem
|
|
4
|
+
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
@rem you may not use this file except in compliance with the License.
|
|
6
|
+
@rem You may obtain a copy of the License at
|
|
7
|
+
@rem
|
|
8
|
+
@rem https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
@rem
|
|
10
|
+
@rem Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
@rem See the License for the specific language governing permissions and
|
|
14
|
+
@rem limitations under the License.
|
|
15
|
+
@rem
|
|
16
|
+
|
|
17
|
+
@if "%DEBUG%" == "" @echo off
|
|
18
|
+
@rem ##########################################################################
|
|
19
|
+
@rem
|
|
20
|
+
@rem Gradle startup script for Windows
|
|
21
|
+
@rem
|
|
22
|
+
@rem ##########################################################################
|
|
23
|
+
|
|
24
|
+
@rem Set local scope for the variables with windows NT shell
|
|
25
|
+
if "%OS%"=="Windows_NT" setlocal
|
|
26
|
+
|
|
27
|
+
set DIRNAME=%~dp0
|
|
28
|
+
if "%DIRNAME%" == "" set DIRNAME=.
|
|
29
|
+
set APP_BASE_NAME=%~n0
|
|
30
|
+
set APP_HOME=%DIRNAME%
|
|
31
|
+
|
|
32
|
+
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
|
33
|
+
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
|
34
|
+
|
|
35
|
+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
36
|
+
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
|
37
|
+
|
|
38
|
+
@rem Find java.exe
|
|
39
|
+
if defined JAVA_HOME goto findJavaFromJavaHome
|
|
40
|
+
|
|
41
|
+
set JAVA_EXE=java.exe
|
|
42
|
+
%JAVA_EXE% -version >NUL 2>&1
|
|
43
|
+
if "%ERRORLEVEL%" == "0" goto execute
|
|
44
|
+
|
|
45
|
+
echo.
|
|
46
|
+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
47
|
+
echo.
|
|
48
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
|
49
|
+
echo location of your Java installation.
|
|
50
|
+
|
|
51
|
+
goto fail
|
|
52
|
+
|
|
53
|
+
:findJavaFromJavaHome
|
|
54
|
+
set JAVA_HOME=%JAVA_HOME:"=%
|
|
55
|
+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|
56
|
+
|
|
57
|
+
if exist "%JAVA_EXE%" goto execute
|
|
58
|
+
|
|
59
|
+
echo.
|
|
60
|
+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
|
61
|
+
echo.
|
|
62
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
|
63
|
+
echo location of your Java installation.
|
|
64
|
+
|
|
65
|
+
goto fail
|
|
66
|
+
|
|
67
|
+
:execute
|
|
68
|
+
@rem Setup the command line
|
|
69
|
+
|
|
70
|
+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@rem Execute Gradle
|
|
74
|
+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
|
75
|
+
|
|
76
|
+
:end
|
|
77
|
+
@rem End local scope for the variables with windows NT shell
|
|
78
|
+
if "%ERRORLEVEL%"=="0" goto mainEnd
|
|
79
|
+
|
|
80
|
+
:fail
|
|
81
|
+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
|
82
|
+
rem the _cmd.exe /c_ return code!
|
|
83
|
+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
|
84
|
+
exit /b 1
|
|
85
|
+
|
|
86
|
+
:mainEnd
|
|
87
|
+
if "%OS%"=="Windows_NT" endlocal
|
|
88
|
+
|
|
89
|
+
:omega
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
package com.solanamobile.seedvault.model
|
|
2
|
-
|
|
3
|
-
import android.net.Uri
|
|
4
|
-
import android.util.Base64
|
|
5
|
-
import com.solanamobile.seedvault.SigningRequest
|
|
6
|
-
import kotlinx.serialization.KSerializer
|
|
7
|
-
import kotlinx.serialization.Serializable
|
|
8
|
-
import kotlinx.serialization.builtins.serializer
|
|
9
|
-
import kotlinx.serialization.descriptors.SerialDescriptor
|
|
10
|
-
import kotlinx.serialization.encoding.Decoder
|
|
11
|
-
import kotlinx.serialization.encoding.Encoder
|
|
12
|
-
|
|
13
|
-
@Serializable
|
|
14
|
-
data class SigningRequestSurrogate(
|
|
15
|
-
val payload: String,
|
|
16
|
-
val requestedSignatures: List<@Serializable(with = UriAsStringSerializer::class) Uri>
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
object SigningRequestSerializer : KSerializer<SigningRequest> {
|
|
20
|
-
override val descriptor: SerialDescriptor = SigningRequestSurrogate.serializer().descriptor
|
|
21
|
-
|
|
22
|
-
override fun serialize(encoder: Encoder, value: SigningRequest) {
|
|
23
|
-
encoder.encodeSerializableValue(SigningRequestSurrogate.serializer(),
|
|
24
|
-
SigningRequestSurrogate(Base64.encodeToString(value.payload, Base64.NO_WRAP), value.requestedSignatures)
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
override fun deserialize(decoder: Decoder): SigningRequest =
|
|
29
|
-
decoder.decodeSerializableValue(SigningRequestSurrogate.serializer()).let {
|
|
30
|
-
val payload = Base64.decode(it.payload, Base64.DEFAULT)
|
|
31
|
-
SigningRequest(payload, ArrayList(it.requestedSignatures))
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
object UriAsStringSerializer : KSerializer<Uri> {
|
|
36
|
-
override val descriptor: SerialDescriptor
|
|
37
|
-
get() = String.serializer().descriptor
|
|
38
|
-
|
|
39
|
-
override fun serialize(encoder: Encoder, value: Uri) {
|
|
40
|
-
encoder.encodeString(value.toString())
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
override fun deserialize(decoder: Decoder): Uri =
|
|
44
|
-
Uri.parse(decoder.decodeString())
|
|
1
|
+
package com.solanamobile.seedvault.model
|
|
2
|
+
|
|
3
|
+
import android.net.Uri
|
|
4
|
+
import android.util.Base64
|
|
5
|
+
import com.solanamobile.seedvault.SigningRequest
|
|
6
|
+
import kotlinx.serialization.KSerializer
|
|
7
|
+
import kotlinx.serialization.Serializable
|
|
8
|
+
import kotlinx.serialization.builtins.serializer
|
|
9
|
+
import kotlinx.serialization.descriptors.SerialDescriptor
|
|
10
|
+
import kotlinx.serialization.encoding.Decoder
|
|
11
|
+
import kotlinx.serialization.encoding.Encoder
|
|
12
|
+
|
|
13
|
+
@Serializable
|
|
14
|
+
data class SigningRequestSurrogate(
|
|
15
|
+
val payload: String,
|
|
16
|
+
val requestedSignatures: List<@Serializable(with = UriAsStringSerializer::class) Uri>
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
object SigningRequestSerializer : KSerializer<SigningRequest> {
|
|
20
|
+
override val descriptor: SerialDescriptor = SigningRequestSurrogate.serializer().descriptor
|
|
21
|
+
|
|
22
|
+
override fun serialize(encoder: Encoder, value: SigningRequest) {
|
|
23
|
+
encoder.encodeSerializableValue(SigningRequestSurrogate.serializer(),
|
|
24
|
+
SigningRequestSurrogate(Base64.encodeToString(value.payload, Base64.NO_WRAP), value.requestedSignatures)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
override fun deserialize(decoder: Decoder): SigningRequest =
|
|
29
|
+
decoder.decodeSerializableValue(SigningRequestSurrogate.serializer()).let {
|
|
30
|
+
val payload = Base64.decode(it.payload, Base64.DEFAULT)
|
|
31
|
+
SigningRequest(payload, ArrayList(it.requestedSignatures))
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
object UriAsStringSerializer : KSerializer<Uri> {
|
|
36
|
+
override val descriptor: SerialDescriptor
|
|
37
|
+
get() = String.serializer().descriptor
|
|
38
|
+
|
|
39
|
+
override fun serialize(encoder: Encoder, value: Uri) {
|
|
40
|
+
encoder.encodeString(value.toString())
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override fun deserialize(decoder: Decoder): Uri =
|
|
44
|
+
Uri.parse(decoder.decodeString())
|
|
45
45
|
}
|
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
package com.solanamobile.seedvault.reactnative
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.bridge.*
|
|
4
|
-
import kotlinx.serialization.json.JsonArray
|
|
5
|
-
import kotlinx.serialization.json.JsonNull
|
|
6
|
-
import kotlinx.serialization.json.JsonObject
|
|
7
|
-
import kotlinx.serialization.json.JsonPrimitive
|
|
8
|
-
import kotlinx.serialization.json.add
|
|
9
|
-
import kotlinx.serialization.json.boolean
|
|
10
|
-
import kotlinx.serialization.json.booleanOrNull
|
|
11
|
-
import kotlinx.serialization.json.buildJsonArray
|
|
12
|
-
import kotlinx.serialization.json.buildJsonObject
|
|
13
|
-
import kotlinx.serialization.json.double
|
|
14
|
-
import kotlinx.serialization.json.doubleOrNull
|
|
15
|
-
import kotlinx.serialization.json.int
|
|
16
|
-
import kotlinx.serialization.json.intOrNull
|
|
17
|
-
import kotlinx.serialization.json.put
|
|
18
|
-
|
|
19
|
-
// Converts a React ReadableArray into a Kotlin ByteArray.
|
|
20
|
-
// Expects ReadableArray to be an Array of ints, where each int represents a byte.
|
|
21
|
-
internal fun ReadableArray.toByteArray(): ByteArray =
|
|
22
|
-
ByteArray(size()) { index ->
|
|
23
|
-
getInt(index).toByte()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Converts a Kotlin ByteArray into a React ReadableArray of ints.
|
|
27
|
-
internal fun ByteArray.toWritableArray(): ReadableArray =
|
|
28
|
-
Arguments.createArray().apply {
|
|
29
|
-
forEach {
|
|
30
|
-
this.pushInt(it.toInt())
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
internal fun ReadableMap.toJson(): JsonObject = buildJsonObject {
|
|
35
|
-
keySetIterator().let { iterator ->
|
|
36
|
-
while (iterator.hasNextKey()) {
|
|
37
|
-
val key = iterator.nextKey()
|
|
38
|
-
when (getType(key)) {
|
|
39
|
-
ReadableType.Array -> put(key, getArray(key)!!.toJson())
|
|
40
|
-
ReadableType.Boolean -> put(key, getBoolean(key))
|
|
41
|
-
ReadableType.Map -> put(key, getMap(key)!!.toJson())
|
|
42
|
-
ReadableType.Null -> put(key, JsonNull)
|
|
43
|
-
ReadableType.Number -> put(key, getDouble(key))
|
|
44
|
-
ReadableType.String -> put(key, getString(key))
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
internal fun ReadableArray.toJson(): JsonArray = buildJsonArray {
|
|
51
|
-
for (i in 0 until size()) {
|
|
52
|
-
when (getType(i)) {
|
|
53
|
-
ReadableType.Array -> add(getArray(i)!!.toJson())
|
|
54
|
-
ReadableType.Boolean -> add(getBoolean(i))
|
|
55
|
-
ReadableType.Map -> add(getMap(i)!!.toJson())
|
|
56
|
-
ReadableType.Null -> {}
|
|
57
|
-
ReadableType.Number -> add(getDouble(i))
|
|
58
|
-
ReadableType.String -> add(getString(i))
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
internal fun JsonObject.toReadableMap(): ReadableMap {
|
|
64
|
-
val map: WritableMap = WritableNativeMap()
|
|
65
|
-
val iterator = keys.iterator()
|
|
66
|
-
while (iterator.hasNext()) {
|
|
67
|
-
val key = iterator.next()
|
|
68
|
-
when (val value = this[key]) {
|
|
69
|
-
is JsonPrimitive -> when {
|
|
70
|
-
value.booleanOrNull != null -> map.putBoolean(key, value.boolean)
|
|
71
|
-
value.doubleOrNull != null -> map.putDouble(key, value.double)
|
|
72
|
-
value.intOrNull != null -> map.putInt(key, value.int)
|
|
73
|
-
value.isString -> map.putString(key, value.content)
|
|
74
|
-
}
|
|
75
|
-
is JsonArray -> map.putArray(key, value.toReadableArray())
|
|
76
|
-
is JsonObject -> map.putMap(key, value.toReadableMap())
|
|
77
|
-
else -> map.putString(key, value.toString())
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return map
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
internal fun JsonArray.toReadableArray(): ReadableArray {
|
|
84
|
-
val array: WritableArray = WritableNativeArray()
|
|
85
|
-
for (i in 0 until size) {
|
|
86
|
-
when (val value = this[i]) {
|
|
87
|
-
is JsonPrimitive -> when {
|
|
88
|
-
value.booleanOrNull != null -> array.pushBoolean(value.boolean)
|
|
89
|
-
value.doubleOrNull != null -> array.pushDouble(value.double)
|
|
90
|
-
value.intOrNull != null -> array.pushInt(value.int)
|
|
91
|
-
value.isString -> array.pushString(value.toString())
|
|
92
|
-
}
|
|
93
|
-
is JsonArray -> array.pushArray(value.toReadableArray())
|
|
94
|
-
is JsonObject -> array.pushMap(value.toReadableMap())
|
|
95
|
-
else -> array.pushString(value.toString())
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return array
|
|
1
|
+
package com.solanamobile.seedvault.reactnative
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.*
|
|
4
|
+
import kotlinx.serialization.json.JsonArray
|
|
5
|
+
import kotlinx.serialization.json.JsonNull
|
|
6
|
+
import kotlinx.serialization.json.JsonObject
|
|
7
|
+
import kotlinx.serialization.json.JsonPrimitive
|
|
8
|
+
import kotlinx.serialization.json.add
|
|
9
|
+
import kotlinx.serialization.json.boolean
|
|
10
|
+
import kotlinx.serialization.json.booleanOrNull
|
|
11
|
+
import kotlinx.serialization.json.buildJsonArray
|
|
12
|
+
import kotlinx.serialization.json.buildJsonObject
|
|
13
|
+
import kotlinx.serialization.json.double
|
|
14
|
+
import kotlinx.serialization.json.doubleOrNull
|
|
15
|
+
import kotlinx.serialization.json.int
|
|
16
|
+
import kotlinx.serialization.json.intOrNull
|
|
17
|
+
import kotlinx.serialization.json.put
|
|
18
|
+
|
|
19
|
+
// Converts a React ReadableArray into a Kotlin ByteArray.
|
|
20
|
+
// Expects ReadableArray to be an Array of ints, where each int represents a byte.
|
|
21
|
+
internal fun ReadableArray.toByteArray(): ByteArray =
|
|
22
|
+
ByteArray(size()) { index ->
|
|
23
|
+
getInt(index).toByte()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Converts a Kotlin ByteArray into a React ReadableArray of ints.
|
|
27
|
+
internal fun ByteArray.toWritableArray(): ReadableArray =
|
|
28
|
+
Arguments.createArray().apply {
|
|
29
|
+
forEach {
|
|
30
|
+
this.pushInt(it.toInt())
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
internal fun ReadableMap.toJson(): JsonObject = buildJsonObject {
|
|
35
|
+
keySetIterator().let { iterator ->
|
|
36
|
+
while (iterator.hasNextKey()) {
|
|
37
|
+
val key = iterator.nextKey()
|
|
38
|
+
when (getType(key)) {
|
|
39
|
+
ReadableType.Array -> put(key, getArray(key)!!.toJson())
|
|
40
|
+
ReadableType.Boolean -> put(key, getBoolean(key))
|
|
41
|
+
ReadableType.Map -> put(key, getMap(key)!!.toJson())
|
|
42
|
+
ReadableType.Null -> put(key, JsonNull)
|
|
43
|
+
ReadableType.Number -> put(key, getDouble(key))
|
|
44
|
+
ReadableType.String -> put(key, getString(key))
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
internal fun ReadableArray.toJson(): JsonArray = buildJsonArray {
|
|
51
|
+
for (i in 0 until size()) {
|
|
52
|
+
when (getType(i)) {
|
|
53
|
+
ReadableType.Array -> add(getArray(i)!!.toJson())
|
|
54
|
+
ReadableType.Boolean -> add(getBoolean(i))
|
|
55
|
+
ReadableType.Map -> add(getMap(i)!!.toJson())
|
|
56
|
+
ReadableType.Null -> {}
|
|
57
|
+
ReadableType.Number -> add(getDouble(i))
|
|
58
|
+
ReadableType.String -> add(getString(i))
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
internal fun JsonObject.toReadableMap(): ReadableMap {
|
|
64
|
+
val map: WritableMap = WritableNativeMap()
|
|
65
|
+
val iterator = keys.iterator()
|
|
66
|
+
while (iterator.hasNext()) {
|
|
67
|
+
val key = iterator.next()
|
|
68
|
+
when (val value = this[key]) {
|
|
69
|
+
is JsonPrimitive -> when {
|
|
70
|
+
value.booleanOrNull != null -> map.putBoolean(key, value.boolean)
|
|
71
|
+
value.doubleOrNull != null -> map.putDouble(key, value.double)
|
|
72
|
+
value.intOrNull != null -> map.putInt(key, value.int)
|
|
73
|
+
value.isString -> map.putString(key, value.content)
|
|
74
|
+
}
|
|
75
|
+
is JsonArray -> map.putArray(key, value.toReadableArray())
|
|
76
|
+
is JsonObject -> map.putMap(key, value.toReadableMap())
|
|
77
|
+
else -> map.putString(key, value.toString())
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return map
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
internal fun JsonArray.toReadableArray(): ReadableArray {
|
|
84
|
+
val array: WritableArray = WritableNativeArray()
|
|
85
|
+
for (i in 0 until size) {
|
|
86
|
+
when (val value = this[i]) {
|
|
87
|
+
is JsonPrimitive -> when {
|
|
88
|
+
value.booleanOrNull != null -> array.pushBoolean(value.boolean)
|
|
89
|
+
value.doubleOrNull != null -> array.pushDouble(value.double)
|
|
90
|
+
value.intOrNull != null -> array.pushInt(value.int)
|
|
91
|
+
value.isString -> array.pushString(value.toString())
|
|
92
|
+
}
|
|
93
|
+
is JsonArray -> array.pushArray(value.toReadableArray())
|
|
94
|
+
is JsonObject -> array.pushMap(value.toReadableMap())
|
|
95
|
+
else -> array.pushString(value.toString())
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return array
|
|
99
99
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
package com.solanamobile.seedvault.reactnative
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.ReactPackage
|
|
4
|
-
import com.facebook.react.bridge.NativeModule
|
|
5
|
-
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
-
import com.facebook.react.uimanager.ViewManager
|
|
7
|
-
|
|
8
|
-
class SeedVaultLibReactNativePackage : ReactPackage {
|
|
9
|
-
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
|
10
|
-
return listOf(SolanaMobileSeedVaultLibModule(reactContext))
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
|
14
|
-
return emptyList()
|
|
15
|
-
}
|
|
1
|
+
package com.solanamobile.seedvault.reactnative
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.ReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.uimanager.ViewManager
|
|
7
|
+
|
|
8
|
+
class SeedVaultLibReactNativePackage : ReactPackage {
|
|
9
|
+
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
|
10
|
+
return listOf(SolanaMobileSeedVaultLibModule(reactContext))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
|
14
|
+
return emptyList()
|
|
15
|
+
}
|
|
16
16
|
}
|