testsigner.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is a test-utility for testing clef-functionality
  2. //
  3. // Start clef with
  4. //
  5. // build/bin/clef --4bytedb=./cmd/clef/4byte.json --rpc
  6. //
  7. // Start geth with
  8. //
  9. // build/bin/geth --nodiscover --maxpeers 0 --signer http://localhost:8550 console --preload=cmd/clef/tests/testsigner.js
  10. //
  11. // and in the console simply invoke
  12. //
  13. // > test()
  14. //
  15. // You can reload the file via `reload()`
  16. function reload(){
  17. loadScript("./cmd/clef/tests/testsigner.js");
  18. }
  19. function init(){
  20. if (typeof accts == 'undefined' || accts.length == 0){
  21. accts = eth.accounts
  22. console.log("Got accounts ", accts);
  23. }
  24. }
  25. init()
  26. function testTx(){
  27. if( accts && accts.length > 0) {
  28. var a = accts[0]
  29. var txdata = eth.signTransaction({from: a, to: a, value: 1, nonce: 1, gas: 1, gasPrice: 1})
  30. var v = parseInt(txdata.tx.v)
  31. console.log("V value: ", v)
  32. if (v == 37 || v == 38){
  33. console.log("Mainnet 155-protected chainid was used")
  34. }
  35. if (v == 27 || v == 28){
  36. throw new Error("Mainnet chainid was used, but without replay protection!")
  37. }
  38. }
  39. }
  40. function testSignText(){
  41. if( accts && accts.length > 0){
  42. var a = accts[0]
  43. var r = eth.sign(a, "0x68656c6c6f20776f726c64"); //hello world
  44. console.log("signing response", r)
  45. }
  46. }
  47. function testClique(){
  48. if( accts && accts.length > 0){
  49. var a = accts[0]
  50. var r = debug.testSignCliqueBlock(a, 0); // Sign genesis
  51. console.log("signing response", r)
  52. if( a != r){
  53. throw new Error("Requested signing by "+a+ " but got sealer "+r)
  54. }
  55. }
  56. }
  57. function test(){
  58. var tests = [
  59. testTx,
  60. testSignText,
  61. testClique,
  62. ]
  63. for( i in tests){
  64. try{
  65. tests[i]()
  66. }catch(err){
  67. console.log(err)
  68. }
  69. }
  70. }