-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from CoongCrafts/coong/feature/sign-raw-message
Sign raw messages
- Loading branch information
Showing
17 changed files
with
678 additions
and
362 deletions.
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
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
41 changes: 41 additions & 0 deletions
41
packages/ui/src/components/pages/Request/RequestSigning/DetailRow.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,41 @@ | ||
import { FC } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import clsx from 'clsx'; | ||
import { Props } from 'types'; | ||
|
||
export enum ValueStyle { | ||
TEXT_BOLD, | ||
BOX, | ||
} | ||
|
||
export interface DetailRowProps extends Props { | ||
name: string; | ||
value: any; | ||
breakWord?: boolean; | ||
style?: ValueStyle; | ||
} | ||
|
||
const DetailRow: FC<DetailRowProps> = ({ name, value, breakWord = false, style = ValueStyle.TEXT_BOLD }) => { | ||
const { t } = useTranslation(); | ||
const renderValue = () => { | ||
switch (style) { | ||
case ValueStyle.BOX: | ||
return ( | ||
<div className='py-2 px-4 bg-black/10 dark:bg-white/15 border border-black/10 dark:border-white/15 w-full'> | ||
{value} | ||
</div> | ||
); | ||
case ValueStyle.TEXT_BOLD: | ||
default: | ||
return <strong className={clsx({ 'break-all': breakWord })}>{value}</strong>; | ||
} | ||
}; | ||
|
||
return ( | ||
<div className='flex items-start mb-2 gap-2' data-testid={`row-${name.replace(' ', '-')}`}> | ||
<div className='text-gray-500 dark:text-gray-200 min-w-[80px] text-right'>{t<string>(name)}: </div> | ||
{renderValue()} | ||
</div> | ||
); | ||
}; | ||
export default DetailRow; |
19 changes: 19 additions & 0 deletions
19
packages/ui/src/components/pages/Request/RequestSigning/RequestDetails.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,19 @@ | ||
import { FC } from 'react'; | ||
import DetailRow, { DetailRowProps } from 'components/pages/Request/RequestSigning/DetailRow'; | ||
import { Props } from 'types'; | ||
|
||
interface RequestDetails extends Props { | ||
rows: DetailRowProps[]; | ||
} | ||
|
||
const RequestDetails: FC<RequestDetails> = ({ className = '', rows }) => { | ||
return ( | ||
<div className={`${className}`}> | ||
{rows.map((row) => ( | ||
<DetailRow key={row.name} {...row} /> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default RequestDetails; |
36 changes: 36 additions & 0 deletions
36
packages/ui/src/components/pages/Request/RequestSigning/RequestSignRawMessage.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,36 @@ | ||
import { FC } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import AccountCard from 'components/pages/Accounts/AccountCard'; | ||
import RequestDetails from 'components/pages/Request/RequestSigning/RequestDetails'; | ||
import SignArea from 'components/pages/Request/RequestSigning/SignArea'; | ||
import { useRawMessageDetails } from 'components/pages/Request/RequestSigning/hooks/useRawMessageDetails'; | ||
import useTargetAccount from 'components/pages/Request/RequestSigning/hooks/useTargetAccount'; | ||
import { RequestProps } from 'components/pages/Request/types'; | ||
import { useWalletState } from 'providers/WalletStateProvider'; | ||
|
||
const RequestSignRawMessage: FC<RequestProps> = ({ className = '', message }) => { | ||
const { t } = useTranslation(); | ||
const { walletState } = useWalletState(); | ||
const targetAccount = useTargetAccount(message); | ||
const detailRows = useRawMessageDetails(message); | ||
|
||
const doSignMessage = async (password: string) => { | ||
await walletState.signRawMessage(password); | ||
}; | ||
|
||
const cancelRequest = () => { | ||
walletState.cancelSignRawMessage(); | ||
}; | ||
|
||
return ( | ||
<div className={className}> | ||
<h2 className='text-center'>{t<string>('Sign Message Request')}</h2> | ||
<p className='mb-2'>{t<string>('You are signing a message with account')}</p> | ||
{targetAccount && <AccountCard account={targetAccount} />} | ||
<RequestDetails rows={detailRows} /> | ||
<SignArea onSign={doSignMessage} onCancel={cancelRequest} signButtonLabel={'Sign Message'} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RequestSignRawMessage; |
36 changes: 36 additions & 0 deletions
36
packages/ui/src/components/pages/Request/RequestSigning/RequestTransactionApproval.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,36 @@ | ||
import { FC } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import AccountCard from 'components/pages/Accounts/AccountCard'; | ||
import RequestDetails from 'components/pages/Request/RequestSigning/RequestDetails'; | ||
import SignArea from 'components/pages/Request/RequestSigning/SignArea'; | ||
import useTargetAccount from 'components/pages/Request/RequestSigning/hooks/useTargetAccount'; | ||
import useTransactionDetails from 'components/pages/Request/RequestSigning/hooks/useTransactionDetails'; | ||
import { RequestProps } from 'components/pages/Request/types'; | ||
import { useWalletState } from 'providers/WalletStateProvider'; | ||
|
||
const RequestTransactionApproval: FC<RequestProps> = ({ className, message }) => { | ||
const { t } = useTranslation(); | ||
const { walletState } = useWalletState(); | ||
const targetAccount = useTargetAccount(message); | ||
const detailRows = useTransactionDetails(message); | ||
|
||
const approveTransaction = async (password: string) => { | ||
await walletState.approveSignExtrinsic(password); | ||
}; | ||
|
||
const cancelRequest = () => { | ||
walletState.cancelSignExtrinsic(); | ||
}; | ||
|
||
return ( | ||
<div className={className}> | ||
<h2 className='text-center'>{t<string>('Transaction Approval Request')}</h2> | ||
<p className='mb-2'>{t<string>('You are approving a transaction with account')}</p> | ||
{targetAccount && <AccountCard account={targetAccount} />} | ||
<RequestDetails className='my-4' rows={detailRows} /> | ||
<SignArea onSign={approveTransaction} onCancel={cancelRequest} signButtonLabel={'Approve Transaction'} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RequestTransactionApproval; |
63 changes: 63 additions & 0 deletions
63
packages/ui/src/components/pages/Request/RequestSigning/SignArea.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,63 @@ | ||
import { FC, FormEvent, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Form } from 'react-router-dom'; | ||
import { toast } from 'react-toastify'; | ||
import { useToggle } from 'react-use'; | ||
import { Button, TextField } from '@mui/material'; | ||
import { Props } from 'types'; | ||
|
||
interface SignAreaProps extends Props { | ||
onSign: (password: string) => void; | ||
onCancel: () => void; | ||
cancelButtonLabel?: string; | ||
signButtonLabel?: string; | ||
} | ||
|
||
const SignArea: FC<SignAreaProps> = ({ onSign, onCancel, cancelButtonLabel = 'Cancel', signButtonLabel = 'Sign' }) => { | ||
const { t } = useTranslation(); | ||
const [password, setPassword] = useState<string>(''); | ||
const [loading, toggleLoading] = useToggle(false); | ||
|
||
const doSign = (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
toggleLoading(true); | ||
|
||
// signing & accounts decryption are synchronous operations | ||
// and might take some time to do | ||
// so we delay it a short amount of time to make sure the UI could be updated (disable button, ...) | ||
// before the signing process begin | ||
// TODO: Moving CPU-intensive operations to worker | ||
setTimeout(async () => { | ||
try { | ||
await onSign(password); | ||
} catch (e: any) { | ||
toggleLoading(false); | ||
toast.error(t<string>(e.message)); | ||
} | ||
}, 200); | ||
}; | ||
|
||
return ( | ||
<Form className='mt-8' onSubmit={doSign}> | ||
<TextField | ||
label={t<string>('Wallet password')} | ||
size='medium' | ||
type='password' | ||
fullWidth | ||
autoFocus | ||
value={password} | ||
onChange={(event) => setPassword(event.target.value)} | ||
/> | ||
<div className='flex mt-4 gap-4'> | ||
<Button size='large' variant='text' className='xs:w-2/5' color='warning' onClick={onCancel}> | ||
{t<string>(cancelButtonLabel)} | ||
</Button> | ||
<Button size='large' className='w-full xs:w-3/5' disabled={!password || loading} type='submit'> | ||
{t<string>(signButtonLabel)} | ||
</Button> | ||
</div> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default SignArea; |
Oops, something went wrong.