Guest User

Untitled

a guest
Jul 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>TREZOR Sign Transaction Test</title>
  5. <script>
  6.  
  7. function trezorSignTx() {
  8. var chainId = document.getElementById('chainId').value;
  9. var address = document.getElementById('address').value;
  10. // spend one change output
  11.  
  12. var address_n = "m/44'/" + chainId + "'/0'/0/0";
  13. console.log('chainId = ' + chainId + '/ address = ' + address);
  14.  
  15. // var address_n = [44 | 0x80000000,
  16. // 60 | 0x80000000,
  17. // 0 | 0x80000000 ,
  18. // 0 ]; // same, in raw form
  19.  
  20. var nonce = '04'; // note - it is hex, not number!!!
  21. var gas_price = '0861c46800';
  22. var gas_limit = '5208';
  23. var to = address.replace("0x", "");
  24. var value = "01"; // in hexadecimal, in wei - this is about 18 ETC
  25. var data = null // for no data
  26. var chain_id = parseInt(chainId); // 1 for ETH, 61 for ETC
  27.  
  28. TrezorConnect.ethereumSignTx(
  29. address_n,
  30. nonce,
  31. gas_price,
  32. gas_limit,
  33. to,
  34. value,
  35. data,
  36. chain_id,
  37. function (response) {
  38. if (response.success) {
  39. console.log('Signature V (recovery parameter):', response.v); // number
  40. console.log('Signature R component:', response.r); // bytes
  41. console.log('Signature S component:', response.s); // bytes
  42. } else {
  43. console.error('Error:', response.error); // error message
  44. }
  45. delete response.success;
  46. response.value = "0x" + value;
  47. response.v = "0x" + response.v.toString(16);
  48. response.r = "0x" + response.r;
  49. response.s = "0x" + response.s;
  50. response.nonce = "0x" + nonce;
  51. response.data = "0x";
  52. response.chainId = chain_id;
  53. response.gasLimit = "0x" + gas_limit;
  54. response.gasPrice = "0x" + gas_price;
  55. response.to = "0x" + to;
  56. document.getElementById("response").innerHTML =
  57. "const ethTx = require('ethereumjs-tx');\n" +
  58. "const txParams = " + JSON.stringify(response, undefined, 2) + ";\n" +
  59. "// Transaction is created\n" +
  60. "const tx = new ethTx(txParams);\n" +
  61. "const serializedTx = tx.serialize();\n" +
  62. "const rawTx = '0x' + serializedTx.toString('hex');\n" +
  63. "var ret = tx.verifySignature();\n" +
  64. "console.log('verifySignature() = ', ret);\n" +
  65. "console.log('SenderAddress = ' + tx.getSenderAddress().toString('hex'));\n" +
  66. "\n" +
  67. "if (txParams.to.toLowerCase().slice(2) == tx.getSenderAddress().toString('hex')) {\n" +
  68. " console.log('SUCCESS!');\n" +
  69. "} else {\n" +
  70. " console.log('FAIL');\n" +
  71. "}\n" +
  72. "\n" +
  73. "//console.log(rawTx)\n" +
  74. "console.log('eth.sendRawTransaction(\"' + rawTx + '\")');\n" +
  75. "";
  76. }, '1.4.2');
  77. }
  78.  
  79. </script>
  80. </head>
  81. <body>
  82. <form>
  83. <select id='chainId'>
  84. <option value="1">ETH</option>
  85. <option value="61">ETC</option>
  86. <option value="256">256</option>
  87. <option value="512">512</option>
  88. <option value="820">CLO</option>
  89. <option value="31102">ESN</option>
  90. </select>
  91. <input id="address" type="text" size="64" placeholder="0x..." value="0x1B7eBfbF517F41d1C2Cc078511Af429d3798ee3A" /> <!-- FIXME -->
  92. </form>
  93. <button onclick="trezorSignTx()">Sign</button>
  94.  
  95. <pre id="response"></pre>
  96.  
  97. <script src="../connect.js"></script>
  98.  
  99. </body>
  100. </html>
Add Comment
Please, Sign In to add comment