@nuggetslife/vc-rn 0.1.0

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.
Files changed (137) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +31 -0
  3. package/android/build.gradle +101 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/AndroidManifestNew.xml +2 -0
  7. package/android/src/main/java/com/nuggetslife/bindings/vc/vc.kt +673 -0
  8. package/android/src/main/java/com/nuggetslife/vc/VcModule.kt +30 -0
  9. package/android/src/main/java/com/nuggetslife/vc/VcPackage.kt +17 -0
  10. package/android/src/main/jniLibs/arm64-v8a/libvc.so +0 -0
  11. package/android/src/main/jniLibs/armeabi-v7a/libvc.so +0 -0
  12. package/android/src/main/jniLibs/x86/libvc.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/libvc.so +0 -0
  14. package/ios/Vc-Bridging-Header.h +2 -0
  15. package/ios/VcModule.mm +14 -0
  16. package/ios/VcModule.swift +23 -0
  17. package/ios/vc.swift +446 -0
  18. package/ios/vc.xcframework/Info.plist +48 -0
  19. package/ios/vc.xcframework/ios-arm64/Headers/module.modulemap +6 -0
  20. package/ios/vc.xcframework/ios-arm64/Headers/vcFFI.h +174 -0
  21. package/ios/vc.xcframework/ios-arm64/libvc.a +0 -0
  22. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/Headers/module.modulemap +6 -0
  23. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/Headers/vcFFI.h +174 -0
  24. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/libvc.a +0 -0
  25. package/ios/vcFFI.h +174 -0
  26. package/lib/commonjs/Bls12381G2KeyPair.js +405 -0
  27. package/lib/commonjs/Bls12381G2KeyPair.js.map +1 -0
  28. package/lib/commonjs/index.d.js +18 -0
  29. package/lib/commonjs/index.d.js.map +1 -0
  30. package/lib/commonjs/index.js +20 -0
  31. package/lib/commonjs/index.js.map +1 -0
  32. package/lib/commonjs/types/BlsCurveName.js +14 -0
  33. package/lib/commonjs/types/BlsCurveName.js.map +1 -0
  34. package/lib/commonjs/types/GenerateKeyPairOptions.js +2 -0
  35. package/lib/commonjs/types/GenerateKeyPairOptions.js.map +1 -0
  36. package/lib/commonjs/types/JsonWebKey.js +2 -0
  37. package/lib/commonjs/types/JsonWebKey.js.map +1 -0
  38. package/lib/commonjs/types/JwkKeyPairOptions.js +6 -0
  39. package/lib/commonjs/types/JwkKeyPairOptions.js.map +1 -0
  40. package/lib/commonjs/types/JwkKty.js +13 -0
  41. package/lib/commonjs/types/JwkKty.js.map +1 -0
  42. package/lib/commonjs/types/KeyPairOptions.js +2 -0
  43. package/lib/commonjs/types/KeyPairOptions.js.map +1 -0
  44. package/lib/commonjs/types/KeyPairSigner.js +2 -0
  45. package/lib/commonjs/types/KeyPairSigner.js.map +1 -0
  46. package/lib/commonjs/types/KeyPairVerifier.js +2 -0
  47. package/lib/commonjs/types/KeyPairVerifier.js.map +1 -0
  48. package/lib/commonjs/types/index.js +20 -0
  49. package/lib/commonjs/types/index.js.map +1 -0
  50. package/lib/commonjs/utils/codec.js +22 -0
  51. package/lib/commonjs/utils/codec.js.map +1 -0
  52. package/lib/commonjs/utils/index.js +19 -0
  53. package/lib/commonjs/utils/index.js.map +1 -0
  54. package/lib/commonjs/validators/Bls12381G2Jwk.js +19 -0
  55. package/lib/commonjs/validators/Bls12381G2Jwk.js.map +1 -0
  56. package/lib/commonjs/validators/JsonWebKey.js +27 -0
  57. package/lib/commonjs/validators/JsonWebKey.js.map +1 -0
  58. package/lib/module/Bls12381G2KeyPair.js +398 -0
  59. package/lib/module/Bls12381G2KeyPair.js.map +1 -0
  60. package/lib/module/index.d.js +9 -0
  61. package/lib/module/index.d.js.map +1 -0
  62. package/lib/module/index.js +14 -0
  63. package/lib/module/index.js.map +1 -0
  64. package/lib/module/types/BlsCurveName.js +8 -0
  65. package/lib/module/types/BlsCurveName.js.map +1 -0
  66. package/lib/module/types/GenerateKeyPairOptions.js +2 -0
  67. package/lib/module/types/GenerateKeyPairOptions.js.map +1 -0
  68. package/lib/module/types/JsonWebKey.js +2 -0
  69. package/lib/module/types/JsonWebKey.js.map +1 -0
  70. package/lib/module/types/JwkKeyPairOptions.js +2 -0
  71. package/lib/module/types/JwkKeyPairOptions.js.map +1 -0
  72. package/lib/module/types/JwkKty.js +7 -0
  73. package/lib/module/types/JwkKty.js.map +1 -0
  74. package/lib/module/types/KeyPairOptions.js +2 -0
  75. package/lib/module/types/KeyPairOptions.js.map +1 -0
  76. package/lib/module/types/KeyPairSigner.js +2 -0
  77. package/lib/module/types/KeyPairSigner.js.map +1 -0
  78. package/lib/module/types/KeyPairVerifier.js +2 -0
  79. package/lib/module/types/KeyPairVerifier.js.map +1 -0
  80. package/lib/module/types/index.js +3 -0
  81. package/lib/module/types/index.js.map +1 -0
  82. package/lib/module/utils/codec.js +13 -0
  83. package/lib/module/utils/codec.js.map +1 -0
  84. package/lib/module/utils/index.js +2 -0
  85. package/lib/module/utils/index.js.map +1 -0
  86. package/lib/module/validators/Bls12381G2Jwk.js +11 -0
  87. package/lib/module/validators/Bls12381G2Jwk.js.map +1 -0
  88. package/lib/module/validators/JsonWebKey.js +19 -0
  89. package/lib/module/validators/JsonWebKey.js.map +1 -0
  90. package/lib/typescript/src/Bls12381G2KeyPair.d.ts +104 -0
  91. package/lib/typescript/src/Bls12381G2KeyPair.d.ts.map +1 -0
  92. package/lib/typescript/src/index.d.ts +2 -0
  93. package/lib/typescript/src/index.d.ts.map +1 -0
  94. package/lib/typescript/src/types/BlsCurveName.d.ts +7 -0
  95. package/lib/typescript/src/types/BlsCurveName.d.ts.map +1 -0
  96. package/lib/typescript/src/types/GenerateKeyPairOptions.d.ts +18 -0
  97. package/lib/typescript/src/types/GenerateKeyPairOptions.d.ts.map +1 -0
  98. package/lib/typescript/src/types/JsonWebKey.d.ts +60 -0
  99. package/lib/typescript/src/types/JsonWebKey.d.ts.map +1 -0
  100. package/lib/typescript/src/types/JwkKeyPairOptions.d.ts +23 -0
  101. package/lib/typescript/src/types/JwkKeyPairOptions.d.ts.map +1 -0
  102. package/lib/typescript/src/types/JwkKty.d.ts +6 -0
  103. package/lib/typescript/src/types/JwkKty.d.ts.map +1 -0
  104. package/lib/typescript/src/types/KeyPairOptions.d.ts +22 -0
  105. package/lib/typescript/src/types/KeyPairOptions.d.ts.map +1 -0
  106. package/lib/typescript/src/types/KeyPairSigner.d.ts +7 -0
  107. package/lib/typescript/src/types/KeyPairSigner.d.ts.map +1 -0
  108. package/lib/typescript/src/types/KeyPairVerifier.d.ts +8 -0
  109. package/lib/typescript/src/types/KeyPairVerifier.d.ts.map +1 -0
  110. package/lib/typescript/src/types/index.d.ts +9 -0
  111. package/lib/typescript/src/types/index.d.ts.map +1 -0
  112. package/lib/typescript/src/utils/codec.d.ts +3 -0
  113. package/lib/typescript/src/utils/codec.d.ts.map +1 -0
  114. package/lib/typescript/src/utils/index.d.ts +2 -0
  115. package/lib/typescript/src/utils/index.d.ts.map +1 -0
  116. package/lib/typescript/src/validators/Bls12381G2Jwk.d.ts +4 -0
  117. package/lib/typescript/src/validators/Bls12381G2Jwk.d.ts.map +1 -0
  118. package/lib/typescript/src/validators/JsonWebKey.d.ts +4 -0
  119. package/lib/typescript/src/validators/JsonWebKey.d.ts.map +1 -0
  120. package/nuggetslife-vc.podspec +42 -0
  121. package/package.json +164 -0
  122. package/src/Bls12381G2KeyPair.ts +447 -0
  123. package/src/index.d.ts +126 -0
  124. package/src/index.tsx +22 -0
  125. package/src/types/BlsCurveName.ts +6 -0
  126. package/src/types/GenerateKeyPairOptions.ts +17 -0
  127. package/src/types/JsonWebKey.ts +68 -0
  128. package/src/types/JwkKeyPairOptions.ts +24 -0
  129. package/src/types/JwkKty.ts +5 -0
  130. package/src/types/KeyPairOptions.ts +21 -0
  131. package/src/types/KeyPairSigner.ts +6 -0
  132. package/src/types/KeyPairVerifier.ts +8 -0
  133. package/src/types/index.ts +8 -0
  134. package/src/utils/codec.ts +12 -0
  135. package/src/utils/index.ts +1 -0
  136. package/src/validators/Bls12381G2Jwk.ts +21 -0
  137. package/src/validators/JsonWebKey.ts +28 -0
@@ -0,0 +1,673 @@
1
+ // This file was autogenerated by some hot garbage in the `uniffi` crate.
2
+ // Trust me, you don't want to mess with it!
3
+
4
+ @file:Suppress("NAME_SHADOWING")
5
+
6
+ package vc;
7
+
8
+ // Common helper code.
9
+ //
10
+ // Ideally this would live in a separate .kt file where it can be unittested etc
11
+ // in isolation, and perhaps even published as a re-useable package.
12
+ //
13
+ // However, it's important that the details of how this helper code works (e.g. the
14
+ // way that different builtin types are passed across the FFI) exactly match what's
15
+ // expected by the Rust code on the other side of the interface. In practice right
16
+ // now that means coming from the exact some version of `uniffi` that was used to
17
+ // compile the Rust component. The easiest way to ensure this is to bundle the Kotlin
18
+ // helpers directly inline like we're doing here.
19
+
20
+ import com.sun.jna.Library
21
+ import com.sun.jna.IntegerType
22
+ import com.sun.jna.Native
23
+ import com.sun.jna.Pointer
24
+ import com.sun.jna.Structure
25
+ import com.sun.jna.Callback
26
+ import com.sun.jna.ptr.*
27
+ import java.nio.ByteBuffer
28
+ import java.nio.ByteOrder
29
+ import java.nio.CharBuffer
30
+ import java.nio.charset.CodingErrorAction
31
+ import java.util.concurrent.ConcurrentHashMap
32
+
33
+ // This is a helper for safely working with byte buffers returned from the Rust code.
34
+ // A rust-owned buffer is represented by its capacity, its current length, and a
35
+ // pointer to the underlying data.
36
+
37
+ @Structure.FieldOrder("capacity", "len", "data")
38
+ open class RustBuffer : Structure() {
39
+ @JvmField var capacity: Int = 0
40
+ @JvmField var len: Int = 0
41
+ @JvmField var data: Pointer? = null
42
+
43
+ class ByValue: RustBuffer(), Structure.ByValue
44
+ class ByReference: RustBuffer(), Structure.ByReference
45
+
46
+ companion object {
47
+ internal fun alloc(size: Int = 0) = uniffiRustCall() { status ->
48
+ UniffiLib.INSTANCE.ffi_vc_rustbuffer_alloc(size, status)
49
+ }.also {
50
+ if(it.data == null) {
51
+ throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})")
52
+ }
53
+ }
54
+
55
+ internal fun create(capacity: Int, len: Int, data: Pointer?): RustBuffer.ByValue {
56
+ var buf = RustBuffer.ByValue()
57
+ buf.capacity = capacity
58
+ buf.len = len
59
+ buf.data = data
60
+ return buf
61
+ }
62
+
63
+ internal fun free(buf: RustBuffer.ByValue) = uniffiRustCall() { status ->
64
+ UniffiLib.INSTANCE.ffi_vc_rustbuffer_free(buf, status)
65
+ }
66
+ }
67
+
68
+ @Suppress("TooGenericExceptionThrown")
69
+ fun asByteBuffer() =
70
+ this.data?.getByteBuffer(0, this.len.toLong())?.also {
71
+ it.order(ByteOrder.BIG_ENDIAN)
72
+ }
73
+ }
74
+
75
+ /**
76
+ * The equivalent of the `*mut RustBuffer` type.
77
+ * Required for callbacks taking in an out pointer.
78
+ *
79
+ * Size is the sum of all values in the struct.
80
+ */
81
+ class RustBufferByReference : ByReference(16) {
82
+ /**
83
+ * Set the pointed-to `RustBuffer` to the given value.
84
+ */
85
+ fun setValue(value: RustBuffer.ByValue) {
86
+ // NOTE: The offsets are as they are in the C-like struct.
87
+ val pointer = getPointer()
88
+ pointer.setInt(0, value.capacity)
89
+ pointer.setInt(4, value.len)
90
+ pointer.setPointer(8, value.data)
91
+ }
92
+
93
+ /**
94
+ * Get a `RustBuffer.ByValue` from this reference.
95
+ */
96
+ fun getValue(): RustBuffer.ByValue {
97
+ val pointer = getPointer()
98
+ val value = RustBuffer.ByValue()
99
+ value.writeField("capacity", pointer.getInt(0))
100
+ value.writeField("len", pointer.getInt(4))
101
+ value.writeField("data", pointer.getPointer(8))
102
+
103
+ return value
104
+ }
105
+ }
106
+
107
+ // This is a helper for safely passing byte references into the rust code.
108
+ // It's not actually used at the moment, because there aren't many things that you
109
+ // can take a direct pointer to in the JVM, and if we're going to copy something
110
+ // then we might as well copy it into a `RustBuffer`. But it's here for API
111
+ // completeness.
112
+
113
+ @Structure.FieldOrder("len", "data")
114
+ open class ForeignBytes : Structure() {
115
+ @JvmField var len: Int = 0
116
+ @JvmField var data: Pointer? = null
117
+
118
+ class ByValue : ForeignBytes(), Structure.ByValue
119
+ }
120
+ // The FfiConverter interface handles converter types to and from the FFI
121
+ //
122
+ // All implementing objects should be public to support external types. When a
123
+ // type is external we need to import it's FfiConverter.
124
+ public interface FfiConverter<KotlinType, FfiType> {
125
+ // Convert an FFI type to a Kotlin type
126
+ fun lift(value: FfiType): KotlinType
127
+
128
+ // Convert an Kotlin type to an FFI type
129
+ fun lower(value: KotlinType): FfiType
130
+
131
+ // Read a Kotlin type from a `ByteBuffer`
132
+ fun read(buf: ByteBuffer): KotlinType
133
+
134
+ // Calculate bytes to allocate when creating a `RustBuffer`
135
+ //
136
+ // This must return at least as many bytes as the write() function will
137
+ // write. It can return more bytes than needed, for example when writing
138
+ // Strings we can't know the exact bytes needed until we the UTF-8
139
+ // encoding, so we pessimistically allocate the largest size possible (3
140
+ // bytes per codepoint). Allocating extra bytes is not really a big deal
141
+ // because the `RustBuffer` is short-lived.
142
+ fun allocationSize(value: KotlinType): Int
143
+
144
+ // Write a Kotlin type to a `ByteBuffer`
145
+ fun write(value: KotlinType, buf: ByteBuffer)
146
+
147
+ // Lower a value into a `RustBuffer`
148
+ //
149
+ // This method lowers a value into a `RustBuffer` rather than the normal
150
+ // FfiType. It's used by the callback interface code. Callback interface
151
+ // returns are always serialized into a `RustBuffer` regardless of their
152
+ // normal FFI type.
153
+ fun lowerIntoRustBuffer(value: KotlinType): RustBuffer.ByValue {
154
+ val rbuf = RustBuffer.alloc(allocationSize(value))
155
+ try {
156
+ val bbuf = rbuf.data!!.getByteBuffer(0, rbuf.capacity.toLong()).also {
157
+ it.order(ByteOrder.BIG_ENDIAN)
158
+ }
159
+ write(value, bbuf)
160
+ rbuf.writeField("len", bbuf.position())
161
+ return rbuf
162
+ } catch (e: Throwable) {
163
+ RustBuffer.free(rbuf)
164
+ throw e
165
+ }
166
+ }
167
+
168
+ // Lift a value from a `RustBuffer`.
169
+ //
170
+ // This here mostly because of the symmetry with `lowerIntoRustBuffer()`.
171
+ // It's currently only used by the `FfiConverterRustBuffer` class below.
172
+ fun liftFromRustBuffer(rbuf: RustBuffer.ByValue): KotlinType {
173
+ val byteBuf = rbuf.asByteBuffer()!!
174
+ try {
175
+ val item = read(byteBuf)
176
+ if (byteBuf.hasRemaining()) {
177
+ throw RuntimeException("junk remaining in buffer after lifting, something is very wrong!!")
178
+ }
179
+ return item
180
+ } finally {
181
+ RustBuffer.free(rbuf)
182
+ }
183
+ }
184
+ }
185
+
186
+ // FfiConverter that uses `RustBuffer` as the FfiType
187
+ public interface FfiConverterRustBuffer<KotlinType>: FfiConverter<KotlinType, RustBuffer.ByValue> {
188
+ override fun lift(value: RustBuffer.ByValue) = liftFromRustBuffer(value)
189
+ override fun lower(value: KotlinType) = lowerIntoRustBuffer(value)
190
+ }
191
+ // A handful of classes and functions to support the generated data structures.
192
+ // This would be a good candidate for isolating in its own ffi-support lib.
193
+ // Error runtime.
194
+ @Structure.FieldOrder("code", "error_buf")
195
+ internal open class UniffiRustCallStatus : Structure() {
196
+ @JvmField var code: Byte = 0
197
+ @JvmField var error_buf: RustBuffer.ByValue = RustBuffer.ByValue()
198
+
199
+ class ByValue: UniffiRustCallStatus(), Structure.ByValue
200
+
201
+ fun isSuccess(): Boolean {
202
+ return code == 0.toByte()
203
+ }
204
+
205
+ fun isError(): Boolean {
206
+ return code == 1.toByte()
207
+ }
208
+
209
+ fun isPanic(): Boolean {
210
+ return code == 2.toByte()
211
+ }
212
+ }
213
+
214
+ class InternalException(message: String) : Exception(message)
215
+
216
+ // Each top-level error class has a companion object that can lift the error from the call status's rust buffer
217
+ interface UniffiRustCallStatusErrorHandler<E> {
218
+ fun lift(error_buf: RustBuffer.ByValue): E;
219
+ }
220
+
221
+ // Helpers for calling Rust
222
+ // In practice we usually need to be synchronized to call this safely, so it doesn't
223
+ // synchronize itself
224
+
225
+ // Call a rust function that returns a Result<>. Pass in the Error class companion that corresponds to the Err
226
+ private inline fun <U, E: Exception> uniffiRustCallWithError(errorHandler: UniffiRustCallStatusErrorHandler<E>, callback: (UniffiRustCallStatus) -> U): U {
227
+ var status = UniffiRustCallStatus();
228
+ val return_value = callback(status)
229
+ uniffiCheckCallStatus(errorHandler, status)
230
+ return return_value
231
+ }
232
+
233
+ // Check UniffiRustCallStatus and throw an error if the call wasn't successful
234
+ private fun<E: Exception> uniffiCheckCallStatus(errorHandler: UniffiRustCallStatusErrorHandler<E>, status: UniffiRustCallStatus) {
235
+ if (status.isSuccess()) {
236
+ return
237
+ } else if (status.isError()) {
238
+ throw errorHandler.lift(status.error_buf)
239
+ } else if (status.isPanic()) {
240
+ // when the rust code sees a panic, it tries to construct a rustbuffer
241
+ // with the message. but if that code panics, then it just sends back
242
+ // an empty buffer.
243
+ if (status.error_buf.len > 0) {
244
+ throw InternalException(FfiConverterString.lift(status.error_buf))
245
+ } else {
246
+ throw InternalException("Rust panic")
247
+ }
248
+ } else {
249
+ throw InternalException("Unknown rust call status: $status.code")
250
+ }
251
+ }
252
+
253
+ // UniffiRustCallStatusErrorHandler implementation for times when we don't expect a CALL_ERROR
254
+ object UniffiNullRustCallStatusErrorHandler: UniffiRustCallStatusErrorHandler<InternalException> {
255
+ override fun lift(error_buf: RustBuffer.ByValue): InternalException {
256
+ RustBuffer.free(error_buf)
257
+ return InternalException("Unexpected CALL_ERROR")
258
+ }
259
+ }
260
+
261
+ // Call a rust function that returns a plain value
262
+ private inline fun <U> uniffiRustCall(callback: (UniffiRustCallStatus) -> U): U {
263
+ return uniffiRustCallWithError(UniffiNullRustCallStatusErrorHandler, callback);
264
+ }
265
+
266
+ // IntegerType that matches Rust's `usize` / C's `size_t`
267
+ public class USize(value: Long = 0) : IntegerType(Native.SIZE_T_SIZE, value, true) {
268
+ // This is needed to fill in the gaps of IntegerType's implementation of Number for Kotlin.
269
+ override fun toByte() = toInt().toByte()
270
+ // Needed until https://youtrack.jetbrains.com/issue/KT-47902 is fixed.
271
+ @Deprecated("`toInt().toChar()` is deprecated")
272
+ override fun toChar() = toInt().toChar()
273
+ override fun toShort() = toInt().toShort()
274
+
275
+ fun writeToBuffer(buf: ByteBuffer) {
276
+ // Make sure we always write usize integers using native byte-order, since they may be
277
+ // casted to pointer values
278
+ buf.order(ByteOrder.nativeOrder())
279
+ try {
280
+ when (Native.SIZE_T_SIZE) {
281
+ 4 -> buf.putInt(toInt())
282
+ 8 -> buf.putLong(toLong())
283
+ else -> throw RuntimeException("Invalid SIZE_T_SIZE: ${Native.SIZE_T_SIZE}")
284
+ }
285
+ } finally {
286
+ buf.order(ByteOrder.BIG_ENDIAN)
287
+ }
288
+ }
289
+
290
+ companion object {
291
+ val size: Int
292
+ get() = Native.SIZE_T_SIZE
293
+
294
+ fun readFromBuffer(buf: ByteBuffer) : USize {
295
+ // Make sure we always read usize integers using native byte-order, since they may be
296
+ // casted from pointer values
297
+ buf.order(ByteOrder.nativeOrder())
298
+ try {
299
+ return when (Native.SIZE_T_SIZE) {
300
+ 4 -> USize(buf.getInt().toLong())
301
+ 8 -> USize(buf.getLong())
302
+ else -> throw RuntimeException("Invalid SIZE_T_SIZE: ${Native.SIZE_T_SIZE}")
303
+ }
304
+ } finally {
305
+ buf.order(ByteOrder.BIG_ENDIAN)
306
+ }
307
+ }
308
+ }
309
+ }
310
+
311
+
312
+ // Map handles to objects
313
+ //
314
+ // This is used when the Rust code expects an opaque pointer to represent some foreign object.
315
+ // Normally we would pass a pointer to the object, but JNA doesn't support getting a pointer from an
316
+ // object reference , nor does it support leaking a reference to Rust.
317
+ //
318
+ // Instead, this class maps USize values to objects so that we can pass a pointer-sized type to
319
+ // Rust when it needs an opaque pointer.
320
+ //
321
+ // TODO: refactor callbacks to use this class
322
+ internal class UniFfiHandleMap<T: Any> {
323
+ private val map = ConcurrentHashMap<USize, T>()
324
+ // Use AtomicInteger for our counter, since we may be on a 32-bit system. 4 billion possible
325
+ // values seems like enough. If somehow we generate 4 billion handles, then this will wrap
326
+ // around back to zero and we can assume the first handle generated will have been dropped by
327
+ // then.
328
+ private val counter = java.util.concurrent.atomic.AtomicInteger(0)
329
+
330
+ val size: Int
331
+ get() = map.size
332
+
333
+ fun insert(obj: T): USize {
334
+ val handle = USize(counter.getAndAdd(1).toLong())
335
+ map.put(handle, obj)
336
+ return handle
337
+ }
338
+
339
+ fun get(handle: USize): T? {
340
+ return map.get(handle)
341
+ }
342
+
343
+ fun remove(handle: USize): T? {
344
+ return map.remove(handle)
345
+ }
346
+ }
347
+
348
+ // FFI type for Rust future continuations
349
+ internal interface UniFffiRustFutureContinuationCallbackType : com.sun.jna.Callback {
350
+ fun callback(continuationHandle: USize, pollResult: Byte);
351
+ }
352
+
353
+ // Contains loading, initialization code,
354
+ // and the FFI Function declarations in a com.sun.jna.Library.
355
+ @Synchronized
356
+ private fun findLibraryName(componentName: String): String {
357
+ val libOverride = System.getProperty("uniffi.component.$componentName.libraryOverride")
358
+ if (libOverride != null) {
359
+ return libOverride
360
+ }
361
+ return "vc"
362
+ }
363
+
364
+ private inline fun <reified Lib : Library> loadIndirect(
365
+ componentName: String
366
+ ): Lib {
367
+ return Native.load<Lib>(findLibraryName(componentName), Lib::class.java)
368
+ }
369
+
370
+ // A JNA Library to expose the extern-C FFI definitions.
371
+ // This is an implementation detail which will be called internally by the public API.
372
+
373
+ internal interface UniffiLib : Library {
374
+ companion object {
375
+ internal val INSTANCE: UniffiLib by lazy {
376
+ loadIndirect<UniffiLib>(componentName = "vc")
377
+ .also { lib: UniffiLib ->
378
+ uniffiCheckContractApiVersion(lib)
379
+ uniffiCheckApiChecksums(lib)
380
+ }
381
+ }
382
+
383
+ }
384
+
385
+ fun uniffi_vc_fn_func_facade(`action`: RustBuffer.ByValue,`options`: RustBuffer.ByValue,uniffi_out_err: UniffiRustCallStatus,
386
+ ): RustBuffer.ByValue
387
+ fun ffi_vc_rustbuffer_alloc(`size`: Int,uniffi_out_err: UniffiRustCallStatus,
388
+ ): RustBuffer.ByValue
389
+ fun ffi_vc_rustbuffer_from_bytes(`bytes`: ForeignBytes.ByValue,uniffi_out_err: UniffiRustCallStatus,
390
+ ): RustBuffer.ByValue
391
+ fun ffi_vc_rustbuffer_free(`buf`: RustBuffer.ByValue,uniffi_out_err: UniffiRustCallStatus,
392
+ ): Unit
393
+ fun ffi_vc_rustbuffer_reserve(`buf`: RustBuffer.ByValue,`additional`: Int,uniffi_out_err: UniffiRustCallStatus,
394
+ ): RustBuffer.ByValue
395
+ fun ffi_vc_rust_future_poll_u8(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
396
+ ): Unit
397
+ fun ffi_vc_rust_future_cancel_u8(`handle`: Pointer,
398
+ ): Unit
399
+ fun ffi_vc_rust_future_free_u8(`handle`: Pointer,
400
+ ): Unit
401
+ fun ffi_vc_rust_future_complete_u8(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
402
+ ): Byte
403
+ fun ffi_vc_rust_future_poll_i8(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
404
+ ): Unit
405
+ fun ffi_vc_rust_future_cancel_i8(`handle`: Pointer,
406
+ ): Unit
407
+ fun ffi_vc_rust_future_free_i8(`handle`: Pointer,
408
+ ): Unit
409
+ fun ffi_vc_rust_future_complete_i8(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
410
+ ): Byte
411
+ fun ffi_vc_rust_future_poll_u16(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
412
+ ): Unit
413
+ fun ffi_vc_rust_future_cancel_u16(`handle`: Pointer,
414
+ ): Unit
415
+ fun ffi_vc_rust_future_free_u16(`handle`: Pointer,
416
+ ): Unit
417
+ fun ffi_vc_rust_future_complete_u16(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
418
+ ): Short
419
+ fun ffi_vc_rust_future_poll_i16(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
420
+ ): Unit
421
+ fun ffi_vc_rust_future_cancel_i16(`handle`: Pointer,
422
+ ): Unit
423
+ fun ffi_vc_rust_future_free_i16(`handle`: Pointer,
424
+ ): Unit
425
+ fun ffi_vc_rust_future_complete_i16(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
426
+ ): Short
427
+ fun ffi_vc_rust_future_poll_u32(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
428
+ ): Unit
429
+ fun ffi_vc_rust_future_cancel_u32(`handle`: Pointer,
430
+ ): Unit
431
+ fun ffi_vc_rust_future_free_u32(`handle`: Pointer,
432
+ ): Unit
433
+ fun ffi_vc_rust_future_complete_u32(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
434
+ ): Int
435
+ fun ffi_vc_rust_future_poll_i32(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
436
+ ): Unit
437
+ fun ffi_vc_rust_future_cancel_i32(`handle`: Pointer,
438
+ ): Unit
439
+ fun ffi_vc_rust_future_free_i32(`handle`: Pointer,
440
+ ): Unit
441
+ fun ffi_vc_rust_future_complete_i32(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
442
+ ): Int
443
+ fun ffi_vc_rust_future_poll_u64(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
444
+ ): Unit
445
+ fun ffi_vc_rust_future_cancel_u64(`handle`: Pointer,
446
+ ): Unit
447
+ fun ffi_vc_rust_future_free_u64(`handle`: Pointer,
448
+ ): Unit
449
+ fun ffi_vc_rust_future_complete_u64(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
450
+ ): Long
451
+ fun ffi_vc_rust_future_poll_i64(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
452
+ ): Unit
453
+ fun ffi_vc_rust_future_cancel_i64(`handle`: Pointer,
454
+ ): Unit
455
+ fun ffi_vc_rust_future_free_i64(`handle`: Pointer,
456
+ ): Unit
457
+ fun ffi_vc_rust_future_complete_i64(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
458
+ ): Long
459
+ fun ffi_vc_rust_future_poll_f32(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
460
+ ): Unit
461
+ fun ffi_vc_rust_future_cancel_f32(`handle`: Pointer,
462
+ ): Unit
463
+ fun ffi_vc_rust_future_free_f32(`handle`: Pointer,
464
+ ): Unit
465
+ fun ffi_vc_rust_future_complete_f32(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
466
+ ): Float
467
+ fun ffi_vc_rust_future_poll_f64(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
468
+ ): Unit
469
+ fun ffi_vc_rust_future_cancel_f64(`handle`: Pointer,
470
+ ): Unit
471
+ fun ffi_vc_rust_future_free_f64(`handle`: Pointer,
472
+ ): Unit
473
+ fun ffi_vc_rust_future_complete_f64(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
474
+ ): Double
475
+ fun ffi_vc_rust_future_poll_pointer(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
476
+ ): Unit
477
+ fun ffi_vc_rust_future_cancel_pointer(`handle`: Pointer,
478
+ ): Unit
479
+ fun ffi_vc_rust_future_free_pointer(`handle`: Pointer,
480
+ ): Unit
481
+ fun ffi_vc_rust_future_complete_pointer(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
482
+ ): Pointer
483
+ fun ffi_vc_rust_future_poll_rust_buffer(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
484
+ ): Unit
485
+ fun ffi_vc_rust_future_cancel_rust_buffer(`handle`: Pointer,
486
+ ): Unit
487
+ fun ffi_vc_rust_future_free_rust_buffer(`handle`: Pointer,
488
+ ): Unit
489
+ fun ffi_vc_rust_future_complete_rust_buffer(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
490
+ ): RustBuffer.ByValue
491
+ fun ffi_vc_rust_future_poll_void(`handle`: Pointer,`callback`: UniFffiRustFutureContinuationCallbackType,`callbackData`: USize,
492
+ ): Unit
493
+ fun ffi_vc_rust_future_cancel_void(`handle`: Pointer,
494
+ ): Unit
495
+ fun ffi_vc_rust_future_free_void(`handle`: Pointer,
496
+ ): Unit
497
+ fun ffi_vc_rust_future_complete_void(`handle`: Pointer,uniffi_out_err: UniffiRustCallStatus,
498
+ ): Unit
499
+ fun uniffi_vc_checksum_func_facade(
500
+ ): Short
501
+ fun ffi_vc_uniffi_contract_version(
502
+ ): Int
503
+
504
+ }
505
+
506
+ private fun uniffiCheckContractApiVersion(lib: UniffiLib) {
507
+ // Get the bindings contract version from our ComponentInterface
508
+ val bindings_contract_version = 25
509
+ // Get the scaffolding contract version by calling the into the dylib
510
+ val scaffolding_contract_version = lib.ffi_vc_uniffi_contract_version()
511
+ if (bindings_contract_version != scaffolding_contract_version) {
512
+ throw RuntimeException("UniFFI contract version mismatch: try cleaning and rebuilding your project")
513
+ }
514
+ }
515
+
516
+ @Suppress("UNUSED_PARAMETER")
517
+ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
518
+ if (lib.uniffi_vc_checksum_func_facade() != 4396.toShort()) {
519
+ throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
520
+ }
521
+ }
522
+
523
+ // Async support
524
+
525
+ // Public interface members begin here.
526
+
527
+
528
+ // Interface implemented by anything that can contain an object reference.
529
+ //
530
+ // Such types expose a `destroy()` method that must be called to cleanly
531
+ // dispose of the contained objects. Failure to call this method may result
532
+ // in memory leaks.
533
+ //
534
+ // The easiest way to ensure this method is called is to use the `.use`
535
+ // helper method to execute a block and destroy the object at the end.
536
+ interface Disposable {
537
+ fun destroy()
538
+ companion object {
539
+ fun destroy(vararg args: Any?) {
540
+ args.filterIsInstance<Disposable>()
541
+ .forEach(Disposable::destroy)
542
+ }
543
+ }
544
+ }
545
+
546
+ inline fun <T : Disposable?, R> T.use(block: (T) -> R) =
547
+ try {
548
+ block(this)
549
+ } finally {
550
+ try {
551
+ // N.B. our implementation is on the nullable type `Disposable?`.
552
+ this?.destroy()
553
+ } catch (e: Throwable) {
554
+ // swallow
555
+ }
556
+ }
557
+
558
+ public object FfiConverterString: FfiConverter<String, RustBuffer.ByValue> {
559
+ // Note: we don't inherit from FfiConverterRustBuffer, because we use a
560
+ // special encoding when lowering/lifting. We can use `RustBuffer.len` to
561
+ // store our length and avoid writing it out to the buffer.
562
+ override fun lift(value: RustBuffer.ByValue): String {
563
+ try {
564
+ val byteArr = ByteArray(value.len)
565
+ value.asByteBuffer()!!.get(byteArr)
566
+ return byteArr.toString(Charsets.UTF_8)
567
+ } finally {
568
+ RustBuffer.free(value)
569
+ }
570
+ }
571
+
572
+ override fun read(buf: ByteBuffer): String {
573
+ val len = buf.getInt()
574
+ val byteArr = ByteArray(len)
575
+ buf.get(byteArr)
576
+ return byteArr.toString(Charsets.UTF_8)
577
+ }
578
+
579
+ fun toUtf8(value: String): ByteBuffer {
580
+ // Make sure we don't have invalid UTF-16, check for lone surrogates.
581
+ return Charsets.UTF_8.newEncoder().run {
582
+ onMalformedInput(CodingErrorAction.REPORT)
583
+ encode(CharBuffer.wrap(value))
584
+ }
585
+ }
586
+
587
+ override fun lower(value: String): RustBuffer.ByValue {
588
+ val byteBuf = toUtf8(value)
589
+ // Ideally we'd pass these bytes to `ffi_bytebuffer_from_bytes`, but doing so would require us
590
+ // to copy them into a JNA `Memory`. So we might as well directly copy them into a `RustBuffer`.
591
+ val rbuf = RustBuffer.alloc(byteBuf.limit())
592
+ rbuf.asByteBuffer()!!.put(byteBuf)
593
+ return rbuf
594
+ }
595
+
596
+ // We aren't sure exactly how many bytes our string will be once it's UTF-8
597
+ // encoded. Allocate 3 bytes per UTF-16 code unit which will always be
598
+ // enough.
599
+ override fun allocationSize(value: String): Int {
600
+ val sizeForLength = 4
601
+ val sizeForString = value.length * 3
602
+ return sizeForLength + sizeForString
603
+ }
604
+
605
+ override fun write(value: String, buf: ByteBuffer) {
606
+ val byteBuf = toUtf8(value)
607
+ buf.putInt(byteBuf.limit())
608
+ buf.put(byteBuf)
609
+ }
610
+ }
611
+
612
+
613
+
614
+
615
+
616
+ sealed class VcException(message: String): Exception(message) {
617
+
618
+ class Generic(message: String) : VcException(message)
619
+
620
+ class BlsSign(message: String) : VcException(message)
621
+
622
+ class BlsVerify(message: String) : VcException(message)
623
+
624
+
625
+ companion object ErrorHandler : UniffiRustCallStatusErrorHandler<VcException> {
626
+ override fun lift(error_buf: RustBuffer.ByValue): VcException = FfiConverterTypeVCError.lift(error_buf)
627
+ }
628
+ }
629
+
630
+ public object FfiConverterTypeVCError : FfiConverterRustBuffer<VcException> {
631
+ override fun read(buf: ByteBuffer): VcException {
632
+
633
+ return when(buf.getInt()) {
634
+ 1 -> VcException.Generic(FfiConverterString.read(buf))
635
+ 2 -> VcException.BlsSign(FfiConverterString.read(buf))
636
+ 3 -> VcException.BlsVerify(FfiConverterString.read(buf))
637
+ else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
638
+ }
639
+
640
+ }
641
+
642
+ override fun allocationSize(value: VcException): Int {
643
+ return 4
644
+ }
645
+
646
+ override fun write(value: VcException, buf: ByteBuffer) {
647
+ when(value) {
648
+ is VcException.Generic -> {
649
+ buf.putInt(1)
650
+ Unit
651
+ }
652
+ is VcException.BlsSign -> {
653
+ buf.putInt(2)
654
+ Unit
655
+ }
656
+ is VcException.BlsVerify -> {
657
+ buf.putInt(3)
658
+ Unit
659
+ }
660
+ }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
661
+ }
662
+
663
+ }
664
+ @Throws(VcException::class)
665
+
666
+ fun `facade`(`action`: String, `options`: String): String {
667
+ return FfiConverterString.lift(
668
+ uniffiRustCallWithError(VcException) { _status ->
669
+ UniffiLib.INSTANCE.uniffi_vc_fn_func_facade(FfiConverterString.lower(`action`),FfiConverterString.lower(`options`),_status)
670
+ })
671
+ }
672
+
673
+
@@ -0,0 +1,30 @@
1
+ package com.nuggetslife.vc
2
+
3
+ import android.util.Log
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
6
+ import com.facebook.react.bridge.ReactMethod
7
+ import com.facebook.react.bridge.Promise
8
+ import vc.facade
9
+
10
+ class VcModule(reactContext: ReactApplicationContext) :
11
+ ReactContextBaseJavaModule(reactContext) {
12
+
13
+ override fun getName(): String {
14
+ return NAME
15
+ }
16
+
17
+ @ReactMethod
18
+ fun vc_facade(action: String, options: String, promise: Promise) {
19
+ try {
20
+ val result = facade(action, options)
21
+ promise.resolve(result)
22
+ } catch (e: Throwable) {
23
+ promise.reject("vc_facade", e)
24
+ }
25
+ }
26
+
27
+ companion object {
28
+ const val NAME = "Vc"
29
+ }
30
+ }