From 594d9bbc1616b7005ca21bbed914d944ac2b3d5b Mon Sep 17 00:00:00 2001 From: David Disu Date: Mon, 6 Dec 2021 14:08:53 +0100 Subject: [PATCH] Implemened sign message --- .../remix-ui/run-tab/src/lib/actions/index.ts | 14 +- .../run-tab/src/lib/components/account.tsx | 123 +++++++++++------- .../run-tab/src/lib/components/settingsUI.tsx | 2 +- .../run-tab/src/lib/reducers/runTab.ts | 2 +- libs/remix-ui/run-tab/src/lib/run-tab.tsx | 5 +- libs/remix-ui/run-tab/src/lib/types/index.ts | 10 +- 6 files changed, 102 insertions(+), 54 deletions(-) diff --git a/libs/remix-ui/run-tab/src/lib/actions/index.ts b/libs/remix-ui/run-tab/src/lib/actions/index.ts index e3f6cd91ed..2d1feaba6d 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/index.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/index.ts @@ -219,8 +219,10 @@ export const createNewBlockchainAccount = async (cbMessage: JSX.Element) => { if (plugin.REACT_API.passphrase === plugin.REACT_API.matchPassphrase) { cb(plugin.REACT_API.passphrase) } else { - return dispatch(displayNotification('Error', 'Passphase does not match', 'OK', null)) + dispatch(displayNotification('Error', 'Passphase does not match', 'OK', null)) } + setPassphrase('') + setMatchPassphrase('') }, () => {})) }, async (error, address) => { @@ -240,3 +242,13 @@ export const setPassphrasePrompt = (passphrase: string) => { export const setMatchPassphrasePrompt = (passphrase: string) => { dispatch(setMatchPassphrase(passphrase)) } + +// eslint-disable-next-line no-undef +export const signMessageWithAddress = (account: string, message: string, modalContent: (hash: string, data: string) => JSX.Element, passphrase?: string) => { + plugin.blockchain.signMessage(message, account, passphrase, (err, msgHash, signedData) => { + if (err) { + return displayPopUp(err) + } + dispatch(displayNotification('Signed Message', modalContent(msgHash, signedData), 'OK', null, () => {}, null)) + }) +} diff --git a/libs/remix-ui/run-tab/src/lib/components/account.tsx b/libs/remix-ui/run-tab/src/lib/components/account.tsx index f164dd20ba..a03ec87aaf 100644 --- a/libs/remix-ui/run-tab/src/lib/components/account.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/account.tsx @@ -10,6 +10,8 @@ export function AccountUI (props: AccountProps) { classList: '', title: '' }) + const [message, setMessage] = useState('') + const [signPassphrase, setSignPassphrase] = useState('') useEffect(() => { if (!selectedAccount && accounts.length > 0) props.setAccount(accounts[0]) @@ -24,7 +26,14 @@ export function AccountUI (props: AccountProps) { }) break - case 'vm': + case 'vm-london': + setPlusOpt({ + classList: '', + title: 'Create a new account' + }) + break + + case 'vm-berlin': setPlusOpt({ classList: '', title: 'Create a new account' @@ -55,54 +64,25 @@ export function AccountUI (props: AccountProps) { }, [props.selectExEnv, props.personalMode]) const newAccount = () => { - props.createNewBlockchainAccount(passphrasePrompt()) + props.createNewBlockchainAccount(passphraseCreationPrompt()) } const signMessage = () => { - // dispatch signMessageWithBlockchainAccounts - // this.blockchain.getAccounts((err, accounts) => { - // if (err) { - // return addTooltip(`Cannot get account list: ${err}`) - // } - - // var signMessageDialog = { title: 'Sign a message', text: 'Enter a message to sign', inputvalue: 'Message to sign' } - // var $txOrigin = this.el.querySelector('#txorigin') - // if (!$txOrigin.selectedOptions[0] && (this.blockchain.isInjectedWeb3() || this.blockchain.isWeb3Provider())) { - // return addTooltip('Account list is empty, please make sure the current provider is properly connected to remix') - // } - - // var account = $txOrigin.selectedOptions[0].value - - // var promptCb = (passphrase) => { - // const modal = modalDialogCustom.promptMulti(signMessageDialog, (message) => { - // this.blockchain.signMessage(message, account, passphrase, (err, msgHash, signedData) => { - // if (err) { - // return addTooltip(err) - // } - // modal.hide() - // modalDialogCustom.alert(yo` - //
- // hash:
- // ${msgHash} - //
signature:
- // ${signedData} - //
- // `) - // }) - // }, false) - // } - - // if (this.blockchain.isWeb3Provider()) { - // return modalDialogCustom.promptPassphrase( - // 'Passphrase to sign a message', - // 'Enter your passphrase for this account to sign the message', - // '', - // promptCb, - // false - // ) - // } - // promptCb() - // }) + if (!accounts[0]) { + return props.tooltip('Account list is empty, please make sure the current provider is properly connected to remix') + } + + if (props.selectExEnv !== 'vm-london' && props.selectExEnv !== 'vm-berlin' && props.selectExEnv !== 'injected') { + return props.modal('Passphrase to sign a message', passphrasePrompt(), 'OK', () => { + props.modal('Sign a message', signMessagePrompt(), 'OK', () => { + props.signMessageWithAddress(selectedAccount, message, signedMessagePrompt, signPassphrase) + }, 'Cancel', null) + }, 'Cancel', null) + } + + props.modal('Sign a message', signMessagePrompt(), 'OK', () => { + props.signMessageWithAddress(selectedAccount, message, signedMessagePrompt) + }, 'Cancel', null) } const handlePassphrase = (e) => { @@ -113,7 +93,15 @@ export function AccountUI (props: AccountProps) { props.setMatchPassphrase(e.target.value) } - const passphrasePrompt = () => { + const handleMessageInput = (e) => { + setMessage(e.target.value) + } + + const handleSignPassphrase = (e) => { + setSignPassphrase(e.target.value) + } + + const passphraseCreationPrompt = () => { return (
Please provide a Passphrase for the account creation
@@ -126,6 +114,45 @@ export function AccountUI (props: AccountProps) { ) } + const passphrasePrompt = () => { + return ( +
Enter your passphrase for this account to sign the message +
+ +
+
+ ) + } + + const signMessagePrompt = () => { + return ( +
Enter a message to sign +
+ +
+
+ ) + } + + const signedMessagePrompt = (msgHash: string, signedData: string) => { + return ( +
+ hash:
+ {msgHash} +
signature:
+ {signedData} +
+ ) + } + return (