11package io.outblock.fcl
22
3+ import android.os.Looper
34import androidx.annotation.WorkerThread
45import com.nftco.flow.sdk.FlowAddress
56import com.nftco.flow.sdk.simpleFlowScript
@@ -14,6 +15,7 @@ import io.outblock.fcl.request.AuthnRequest
1415import io.outblock.fcl.request.AuthzSend
1516import io.outblock.fcl.request.SignMessageSend
1617import io.outblock.fcl.request.builder.FclBuilder
18+ import io.outblock.fcl.utils.ioScope
1719import kotlinx.coroutines.runBlocking
1820
1921object Fcl {
@@ -67,11 +69,17 @@ object Fcl {
6769 */
6870 @WorkerThread
6971 fun authenticate (provider : Provider ): AuthnResponse {
72+ assert (Thread .currentThread() != Looper .getMainLooper().thread) { " can't call this method in main thread." }
73+
7074 val resp = AuthnRequest ().authenticate(provider)
7175 currentUser = User .fromAuthn(resp)
7276 return AuthnResponse (resp.data?.addr, resp.status, resp.reason)
7377 }
7478
79+ fun authenticateAsync (provider : Provider , callback : (response: AuthnResponse ) -> Unit ) {
80+ ioScope { callback(authenticate(provider)) }
81+ }
82+
7583 /* *
7684 * Mutate the chain: Send arbitrary transactions with your own signatures or via a user's wallet to perform state changes on chain.
7785 *
@@ -99,6 +107,7 @@ object Fcl {
99107 */
100108 @WorkerThread
101109 fun mutate (builder : FclBuilder .() -> Unit ): String {
110+ assert (Thread .currentThread() != Looper .getMainLooper().thread) { " can't call this method in main thread." }
102111 return runBlocking { AuthzSend ().send(builder) }
103112 }
104113
@@ -125,6 +134,8 @@ object Fcl {
125134 */
126135 @WorkerThread
127136 fun query (builder : FclBuilder .() -> Unit ): String {
137+ assert (Thread .currentThread() != Looper .getMainLooper().thread) { " can't call this method in main thread." }
138+
128139 val outBuilder = FclBuilder ().apply { builder(this ) }
129140
130141 assert (! outBuilder.cadence.isNullOrBlank()) { " Script is empty" }
@@ -141,6 +152,8 @@ object Fcl {
141152 */
142153 @WorkerThread
143154 fun signMessage (message : String ): String {
155+ assert (Thread .currentThread() != Looper .getMainLooper().thread) { " can't call this method in main thread." }
156+
144157 return runBlocking { SignMessageSend ().sign(message) }
145158 }
146159
0 commit comments