Skip to content
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
wants to merge 19 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/neuron-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@nervosnetwork/ckb-sdk-core": "0.109.0",
"@nervosnetwork/ckb-sdk-utils": "0.109.0",
"canvg": "2.0.0",
"ckb-walletconnect-wallet-sdk": "0.0.1-alpha.9",
Copy link
Collaborator

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.

"i18next": "21.10.0",
"immer": "9.0.21",
"jsqr": "1.4.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-ui/src/components/SignAndVerify/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ interface PasswordDialogProps {
show: boolean
walletName: string
onCancel: () => void
onSubmit: (pwd: string) => Promise<ControllerResponse>
onSubmit: (pwd: string) => Promise<ControllerResponse | void>
}

const PasswordDialog = ({ show, walletName, onCancel, onSubmit }: PasswordDialogProps) => {
export const PasswordDialog = ({ show, walletName, onCancel, onSubmit }: PasswordDialogProps) => {
const [t, i18n] = useTranslation()
const [password, setPassword] = useState('')
const [error, setError] = useState('')
Expand Down Expand Up @@ -63,7 +63,7 @@ const PasswordDialog = ({ show, walletName, onCancel, onSubmit }: PasswordDialog
setLoading(true)
onSubmit(password)
.then(res => {
if (res.status === ErrorCode.PasswordIncorrect) {
if (res?.status === ErrorCode.PasswordIncorrect) {
setError((res.message as { content: string }).content)
}
})
Expand Down
226 changes: 226 additions & 0 deletions packages/neuron-ui/src/components/WCSignTransactionDialog/index.tsx
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
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;
}
Loading
Loading