-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBridgeGUI.kt
283 lines (252 loc) · 7.95 KB
/
BridgeGUI.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
package com.particlewallet.module
import android.content.Intent
import android.util.Log
import network.blankj.utilcode.util.GsonUtils
import network.blankj.utilcode.util.LogUtils
import com.connect.common.IConnectAdapter
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.api.service.DBService
import com.particle.base.ParticleNetwork
import com.particle.base.model.MobileWCWalletName
import com.particle.gui.ParticleWallet
import com.particle.gui.ParticleWallet.navigatorBuy
import com.particle.gui.ParticleWallet.setPayDisabled
import com.particle.gui.ParticleWallet.setSwapDisabled
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.swap.SwapConfig
import com.particle.gui.ui.token_detail.TokenTransactionRecordsParams
import com.particle.gui.utils.WalletUtils
import com.particle.network.ParticleNetworkAuth.getAddress
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 network.particle.chains.ChainInfo
import org.json.JSONObject
import java.math.BigInteger
class BridgeGUI(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
}
}
fun setPNWallet() {
if (!isUIModuleInit()) return;
BridgeScope.launch {
val address = ParticleNetwork.getAddress()
val wallet = WalletUtils.createSelectedWallet(address, MobileWCWalletName.Particle.name)
WalletUtils.setWalletChain(wallet)
}
}
fun createSelectedWallet(publicAddress: String, adapter: IConnectAdapter) {
BridgeScope.launch {
val wallet = WalletUtils.createSelectedWallet(publicAddress, adapter)
WalletUtils.setWalletChain(wallet)
}
}
}
@ReactMethod
fun init() {
isUIInit = try {
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(disable: Boolean) {
LogUtils.d("setPayDisabled", disable.toString());
ParticleWallet.setPayDisabled(disable)
}
@ReactMethod
fun setSwapDisabled(disable: Boolean) {
LogUtils.d("setSwapDisabled", disable.toString());
ParticleWallet.setSwapDisabled(disable)
}
@ReactMethod
fun getSwapDisabled(callback: Callback) {
callback.invoke(ParticleWallet.getSwapDisabled())
}
@ReactMethod
fun navigatorPay() {
currentActivity?.let {
ParticleWallet.navigatorBuy(it)
}
}
/**
* const obj = { from_token_address: fromTokenAddress, to_token_address: toTokenAddress, amount: amount }
* const json = JSON.stringify(obj);
*/
@ReactMethod
fun navigatorSwap(jsonParams: String) {
try {
val jobj = JSONObject(jsonParams)
val fromTokenAddress = jobj.optString("from_token_address")
val toTokenAddress = jobj.optString("to_token_address")
val config = SwapConfig(
fromTokenAddress,
toTokenAddress
)
PNRouter.navigatorSwap(config);
} catch (e: Exception) {
e.printStackTrace()
PNRouter.navigatorSwap(null);
}
}
@ReactMethod
fun showTestNetwork(show: Boolean) {
LogUtils.d("showTestNetwork", show);
ParticleWallet.setShowTestNetworkSetting(show)
}
@ReactMethod
fun showManageWallet(show: Boolean) {
LogUtils.d("showManageWallet", show);
ParticleWallet.setShowManageWalletSetting(show)
}
/**
* GUI
*/
@ReactMethod
fun supportChain(jsonParams: String) {
LogUtils.d("setSupportChain", jsonParams);
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) {
LogUtils.d("switchWallet", jsonParams);
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)
}
}
override fun getName(): String {
return "BridgeGUI"
}
}