-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: integrate WalletConnet #2880
Draft
devchenyan
wants to merge
19
commits into
nervosnetwork:develop
Choose a base branch
from
devchenyan:feat-walletconnect
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
96fb4d6
feat: integrate WalletConnet
devchenyan f831fe1
Merge branch 'develop'
devchenyan 1823462
fix: filename
devchenyan aea7020
Merge branch 'develop'
devchenyan a5d27a7
fix
devchenyan 288aaac
Merge branch 'develop'
devchenyan b2cf0cf
feat: update
devchenyan 121e76f
Merge branch 'develop'
devchenyan b682672
feat: askForMediaAccess
devchenyan 69e9b31
feat: events
devchenyan e6a0370
feat: update screenScan
devchenyan 0138f11
Merge branch 'develop' into feat-walletconnect
devchenyan 27e91b6
feat: add accountName
devchenyan c00bb75
feat: @ckb-connect
devchenyan 90c5e1a
Merge branch 'develop' into feat-walletconnect
devchenyan c266a7d
fix
devchenyan d450828
Merge branch 'develop' into feat-walletconnect
devchenyan 40d80e0
feat: wc support acp
devchenyan d063d9b
Merge branch 'develop' into feat-walletconnect
devchenyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
packages/neuron-ui/src/components/WCSignTransactionDialog/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
import React, { useState, useCallback, useMemo, useEffect } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
import { useTranslation } from 'react-i18next' | ||
import TextField from 'widgets/TextField' | ||
import Dialog from 'widgets/Dialog' | ||
import { ErrorCode, RoutePath, isSuccessResponse, errorFormatter } from 'utils' | ||
import { | ||
useState as useGlobalState, | ||
useDispatch, | ||
AppActions, | ||
sendTransaction, | ||
sendCreateSUDTAccountTransaction, | ||
sendSUDTTransaction, | ||
} from 'states' | ||
import { SignTransactionParams } from 'ckb-walletconnect-wallet-sdk' | ||
import { OfflineSignType, OfflineSignStatus, signAndExportTransaction } from 'services/remote' | ||
import { PasswordIncorrectException } from 'exceptions' | ||
import styles from './wcSignTransactionDialog.module.scss' | ||
|
||
const WCSignTransactionDialog = ({ | ||
wallet, | ||
data, | ||
onDismiss, | ||
}: { | ||
wallet: State.Wallet | ||
data: SignTransactionParams | ||
onDismiss: () => void | ||
}) => { | ||
const { | ||
app: { | ||
send: { description }, | ||
}, | ||
experimental, | ||
} = useGlobalState() | ||
|
||
const walletID = wallet.id | ||
const { | ||
transaction, | ||
type: signType = OfflineSignType.Regular, | ||
status: signStatus = OfflineSignStatus.Unsigned, | ||
asset_account: assetAccount, | ||
actionType, | ||
} = data | ||
|
||
const isBroadcast = actionType === 'signAndSend' | ||
|
||
const dispatch = useDispatch() | ||
const [t] = useTranslation() | ||
const navigate = useNavigate() | ||
|
||
const [password, setPassword] = useState('') | ||
const [error, setError] = useState('') | ||
const [isSigning, setIsSigning] = useState(false) | ||
|
||
useEffect(() => { | ||
setPassword('') | ||
setError('') | ||
}, [signType, setError, setPassword]) | ||
|
||
const disabled = !password || isSigning | ||
|
||
const signAndExport = useCallback(async () => { | ||
const res = await signAndExportTransaction({ | ||
transaction, | ||
type: signType, | ||
status: signStatus, | ||
walletID, | ||
password, | ||
}) | ||
if (!isSuccessResponse(res)) { | ||
setError(errorFormatter(res.message, t)) | ||
return | ||
} | ||
dispatch({ | ||
type: AppActions.UpdateLoadedTransaction, | ||
payload: res.result!, | ||
}) | ||
onDismiss() | ||
}, [data, dispatch, onDismiss, t, password, walletID]) | ||
|
||
const onSubmit = useCallback( | ||
async (e?: React.FormEvent) => { | ||
if (e) { | ||
e.preventDefault() | ||
} | ||
if (disabled) { | ||
return | ||
} | ||
setIsSigning(true) | ||
if (!isBroadcast) { | ||
await signAndExport() | ||
setIsSigning(false) | ||
return | ||
} | ||
try { | ||
switch (signType) { | ||
case OfflineSignType.Regular: { | ||
if (isSigning) { | ||
break | ||
} | ||
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(({ status }) => { | ||
if (isSuccessResponse({ status })) { | ||
navigate(RoutePath.History) | ||
} else if (status === ErrorCode.PasswordIncorrect) { | ||
throw new PasswordIncorrectException() | ||
} | ||
}) | ||
break | ||
} | ||
case OfflineSignType.UnlockDAO: { | ||
if (isSigning) { | ||
break | ||
} | ||
await sendTransaction({ walletID, tx: transaction, description, password })(dispatch).then(({ status }) => { | ||
if (isSuccessResponse({ status })) { | ||
dispatch({ | ||
type: AppActions.SetGlobalDialog, | ||
payload: 'unlock-success', | ||
}) | ||
} else if (status === ErrorCode.PasswordIncorrect) { | ||
throw new PasswordIncorrectException() | ||
} | ||
}) | ||
break | ||
} | ||
case OfflineSignType.CreateSUDTAccount: { | ||
const params: Controller.SendCreateSUDTAccountTransaction.Params = { | ||
walletID, | ||
assetAccount: assetAccount ?? experimental?.assetAccount, | ||
tx: transaction, | ||
password, | ||
} | ||
await sendCreateSUDTAccountTransaction(params)(dispatch).then(({ status }) => { | ||
if (isSuccessResponse({ status })) { | ||
navigate(RoutePath.History) | ||
} else if (status === ErrorCode.PasswordIncorrect) { | ||
throw new PasswordIncorrectException() | ||
} | ||
}) | ||
break | ||
} | ||
case OfflineSignType.SendSUDT: { | ||
const params: Controller.SendSUDTTransaction.Params = { | ||
walletID, | ||
tx: transaction, | ||
password, | ||
} | ||
await sendSUDTTransaction(params)(dispatch).then(({ status }) => { | ||
if (isSuccessResponse({ status })) { | ||
navigate(RoutePath.History) | ||
} else if (status === ErrorCode.PasswordIncorrect) { | ||
throw new PasswordIncorrectException() | ||
} | ||
}) | ||
break | ||
} | ||
default: { | ||
break | ||
} | ||
} | ||
} catch (err) { | ||
if (err instanceof PasswordIncorrectException) { | ||
setError(t(err.message)) | ||
} | ||
} | ||
}, | ||
[ | ||
dispatch, | ||
walletID, | ||
password, | ||
signType, | ||
description, | ||
navigate, | ||
isSigning, | ||
transaction, | ||
disabled, | ||
experimental, | ||
setError, | ||
t, | ||
isBroadcast, | ||
signAndExport, | ||
assetAccount, | ||
] | ||
) | ||
|
||
const onChange = useCallback( | ||
(e: React.SyntheticEvent<HTMLInputElement>) => { | ||
const { value } = e.target as HTMLInputElement | ||
setPassword(value) | ||
setError('') | ||
}, | ||
[setPassword, setError] | ||
) | ||
|
||
const title = useMemo(() => { | ||
return !isBroadcast ? t('offline-sign.sign-and-export') : t('offline-sign.sign-and-broadcast') | ||
}, [isBroadcast, t]) | ||
|
||
return ( | ||
<Dialog | ||
show | ||
title={title} | ||
onCancel={onDismiss} | ||
onConfirm={onSubmit} | ||
disabled={disabled} | ||
isLoading={isSigning} | ||
contentClassName={styles.content} | ||
> | ||
<TextField | ||
label={t('password-request.password')} | ||
value={password} | ||
field="password" | ||
type="password" | ||
title={t('password-request.password')} | ||
onChange={onChange} | ||
autoFocus | ||
className={styles.passwordInput} | ||
error={error} | ||
/> | ||
</Dialog> | ||
) | ||
} | ||
|
||
WCSignTransactionDialog.displayName = 'WCSignTransactionDialog' | ||
|
||
export default WCSignTransactionDialog |
10 changes: 10 additions & 0 deletions
10
...ages/neuron-ui/src/components/WCSignTransactionDialog/wcSignTransactionDialog.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@import '../../styles/mixin.scss'; | ||
|
||
.content { | ||
width: 648px; | ||
padding: 16px 16px 0; | ||
} | ||
|
||
.passwordInput { | ||
margin-top: 16px; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be more convenient to move the SDK to this repository and use the yarn workspace feature, or import the SDK via git submodule. This would simplify development as it would not require publishing every change made to the SDK.