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
Changes from 1 commit
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
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent;
import android.database.ContentObserver
import android.net.Uri
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.util.Log
import com.facebook.react.bridge.*
Expand Down Expand Up @@ -185,7 +186,68 @@ class SolanaMobileSeedVaultLibModule(val reactContext: ReactApplicationContext)
private fun signTransactions(authToken: String, signingRequests: List<SigningRequest>) {
Log.d(TAG, "Requesting provided transactions to be signed...")
val intent = Wallet.signTransactions(authToken.toLong(), ArrayList(signingRequests))
reactContext.currentActivity?.startActivityForResult(intent, REQUEST_SIGN_TRANSACTIONS);
reactContext.currentActivity?.startActivityForResult(intent, REQUEST_SIGN_TRANSACTIONS)
}

@ReactMethod
fun signTransactionAsync(authToken: String, derivationPath: String, transaction: ReadableArray, promise: Promise) {
signTransactionsAsync(authToken, listOf(SigningRequest(transaction.toByteArray(), arrayListOf(Uri.parse(derivationPath)))), promise)
}

@ReactMethod
fun signTransactionsAsync(authToken: String, signingRequestsJson: String, promise: Promise) {
val signingRequests: List<SigningRequest> = json.decodeFromString(ListSerializer(SigningRequestSerializer), signingRequestsJson)
signTransactionsAsync(authToken, signingRequests, promise)
}

private fun signTransactionsAsync(authToken: String, signingRequests: List<SigningRequest>, promise: Promise) {
Log.d(TAG, "Requesting provided transactions to be signed...")
val intent = Wallet.signTransactions(authToken.toLong(), ArrayList(signingRequests))
registerForActivityResult(intent, REQUEST_SIGN_TRANSACTIONS) { resultCode, data ->
try {
val result = Wallet.onSignTransactionsResult(resultCode, data)
Log.d(TAG, "Transactions signed: signatures=$result")

promise.resolve(
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() }))
}
})
)
} catch (e: Wallet.ActionFailedException) {
Log.e(TAG, "Transaction signing failed", e)
promise.reject(e)
}
}
}

private fun registerForActivityResult(intent: Intent, requestCode: Int, callback: (resultCode: Int, data: Intent?) -> Unit) {
val timeout = object : CountDownTimer(30000, 30000) {
Funkatronics marked this conversation as resolved.
Show resolved Hide resolved
override fun onTick(millisUntilFinished: Long) {}
override fun onFinish() {
reactContext.currentActivity?.finishActivity(requestCode)
Funkatronics marked this conversation as resolved.
Show resolved Hide resolved
}
}

reactContext.addActivityEventListener(object : BaseActivityEventListener() {
override fun onActivityResult(
activity: Activity?,
receivedRequestCode: Int,
resultCode: Int,
data: Intent?
) {
if (receivedRequestCode == requestCode) {
reactContext.removeActivityEventListener(this)
callback(resultCode, data)
timeout.cancel()
}
}
})

reactContext.currentActivity?.startActivityForResult(intent, requestCode)
timeout.start()
}

@ReactMethod
Expand Down