github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/cmd/ethkey/message_test.go (about)

     1  // Copyright 2018 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  
    17  package main
    18  
    19  import (
    20  	"github.com/TTCECO/gttc/common/hexutil"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  )
    26  
    27  func TestMessageSignVerify(t *testing.T) {
    28  	tmpdir, err := ioutil.TempDir("", "ethkey-test")
    29  	if err != nil {
    30  		t.Fatal("Can't create temporary directory:", err)
    31  	}
    32  	defer os.RemoveAll(tmpdir)
    33  
    34  	keyfile := filepath.Join(tmpdir, "the-keyfile")
    35  	message := "test message"
    36  
    37  	// Create the key.
    38  	generate := runEthkey(t, "generate", keyfile)
    39  	generate.Expect(`
    40  !! Unsupported terminal, password will be echoed.
    41  Passphrase: {{.InputLine "foobar"}}
    42  Repeat passphrase: {{.InputLine "foobar"}}
    43  `)
    44  	_, matches := generate.ExpectRegexp(`Address: (`+hexutil.CustomHexPrefix +`[0-9a-fA-F]{40})\n`)
    45  	address := matches[1]
    46  	generate.ExpectExit()
    47  
    48  	// Sign a message.
    49  	sign := runEthkey(t, "signmessage", keyfile, message)
    50  	sign.Expect(`
    51  !! Unsupported terminal, password will be echoed.
    52  Passphrase: {{.InputLine "foobar"}}
    53  `)
    54  	_, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
    55  	signature := matches[1]
    56  	sign.ExpectExit()
    57  
    58  	// Verify the message.
    59  	verify := runEthkey(t, "verifymessage", address, signature, message)
    60  	_, matches = verify.ExpectRegexp(`
    61  Signature verification successful!
    62  Recovered public key: [0-9a-f]+
    63  Recovered address: (`+hexutil.CustomHexPrefix +`[0-9a-fA-F]{40})
    64  `)
    65  	recovered := matches[1]
    66  	verify.ExpectExit()
    67  
    68  	if recovered != address {
    69  		t.Error("recovered address doesn't match generated key")
    70  	}
    71  }