github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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  	"path/filepath"
    21  	"testing"
    22  )
    23  
    24  func TestMessageSignVerify(t *testing.T) {
    25  	tmpdir := t.TempDir()
    26  
    27  	keyfile := filepath.Join(tmpdir, "the-keyfile")
    28  	message := "test message"
    29  
    30  	// Create the key.
    31  	generate := runEthkey(t, "generate", "--lightkdf", keyfile)
    32  	generate.Expect(`
    33  !! Unsupported terminal, password will be echoed.
    34  Password: {{.InputLine "foobar"}}
    35  Repeat password: {{.InputLine "foobar"}}
    36  `)
    37  	_, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`)
    38  	address := matches[1]
    39  	generate.ExpectExit()
    40  
    41  	// Sign a message.
    42  	sign := runEthkey(t, "signmessage", keyfile, message)
    43  	sign.Expect(`
    44  !! Unsupported terminal, password will be echoed.
    45  Password: {{.InputLine "foobar"}}
    46  `)
    47  	_, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
    48  	signature := matches[1]
    49  	sign.ExpectExit()
    50  
    51  	// Verify the message.
    52  	verify := runEthkey(t, "verifymessage", address, signature, message)
    53  	_, matches = verify.ExpectRegexp(`
    54  Signature verification successful!
    55  Recovered public key: [0-9a-f]+
    56  Recovered address: (0x[0-9a-fA-F]{40})
    57  `)
    58  	recovered := matches[1]
    59  	verify.ExpectExit()
    60  
    61  	if recovered != address {
    62  		t.Error("recovered address doesn't match generated key")
    63  	}
    64  }