From 2c6ee483d3085e1d7a20ae6124e34036c47812fb Mon Sep 17 00:00:00 2001 From: jesush Date: Thu, 23 Aug 2018 00:57:25 -0400 Subject: [PATCH] Add ability to sign messages for web3 providers and Javascript VM --- src/app/tabs/run-tab.js | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index c94e8ea996..5c36615b1e 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -13,6 +13,8 @@ var helper = require('../../lib/helper.js') var executionContext = require('../../execution-context') var modalDialogCustom = require('../ui/modal-dialog-custom') var copyToClipboard = require('../ui/copy-to-clipboard') +const Buffer = require('safe-buffer').Buffer +var Personal = require('web3-eth-personal') var Card = require('../ui/card') var Recorder = require('../../recorder') var addTooltip = require('../ui/tooltip') @@ -527,6 +529,7 @@ function settings (container, self) {
${copyToClipboard(() => document.querySelector('#runTabView #txorigin').value)} +
` @@ -625,6 +628,65 @@ function settings (container, self) { } }) } + function signMessage (event) { + self._deps.udapp.getAccounts((err, accounts) => { + if (err) { addTooltip(`Cannot get account list: ${err}`) } + var signMessageDialog = {'title': 'Sign a message', 'text': 'Enter a message to sign', 'inputvalue': 'Message to sign' } + var $txOrigin = container.querySelector('#txorigin') + var account = $txOrigin.selectedOptions[0].value + var isVM = executionContext.isVM() + var isInjected = executionContext.getProvider() === "injected" + function alertSignedData(error, signedData) { + if (error && error.message !== '') { + console.log(error) + addTooltip(error.message) + } else { + modalDialogCustom.alert(signedData) + } + } + if (isVM) { + modalDialogCustom.promptMulti(signMessageDialog, (message) => { + const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) + var privKey = self._deps.udapp.accounts[account].privateKey + try { + var rsv = ethJSUtil.ecsign(personalMsg, privKey) + var rsvJson = JSON.stringify(rsv, null, '\t') + var signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) + modalDialogCustom.alert(rsvJson + '\n' + signedData) + } catch (e) { + addTooltip(e.message) + return + } + }, false) + } else if (isInjected) { + modalDialogCustom.promptMulti(signMessageDialog, (message) => { + const hashedMsg = executionContext.web3().sha3(message) + try { + executionContext.web3().eth.sign(account, hashedMsg, alertSignedData) + } catch (e) { + addTooltip(e.message) + console.log(e) + return + } + }) + } else { + modalDialogCustom.promptPassphrase('Passphrase to sign a message', 'Enter your passphrase for this account to sign the message', '', (passphrase) => { + modalDialogCustom.promptMulti(signMessageDialog, (message) => { + const hashedMsg = executionContext.web3().sha3(message) + try { + var personal = new Personal(executionContext.web3().currentProvider) + personal.sign('0x' + Buffer.from(message).toString('hex'), account, passphrase, alertSignedData) + } catch (e) { + addTooltip(e.message) + comsole.log(e) + return + } + }) + }, false) + } + + }) + } return el }