Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Commit

Permalink
add metamask + ledger sign messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tayvano committed Aug 8, 2017
1 parent 8cf00db commit 86dd03e
Show file tree
Hide file tree
Showing 15 changed files with 617 additions and 727 deletions.
138 changes: 75 additions & 63 deletions app/includes/signMsg.tpl
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
<!-- Sign Message Panel -->
<h2>
<a translate="NAV_SignMsg" ng-class="{ 'isActive': visibility=='signView'}" ng-click="setVisibility('signView')"> Sign Message </a>
or
<a translate="MSG_verify" ng-class="{ 'isActive': visibility=='verifyView'}" ng-click="setVisibility('verifyView')"> Verify Message </a>
</h2>

<article class="col-xs-12 clearfix" ng-switch on="visibility">

<!-- Sign Message Stuff -->
<section ng-switch-when="signView">

<!-- Message -->
<div class="account-help-icon">
<img src="images/icon-help.svg" class="help-icon" />
<ul class="account-help-text">
<li translate="MSG_info2">Include your nickname and where you use the nickname so someone else cannot use it.</li>
<li translate="MSG_info3">Include a specific reason for the message so it cannot be reused for a different purpose.</li>
</ul>
<h5 translate="MSG_message"> Message </h5>
</div>


<textarea class="form-control" ng-model="signMsg.message" placeholder="This is user kvhnuke on reddit. The purpose of this message is to prove that 0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8 is my address." rows="5"></textarea>

<!-- Date -->
<div class="account-help-icon">
<img src="images/icon-help.svg" class="help-icon" />
<p class="account-help-text" translate="MSG_info1">Include the current date so the signature cannot be reused on a different date.</p>
<h5 translate="MSG_date"> Date </h5>
</div>
<input class="form-control" ng-model="signMsg.dateTime" type="text" placeholder="1 JAN 2016 14:15" />

<!-- Sign Message Button -->
<a class="btn btn-info btn-block" ng-click="generateSignedMsg()" translate="NAV_SignMsg" ng-show="wallet!=null"> Sign Message </a>


<!-- Signed Message -->
<div ng-show="signMsg.signedMsg">
<h5 translate="MSG_signature"> Signature </h5>
<textarea class="form-control" rows="4" readonly title="Signature">{{signMsg.signedMsg }}</textarea>
</div>
</section>
<!-- / Sign Message Stuff -->



<!-- Verify Message Stuff -->
<section ng-switch-when="verifyView">
<!-- Signature -->
<h5 translate="MSG_signature"> Signature </h5>
<textarea class="form-control" ng-model="verifyMsg.signedMsg" rows="6" placeholder='{ "address":"0xA7DeFf12461661212734dB35AdE9aE7d987D648c","msg":"This message was signed by kvhnuke | 28 NOV 2016 1:38AM","sig":"0x2190fdf0a011863fed22050372088d08404eb7cae020b73f24cfeca967773b3867f53f1ec96baffbdc6ec9dd3ca7c2ba4eed19a500f65cb3c343e24091b0c4f21b"}'></textarea>

<!-- Verify Signature Message Button -->
<a class="btn btn-info btn-block" ng-click="verifySignedMessage()" translate="MSG_verify" ng-show="verifyMsg.signedMsg!=''"> Verify Message </a>



</section>
<!-- / Verify Message Stuff -->

</article>
<!-- / Sign Message Panel -->
<article class="col-xs-12 clearfix">
<div class="block text-center">
<h2>
<a translate="NAV_SignMsg" ng-class="{ 'isActive': visibility=='signView'}" ng-click="setVisibility('signView')"> Sign Message </a>
or
<a translate="MSG_verify" ng-class="{ 'isActive': visibility=='verifyView'}" ng-click="setVisibility('verifyView')"> Verify Message </a>
</h2>
</div>
</article>

<article class="col-xs-12 clearfix" ng-switch on="visibility">

<section class="block" ng-switch-when="signView">

<h4 translate="MSG_message"> Message </h4>

<textarea class="form-control"
ng-model="signMsg.message"
placeholder="This is a sweet message that you are signing to prove that you own the address you say you own."
rows="5"
ng-disabled="signMsg.signedMsg">
</textarea>

<p class="small">
<em translate="MSG_info2">
Include your nickname and where you use the nickname so someone else cannot use it.
</em>
<em translate="MSG_info3">
Include a specific reason for the message so it cannot be reused for a different purpose.
</em>
</p>

<br />

<a class="btn btn-info btn-block"
ng-click="generateSignedMsg()"
translate="NAV_SignMsg"
ng-show="wallet!=null">
Sign Message
</a>

<div ng-show="signMsg.signedMsg">
<h4 translate="MSG_signature"> Signature </h4>
<textarea class="form-control"
rows="8"
style="word-break: break-all;"
readonly
title="Signature">{{ signMsg.signedMsg }}</textarea>
</div>
</section>


<section class="block" ng-switch-when="verifyView">

<h5 translate="MSG_signature"> Signature </h5>
<textarea class="form-control"
ng-model="verifyMsg.signedMsg"
rows="8"
placeholder='{"address":"0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8","msg":"asdfasdfasdf","sig":"0x4771d78f13ba8abf608457f12471f427ca8f2fb046c1acb3f5969eefdfe452a10c9154136449f595a654b44b3b0163e86dd099beaca83bfd52d64c21da2221bb1c","version":"mew_v2"}'>
</textarea>

<a class="btn btn-info btn-block"
ng-click="verifySignedMessage()"
translate="MSG_verify"
ng-show="verifyMsg.signedMsg!=''"> Verify Message
</a>

<p class="alert alert-success"
ng-show="verifiedMsg.address!=null">
<strong>{{ verifiedMsg.address }}</strong> did sign the message <strong>{{ verifiedMsg.msg }}</strong>.
</p>

</section>

</article>
13 changes: 0 additions & 13 deletions app/includes/viewWalletInfo-content.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,6 @@
</div>
</section>

<section class="block" ng-show="wallet.type=='default'">
@@if (site === 'cx' ) {
<main ng-controller='signMsgCtrl' ng-show="wallet!=null">
@@if (site === 'cx' ) { @@include( './signMsg.tpl', { "site": "cx" } ) }
</main>
}
@@if (site === 'mew' ) {
<main class="tab-pane active" ng-show="wallet!=null" ng-controller='signMsgCtrl' >
@@if (site === 'mew') { @@include( './signMsg.tpl', { "site": "mew" } ) }
</main>
}
</section>

</article>

<article class="col-sm-4">
Expand Down
20 changes: 16 additions & 4 deletions app/layouts/signmsg.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
@@include( '../includes/headerEmbedded.tpl', { "site": "mew" } )
<section ng-controller="tabsCtrl">

<section ng-controller="tabsCtrl" ng-cloak>

<section class="container" style="min-height: 50%" ng-controller='signMsgCtrl'>
<!-- tab panes -->

<div class="tab-content">

<div class="clearfix">
@@include( '../includes/signMsg.tpl', { "site": "mew" } )
<div ng-show="visibility=='signView' && wallet==null"><wallet-decrypt-drtv></wallet-decrypt-drtv></div>
</div>

<div class="clearfix col-xs-12" ng-show="visibility=='signView' && wallet==null">
<wallet-decrypt-drtv></wallet-decrypt-drtv>
</div>

</div>
<!-- /tab panes -->

@@include('../includes/alerts.tpl')

</section>

</section>

<br /><br /><br /><br /><br />

@@include( '../includes/footerEmbedded.tpl', { "site": "mew" } )
4 changes: 1 addition & 3 deletions app/scripts/controllers/decryptWalletCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,11 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
};
$scope.scanMetamask = function() {
window.web3.eth.getAccounts(function (err, accounts) {
if (err) throw err

if (err) $scope.notifier.danger(err + '. Are you sure you are on a secure (SSL / HTTPS) connection?')
var address = accounts[0]
var addressBuffer = Buffer.from(address.slice(2), 'hex');
var wallet = new Web3Wallet(addressBuffer);
wallet.setBalance(false);

// set wallet
$scope.wallet = wallet
walletService.wallet = wallet
Expand Down
5 changes: 0 additions & 5 deletions app/scripts/controllers/sendTxCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ var sendTxCtrl = function($scope, $sce, walletService) {
$scope.tx.readOnly = globalFuncs.urlGet('readOnly') == null ? false : true;
var currentTab = $scope.globalService.currentTab;
var tabs = $scope.globalService.tabs;

if (currentTab == tabs.sendTransaction.id) {
//this is for you @tayvano
}
// Tokens
$scope.tokenVisibility = "hidden";
$scope.tokenTx = {
to: '',
Expand Down
108 changes: 90 additions & 18 deletions app/scripts/controllers/signMsgCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,122 @@ var signMsgCtrl = function($scope, $sce, walletService) {
});
$scope.signMsg = {
message: '',
dateTime: '',
status: '',
signedMsg: ''
}
$scope.verifyMsg = {
signedMsg: '',
status: ''
}

$scope.generateSignedMsg = function() {
try {
var thisMsg = $scope.signMsg.message
var thisDate = $scope.signMsg.dateTime
var thisSpacer = (thisMsg.length > 0 && thisDate.length > 0) ? ' ' : ''
var thisMessage = thisMsg + thisSpacer + thisDate
var hash = ethUtil.hashPersonalMessage(ethUtil.toBuffer(thisMessage))
var signed = ethUtil.ecsign(hash, $scope.wallet.getPrivateKey())
var combined = Buffer.concat([Buffer.from(signed.r), Buffer.from(signed.s), Buffer.from([signed.v])])
var combinedHex = combined.toString('hex')
$scope.signMsg.signedMsg = JSON.stringify({
address: $scope.wallet.getChecksumAddressString(),
msg: thisMessage,
sig: '0x' + combinedHex,
standard: true
})
var thisMessage = $scope.signMsg.message
var hwType = $scope.wallet.getHWType()

// Sign via MetaMask
if ((typeof hwType != "undefined") && (hwType == "web3")) {

var msg = ethUtil.bufferToHex(new Buffer(thisMessage, 'utf8'))
var signingAddr = web3.eth.accounts[0]
var params = [msg, signingAddr]
var method = 'personal_sign'
$scope.notifier.info("Sent message for signing via MetaMask / Mist.")

web3.currentProvider.sendAsync({
method,
params,
signingAddr,
}, function(err, result) {
if (err) return $scope.notifier.danger(err)
if (result.error) return $scope.notifier.danger(result.error)
$scope.signMsg.signedMsg = JSON.stringify({
address: signingAddr,
msg: thisMessage,
sig: result.result,
version: '2'
}, null, 2)
$scope.notifier.success('Successfully Signed Message with ' + signingAddr)
})

// Sign via Ledger
} else if ((typeof hwType != "undefined") && (hwType == "ledger")) {
var msg = Buffer.from(thisMessage).toString("hex")
var app = new ledgerEth($scope.wallet.getHWTransport());
var localCallback = function(signed, error) {
if (typeof error != "undefined") {
error = error.errorCode ? u2f.getErrorByCode(error.errorCode) : error;
if (callback !== undefined) callback({
isError: true,
error: error
});
return;
}
var combined = signed['r'] + signed['s'] + signed['v']
var combinedHex = combined.toString('hex')
var signingAddr = $scope.wallet.getAddressString()
$scope.signMsg.signedMsg = JSON.stringify({
address: $scope.wallet.getAddressString(),
msg: thisMessage,
sig: '0x' + combinedHex,
version: '2'
}, null, 2)
$scope.notifier.success('Successfully Signed Message with ' + signingAddr)
}
app.signPersonalMessage_async( $scope.wallet.getPath(), msg, localCallback);

// Sign via PK
} else {
var msg = ethUtil.hashPersonalMessage(ethUtil.toBuffer(thisMessage))
var signed = ethUtil.ecsign(msg, $scope.wallet.getPrivateKey())
console.log(signed.r)
console.log(signed.s)
console.log([signed.v])
var combined = Buffer.concat([Buffer.from(signed.r), Buffer.from(signed.s), Buffer.from([signed.v])])
var combinedHex = combined.toString('hex')
var signingAddr = $scope.wallet.getAddressString()
$scope.signMsg.signedMsg = JSON.stringify({
address: $scope.wallet.getAddressString(),
msg: thisMessage,
sig: '0x' + combinedHex,
version: '2'
}, null, 2)
$scope.notifier.success('Successfully Signed Message with ' + signingAddr)
}

} catch (e) {
$scope.notifier.danger(e)
}
}

$scope.verifySignedMessage = function() {
try {

var json = JSON.parse($scope.verifyMsg.signedMsg)
var sig = new Buffer(ethFuncs.getNakedAddress(json.sig), 'hex')
if (sig.length != 65) throw globalFuncs.errorMsgs[12]
sig[64] = sig[64] == 0 || sig[64] == 1 ? sig[64] + 27 : sig[64]
var hash = json.standard ? ethUtil.hashPersonalMessage(ethUtil.toBuffer(json.msg)) : ethUtil.sha3(json.msg)
var hash = json.version=='2' ? ethUtil.hashPersonalMessage(ethUtil.toBuffer(json.msg)) : ethUtil.sha3(json.msg)
var pubKey = ethUtil.ecrecover(hash, sig[64], sig.slice(0, 32), sig.slice(32, 64))
if (ethFuncs.getNakedAddress(json.address) != ethUtil.pubToAddress(pubKey).toString('hex')) throw globalFuncs.errorMsgs[12]
else $scope.notifier.success(globalFuncs.successMsgs[6])
else {
$scope.notifier.success(globalFuncs.successMsgs[6])
$scope.verifiedMsg = {
address: json.address,
msg: json.msg,
sig: json.sig,
version: json.version
}
}

} catch (e) {
$scope.notifier.danger(e);
}
}

$scope.setVisibility = function(str) {
$scope.visibility = str;
}

}
module.exports = signMsgCtrl
module.exports = signMsgCtrl
2 changes: 1 addition & 1 deletion app/scripts/controllers/tabsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var tabsCtrl = function($scope, globalService, $translate, $sce) {
$scope.notifier.danger(globalFuncs.errorMsgs[32]);
} else {
$scope.nodeIsConnected = true;
$scope.notifier.info( globalFuncs.successMsgs[5] + '— Now, check the URL: <strong>' + window.location.href + '.</strong> <br /> Network: <strong>' + $scope.nodeType + ' </strong> provided by <strong>' + $scope.nodeService + '.</strong>', 0)
$scope.notifier.info( globalFuncs.successMsgs[5] + '— Now, check the URL: <strong>' + window.location.href + '.</strong> <br /> Network: <strong>' + $scope.nodeType + ' </strong> provided by <strong>' + $scope.nodeService + '.</strong>', 10)
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions app/scripts/directives/walletDecryptDrtv.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ <h4 translate="decrypt_Access"> Select the format of your private key: </h4>
<span translate="x_Keystore2">Keystore / JSON File</span>
</label>

<label class="radio" ng-hide="globalService.currentTab==globalService.tabs.signMsg.id">
<input aria-flowto="aria2" type="radio" aria-label="Ledger Wallet hardware wallet" ng-model="walletType" value="ledger" />Ledger Wallet
<label class="radio">
<input aria-flowto="aria2" type="radio" aria-label="Ledger Wallet hardware wallet" ng-model="walletType" value="ledger" />
<span>Ledger Wallet</span>
</label>

<label class="radio">
Expand Down
Loading

0 comments on commit 86dd03e

Please sign in to comment.