-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSignForm.js
More file actions
1360 lines (1266 loc) · 46.4 KB
/
SignForm.js
File metadata and controls
1360 lines (1266 loc) · 46.4 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { useState, useEffect } from 'react'
import { useTranslation, Trans } from 'next-i18next'
import { useRouter } from 'next/router'
import Link from 'next/link'
import axios from 'axios'
import Image from 'next/image'
import Select from 'react-select'
import { useIsMobile } from '../utils/mobile'
import {
server,
devNet,
delay,
encode,
networkId,
floatToXlfHex,
rewardRateHuman,
encodeAddressR,
isAddressValid,
removeQueryParams,
webSiteName,
xahauNetwork
} from '../utils'
import { duration } from '../utils/format'
import { payloadXamanPost, xamanWsConnect, xamanCancel, xamanProcessSignedData } from '../utils/xaman'
import { gemwalletTxSend } from '../utils/gemwallet'
import { ledgerwalletTxSend } from '../utils/ledgerwallet'
import { trezorTxSend } from '../utils/trezor'
import { metamaskTxSend } from '../utils/metamask'
import { crossmarkTxSend } from '../utils/crossmark'
import XamanQr from './Xaman/Qr'
import CheckBox from './UI/CheckBox'
import TargetTableSelect from './UI/TargetTableSelect'
import { submitProAddressToVerify } from '../utils/pro'
import { setAvatar } from '../utils/blobVerifications'
import SetAvatar from './SignForms/SetAvatar'
import SetDomain from './SignForms/SetDomain'
import SetDid from './SignForms/SetDid'
import NFTokenCreateOffer from './SignForms/NFTokenCreateOffer'
import NftTransfer from './SignForms/NftTransfer'
import { WalletConnect } from './Walletconnect'
import NFTokenModify from './SignForms/NFTokenModify'
const qr = '/images/qr.gif'
const voteTxs = ['castVoteRewardDelay', 'castVoteRewardRate', 'castVoteHook', 'castVoteSeat']
const askInfoScreens = [
...voteTxs,
'NFTokenAcceptOffer',
'NFTokenCreateOffer',
'NFTokenBurn',
'setDomain',
'setDid',
'setAvatar',
'nftTransfer',
'NFTokenModify'
]
const noCheckboxScreens = [...voteTxs, 'setDomain', 'setDid', 'setAvatar']
let transactionFetched = false
export default function SignForm({
setSignRequest,
account,
signRequest,
uuid,
setRefreshPage,
saveAddressData,
setAccount,
wcSession,
setWcSession
}) {
const { t } = useTranslation()
const router = useRouter()
const isMobile = useIsMobile()
const [screen, setScreen] = useState('')
const [status, setStatus] = useState('')
const [showXamanQr, setShowXamanQr] = useState(false)
const [xamanQrSrc, setXamanQrSrc] = useState(qr)
const [xamanUuid, setXamanUuid] = useState(null)
const [expiredQr, setExpiredQr] = useState(false)
const [agreedToRisks, setAgreedToRisks] = useState(false)
const [formError, setFormError] = useState(false)
const [hookData, setHookData] = useState({})
const [seatData, setSeatData] = useState({})
const [targetLayer, setTargetLayer] = useState(signRequest?.layer)
const [erase, setErase] = useState(false)
const [awaiting, setAwaiting] = useState(false)
const [preparedTx, setPreparedTx] = useState(null)
const [rewardRate, setRewardRate] = useState()
const [rewardDelay, setRewardDelay] = useState()
const [choosenWallet, setChoosenWallet] = useState(null)
useEffect(() => {
if (!signRequest) return
//deeplink doesnt work on mobiles when it's not in the onClick event
if (!isMobile) {
txSend()
} else {
//if mobile, but if loggedin as walletconnect
if (account?.address) {
if (account?.wallet === 'walletconnect') {
txSend()
return
}
}
setScreen('choose-app') //can be refactored, so it opens on previous click
}
setHookData({})
setSeatData({})
setErase(false)
//eslint-disable-next-line react-hooks/exhaustive-deps
}, [signRequest])
useEffect(() => {
if (!uuid) return
setScreen('xaman')
setShowXamanQr(false)
setStatus(t('signin.xaman.statuses.wait'))
xamanProcessSignedData({ uuid, afterSigning, onSignIn, afterSubmitExe })
//eslint-disable-next-line react-hooks/exhaustive-deps
}, [uuid])
if (!signRequest) return null
const txSend = (options) => {
//when the request is wallet specific it's a priority, logout if not matched
//when request is not wallet specific, use the account wallet if loggedin
let wallet = signRequest?.wallet || account?.wallet
if (account?.wallet && wallet && account.wallet !== wallet) {
// if loggedin, but account wallet is different from the one in the request
// loggout from the account
setAccount({ ...account, address: null, username: null, wallet: null })
}
if (!wallet) {
// when request is not wallet specific and user is not loggedin
// check saved wallet from options.wallet on previous steps
wallet = choosenWallet
if (!wallet && options?.wallet) {
wallet = options.wallet
// when user choosed a wallet in the form 'choose-app', save the wallet in choosenWallet
setChoosenWallet(options.wallet)
}
}
if (!wallet) {
setScreen('choose-app')
return
}
//default login
let tx = { TransactionType: 'SignIn' }
if (signRequest.request) {
tx = signRequest.request
}
if (wallet === 'xaman' && signRequest.data?.signOnly) {
//for Xaman make "SignIn" when signing only.
tx.TransactionType = 'SignIn'
}
if (tx.TransactionType === 'NFTokenAcceptOffer' && !agreedToRisks && signRequest.offerAmount !== '0') {
setScreen('NFTokenAcceptOffer')
return
}
if (signRequest.action === 'nftTransfer') {
tx.Amount = '0'
if (!agreedToRisks) {
setScreen('nftTransfer')
return
} else {
if (!signRequest.request?.Destination) {
setStatus(t('form.error.address-empty'))
setFormError(true)
return
}
}
}
if (tx.TransactionType === 'NFTokenCreateOffer' || tx.TransactionType === 'URITokenCreateSellOffer') {
if (!agreedToRisks) {
setScreen('NFTokenCreateOffer')
return
} else {
if (signRequest.privateOffer && !signRequest.request?.Destination) {
setStatus(t('form.error.address-empty'))
setFormError(true)
return
}
if (!signRequest.request?.Amount) {
setStatus(t('form.error.price-empty'))
setFormError(true)
return
}
}
}
if (tx.TransactionType === 'NFTokenBurn' && !agreedToRisks) {
setScreen('NFTokenBurn')
return
}
if (tx.TransactionType === 'NFTokenModify' && !agreedToRisks) {
setScreen('NFTokenModify')
return
}
if (signRequest.action === 'setDomain' && !agreedToRisks) {
setScreen('setDomain')
return
}
if (signRequest.action === 'setDid' && !agreedToRisks) {
setScreen('setDid')
return
}
if (signRequest.action === 'setAvatar' && !agreedToRisks) {
setScreen('setAvatar')
return
}
if (signRequest.action && voteTxs.includes(signRequest.action) && !agreedToRisks) {
setScreen(signRequest.action)
return
}
if (signRequest.action === 'castVoteHook' && agreedToRisks && (hookData.value || erase)) {
let hookTopic = '2' //default
if (!hookData.topic) {
if (hookData.topic === 0) {
hookTopic = '0'
}
} else {
hookTopic = hookData.topic
}
tx.HookParameters = [
{
HookParameter: {
HookParameterName: '4C', // L - layer
HookParameterValue: '0' + targetLayer // 01 for L1 table, 02 for L2 table
}
},
{
HookParameter: {
HookParameterName: '54', // T - topic type
HookParameterValue: '480' + hookTopic // H/48 [0x00-0x09]
}
},
{
HookParameter: {
HookParameterName: '56', // V - vote data
HookParameterValue: erase
? '0000000000000000000000000000000000000000000000000000000000000000'
: hookData.value
}
}
]
}
if (signRequest.action === 'castVoteSeat' && agreedToRisks && (seatData.address || erase)) {
tx.HookParameters = [
{
HookParameter: {
HookParameterName: '4C', // L - layer
HookParameterValue: '0' + targetLayer // 01 for L1 table, 02 for L2 table
}
},
{
HookParameter: {
HookParameterName: '54', // T - topic type
HookParameterValue: '53' + (seatData.seat || '13') // S - seat, seat number 0-13 (19)
}
},
{
HookParameter: {
HookParameterName: '56', // V - vote data
HookParameterValue: erase ? '0000000000000000000000000000000000000000' : encodeAddressR(seatData.address)
}
}
]
}
// add memo with domain and source tag
if (!signRequest.data?.signOnly) {
const client = {
Memo: {
MemoData: encode(server?.replace(/^https?:\/\//, ''))
}
}
if (
tx.Memos &&
tx.Memos.length &&
tx.Memos[0]?.Memo?.MemoData !== client.Memo.MemoData &&
tx.Memos[1]?.Memo?.MemoData !== client.Memo.MemoData
) {
tx.Memos.push(client)
} else {
tx.Memos = [client]
}
// Only set source tag if it's not already set from the send page
if (!tx.SourceTag) {
tx.SourceTag = 42697468
}
}
if (!tx.Account && account?.address) {
tx.Account = account.address
}
if (tx.TransactionType === 'Payment') {
tx.Flags = 2147483648
}
//add network ID to transactions for xahau, xahau-testnet and xahau-jshooks
if (networkId === 21337 || networkId === 21338 || networkId === 31338) {
tx.NetworkID = networkId
}
if (wallet === 'xaman') {
xamanTxSending(tx)
} else if (wallet === 'gemwallet') {
gemwalletTxSending(tx)
} else if (wallet === 'ledgerwallet') {
ledgerwalletTxSending(tx)
} else if (wallet === 'trezor') {
trezorTxSending(tx)
} else if (wallet === 'metamask') {
metamaskTxSending(tx)
} else if (wallet === 'walletconnect') {
walletconnectTxSending(tx)
} else if (wallet === 'crossmark') {
crossmarkTxSending(tx)
}
}
const onSignIn = async ({ address, wallet, redirectName }) => {
if (address) {
await saveAddressData({ address, wallet })
//if redirect
if (redirectName) {
signInCancelAndClose()
if (redirectName === 'nfts') {
router.push('/nfts/' + address)
return
} else if (redirectName === 'nft-offers') {
router.push('/nft-offers/' + address)
return
} else if (redirectName === 'account') {
router.push('/account/' + address)
return
}
}
}
}
const gemwalletTxSending = (tx) => {
gemwalletTxSend({ tx, signRequest, afterSubmitExe, afterSigning, onSignIn, setStatus, account, setAwaiting, t })
setScreen('gemwallet')
setStatus(t('signin.statuses.check-app', { appName: 'GemWallet' }))
}
const crossmarkTxSending = (tx) => {
crossmarkTxSend({
tx,
signRequest,
afterSubmitExe,
afterSigning,
onSignIn,
setStatus,
account,
setAwaiting,
t
})
setScreen('crossmark')
setStatus(t('signin.statuses.check-app', { appName: 'Crossmark' }))
}
const ledgerwalletTxSending = (tx) => {
setScreen('ledgerwallet')
setStatus(
'Please, connect your Ledger Wallet and open the XRP app. Note: Nano S does not support some transactions.'
)
ledgerwalletTxSend({ tx, signRequest, afterSubmitExe, afterSigning, onSignIn, setStatus, setAwaiting, t })
}
const trezorTxSending = (tx) => {
setScreen('trezor')
if (tx.TransactionType !== 'SignIn' && tx.TransactionType !== 'Payment') {
setStatus('Unfortunatelly, Trezor supports only XRP Payments, and does not allow other Transaction Types =(')
return
}
setStatus('Please, connect your Trezor Wallet.')
trezorTxSend({ tx, signRequest, afterSubmitExe, afterSigning, onSignIn, setStatus, setAwaiting, t })
}
const metamaskTxSending = async (tx) => {
setScreen('metamask')
if (tx.TransactionType.includes('URIToken')) {
setStatus('Unfortunatelly, Metamask XRPL Snap does not support URIToken Transaction Types yet.')
return
}
setStatus('Please, connect your Metamask Wallet.')
metamaskTxSend({ tx, signRequest, afterSubmitExe, afterSigning, onSignIn, setStatus, setAwaiting, t })
}
const walletconnectTxSending = async (tx) => {
setScreen('walletconnect')
setPreparedTx(tx)
setStatus('WalletConnect modal is loading...')
}
const xamanTxSending = (tx) => {
let signInPayload = {
options: {
expire: 3
},
txjson: tx
}
if (signRequest.data?.signOnly) {
signInPayload.options.submit = false
}
//for Xaman to sign transaction in the right network
let forceNetwork = null
if (networkId === 0) {
forceNetwork = 'MAINNET'
} else if (networkId === 1) {
forceNetwork = 'TESTNET'
} else if (networkId === 2) {
forceNetwork = 'DEVNET'
}
signInPayload.options.force_network = forceNetwork
signInPayload.custom_meta = { blob: {} }
if (signRequest.redirect) {
signInPayload.custom_meta.blob.redirect = signRequest.redirect
}
if (signRequest.broker) {
signInPayload.custom_meta.blob.broker = signRequest.broker.name
}
if (signRequest.data) {
signInPayload.custom_meta.blob.data = signRequest.data
}
setStatus(t('signin.xaman.statuses.wait'))
if (isMobile) {
setStatus(t('signin.xaman.statuses.redirecting'))
//return to the same page
signInPayload.options.return_url = {
app: server + router.asPath + (router.asPath.includes('?') ? '&' : '?') + 'uuid={id}'
}
if (tx.TransactionType === 'Payment') {
//for username receipts
signInPayload.options.return_url.app += '&receipt=true'
}
} else {
const xamanUserToken = localStorage.getItem('xamanUserToken')
if (xamanUserToken) {
signInPayload.user_token = xamanUserToken
}
setShowXamanQr(true)
}
payloadXamanPost(signInPayload, onPayloadResponse)
setScreen('xaman')
}
const onPayloadResponse = (data) => {
if (!data || data.error) {
setShowXamanQr(false)
setStatus(data.error)
return
}
setXamanUuid(data.uuid)
setXamanQrSrc(data.refs.qr_png)
setExpiredQr(false)
if (data.pushed) {
setStatus(t('signin.xaman.statuses.check-push'))
}
if (isMobile) {
if (data.next && data.next.always) {
window.location = data.next.always
} else {
console.log('payload next.always is missing')
}
} else {
setShowXamanQr(true)
setStatus(t('signin.xaman.scan-qr'))
//connect to xaman websocket only if it didn't redirect to the xaman app
xamanWsConnect(data.refs.websocket_status, xamanWsConnected)
}
}
const xamanWsConnected = (obj) => {
if (obj.status === 'canceled') {
//cancel button pressed in xaman app
closeSignInFormAndRefresh()
} else if (obj.opened) {
setStatus(t('signin.statuses.check-app', { appName: 'Xaman' }))
} else if (obj.signed) {
setShowXamanQr(false)
setStatus(t('signin.xaman.statuses.wait'))
xamanProcessSignedData({ uuid: obj.payload_uuidv4, afterSigning, onSignIn, afterSubmitExe })
} else if (obj.expires_in_seconds) {
if (obj.expires_in_seconds <= 0) {
setExpiredQr(true)
setStatus(t('signin.xaman.statuses.expired'))
}
}
}
const checkTxInCrawler = async ({ txid, redirectName, txType }) => {
//TODO: Check the case for failed transactions.
setAwaiting(true)
setStatus(t('signin.status.awaiting-crawler'))
//txid can be in the ledger or not, so we need to check it in the ledger
if (txid && !transactionFetched) {
const response = await axios('xrpl/transaction/' + txid)
if (response.data) {
transactionFetched = true
const { validated, inLedger, ledger_index, meta, TransactionType } = response.data
const includedInLedger = inLedger || ledger_index
if (validated && includedInLedger) {
if (TransactionType === 'NFTokenMint') {
if (meta.nftoken_id) {
checkCrawlerStatus({ inLedger: includedInLedger, param: meta.nftoken_id, type: TransactionType })
} else {
//if no token found
closeSignInFormAndRefresh()
}
return
} else if (TransactionType === 'URITokenMint') {
let foundToken = false
for (let i = 0; i < meta.AffectedNodes.length; i++) {
const node = meta.AffectedNodes[i]
if (node.CreatedNode?.LedgerEntryType === 'URIToken') {
checkCrawlerStatus({
inLedger: includedInLedger,
param: node.CreatedNode.LedgerIndex,
type: TransactionType
})
foundToken = true
break
}
}
if (!foundToken) {
closeSignInFormAndRefresh()
}
return
} else if (
TransactionType === 'Payment' ||
TransactionType === 'CheckCreate' ||
TransactionType === 'EscrowCreate'
) {
if (signRequest?.callback) {
signRequest.callback({
result: response.data
})
} else {
// For mobile, redirect to transaction page
router.push('/tx/' + response.data.hash)
return
}
closeSignInFormAndRefresh()
return
}
checkCrawlerStatus({ inLedger: includedInLedger, type: TransactionType })
} else {
if (txType === 'Payment' || txType === 'CheckCreate' || txType === 'EscrowCreate') {
closeSignInFormAndRefresh()
return
}
//if not validated or if no ledger info received, delay for 1.5 seconds
delay(1500, checkTxInCrawler, { txid, redirectName })
}
} else {
//if no info on transaction, delay 1.5 sec and try again
delay(1500, checkTxInCrawler, { txid, redirectName })
}
} else {
//if no tx data, delay 3 sec
delay(3000, closeSignInFormAndRefresh)
}
}
const afterSigning = async ({ signRequestData, blob, address }) => {
if (signRequestData?.action === 'pro-add-address') {
//add address to the list
submitProAddressToVerify(
{
address: signRequestData.address,
name: signRequestData.name,
blob
},
(res) => {
if (res?.error) {
setStatus(t(res.error))
} else {
closeSignInFormAndRefresh()
}
}
)
return
}
if (signRequestData?.action === 'set-avatar') {
//add address to the list
setAvatar({ address, blob }, (res) => {
if (res?.error) {
setStatus(t(res.error))
} else {
if (signRequestData?.redirect === 'account') {
delay(3000, () => {
closeSignInFormAndRefresh()
router.push('/account/' + address)
})
} else {
delay(3000, closeSignInFormAndRefresh)
}
}
})
return
}
}
const afterSubmitExe = async ({ redirectName, broker, txHash, txType }) => {
//if broker, notify about the offer
if (broker) {
setStatus(t('signin.status.awaiting-broker', { serviceName: broker }))
if (broker === 'bidds') {
setAwaiting(true)
const response = await axios('/v2/bidds/transaction/broker/' + txHash).catch(() => {
console.log('ERROR: can not get bidds transaction')
setStatus(t('signin.status.failed-broker', { serviceName: broker }))
closeSignInFormAndRefresh() //setAwaiting false inside
})
setAwaiting(false)
if (response?.data) {
/*
{
"status": true,
"code": 200,
"message": "Data Fetch Successfully",
"data": [
{
"Amount": "8880000",
"Destination": "rn6CYo6uSxR6fP7jWg3c8SL5jrqTc2GjCS",
"NFTokenID": "00081B580D828F028B88C7A78C67A2A9719DDB0A902A927EA72C172100000588",
"Owner": "rDzvW4ddvvDXhJNEGFWGkPQ9SYuUeMjKU5",
"Index": "E8E06CE995ABAA2D30AAE21725DFB4D27268F501113E4333120B6CC7E009171A",
"Date": "2023-11-07T10:18:31.000Z"
}
]
}
*/
const responseData = response.data
if (responseData.status && responseData.data?.hash) {
// hash of the offer accept transaction
checkTxInCrawler({ txid: responseData.data.hash, redirectName })
} else {
setStatus(t('signin.status.failed-broker', { serviceName: broker }))
delay(3000, closeSignInFormAndRefresh)
}
} else {
setStatus(t('signin.status.failed-broker', { serviceName: broker }))
delay(3000, closeSignInFormAndRefresh)
}
}
return
}
// For NFT and DID transaction, lets wait for crawler to finish it's job
if (
txType?.includes('NFToken') ||
txType?.includes('URIToken') ||
txType?.includes('DID') ||
txType === 'Payment' ||
txType === 'CheckCreate' ||
txType === 'EscrowCreate'
) {
checkTxInCrawler({ txid: txHash, redirectName, txType })
return
} else {
if (txType === 'AccountSet' && signRequest?.callback) {
signRequest.callback()
}
// no checks or delays for non NFT/DID transactions
closeSignInFormAndRefresh()
}
}
const checkCrawlerStatus = async ({ inLedger, param, type }) => {
let crawler = xahauNetwork ? 'uritokens' : 'nftokens'
if (type && type.includes('DID')) {
crawler = 'dids'
}
const crawlerResponse = await axios('v2/statistics/' + crawler + '/crawler')
if (crawlerResponse.data) {
const { ledgerIndex } = crawlerResponse.data
// if crawler 10 ledgers behind, update right away
// the backend suppose to return info directly from ledger when crawler 30 seconds behind
// othewrwise wait until crawler catch up with the ledger where this transaction was included
if (ledgerIndex >= inLedger || inLedger - 10 > ledgerIndex) {
if (param) {
//when we are back from xaman, there no signRequest, we can not call a callback
// shall we redirect to nft page instead?
if (signRequest?.callback) {
signRequest.callback(param)
} else {
//we are on mobile
if (type === 'NFTokenMint' || type === 'URITokenMint') {
router.push('/nft/' + param)
return
}
}
}
closeSignInFormAndRefresh()
} else {
//check again in 1 second if crawler ctached up with the ledger where transaction was included
delay(1000, checkCrawlerStatus, { inLedger, param, type })
}
}
}
const closeSignInFormAndRefresh = () => {
signInCancelAndClose()
setRefreshPage(Date.now())
}
const signInCancelAndClose = () => {
if (screen === 'xaman') {
setXamanQrSrc(qr)
xamanCancel(xamanUuid)
}
if (uuid) {
removeQueryParams(router, ['uuid'])
}
setScreen('choose-app')
setSignRequest(null)
setAwaiting(false)
setStatus('')
transactionFetched = false
}
const buttonStyle = {
margin: '0 10px'
}
const onRewardDelayChange = (e) => {
setStatus('')
let newRequest = signRequest
let delay = e.target.value
setRewardDelay(delay)
delay = delay.trim()
let n = Math.floor(Number(delay))
if (n !== Infinity && String(n) === delay && n > 0) {
newRequest.request.HookParameters = [
{
HookParameter: {
HookParameterName: '4C', // L - layer
HookParameterValue: '01' // 01 for L1 table, 02 for L2 table
}
},
{
HookParameter: {
HookParameterName: '54', // T - topic type
HookParameterValue: '5244' // RD - Reward delay
}
},
{
HookParameter: {
HookParameterName: '56', // V - vote data
HookParameterValue: floatToXlfHex(delay) // "0000A7DCF750D554" - 60 seconds
}
}
]
setSignRequest(newRequest)
setAgreedToRisks(true)
} else {
setStatus('Delay should be a positive integer')
setAgreedToRisks(false)
}
}
const onRewardRateChange = (e) => {
setStatus('')
let newRequest = signRequest
let rate = e.target.value
setRewardRate(rate)
rate = rate.trim()
if (rate >= 0 && rate <= 1) {
newRequest.request.HookParameters = [
{
HookParameter: {
HookParameterName: '4C', // L - layer
HookParameterValue: '01' // 01 for L1 table, 02 for L2 table
}
},
{
HookParameter: {
HookParameterName: '54', // T - topic type
HookParameterValue: '5252' // RR - reward rate
}
},
{
HookParameter: {
HookParameterName: '56', // V - vote data
HookParameterValue: floatToXlfHex(rate)
}
}
]
setSignRequest(newRequest)
setAgreedToRisks(true)
} else {
setStatus('Rate should be a number from 0 to 1')
setAgreedToRisks(false)
}
}
const onSeatSelect = (data) => {
let seatObj = seatData
seatObj.seat = data.value
setSeatData(seatObj)
}
const onSeatValueChange = (value) => {
setStatus('')
setAgreedToRisks(false)
if (!value) return
if (!isAddressValid(value)) {
setStatus('Invalid address')
return
}
setAgreedToRisks(true)
let seatObj = seatData
seatObj.address = value
setSeatData(seatObj)
}
const onPlaceSelect = (topic) => {
let hookObj = hookData
hookObj.topic = topic.value
setHookData(hookObj)
}
const onHookValueChange = (value) => {
setStatus('')
setAgreedToRisks(false)
if (!value) return
if (value.length !== 64) {
setStatus('Invalid Hook value')
return
}
setAgreedToRisks(true)
let hookObj = hookData
hookObj.value = value
setHookData(hookObj)
}
const onEraseCheck = () => {
setStatus('')
if (!erase) {
setAgreedToRisks(true)
} else {
setAgreedToRisks(false)
}
setErase(!erase)
}
const xls35Sell = signRequest?.request?.TransactionType === 'URITokenCreateSellOffer'
const checkBoxText = (screen, signRequest) => {
if (screen === 'nftTransfer')
return (
<Trans i18nKey="signin.confirm.nft-transfer">
I'm offering that NFT for FREE to the Destination account,{' '}
<span className="orange bold">the destination account would need to accept the NFT transfer</span>.
</Trans>
)
if (screen === 'NFTokenBurn') return t('signin.confirm.nft-burn')
if (screen === 'NFTokenModify') return 'I understand that URI will be updated for this NFT.'
if (screen === 'NFTokenCreateOffer' && (signRequest.request.Flags === 1 || xls35Sell)) {
return t('signin.confirm.nft-create-sell-offer')
}
return (
<Trans i18nKey="signin.confirm.nft-accept-offer">
I admit that {{ webSiteName }} gives me access to a decentralised marketplace, and it cannot verify or guarantee
the authenticity and legitimacy of any NFTs. I confirm that I've read the{' '}
<Link href="/terms-and-conditions" target="_blank">
Terms and conditions
</Link>
, and I agree with all the terms to buy, sell or use any NFTs on {{ webSiteName }}.
</Trans>
)
}
const walletNames = {
xaman: 'Xaman',
gemwallet: 'GemWallet',
ledgerwallet: 'Ledger Wallet',
trezor: 'Trezor',
metamask: 'Metamask',
walletconnect: 'WalletConnect',
crossmark: 'Crossmark'
}
return (
<>
{(networkId === 0 || networkId === 1) && (
<WalletConnect
tx={preparedTx}
signRequest={signRequest}
setStatus={setStatus}
afterSubmitExe={afterSubmitExe}
onSignIn={onSignIn}
setAwaiting={setAwaiting}
afterSigning={afterSigning}
session={wcSession}
setSession={setWcSession}
/>
)}
{screen && (
<div className="sign-in-form">
<div className="sign-in-body center">
<div className="close-button" onClick={signInCancelAndClose}></div>
{askInfoScreens.includes(screen) ? (
<>
<div className="header">
{screen === 'NFTokenBurn' && t('signin.confirm.nft-burn-header')}
{screen === 'NFTokenModify' && "Update NFT's URI"}
{screen === 'NFTokenAcceptOffer' &&
(signRequest.offerType === 'buy'
? t('signin.confirm.nft-accept-buy-offer-header')
: t('signin.confirm.nft-accept-sell-offer-header'))}
{screen === 'NFTokenCreateOffer' &&
(signRequest.request.Flags === 1 || xls35Sell
? t('signin.confirm.nft-create-sell-offer-header')
: t('signin.confirm.nft-create-buy-offer-header'))}
{screen === 'nftTransfer' && t('signin.confirm.nft-create-transfer-offer-header')}
{screen === 'setDomain' && t('signin.confirm.set-domain')}
{screen === 'setDid' && t('signin.confirm.set-did')}
{screen === 'setAvatar' && t('signin.confirm.set-avatar')}
{voteTxs.includes(screen) && 'Cast a vote'}
</div>
{screen === 'NFTokenCreateOffer' && (
<NFTokenCreateOffer
signRequest={signRequest}
setSignRequest={setSignRequest}
setStatus={setStatus}
setFormError={setFormError}
/>
)}
{screen === 'NFTokenModify' && (
<NFTokenModify signRequest={signRequest} setSignRequest={setSignRequest} setStatus={setStatus} />
)}
{screen === 'nftTransfer' && (
<NftTransfer
signRequest={signRequest}
setSignRequest={setSignRequest}
setStatus={setStatus}
setFormError={setFormError}
/>
)}
{screen === 'setDomain' && (
<SetDomain
setSignRequest={setSignRequest}
signRequest={signRequest}
setStatus={setStatus}
setAgreedToRisks={setAgreedToRisks}
/>
)}
{screen === 'setDid' && (
<SetDid
setSignRequest={setSignRequest}
signRequest={signRequest}
setStatus={setStatus}