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