-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathParticleWalletModule.kt
457 lines (406 loc) · 14.2 KB
/
ParticleWalletModule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package com.particlewallet
import android.content.Intent
import android.util.Log
import network.blankj.utilcode.util.GsonUtils
import network.blankj.utilcode.util.LogUtils
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.google.gson.reflect.TypeToken
import com.particle.base.ParticleNetwork
import com.particle.gui.ParticleWallet
import com.particle.gui.router.PNRouter
import com.particle.gui.router.RouterPath
import com.particle.gui.ui.nft_detail.NftDetailParams
import com.particle.gui.ui.receive.ReceiveData
import com.particle.gui.ui.send.WalletSendParams
import com.particle.gui.ui.token_detail.TokenTransactionRecordsParams
import com.particle.gui.utils.WalletUtils
import com.particlewallet.model.InitData
import com.particlewallet.ui.PNLoginOptActivity
import com.particlewallet.utils.BridgeScope
import com.particlewallet.utils.ChainUtils
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.math.BigInteger
import com.particle.api.service.DBService
import com.particle.base.LanguageEnum
import com.particle.gui.ParticleWallet.displayNFTContractAddresses
import com.particle.gui.ui.swap.SwapConfig
import network.particle.chains.ChainInfo
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import com.connect.common.IParticleConnectAdapter
import com.particle.api.infrastructure.db.table.WalletInfo
import com.particle.base.model.MobileWCWalletName
import com.particle.connect.ParticleConnect
import com.particle.gui.ParticleWallet.priorityNFTContractAddresses
import com.particle.gui.ParticleWallet.priorityTokenAddresses
import com.particle.network.ParticleNetworkAuth.getAddress
import particle.auth.adapter.ParticleConnectAdapter
class ParticleWalletPlugin(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
companion object {
var loginOptCallback: Callback? = null
var isUIInit = false
fun isUIModuleInit(): Boolean {
return if (isUIInit) {
true
} else {
Log.e("UIBridge", "UI module is not init!!!")
false
}
}
}
@ReactMethod
fun createSelectedWallet(publicAddress: String, walletType: String, walletName: String?) {
val adapter = ParticleConnect.getAdapters().first { it.name.equals(walletType, true) }
if (!TextUtils.isEmpty(walletName) && adapter is IParticleConnectAdapter) {
BridgeScope.launch {
val wallet = WalletInfo.createWallet(
ParticleNetwork.getAddress(),
ParticleNetwork.chainInfo.name,
ParticleNetwork.chainInfo.id,
1,
walletName!!,
adapter.name
)
ParticleWallet.setWallet(wallet)
}
} else {
BridgeScope.launch {
val wallet = WalletUtils.createSelectedWallet(publicAddress, adapter)
WalletUtils.setWalletChain(wallet)
}
}
}
private val mainHandler = Handler(Looper.getMainLooper())
@ReactMethod
fun init() {
isUIInit = try {
mainHandler.post {
ParticleWallet.init(reactApplicationContext, null)
}
true
} catch (e: Exception) {
false
}
}
/**
* navigatorWallet
*/
@ReactMethod
fun navigatorWallet(display: Int) {
if (!isUIModuleInit()) return;
PNRouter.build(RouterPath.Wallet).withInt(RouterPath.Wallet.toString(), display)
.navigation();
}
@ReactMethod
fun navigatorTokenReceive(tokenAddress: String) {
if (!isUIModuleInit()) return;
try {
val receiveData = ReceiveData(tokenAddress);
PNRouter.build(RouterPath.TokenReceive)
.withParcelable(RouterPath.TokenReceive.toString(), receiveData).navigation();
} catch (e: Exception) {
e.printStackTrace();
}
}
/**
* {
* token_address: "0x0000000000000000000000000000000000000000",
* to_address:"0x0000000000000000000000000000000000000000",
* amount: 111,
* }
*/
@ReactMethod
fun navigatorTokenSend(json: String) {
if (!isUIModuleInit()) return;
// LogUtils.d("navigatorTokenSend", json);
try {
val jsonObject = JSONObject(json);
val tokenAddress = jsonObject.getString("token_address");
val toAddress = jsonObject.getString("to_address");
val amount = jsonObject.getLong("amount");
val params = WalletSendParams(
tokenAddress, toAddress, BigInteger.valueOf(amount)
);
PNRouter.build(RouterPath.TokenSend, params).navigation();
} catch (e: Exception) {
e.printStackTrace();
}
}
@ReactMethod
fun navigatorTokenTransactionRecords(tokenAddress: String) {
if (!isUIModuleInit()) return;
try {
val params = TokenTransactionRecordsParams(tokenAddress);
PNRouter.build(RouterPath.TokenTransactionRecords, params).navigation();
} catch (e: Exception) {
e.printStackTrace();
}
}
/**
* {
* "mint":"0x0000000000000000000000000000000000000000",
* "receiver_address":"0x0000000000000000000000000000000000000000",
* }
*/
@ReactMethod
fun navigatorNFTSend(json: String) {
if (!isUIModuleInit()) return;
try {
val jsonObject = JSONObject(json);
val mint = jsonObject.getString("mint");
val receiverAddress = jsonObject.getString("receiver_address");
val params = NftDetailParams(mint, receiverAddress);
PNRouter.build(RouterPath.NftDetails, params).navigation();
} catch (e: Exception) {
e.printStackTrace();
}
}
@ReactMethod
fun navigatorNFTDetails(mint: String) {
if (!isUIModuleInit()) return;
try {
val params = NftDetailParams(mint, null);
PNRouter.build(RouterPath.NftDetails, params).navigation();
} catch (e: Exception) {
e.printStackTrace();
}
}
@ReactMethod
fun logoutWallet(publicAddress: String) {
if (!isUIModuleInit()) return;
BridgeScope.launch {
DBService.walletInfoDao.deleteWallet(publicAddress)
}
}
@ReactMethod
fun getPayDisabled(callback: Callback) {
callback.invoke(ParticleWallet.getPayDisabled())
}
@ReactMethod
fun setPayDisabled(enable: Boolean) {
ParticleWallet.setPayDisabled(enable)
}
@ReactMethod
fun setSwapDisabled(disabled: Boolean) {
ParticleWallet.setSwapDisabled(disabled)
}
@ReactMethod
fun getSwapDisabled(callback: Callback) {
callback.invoke(ParticleWallet.getSwapDisabled())
}
@ReactMethod
fun getBridgeDisabled(callback: Callback){
callback.invoke(ParticleWallet.getBridgeDisabled())
}
@ReactMethod
fun setBridgeDisabled(disable: Boolean) {
LogUtils.d("setBridgeDisabled", disable.toString());
ParticleWallet.setBridgeDisabled(disable)
}
@ReactMethod
fun navigatorPay() {
currentActivity?.apply {
ParticleNetwork.openBuy(this)
}
}
/**
* const obj = { from_token_address: fromTokenAddress, to_token_address: toTokenAddress, amount: amount }
* const json = JSON.stringify(obj);
*/
@ReactMethod
fun navigatorSwap(jsonParams: String) {
//unsupported jsonParams
try {
val jobj = JSONObject(jsonParams)
val swapConfig =
SwapConfig(
jobj.optString("from_token_address"),
jobj.optString("to_token_address"),
"0"
)
PNRouter.navigatorSwap(swapConfig);
} catch (e: Exception) {
e.printStackTrace()
PNRouter.navigatorSwap(null);
}
}
@ReactMethod
fun setShowTestNetwork(show: Boolean) {
ParticleWallet.setShowTestNetworkSetting(show)
}
@ReactMethod
fun setShowManageWallet(show: Boolean) {
ParticleWallet.setShowManageWalletSetting(show)
}
@ReactMethod
fun navigatorLoginList(callback: Callback) {
loginOptCallback = callback
currentActivity?.startActivity(
Intent(
currentActivity,
PNLoginOptActivity::class.java
)
)
}
@ReactMethod
fun navigatorWalletConnect(callback: Callback) {
loginOptCallback = callback
currentActivity?.startActivity(
PNLoginOptActivity.newIntent(
currentActivity!!,
true
)
)
}
/**
* GUI
*/
@ReactMethod
fun setSupportChain(jsonParams: String) {
if (!isUIModuleInit()) return;
val chains = GsonUtils.fromJson<List<InitData>>(
jsonParams, object : TypeToken<List<InitData>>() {}.type
)
val supportChains: MutableList<ChainInfo> = java.util.ArrayList()
for (chain in chains) {
val chainInfo: ChainInfo = ChainUtils.getChainInfo(chain.chainId)
supportChains.add(chainInfo)
}
ParticleWallet.setSupportChain(supportChains)
}
@ReactMethod
fun switchWallet(jsonParams: String, callback: Callback) {
if (!isUIModuleInit()) return
try {
val jsonObject = JSONObject(jsonParams);
val walletType = jsonObject.getString("wallet_type");
val publicKey = jsonObject.getString("public_address");
BridgeScope.launch {
WalletUtils.createSelectedWallet(publicKey, walletType)
callback.invoke(true)
}
} catch (e: Exception) {
callback.invoke(false)
}
}
@ReactMethod
fun setLanguage(language: String) {
if (language.isEmpty()) {
return
}
if (language.equals("zh_hans")) {
ParticleNetwork.setLanguage(LanguageEnum.ZH_CN)
} else if (language.equals("zh_hant")) {
ParticleNetwork.setLanguage(LanguageEnum.ZH_TW)
} else if (language.equals("ja")) {
ParticleNetwork.setLanguage(LanguageEnum.JA)
} else if (language.equals("ko")) {
ParticleNetwork.setLanguage(LanguageEnum.KO)
} else {
ParticleNetwork.setLanguage(LanguageEnum.EN)
}
}
@ReactMethod
fun setSupportWalletConnect(isEnable: Boolean) {
ParticleWallet.setSupportWalletConnect(isEnable)
}
@ReactMethod
fun navigatorBuyCrypto(jsonParams: String?) {
LogUtils.d("navigatorBuyCrypto", jsonParams)
LogUtils.d("navigatorBuyCrypto", jsonParams)
if (!isUIModuleInit()) return;
val jsonObject = JSONObject(jsonParams)
val walletAddress = jsonObject.optString("wallet_address")
val chainName = jsonObject.optString("network")
val cryptoCoin = jsonObject.optString("crypto_coin")
val fiatCoin = jsonObject.optString("fiat_coin")
val fiatAmt = jsonObject.optInt("fiat_amt")
val fixCryptoCoin = jsonObject.optBoolean("fix_crypto_coin")
val fixFiatAmt = jsonObject.optBoolean("fix_fiat_amt")
val fixFiatCoin = jsonObject.optBoolean("fix_fiat_coin")
val theme = jsonObject.optString("theme")
val language = jsonObject.optString("language")
currentActivity?.apply {
ParticleNetwork.openBuy(
this,
walletAddress = walletAddress,
amount = fiatAmt,
fiatCoin = fiatCoin,
cryptoCoin = cryptoCoin,
fixFiatCoin = fixFiatCoin,
fixFiatAmt = fixFiatAmt,
fixCryptoCoin = fixCryptoCoin,
theme = theme,
language = language,
chainName = chainName
)
}
}
@ReactMethod
fun setDisplayTokenAddresses(tokenAddressJson: String) {
val tokenAddresses = GsonUtils.fromJson<List<String>>(
tokenAddressJson, object : TypeToken<List<String>>() {}.type
)
ParticleWallet.displayTokenAddresses(tokenAddresses as MutableList<String>)
}
@ReactMethod
fun setDisplayNFTContractAddresses(nftContractAddresses: String) {
val nftContractAddressList = GsonUtils.fromJson<List<String>>(
nftContractAddresses, object : TypeToken<List<String>>() {}.type
)
ParticleNetwork.displayNFTContractAddresses(nftContractAddressList as MutableList<String>)
}
@ReactMethod
fun setPriorityTokenAddresses(addresses: String) {
ParticleNetwork.priorityTokenAddresses(
GsonUtils.fromJson<List<String>>(
addresses, object : TypeToken<List<String>>() {}.type
) as MutableList<String>
)
}
@ReactMethod
fun setPriorityNFTContractAddresses(addresses: String) {
ParticleNetwork.priorityNFTContractAddresses(
GsonUtils.fromJson<List<String>>(
addresses, object : TypeToken<List<String>>() {}.type
) as MutableList<String>
)
}
@ReactMethod
fun setShowLanguageSetting(isShow: Boolean) {
ParticleWallet.setShowLanguageSetting(isShow)
}
@ReactMethod
fun setShowAppearanceSetting(isShow: Boolean) {
ParticleWallet.setShowAppearanceSetting(isShow)
}
@ReactMethod
fun setSupportAddToken(isShow: Boolean) {
ParticleWallet.setSupportAddToken(isShow)
}
@ReactMethod
fun setSupportDappBrowser(isShow: Boolean) {
ParticleWallet.setSupportDappBrowser(isShow)
}
@ReactMethod
fun setCustomWalletIcon(icon: String) {
ParticleWallet.setWalletIcon(icon)
}
@ReactMethod
fun navigatorDappBrowser(url: String?) {
if (TextUtils.isEmpty(url)) {
ParticleWallet.navigatorDAppBrowser("")
} else {
ParticleWallet.navigatorDAppBrowser(url!!)
}
}
override fun getName(): String {
return "ParticleWalletPlugin"
}
}