@@ -22,8 +22,9 @@ import kotlinx.coroutines.flow.*
2222import kotlinx.coroutines.launch
2323import java.util.Base64
2424
25- class RNZcashModule (private val reactContext : ReactApplicationContext ) :
26- ReactContextBaseJavaModule (reactContext) {
25+ class RNZcashModule (
26+ private val reactContext : ReactApplicationContext ,
27+ ) : ReactContextBaseJavaModule(reactContext) {
2728 /* *
2829 * Scope for anything that out-lives the synchronizer, meaning anything that can be used before
2930 * the synchronizer starts or after it stops. Everything else falls within the scope of the
@@ -46,135 +47,139 @@ class RNZcashModule(private val reactContext: ReactApplicationContext) :
4647 defaultPort : Int = 9067,
4748 newWallet : Boolean ,
4849 promise : Promise ,
49- ) = moduleScope.launch {
50- promise.wrap {
51- val network = networks.getOrDefault(networkName, ZcashNetwork .Mainnet )
52- val endpoint = LightWalletEndpoint (defaultHost, defaultPort, true )
53- val seedPhrase = SeedPhrase .new(seed)
54- val initMode = if (newWallet) WalletInitMode .NewWallet else WalletInitMode .ExistingWallet
55- if (! synchronizerMap.containsKey(alias)) {
56- synchronizerMap[alias] =
57- Synchronizer .new(
58- reactApplicationContext,
59- network,
60- alias,
61- endpoint,
62- seedPhrase.toByteArray(),
63- BlockHeight .new(birthdayHeight.toLong()),
64- initMode,
65- ) as SdkSynchronizer
66- }
67- val wallet = getWallet(alias)
68- val scope = wallet.coroutineScope
69- combine(wallet.progress, wallet.networkHeight) { progress, networkHeight ->
70- return @combine mapOf (" progress" to progress, " networkHeight" to networkHeight)
71- }.collectWith(scope) { map ->
72- val progress = map[" progress" ] as PercentDecimal
73- var networkBlockHeight = map[" networkHeight" ] as BlockHeight ?
74- if (networkBlockHeight == null ) networkBlockHeight = BlockHeight .new(birthdayHeight.toLong())
75-
76- sendEvent(" UpdateEvent" ) { args ->
77- args.putString(" alias" , alias)
78- args.putInt(
79- " scanProgress" ,
80- progress.toPercentage(),
81- )
82- args.putInt(" networkBlockHeight" , networkBlockHeight.value.toInt())
50+ ) {
51+ moduleScope.launch {
52+ promise.wrap {
53+ val network = networks.getOrDefault(networkName, ZcashNetwork .Mainnet )
54+ val endpoint = LightWalletEndpoint (defaultHost, defaultPort, true )
55+ val seedPhrase = SeedPhrase .new(seed)
56+ val initMode = if (newWallet) WalletInitMode .NewWallet else WalletInitMode .ExistingWallet
57+ if (! synchronizerMap.containsKey(alias)) {
58+ synchronizerMap[alias] =
59+ Synchronizer .new(
60+ reactApplicationContext,
61+ network,
62+ alias,
63+ endpoint,
64+ seedPhrase.toByteArray(),
65+ BlockHeight .new(birthdayHeight.toLong()),
66+ initMode,
67+ ) as SdkSynchronizer
8368 }
84- }
85- wallet.status.collectWith(scope) { status ->
86- sendEvent(" StatusEvent" ) { args ->
87- args.putString(" alias" , alias)
88- args.putString(" name" , status.toString())
69+ val wallet = getWallet(alias)
70+ val scope = wallet.coroutineScope
71+ combine(wallet.progress, wallet.networkHeight) { progress, networkHeight ->
72+ return @combine mapOf (" progress" to progress, " networkHeight" to networkHeight)
73+ }.collectWith(scope) { map ->
74+ val progress = map[" progress" ] as PercentDecimal
75+ var networkBlockHeight = map[" networkHeight" ] as BlockHeight ?
76+ if (networkBlockHeight == null ) networkBlockHeight = BlockHeight .new(birthdayHeight.toLong())
77+
78+ sendEvent(" UpdateEvent" ) { args ->
79+ args.putString(" alias" , alias)
80+ args.putInt(
81+ " scanProgress" ,
82+ progress.toPercentage(),
83+ )
84+ args.putInt(" networkBlockHeight" , networkBlockHeight.value.toInt())
85+ }
8986 }
90- }
91- wallet.transactions.collectWith(scope) { txList ->
92- scope.launch {
93- val nativeArray = Arguments .createArray()
94- txList.filter { tx -> tx.transactionState != TransactionState .Expired }.map { tx ->
95- launch {
96- val parsedTx = parseTx(wallet, tx)
97- nativeArray.pushMap(parsedTx)
87+ wallet.status.collectWith(scope) { status ->
88+ sendEvent(" StatusEvent" ) { args ->
89+ args.putString(" alias" , alias)
90+ args.putString(" name" , status.toString())
91+ }
92+ }
93+ wallet.transactions.collectWith(scope) { txList ->
94+ scope.launch {
95+ val nativeArray = Arguments .createArray()
96+ txList
97+ .filter { tx -> tx.transactionState != TransactionState .Expired }
98+ .map { tx ->
99+ launch {
100+ val parsedTx = parseTx(wallet, tx)
101+ nativeArray.pushMap(parsedTx)
102+ }
103+ }.forEach { it.join() }
104+
105+ sendEvent(" TransactionEvent" ) { args ->
106+ args.putString(" alias" , alias)
107+ args.putArray(
108+ " transactions" ,
109+ nativeArray,
110+ )
98111 }
99- }.forEach { it.join() }
112+ }
113+ }
114+ combine(
115+ wallet.transparentBalance,
116+ wallet.saplingBalances,
117+ wallet.orchardBalances,
118+ ) { transparentBalance: Zatoshi ? , saplingBalances: WalletBalance ? , orchardBalances: WalletBalance ? ->
119+ return @combine Balances (
120+ transparentBalance = transparentBalance,
121+ saplingBalances = saplingBalances,
122+ orchardBalances = orchardBalances,
123+ )
124+ }.collectWith(scope) { map ->
125+ val transparentBalance = map.transparentBalance
126+ val saplingBalances = map.saplingBalances
127+ val orchardBalances = map.orchardBalances
128+
129+ val transparentAvailableZatoshi = transparentBalance ? : Zatoshi (0L )
130+ val transparentTotalZatoshi = transparentBalance ? : Zatoshi (0L )
100131
101- sendEvent(" TransactionEvent" ) { args ->
132+ val saplingAvailableZatoshi = saplingBalances?.available ? : Zatoshi (0L )
133+ val saplingTotalZatoshi = saplingBalances?.total ? : Zatoshi (0L )
134+
135+ val orchardAvailableZatoshi = orchardBalances?.available ? : Zatoshi (0L )
136+ val orchardTotalZatoshi = orchardBalances?.total ? : Zatoshi (0L )
137+
138+ sendEvent(" BalanceEvent" ) { args ->
102139 args.putString(" alias" , alias)
103- args.putArray(
104- " transactions" ,
105- nativeArray,
106- )
140+ args.putString(" transparentAvailableZatoshi" , transparentAvailableZatoshi.value.toString())
141+ args.putString(" transparentTotalZatoshi" , transparentTotalZatoshi.value.toString())
142+ args.putString(" saplingAvailableZatoshi" , saplingAvailableZatoshi.value.toString())
143+ args.putString(" saplingTotalZatoshi" , saplingTotalZatoshi.value.toString())
144+ args.putString(" orchardAvailableZatoshi" , orchardAvailableZatoshi.value.toString())
145+ args.putString(" orchardTotalZatoshi" , orchardTotalZatoshi.value.toString())
107146 }
108147 }
109- }
110- combine(
111- wallet.transparentBalance,
112- wallet.saplingBalances,
113- wallet.orchardBalances,
114- ) { transparentBalance: Zatoshi ? , saplingBalances: WalletBalance ? , orchardBalances: WalletBalance ? ->
115- return @combine Balances (
116- transparentBalance = transparentBalance,
117- saplingBalances = saplingBalances,
118- orchardBalances = orchardBalances,
119- )
120- }.collectWith(scope) { map ->
121- val transparentBalance = map.transparentBalance
122- val saplingBalances = map.saplingBalances
123- val orchardBalances = map.orchardBalances
124-
125- val transparentAvailableZatoshi = transparentBalance ? : Zatoshi (0L )
126- val transparentTotalZatoshi = transparentBalance ? : Zatoshi (0L )
127-
128- val saplingAvailableZatoshi = saplingBalances?.available ? : Zatoshi (0L )
129- val saplingTotalZatoshi = saplingBalances?.total ? : Zatoshi (0L )
130-
131- val orchardAvailableZatoshi = orchardBalances?.available ? : Zatoshi (0L )
132- val orchardTotalZatoshi = orchardBalances?.total ? : Zatoshi (0L )
133-
134- sendEvent(" BalanceEvent" ) { args ->
135- args.putString(" alias" , alias)
136- args.putString(" transparentAvailableZatoshi" , transparentAvailableZatoshi.value.toString())
137- args.putString(" transparentTotalZatoshi" , transparentTotalZatoshi.value.toString())
138- args.putString(" saplingAvailableZatoshi" , saplingAvailableZatoshi.value.toString())
139- args.putString(" saplingTotalZatoshi" , saplingTotalZatoshi.value.toString())
140- args.putString(" orchardAvailableZatoshi" , orchardAvailableZatoshi.value.toString())
141- args.putString(" orchardTotalZatoshi" , orchardTotalZatoshi.value.toString())
142- }
143- }
144148
145- fun handleError (
146- level : String ,
147- error : Throwable ? ,
148- ) {
149- sendEvent(" ErrorEvent" ) { args ->
150- args.putString(" alias" , alias)
151- args.putString(" level" , level)
152- args.putString(" message" , error?.message ? : " Unknown error" )
149+ fun handleError (
150+ level : String ,
151+ error : Throwable ? ,
152+ ) {
153+ sendEvent(" ErrorEvent" ) { args ->
154+ args.putString(" alias" , alias)
155+ args.putString(" level" , level)
156+ args.putString(" message" , error?.message ? : " Unknown error" )
157+ }
153158 }
154- }
155159
156- // Error listeners
157- wallet.onCriticalErrorHandler = { error ->
158- handleError(" critical" , error)
159- false
160- }
161- wallet.onProcessorErrorHandler = { error ->
162- handleError(" error" , error)
163- true
164- }
165- wallet.onSetupErrorHandler = { error ->
166- handleError(" error" , error)
167- false
168- }
169- wallet.onSubmissionErrorHandler = { error ->
170- handleError(" error" , error)
171- false
172- }
173- wallet.onChainErrorHandler = { errorHeight, rewindHeight ->
174- val message = " Chain error detected at height: $errorHeight . Rewinding to: $rewindHeight "
175- handleError(" error" , Throwable (message))
160+ // Error listeners
161+ wallet.onCriticalErrorHandler = { error ->
162+ handleError(" critical" , error)
163+ false
164+ }
165+ wallet.onProcessorErrorHandler = { error ->
166+ handleError(" error" , error)
167+ true
168+ }
169+ wallet.onSetupErrorHandler = { error ->
170+ handleError(" error" , error)
171+ false
172+ }
173+ wallet.onSubmissionErrorHandler = { error ->
174+ handleError(" error" , error)
175+ false
176+ }
177+ wallet.onChainErrorHandler = { errorHeight, rewindHeight ->
178+ val message = " Chain error detected at height: $errorHeight . Rewinding to: $rewindHeight "
179+ handleError(" error" , Throwable (message))
180+ }
181+ return @wrap null
176182 }
177- return @wrap null
178183 }
179184 }
180185
@@ -349,9 +354,10 @@ class RNZcashModule(private val reactContext: ReactApplicationContext) :
349354 val proposal = Proposal .fromByteArray(proposalByteArray)
350355
351356 val txs =
352- wallet.coroutineScope.async {
353- wallet.createProposedTransactions(proposal, usk).take(proposal.transactionCount()).toList()
354- }.await()
357+ wallet.coroutineScope
358+ .async {
359+ wallet.createProposedTransactions(proposal, usk).take(proposal.transactionCount()).toList()
360+ }.await()
355361 val txid = txs[txs.lastIndex].txIdString() // The last transfer is the most relevant to the user
356362 promise.resolve(txid)
357363 } catch (t: Throwable ) {
@@ -445,9 +451,7 @@ class RNZcashModule(private val reactContext: ReactApplicationContext) :
445451 /* *
446452 * Retrieve wallet object from synchronizer map
447453 */
448- private fun getWallet (alias : String ): SdkSynchronizer {
449- return synchronizerMap[alias] ? : throw Exception (" Wallet not found" )
450- }
454+ private fun getWallet (alias : String ): SdkSynchronizer = synchronizerMap[alias] ? : throw Exception (" Wallet not found" )
451455
452456 /* *
453457 * Wrap the given block of logic in a promise, rejecting for any error.
0 commit comments