Skip to content

Commit a5d27a7

Browse files
committed
fix
1 parent aea7020 commit a5d27a7

File tree

11 files changed

+133
-79
lines changed

11 files changed

+133
-79
lines changed

packages/neuron-ui/src/components/SignAndVerify/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface PasswordDialogProps {
2929
show: boolean
3030
walletName: string
3131
onCancel: () => void
32-
onSubmit: (pwd: string) => Promise<ControllerResponse | void>
32+
onSubmit: (pwd: string) => Promise<ControllerResponse>
3333
}
3434

3535
export const PasswordDialog = ({ show, walletName, onCancel, onSubmit }: PasswordDialogProps) => {
@@ -63,7 +63,7 @@ export const PasswordDialog = ({ show, walletName, onCancel, onSubmit }: Passwor
6363
setLoading(true)
6464
onSubmit(password)
6565
.then(res => {
66-
if (res?.status === ErrorCode.PasswordIncorrect) {
66+
if (res.status === ErrorCode.PasswordIncorrect) {
6767
setError((res.message as { content: string }).content)
6868
}
6969
})

packages/neuron-ui/src/components/WCSignTransactionDialog/index.tsx

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useState, useCallback, useMemo, useEffect } from 'react'
1+
import React, { useState, useCallback, useEffect } from 'react'
22
import { useNavigate } from 'react-router-dom'
33
import { useTranslation } from 'react-i18next'
44
import TextField from 'widgets/TextField'
55
import Dialog from 'widgets/Dialog'
6-
import { ErrorCode, RoutePath, isSuccessResponse, errorFormatter } from 'utils'
6+
import { ErrorCode, isSuccessResponse, errorFormatter } from 'utils'
77
import {
88
useState as useGlobalState,
99
useDispatch,
@@ -12,19 +12,21 @@ import {
1212
sendCreateSUDTAccountTransaction,
1313
sendSUDTTransaction,
1414
} from 'states'
15-
import { SignTransactionParams } from 'ckb-walletconnect-wallet-sdk'
16-
import { OfflineSignType, OfflineSignStatus, signAndExportTransaction } from 'services/remote'
15+
import { SessionRequest } from 'ckb-walletconnect-wallet-sdk'
16+
import { OfflineSignType, OfflineSignStatus, signAndExportTransaction, signTransactionOnly } from 'services/remote'
1717
import { PasswordIncorrectException } from 'exceptions'
1818
import styles from './wcSignTransactionDialog.module.scss'
1919

2020
const WCSignTransactionDialog = ({
2121
wallet,
22-
data,
22+
event,
2323
onDismiss,
24+
onApproveRequest,
2425
}: {
2526
wallet: State.Wallet
26-
data: SignTransactionParams
27+
event: SessionRequest
2728
onDismiss: () => void
29+
onApproveRequest: (event: SessionRequest, options: any) => void
2830
}) => {
2931
const {
3032
app: {
@@ -34,6 +36,7 @@ const WCSignTransactionDialog = ({
3436
} = useGlobalState()
3537

3638
const walletID = wallet.id
39+
const data = event.params.request.params
3740
const {
3841
transaction,
3942
type: signType = OfflineSignType.Regular,
@@ -78,6 +81,22 @@ const WCSignTransactionDialog = ({
7881
onDismiss()
7982
}, [data, dispatch, onDismiss, t, password, walletID])
8083

84+
const sign = useCallback(async () => {
85+
const res = await signTransactionOnly({
86+
transaction,
87+
type: signType,
88+
status: signStatus,
89+
walletID,
90+
password,
91+
})
92+
if (!isSuccessResponse(res)) {
93+
setError(errorFormatter(res.message, t))
94+
return
95+
}
96+
onApproveRequest(event, res.result)
97+
onDismiss()
98+
}, [data, dispatch, onDismiss, t, password, walletID])
99+
81100
const onSubmit = useCallback(
82101
async (e?: React.FormEvent) => {
83102
if (e) {
@@ -88,7 +107,7 @@ const WCSignTransactionDialog = ({
88107
}
89108
setIsSigning(true)
90109
if (!isBroadcast) {
91-
await signAndExport()
110+
await sign()
92111
setIsSigning(false)
93112
return
94113
}
@@ -98,10 +117,11 @@ const WCSignTransactionDialog = ({
98117
if (isSigning) {
99118
break
100119
}
101-
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(({ status }) => {
102-
if (isSuccessResponse({ status })) {
103-
navigate(RoutePath.History)
104-
} else if (status === ErrorCode.PasswordIncorrect) {
120+
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(res => {
121+
if (isSuccessResponse(res.status)) {
122+
onApproveRequest(event, res.result)
123+
onDismiss()
124+
} else if (res.status === ErrorCode.PasswordIncorrect) {
105125
throw new PasswordIncorrectException()
106126
}
107127
})
@@ -111,13 +131,11 @@ const WCSignTransactionDialog = ({
111131
if (isSigning) {
112132
break
113133
}
114-
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(({ status }) => {
115-
if (isSuccessResponse({ status })) {
116-
dispatch({
117-
type: AppActions.SetGlobalDialog,
118-
payload: 'unlock-success',
119-
})
120-
} else if (status === ErrorCode.PasswordIncorrect) {
134+
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(res => {
135+
if (isSuccessResponse(res)) {
136+
onApproveRequest(event, res.result)
137+
onDismiss()
138+
} else if (res.status === ErrorCode.PasswordIncorrect) {
121139
throw new PasswordIncorrectException()
122140
}
123141
})
@@ -130,10 +148,11 @@ const WCSignTransactionDialog = ({
130148
tx: transaction,
131149
password,
132150
}
133-
await sendCreateSUDTAccountTransaction(params)(dispatch).then(({ status }) => {
134-
if (isSuccessResponse({ status })) {
135-
navigate(RoutePath.History)
136-
} else if (status === ErrorCode.PasswordIncorrect) {
151+
await sendCreateSUDTAccountTransaction(params)(dispatch).then(res => {
152+
if (isSuccessResponse(res)) {
153+
onApproveRequest(event, res.result)
154+
onDismiss()
155+
} else if (res.status === ErrorCode.PasswordIncorrect) {
137156
throw new PasswordIncorrectException()
138157
}
139158
})
@@ -145,10 +164,11 @@ const WCSignTransactionDialog = ({
145164
tx: transaction,
146165
password,
147166
}
148-
await sendSUDTTransaction(params)(dispatch).then(({ status }) => {
149-
if (isSuccessResponse({ status })) {
150-
navigate(RoutePath.History)
151-
} else if (status === ErrorCode.PasswordIncorrect) {
167+
await sendSUDTTransaction(params)(dispatch).then(res => {
168+
if (isSuccessResponse(res)) {
169+
onApproveRequest(event, res.result)
170+
onDismiss()
171+
} else if (res.status === ErrorCode.PasswordIncorrect) {
152172
throw new PasswordIncorrectException()
153173
}
154174
})
@@ -192,14 +212,10 @@ const WCSignTransactionDialog = ({
192212
[setPassword, setError]
193213
)
194214

195-
const title = useMemo(() => {
196-
return !isBroadcast ? t('offline-sign.sign-and-export') : t('offline-sign.sign-and-broadcast')
197-
}, [isBroadcast, t])
198-
199215
return (
200216
<Dialog
201217
show
202-
title={title}
218+
title={t('wallet-connect.sign-confirmation')}
203219
onCancel={onDismiss}
204220
onConfirm={onSubmit}
205221
disabled={disabled}

packages/neuron-ui/src/components/WalletConnect/ItemComponents.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import React, { useState, useCallback } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import Button from 'widgets/Button'
4-
import { clsx } from 'utils'
4+
import { clsx, CONSTANTS, shannonToCKBFormatter } from 'utils'
55
import { SCRIPT_BASES } from 'utils/const'
6-
import { Proposal, Session, SessionRequest } from 'ckb-walletconnect-wallet-sdk'
6+
import { scriptToAddress } from '@nervosnetwork/ckb-sdk-utils'
7+
import { useState as useGlobalState } from 'states'
8+
import { Proposal, Session, SessionRequest, SignTransactionParams } from 'ckb-walletconnect-wallet-sdk'
79
import { DetailIcon } from 'widgets/Icons/icon'
810
import styles from './walletConnect.module.scss'
911

12+
const { MAINNET_TAG } = CONSTANTS
13+
1014
interface PrososalItemProps {
1115
data: Proposal
1216
key: string
@@ -169,10 +173,20 @@ interface TransactionItemProps {
169173
}
170174

171175
export const TransactionItem = ({ data, approve, sessions, onRejectRequest, key }: TransactionItemProps) => {
176+
const {
177+
chain: { networkID },
178+
settings: { networks },
179+
} = useGlobalState()
172180
const [t] = useTranslation()
181+
const network = networks.find(n => n.id === networkID)
182+
const isMainnet = network != null && network.chain === MAINNET_TAG
173183
const session = sessions.find(item => item.topic === data.topic)
174184
const { name = '', url = '' } = session?.peer?.metadata || {}
175-
const { params } = data.params.request
185+
const params = data.params.request.params as SignTransactionParams
186+
187+
const { outputs = [], fee, description } = params.transaction
188+
const firstOutputAddress = outputs.length ? scriptToAddress(outputs[0].lock, isMainnet) : ''
189+
const capacity = outputs.reduce((pre, cur) => BigInt(pre) + BigInt(cur.capacity), BigInt(0)).toString()
176190

177191
return (
178192
<div className={clsx(styles.itemWrap, styles.transactionItem)} key={key}>
@@ -188,20 +202,23 @@ export const TransactionItem = ({ data, approve, sessions, onRejectRequest, key
188202
<div>
189203
<h3>{t('transaction.address')}</h3>
190204
<p>
191-
<span className={styles.address}>ckb1qrfkjktl73ljn77q...v837h2rmne7esqhxzhpm</span> (+2 addresses)
205+
<span className={styles.address}>
206+
{firstOutputAddress.slice(0, 16)}...{firstOutputAddress.slice(-16)}
207+
</span>{' '}
208+
(+{outputs.length} addresses)
192209
</p>
193210
<h3>{t('send.fee')}</h3>
194-
<p>{params.transaction.fee}</p>
211+
<p>{shannonToCKBFormatter(fee)}</p>
195212
<h3>{t('send.description')}</h3>
196-
<p>{params.transaction.description}</p>
213+
<p>{description}</p>
197214
<button type="button" className={styles.detailButton} onClick={() => {}} data-hash="hash00000">
198215
<DetailIcon />
199216
{t('wallet-connect.view-details')}
200217
</button>
201218
</div>
202219
<div>
203220
<h3>{t('transaction.amount')}</h3>
204-
<p>-</p>
221+
<p>{shannonToCKBFormatter(capacity)}</p>
205222
<h3>{t('wallet-connect.locked-period')}</h3>
206223
<p>-</p>
207224
</div>

packages/neuron-ui/src/components/WalletConnect/hooks.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback } from 'react'
22
import { useState as useGlobalState } from 'states'
33
import { connect, disconnect, approveSession, rejectSession, approveRequest, rejectRequest } from 'services/remote'
44
import { ControllerResponse } from 'services/remote/remoteApiWrapper'
5+
import { SessionRequest } from 'ckb-walletconnect-wallet-sdk'
56

67
export const useWalletConnect = () => {
78
const {
@@ -16,10 +17,11 @@ export const useWalletConnect = () => {
1617
return res
1718
}, [])
1819

19-
const onDisconnect = useCallback(async e => {
20-
const { topic } = e.target.dataset
21-
const res: ControllerResponse = await disconnect(topic)
22-
return res
20+
const onDisconnect = useCallback(async (e: React.SyntheticEvent<HTMLButtonElement>) => {
21+
const { topic } = e.currentTarget.dataset
22+
if (topic) {
23+
await disconnect(topic)
24+
}
2325
}, [])
2426

2527
const onApproveSession = useCallback(async (id, scriptBases) => {
@@ -30,22 +32,22 @@ export const useWalletConnect = () => {
3032
return res
3133
}, [])
3234

33-
const onRejectSession = useCallback(async e => {
34-
const { id } = e.target.dataset
35+
const onRejectSession = useCallback(async (e: React.SyntheticEvent<HTMLButtonElement>) => {
36+
const { id } = e.currentTarget.dataset
3537
await rejectSession({ id: Number(id) })
3638
}, [])
3739

3840
const onApproveRequest = useCallback(
39-
async (event, options) => {
41+
async (event: SessionRequest, options: any) => {
4042
const res: ControllerResponse = await approveRequest({ event, options })
4143
return res
4244
},
4345
[requests]
4446
)
4547

4648
const onRejectRequest = useCallback(
47-
async e => {
48-
const { id } = e.target.dataset
49+
async (e: React.SyntheticEvent<HTMLButtonElement>) => {
50+
const { id } = e.currentTarget.dataset
4951
const event = requests.find(item => item.id === Number(id))
5052
if (event) {
5153
await rejectRequest({ event })

packages/neuron-ui/src/components/WalletConnect/index.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,22 @@ const WalletConnect = () => {
8080
const approveSignTransaction = useCallback(
8181
item => {
8282
setCurrentEvent(item)
83-
8483
setDialogType('signTransaction')
8584
},
86-
[onApproveRequest]
85+
[setCurrentEvent, setDialogType]
8786
)
8887

8988
const handleSignMessage = useCallback(
9089
async password => {
91-
if (!currentEvent) return null
92-
93-
const { address, message } = currentEvent.params.request.params
90+
const { address, message } = currentEvent!.params.request.params
9491
const res: ControllerResponse = await signMessage({
9592
walletID: wallet?.id ?? '',
9693
address,
9794
message,
9895
password,
9996
})
10097
if (isSuccessResponse(res)) {
101-
onApproveRequest(currentEvent, res.result)
98+
onApproveRequest(currentEvent!, res.result)
10299
setDialogType('')
103100
} else if (res.status === ErrorCode.PasswordIncorrect) {
104101
// pass through this kind of error
@@ -245,15 +242,17 @@ const WalletConnect = () => {
245242
}}
246243
/>
247244

248-
<PasswordDialog
249-
show={dialogType === 'pwd' && !!currentEvent}
250-
walletName={wallet?.name}
251-
onCancel={onDismiss}
252-
onSubmit={handleSignMessage}
253-
/>
245+
{dialogType === 'pwd' && currentEvent ? (
246+
<PasswordDialog show walletName={wallet?.name} onCancel={onDismiss} onSubmit={handleSignMessage} />
247+
) : null}
254248

255-
{dialogType === 'signTransaction' ? (
256-
<WCSignTransactionDialog wallet={wallet} onDismiss={onDismiss} data={currentEvent?.params.request.params} />
249+
{dialogType === 'signTransaction' && currentEvent ? (
250+
<WCSignTransactionDialog
251+
wallet={wallet}
252+
onDismiss={onDismiss}
253+
onApproveRequest={onApproveRequest}
254+
event={currentEvent}
255+
/>
257256
) : null}
258257
</PageContainer>
259258
)

packages/neuron-ui/src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,8 @@
11681168
"sign-info": "Signature Information",
11691169
"transfer-info": "Payment Information",
11701170
"view-details": "View Details",
1171-
"locked-period": "Locked Period"
1171+
"locked-period": "Locked Period",
1172+
"sign-confirmation": "Signature Confirmation"
11721173
}
11731174
}
11741175
}

packages/neuron-ui/src/locales/zh-tw.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@
11391139
"sign-info": "簽名信息",
11401140
"transfer-info": "支付信息",
11411141
"view-details": "查看詳情",
1142-
"locked-period": "鎖定時間"
1142+
"locked-period": "鎖定時間",
1143+
"sign-confirmation": "簽名確認"
11431144
}
11441145
}
11451146
}

packages/neuron-ui/src/locales/zh.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,8 @@
11601160
"sign-info": "签名信息",
11611161
"transfer-info": "支付信息",
11621162
"view-details": "查看详情",
1163-
"locked-period": "锁定时间"
1163+
"locked-period": "锁定时间",
1164+
"sign-confirmation": "签名确认"
11641165
}
11651166
}
11661167
}

0 commit comments

Comments
 (0)