testsigner.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. // This file is a test-utility for testing clef-functionality
  17. //
  18. // Start clef with
  19. //
  20. // build/bin/clef --4bytedb=./cmd/clef/4byte.json --rpc
  21. //
  22. // Start geth with
  23. //
  24. // build/bin/geth --nodiscover --maxpeers 0 --signer http://localhost:8550 console --preload=cmd/clef/tests/testsigner.js
  25. //
  26. // and in the console simply invoke
  27. //
  28. // > test()
  29. //
  30. // You can reload the file via `reload()`
  31. function reload(){
  32. loadScript("./cmd/clef/tests/testsigner.js");
  33. }
  34. function init(){
  35. if (typeof accts == 'undefined' || accts.length == 0){
  36. accts = eth.accounts
  37. console.log("Got accounts ", accts);
  38. }
  39. }
  40. init()
  41. function testTx(){
  42. if( accts && accts.length > 0) {
  43. var a = accts[0]
  44. var txdata = eth.signTransaction({from: a, to: a, value: 1, nonce: 1, gas: 1, gasPrice: 1})
  45. var v = parseInt(txdata.tx.v)
  46. console.log("V value: ", v)
  47. if (v == 37 || v == 38){
  48. console.log("Mainnet 155-protected chainid was used")
  49. }
  50. if (v == 27 || v == 28){
  51. throw new Error("Mainnet chainid was used, but without replay protection!")
  52. }
  53. }
  54. }
  55. function testSignText(){
  56. if( accts && accts.length > 0){
  57. var a = accts[0]
  58. var r = eth.sign(a, "0x68656c6c6f20776f726c64"); //hello world
  59. console.log("signing response", r)
  60. }
  61. }
  62. function testClique(){
  63. if( accts && accts.length > 0){
  64. var a = accts[0]
  65. var r = debug.testSignCliqueBlock(a, 0); // Sign genesis
  66. console.log("signing response", r)
  67. if( a != r){
  68. throw new Error("Requested signing by "+a+ " but got sealer "+r)
  69. }
  70. }
  71. }
  72. function test(){
  73. var tests = [
  74. testTx,
  75. testSignText,
  76. testClique,
  77. ]
  78. for( i in tests){
  79. try{
  80. tests[i]()
  81. }catch(err){
  82. console.log(err)
  83. }
  84. }
  85. }