github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/ethkey/message_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"io/ioutil"
    16  	"os"
    17  	"path/filepath"
    18  	"testing"
    19  )
    20  
    21  func TestMessageSignVerify(t *testing.T) {
    22  	tmpdir, err := ioutil.TempDir("", "ethkey-test")
    23  	if err != nil {
    24  		t.Fatal("Can't create temporary directory:", err)
    25  	}
    26  	defer os.RemoveAll(tmpdir)
    27  
    28  	keyfile := filepath.Join(tmpdir, "the-keyfile")
    29  	message := "test message"
    30  
    31  	// Create the key.
    32  	generate := runEthkey(t, "generate", keyfile)
    33  	generate.Expect(`
    34  !! Unsupported terminal, password will be echoed.
    35  Passphrase: {{.InputLine "foobar"}}
    36  Repeat passphrase: {{.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  Passphrase: {{.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  }