github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/ethkey/message_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:27</date> 10 //</624342589528608768> 11 12 13 package main 14 15 import ( 16 "io/ioutil" 17 "os" 18 "path/filepath" 19 "testing" 20 ) 21 22 func TestMessageSignVerify(t *testing.T) { 23 tmpdir, err := ioutil.TempDir("", "ethkey-test") 24 if err != nil { 25 t.Fatal("Can't create temporary directory:", err) 26 } 27 defer os.RemoveAll(tmpdir) 28 29 keyfile := filepath.Join(tmpdir, "the-keyfile") 30 message := "test message" 31 32 //创建密钥。 33 generate := runEthkey(t, "generate", keyfile) 34 generate.Expect(` 35 !! Unsupported terminal, password will be echoed. 36 Passphrase: {{.InputLine "foobar"}} 37 Repeat passphrase: {{.InputLine "foobar"}} 38 `) 39 _, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`) 40 address := matches[1] 41 generate.ExpectExit() 42 43 //签署一条消息。 44 sign := runEthkey(t, "signmessage", keyfile, message) 45 sign.Expect(` 46 !! Unsupported terminal, password will be echoed. 47 Passphrase: {{.InputLine "foobar"}} 48 `) 49 _, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`) 50 signature := matches[1] 51 sign.ExpectExit() 52 53 //验证消息。 54 verify := runEthkey(t, "verifymessage", address, signature, message) 55 _, matches = verify.ExpectRegexp(` 56 Signature verification successful! 57 Recovered public key: [0-9a-f]+ 58 Recovered address: (0x[0-9a-fA-F]{40}) 59 `) 60 recovered := matches[1] 61 verify.ExpectExit() 62 63 if recovered != address { 64 t.Error("recovered address doesn't match generated key") 65 } 66 } 67