github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/cmd/clef/tests/testsigner.js (about)

     1  // Copyright 2019 The go-simplechain Authors
     2  // This file is part of go-simplechain.
     3  //
     4  // go-simplechain 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-simplechain 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-simplechain. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // This file is a test-utility for testing clef-functionality
    18  //
    19  // Start clef with
    20  //
    21  // build/bin/clef --4bytedb=./cmd/clef/4byte.json --rpc
    22  //
    23  // Start geth with
    24  //
    25  // build/bin/geth --nodiscover --maxpeers 0 --signer http://localhost:8550 console --preload=cmd/clef/tests/testsigner.js
    26  //
    27  // and in the console simply invoke
    28  //
    29  // > test()
    30  //
    31  // You can reload the file via `reload()`
    32  
    33  function reload() {
    34      loadScript("./cmd/clef/tests/testsigner.js");
    35  }
    36  
    37  function init() {
    38      if (typeof accts == 'undefined' || accts.length == 0) {
    39          accts = eth.accounts
    40          console.log("Got accounts ", accts);
    41      }
    42  }
    43  init()
    44  
    45  function testTx() {
    46      if (accts && accts.length > 0) {
    47          var a = accts[0]
    48          var txdata = eth.signTransaction({
    49              from: a,
    50              to: a,
    51              value: 1,
    52              nonce: 1,
    53              gas: 1,
    54              gasPrice: 1
    55          })
    56          var v = parseInt(txdata.tx.v)
    57          console.log("V value: ", v)
    58          if (v == 37 || v == 38) {
    59              console.log("Mainnet 155-protected chainid was used")
    60          }
    61          if (v == 27 || v == 28) {
    62              throw new Error("Mainnet chainid was used, but without replay protection!")
    63          }
    64      }
    65  }
    66  
    67  function testSignText() {
    68      if (accts && accts.length > 0) {
    69          var a = accts[0]
    70          var r = eth.sign(a, "0x68656c6c6f20776f726c64"); //hello world
    71          console.log("signing response", r)
    72      }
    73  }
    74  
    75  function testClique() {
    76      if (accts && accts.length > 0) {
    77          var a = accts[0]
    78          var r = debug.testSignCliqueBlock(a, 0); // Sign genesis
    79          console.log("signing response", r)
    80          if (a != r) {
    81              throw new Error("Requested signing by " + a + " but got sealer " + r)
    82          }
    83      }
    84  }
    85  
    86  function test() {
    87      var tests = [
    88          testTx,
    89          testSignText,
    90          testClique,
    91      ]
    92      for (i in tests) {
    93          try {
    94              tests[i]()
    95          } catch (err) {
    96              console.log(err)
    97          }
    98      }
    99  }