github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/qctkey/message_test.go (about)

     1  
     2  package main
     3  
     4  import (
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestMessageSignVerify(t *testing.T) {
    12  	tmpdir, err := ioutil.TempDir("", "letkey-test")
    13  	if err != nil {
    14  		t.Fatal("Can't create temporary directory:", err)
    15  	}
    16  	defer os.RemoveAll(tmpdir)
    17  
    18  	keyfile := filepath.Join(tmpdir, "the-keyfile")
    19  	message := "test message"
    20  
    21  	// Create the key.
    22  	generate := runEthkey(t, "generate", keyfile)
    23  	generate.Expect(`
    24  !! Unsupported terminal, password will be echoed.
    25  Passphrase: {{.InputLine "foobar"}}
    26  Repeat passphrase: {{.InputLine "foobar"}}
    27  `)
    28  	_, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`)
    29  	address := matches[1]
    30  	generate.ExpectExit()
    31  
    32  	// Sign a message.
    33  	sign := runEthkey(t, "signmessage", keyfile, message)
    34  	sign.Expect(`
    35  !! Unsupported terminal, password will be echoed.
    36  Passphrase: {{.InputLine "foobar"}}
    37  `)
    38  	_, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
    39  	signature := matches[1]
    40  	sign.ExpectExit()
    41  
    42  	// Verify the message.
    43  	verify := runEthkey(t, "verifymessage", address, signature, message)
    44  	_, matches = verify.ExpectRegexp(`
    45  Signature verification successful!
    46  Recovered public key: [0-9a-f]+
    47  Recovered address: (0x[0-9a-fA-F]{40})
    48  `)
    49  	recovered := matches[1]
    50  	verify.ExpectExit()
    51  
    52  	if recovered != address {
    53  		t.Error("recovered address doesn't match generated key")
    54  	}
    55  }