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

Commit

Permalink
Merge pull request #69 from trezor/0xProject-master
Browse files Browse the repository at this point in the history
sign and verify ethereum message
  • Loading branch information
karelbilek committed Aug 9, 2017
2 parents ad30eae + 56a7b3e commit ce179c9
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 30 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ TrezorConnect.signTx(inputs, outputs, function (result) {
## Sign Ethereum transaction

```javascript
TrezorConnect.signEthereumTx(
TrezorConnect.ethereumSignTx(
address_n, // address path - either array or string, see example
nonce, // nonce - hexadecimal string
gas_price, // gas price - hexadecimal string
Expand Down Expand Up @@ -317,6 +317,53 @@ TrezorConnect.signMessage(path, message, function (result) {

The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Sign Ethereum message

`TrezorConnect.ethereumSignMessage(path, message, callback)` asks device to
sign a message using the private key derived by given BIP32 path. Path can be specified
either as an array of numbers or as string m/A'/B'/C/...

Message is signed and address + signature is returned

[Example:](examples/signmsg-ethereum.html)

```javascript
var path="m/44'/0'/0";
var message="Example message";
TrezorConnect.ethereumSignMessage(path, message, function (result) {
if (result.success) {
console.log('Message signed!', result.signature); // signature in hex
console.log('Signing address:', result.address); // address in standard b58c form
} else {
console.error('Error:', result.error); // error message
}
});
```
The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Verify Ethereum message

`TrezorConnect.ethereumVerifyMessage(address, signature, message, callback)` asks device to
verify a message using the ethereum address and signature.

Message is verified and success is returned.

[Example:](examples/signmsg-ethereum.html)

```javascript
var address="b1125f399310202822d7ee3eed38a65481a928ec"; // address in hex
var signature="7eb0c3ebaaabc8ff67a5413a79512293f0184ed3d136fc873f188b3dd39e043f3036f42c75c7c05e236b37f75dbe4b154437391bbe219e5e8d7d69ac4d89d6231c"; // signature in hex
var message="Example message"; // message utf8
TrezorConnect.ethereumVerifyMessage(path, signature, message, function (result) {
if (result.success) {
console.log(result.success);
} else {
console.error('Error:', result.error); // error message
}
});
```
The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Symmetric key-value encryption

`TrezorConnect.cipherKeyValue(path, key, value, encrypt, ask_on_encrypt, ask_on_decrypt, callback)` asks device to
Expand Down
62 changes: 62 additions & 0 deletions connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
};

// new implementation with ethereum at beginnig
this.ethereumSignTx = function() {
this.signEthereumTx.apply(this, arguments);
}

// old fallback
this.signEthereumTx = function (
address_n,
nonce,
Expand Down Expand Up @@ -386,6 +392,34 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
};

/**
* Sign an Ethereum message
*
* @param {string|array} path
* @param {string} message to sign (ascii)
* @param {string|function(SignMessageResult)} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.ethereumSignMessage = function (
path,
message,
callback,
requiredFirmware
) {
if (typeof path === 'string') {
path = parseHDPath(path);
}
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}
manager.sendWithChannel(_fwStrFix({
type: 'signethmsg',
path: path,
message: message,
}, requiredFirmware), callback);
};

/**
* Verify message
*
Expand Down Expand Up @@ -420,6 +454,34 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
};

/**
* Verify ethereum message
*
* @param {string} address
* @param {string} signature (base64)
* @param {string} message (string)
* @param {string|function()} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.ethereumVerifyMessage = function (
address,
signature,
message,
callback,
requiredFirmware
) {
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}
manager.sendWithChannel(_fwStrFix({
type: 'verifyethmsg',
address: address,
signature: signature,
message: message,
}, requiredFirmware), callback);
};

/**
* Symmetric key-value encryption
*
Expand Down
127 changes: 127 additions & 0 deletions examples/signmsg-ethereum.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>TREZOR Ethereum Sign Message Test</title>
<style>
table {
display: inline-block;
width: 49%;
vertical-align: top;
}
table tr:first-child {
font-weight: bold;
font-size: 18px;
text-indent: 100px;
}
table td {
vertical-align: top;
box-sizing: border-box;
clear: both;
}
table tr td:first-child {
width: 100px;
}
input {
width: 350px;
}
textarea {
width: 350px;
height: 100px;
}
</style>
<script>

function trezorSignMessage() {

var path = document.getElementById("path");
var message = document.getElementById("message");
var messageV = document.getElementById("messageV");
var address = document.getElementById("address");
var signature = document.getElementById("signature");


TrezorConnect.ethereumSignMessage(path.value, message.value, function (response) {
if (response.success) {
address.value = response.address;
messageV.value = message.value;
signature.value = response.signature;
} else {
address.value = "";
messageV.value = "";
signature.value = "";
address.appendChild(document.createTextNode("Error: " + response.error));

}
document.getElementById("response").innerHTML = JSON.stringify(response, undefined, 2);
});
}

function trezorVerifyMessage() {

var messageV = document.getElementById("messageV");
var address = document.getElementById("address");
var signature = document.getElementById("signature");


TrezorConnect.ethereumVerifyMessage(address.value, signature.value, messageV.value, function (response) {
document.getElementById("response").innerHTML = JSON.stringify(response, undefined, 2);
});
}

</script>
</head>
<body>

<table>
<tr>
<td colspan="2" style="font-weight:bold">Sign</td>
</tr>
<tr>
<td>Path:</td>
<td><input id="path" value="m/44'/60'/0'/0" /></td>
</tr>
<tr>
<td>Message:</td>
<td><input id="message" value="Example message" size="64" /></td>
</tr>
<tr>
<td></td>
<td>
<button onclick="trezorSignMessage()">Sign Message</button>
</td>
</tr>
</table>

<table>
<tr>
<td colspan="2" style="font-weight:bold">Verify</td>
</tr>
<tr>
<td>Address:</td>
<td><input id="address" value="b1125f399310202822d7ee3eed38a65481a928ec" /></td>
</tr>
<tr>
<td>Message:</td>
<td><input id="messageV" value="Example message" /></td>
</tr>
<tr>
<td>Signature:</td>
<td><textarea id="signature">7eb0c3ebaaabc8ff67a5413a79512293f0184ed3d136fc873f188b3dd39e043f3036f42c75c7c05e236b37f75dbe4b154437391bbe219e5e8d7d69ac4d89d6231c</textarea></td>
</tr>
<tr>
<td></td>
<td>
<button onclick="trezorVerifyMessage()">Verify Message</button>
</td>
</tr>
</table>

<hr>

<pre id="response"></pre>

<script src="../connect.js"></script>

</body>
</html>
2 changes: 1 addition & 1 deletion examples/signtx-ethereum.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// var data = null // for no data
var chain_id = 1; // 1 for ETH, 61 for ETC

TrezorConnect.signEthereumTx(
TrezorConnect.ethereumSignTx(
address_n,
nonce,
gas_price,
Expand Down
2 changes: 1 addition & 1 deletion popup/config_signed.bin

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion popup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"cb-insight": "^0.1.0",
"es6-promise": "^2.3.0",
"hd-wallet": "1.0.13-backfix-bitcore3",
"trezor.js": "6.0.3",
"trezor.js": "6.1.0",
"whatwg-fetch": "^0.11.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions popup/popup-dist.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions popup/popup-dist.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<div id="operation_login" style="display: none">Sign in to</div>
<div id="operation_signmsg" style="display: none">Sign a message</div>
<div id="operation_verifymsg" style="display: none">Verify a message</div>
<div id="operation_signethmsg" style="display: none">Sign Ethereum message</div>
<div id="operation_verifyethmsg" style="display: none">Verify Ethereum message</div>
<div id="operation_xpubkey" style="display: none">Export public key</div>
<div id="operation_freshaddress" style="display: none">Export fresh address</div>
<div id="operation_accountinfo" style="display: none">Export account info</div>
Expand Down
Loading

0 comments on commit ce179c9

Please sign in to comment.