From 421b352c97843ca2ecef27e0b46eddebb37cb9c2 Mon Sep 17 00:00:00 2001 From: mbidenaio Date: Mon, 10 Jul 2023 11:00:01 +0500 Subject: [PATCH] Add dna://sign method --- locales/bg/translation.json | 5 +- locales/de/translation.json | 5 +- locales/el/translation.json | 5 +- locales/en/translation.json | 5 +- locales/es/translation.json | 5 +- locales/fr/translation.json | 5 +- locales/hi/translation.json | 5 +- locales/hr/translation.json | 5 +- locales/id/translation.json | 5 +- locales/it/translation.json | 5 +- locales/ja/translation.json | 5 +- locales/ko/translation.json | 5 +- locales/pl/translation.json | 5 +- locales/pt/translation.json | 5 +- locales/ro/translation.json | 5 +- locales/ru/translation.json | 5 +- locales/sl/translation.json | 5 +- locales/sr/translation.json | 5 +- locales/sv/translation.json | 5 +- locales/tr/translation.json | 5 +- locales/uk/translation.json | 5 +- locales/zh/translation.json | 4 +- renderer/screens/dna/containers.js | 138 ++++++++++++++++++++++----- renderer/screens/dna/hooks.js | 1 + renderer/screens/dna/utils.js | 14 ++- renderer/shared/components/layout.js | 59 +++++++++--- 26 files changed, 261 insertions(+), 60 deletions(-) diff --git a/locales/bg/translation.json b/locales/bg/translation.json index ea8d55006..9f9e69a9a 100644 --- a/locales/bg/translation.json +++ b/locales/bg/translation.json @@ -949,5 +949,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/de/translation.json b/locales/de/translation.json index cf1b5bfee..2967e3e5c 100644 --- a/locales/de/translation.json +++ b/locales/de/translation.json @@ -946,5 +946,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/el/translation.json b/locales/el/translation.json index 1fd0ace54..679ece431 100644 --- a/locales/el/translation.json +++ b/locales/el/translation.json @@ -949,5 +949,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/en/translation.json b/locales/en/translation.json index 7a9db33c6..9eb7d1985 100644 --- a/locales/en/translation.json +++ b/locales/en/translation.json @@ -949,5 +949,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/es/translation.json b/locales/es/translation.json index 2ac7e9f20..77642e966 100644 --- a/locales/es/translation.json +++ b/locales/es/translation.json @@ -946,5 +946,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/fr/translation.json b/locales/fr/translation.json index 37fc413d3..173087cc0 100644 --- a/locales/fr/translation.json +++ b/locales/fr/translation.json @@ -949,5 +949,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/hi/translation.json b/locales/hi/translation.json index 2fc30ebcb..056d0f81b 100644 --- a/locales/hi/translation.json +++ b/locales/hi/translation.json @@ -949,5 +949,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/hr/translation.json b/locales/hr/translation.json index 0a25e8561..4586f00d0 100644 --- a/locales/hr/translation.json +++ b/locales/hr/translation.json @@ -956,5 +956,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/id/translation.json b/locales/id/translation.json index 8b66f8741..46fe445ea 100644 --- a/locales/id/translation.json +++ b/locales/id/translation.json @@ -938,5 +938,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/it/translation.json b/locales/it/translation.json index a48215655..d529769e9 100644 --- a/locales/it/translation.json +++ b/locales/it/translation.json @@ -946,5 +946,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/ja/translation.json b/locales/ja/translation.json index 7d6f6d883..99b83de08 100644 --- a/locales/ja/translation.json +++ b/locales/ja/translation.json @@ -941,5 +941,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/ko/translation.json b/locales/ko/translation.json index c2962b5c0..15e4186f1 100644 --- a/locales/ko/translation.json +++ b/locales/ko/translation.json @@ -936,5 +936,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/pl/translation.json b/locales/pl/translation.json index 882625b31..a71208d80 100644 --- a/locales/pl/translation.json +++ b/locales/pl/translation.json @@ -953,5 +953,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/pt/translation.json b/locales/pt/translation.json index 08c04b875..bd49c8c95 100644 --- a/locales/pt/translation.json +++ b/locales/pt/translation.json @@ -946,5 +946,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/ro/translation.json b/locales/ro/translation.json index 6590e3c97..ede2ce9cd 100644 --- a/locales/ro/translation.json +++ b/locales/ro/translation.json @@ -950,5 +950,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/ru/translation.json b/locales/ru/translation.json index a1b469078..e1774dd4b 100644 --- a/locales/ru/translation.json +++ b/locales/ru/translation.json @@ -950,5 +950,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/sl/translation.json b/locales/sl/translation.json index c7e89dba5..4b8e9e768 100644 --- a/locales/sl/translation.json +++ b/locales/sl/translation.json @@ -966,5 +966,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/sr/translation.json b/locales/sr/translation.json index d35af1335..44ca7b4aa 100644 --- a/locales/sr/translation.json +++ b/locales/sr/translation.json @@ -956,5 +956,8 @@ "Show status": "Прикажи статус", "Ads": "Рекламе", "Client version: {{version}}": "Верзија клијента: {{version}}", - "Node version: {{version}}": "Верзија нода: {{version}}" + "Node version: {{version}}": "Верзија нода: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/sv/translation.json b/locales/sv/translation.json index 7e61aa54e..a8c97b4f4 100644 --- a/locales/sv/translation.json +++ b/locales/sv/translation.json @@ -943,5 +943,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/tr/translation.json b/locales/tr/translation.json index 014ccf65e..53ce31a10 100644 --- a/locales/tr/translation.json +++ b/locales/tr/translation.json @@ -945,5 +945,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/uk/translation.json b/locales/uk/translation.json index b0611dc72..dd023045c 100644 --- a/locales/uk/translation.json +++ b/locales/uk/translation.json @@ -956,5 +956,8 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Node version: {{version}}": "Node version: {{version}}", + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/locales/zh/translation.json b/locales/zh/translation.json index 232dd5588..cb6141521 100644 --- a/locales/zh/translation.json +++ b/locales/zh/translation.json @@ -936,5 +936,7 @@ "Show status": "Show status", "Ads": "Ads", "Client version: {{version}}": "Client version: {{version}}", - "Node version: {{version}}": "Node version: {{version}}" + "Confirm signature": "Confirm signature", + "Please confirm that you want to sign the message": "Please confirm that you want to sign the message", + "Message": "Message" } diff --git a/renderer/screens/dna/containers.js b/renderer/screens/dna/containers.js index 232a86fa5..0dfaad97d 100644 --- a/renderer/screens/dna/containers.js +++ b/renderer/screens/dna/containers.js @@ -33,6 +33,7 @@ import { DNA_SEND_CONFIRM_TRESHOLD, appendTxHash, handleCallbackUrl, + appendParam, } from './utils' import { Dialog, @@ -70,10 +71,9 @@ export function DnaSignInDialog({ const {address} = useIdentityState() - const callbackUrlObject = React.useMemo( - () => new URL(callbackUrl), - [callbackUrl] - ) + const callbackUrlObject = React.useMemo(() => new URL(callbackUrl), [ + callbackUrl, + ]) const callbackFaviconUrl = React.useMemo( () => faviconUrl || new URL('favicon.ico', callbackUrlObject.origin), @@ -132,7 +132,7 @@ export function DnaSignInDialog({ address, }) .then(signNonce) - .then((signature) => + .then(signature => authenticate(authenticationEndpoint, { token, signature, @@ -156,6 +156,98 @@ export function DnaSignInDialog({ ) } +export function DnaSignDialog({ + message, + format, + callbackUrl, + faviconUrl, + onSignError, + onClose, + ...props +}) { + const {t} = useTranslation() + + const initialRef = React.useRef() + + const {address} = useIdentityState() + + const callbackUrlObject = React.useMemo(() => new URL(callbackUrl), [ + callbackUrl, + ]) + + const callbackFaviconUrl = React.useMemo( + () => faviconUrl || new URL('favicon.ico', callbackUrlObject.origin), + [callbackUrlObject.origin, faviconUrl] + ) + + return ( + + + + {t('Please confirm that you want to sign the message')} + + + {callbackFaviconUrl ? ( + + ) : ( + + )} + + + + + + + + + + {t('Cancel')} + { + signNonce(message, format) + .then(signature => { + if (isValidUrl(callbackUrl)) { + const callbackUrlWithSignature = appendParam( + callbackUrl, + 'signature', + signature + ) + global.openExternal(callbackUrlWithSignature.href) + } else onSignError('Invalid callback URL') + }) + .catch(({message: errorMessage}) => { + global.logger.error(errorMessage) + if (onSignError) onSignError(errorMessage) + }) + .finally(onClose) + }} + > + {t('Confirm')} + + + + ) +} + export function DnaSendDialog({ address: to, amount, @@ -182,15 +274,15 @@ export function DnaSendDialog({ const [confirmationAmount, setConfirmationAmount] = React.useState() - const areSameAmounts = React.useMemo( - () => +confirmationAmount === +amount, - [amount, confirmationAmount] - ) + const areSameAmounts = React.useMemo(() => +confirmationAmount === +amount, [ + amount, + confirmationAmount, + ]) - const isExceededBalance = React.useMemo( - () => +amount > balance, - [amount, balance] - ) + const isExceededBalance = React.useMemo(() => +amount > balance, [ + amount, + balance, + ]) const [isSubmitting, setIsSubmitting] = React.useState() @@ -242,7 +334,7 @@ export function DnaSendDialog({ setConfirmationAmount(e.target.value)} + onChange={e => setConfirmationAmount(e.target.value)} /> {Number.isFinite(+confirmationAmount) && !areSameAmounts && ( @@ -313,7 +405,7 @@ export function DnaSendDialog({ }) }, }) - .catch((error) => { + .catch(error => { global.logger.error(error) onDepositError({ error: error?.message, @@ -392,10 +484,10 @@ export function DnaRawDialog({ [amount, confirmationAmount] ) - const isExceededBalance = React.useMemo( - () => +amount > balance, - [amount, balance] - ) + const isExceededBalance = React.useMemo(() => +amount > balance, [ + amount, + balance, + ]) const [isSubmitting, setIsSubmitting] = React.useState() @@ -484,7 +576,7 @@ export function DnaRawDialog({ setConfirmationAmount(e.target.value)} + onChange={e => setConfirmationAmount(e.target.value)} /> {Number.isFinite(+confirmationAmount) && !didConfirmAmount && ( @@ -523,7 +615,7 @@ export function DnaRawDialog({ }) .then(() => setIsSubmitting(true)) .then(() => callRpc('dna_sendTransaction', parsedTx)) - .then(async (hash) => { + .then(async hash => { if (isValidUrl(callbackUrl)) { const callbackUrlWithHash = appendTxHash(callbackUrl, hash) @@ -552,7 +644,7 @@ export function DnaRawDialog({ // eslint-disable-next-line no-shadow onHtml: ({url}) => onSendSuccess({hash, url}), }) - .catch((error) => { + .catch(error => { global.logger.error(error) onSendError({ error: error?.message, @@ -691,7 +783,7 @@ export function DnaSendFailedDialog({ }) } }, - }).catch((error) => { + }).catch(error => { global.logger.error(error) onRetryFailed({ error: error?.message, diff --git a/renderer/screens/dna/hooks.js b/renderer/screens/dna/hooks.js index bd6861acf..960d08c03 100644 --- a/renderer/screens/dna/hooks.js +++ b/renderer/screens/dna/hooks.js @@ -9,6 +9,7 @@ export const DnaLinkMethod = { RawTx: 'raw', Vote: 'vote', Invite: 'invite', + Sign: 'sign', } export function useDnaLink({onInvalidLink}) { diff --git a/renderer/screens/dna/utils.js b/renderer/screens/dna/utils.js index cb26c512b..484030b4b 100644 --- a/renderer/screens/dna/utils.js +++ b/renderer/screens/dna/utils.js @@ -59,12 +59,16 @@ export async function startSession(nonceEndpoint, {token, address}) { throw new Error(`You must start prefix with ${DNA_NONCE_PREFIX}`) } -export async function signNonce(nonce) { +export async function signNonce(nonce, format) { + const params = [nonce] + if (format) { + params.push(format) + } const { data: {result, error}, } = await apiClient().post('/', { method: 'dna_sign', - params: [nonce], + params, id: 1, }) if (error) throw new Error(error.message) @@ -104,8 +108,12 @@ export async function sendDna({from, to, amount, comment}) { } export function appendTxHash(url, hash) { + return appendParam(url, 'tx', hash) +} + +export function appendParam(url, name, value) { const txUrl = new URL(url) - txUrl.searchParams.append('tx', hash) + txUrl.searchParams.append(name, value) return txUrl } diff --git a/renderer/shared/components/layout.js b/renderer/shared/components/layout.js index 9d61542bf..1001e6847 100644 --- a/renderer/shared/components/layout.js +++ b/renderer/shared/components/layout.js @@ -37,6 +37,7 @@ import { DnaRawDialog, DnaSendFailedDialog, DnaSendSucceededDialog, + DnaSignDialog, } from '../../screens/dna/containers' import { useAutoUpdateState, @@ -111,10 +112,10 @@ export default function Layout({ React.useEffect(() => { if (global.isDev) return - const handleMouseWheel = (e) => { + const handleMouseWheel = e => { if (e.ctrlKey) { e.preventDefault() - setZoomLevel((level) => + setZoomLevel(level => Math.min(Math.max(-5, level + e.deltaY * -0.01), 5) ) } @@ -216,6 +217,24 @@ export default function Layout({ }, }) + const {onOpen: onOpenSignDialog, ...dnaSignDisclosure} = useDisclosure() + + const handleReceiveDnaSignLink = React.useCallback(() => { + if (isNotOffline) onOpenSignDialog() + }, [isNotOffline, onOpenSignDialog]) + + const { + params: {message: dnaSignMessage, ...dnaSignParams}, + } = useDnaLinkMethod(DnaLinkMethod.Sign, { + onReceive: handleReceiveDnaSignLink, + onInvalidLink: () => { + failToast({ + title: t('Invalid DNA link'), + description: t(`You must provide valid URL including protocol version`), + }) + }, + }) + return ( )} + {Boolean(dnaSignMessage) && ( + + )} + @@ -283,10 +311,12 @@ function NormalApp({skipBanner, children}) { useValidationToast() - const [validationNotificationEpoch, setValidationNotificationEpoch] = - React.useState( - () => loadPersistentStateValue('validationNotification', 'epoch') || 0 - ) + const [ + validationNotificationEpoch, + setValidationNotificationEpoch, + ] = React.useState( + () => loadPersistentStateValue('validationNotification', 'epoch') || 0 + ) React.useEffect(() => { if ( @@ -415,8 +445,13 @@ function NormalApp({skipBanner, children}) { function SyncingApp() { const {t} = useTranslation() - const {currentBlock, highestBlock, genesisBlock, wrongTime, message} = - useChainState() + const { + currentBlock, + highestBlock, + genesisBlock, + wrongTime, + message, + } = useChainState() const {address} = useIdentityState() @@ -599,7 +634,7 @@ function OfflineApp() { const isUnsupportedMacosVersion = runInternalNode && !useExternalNode && unsupportedMacosVersion - const toMb = (b) => + const toMb = b => (b / (1024 * 1024)).toLocaleString(undefined, { maximumFractionDigits: 2, }) @@ -785,7 +820,7 @@ function HardForkScreen({ {t('Changes')} - {changes.map((change) => ( + {changes.map(change => ( {change} ))} {changes.length === 0 && No changes 🤷‍♂️} @@ -880,7 +915,7 @@ function HardForkScreen({ {canVote && (
{ + onSubmit={e => { e.preventDefault() const {votingOption} = e.target.elements @@ -930,7 +965,7 @@ function HardForkScreen({ isOpen={eitherState(currentActivateMining, 'showing')} isCloseable={false} isLoading={eitherState(currentActivateMining, 'showing.mining')} - onChangeMode={(value) => { + onChangeMode={value => { sendActivateMining({type: 'CHANGE_MODE', mode: value}) }} // eslint-disable-next-line no-shadow