github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/clef/tests/testsigner.js (about)

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