github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/ethkey/message_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "io/ioutil" 29 "os" 30 "path/filepath" 31 "testing" 32 ) 33 34 func TestMessageSignVerify(t *testing.T) { 35 tmpdir, err := ioutil.TempDir("", "ethkey-test") 36 if err != nil { 37 t.Fatal("Can't create temporary directory:", err) 38 } 39 defer os.RemoveAll(tmpdir) 40 41 keyfile := filepath.Join(tmpdir, "the-keyfile") 42 message := "test message" 43 44 //创建密钥。 45 generate := runEthkey(t, "generate", keyfile) 46 generate.Expect(` 47 !! Unsupported terminal, password will be echoed. 48 Passphrase: {{.InputLine "foobar"}} 49 Repeat passphrase: {{.InputLine "foobar"}} 50 `) 51 _, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`) 52 address := matches[1] 53 generate.ExpectExit() 54 55 //签署一条消息。 56 sign := runEthkey(t, "signmessage", keyfile, message) 57 sign.Expect(` 58 !! Unsupported terminal, password will be echoed. 59 Passphrase: {{.InputLine "foobar"}} 60 `) 61 _, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`) 62 signature := matches[1] 63 sign.ExpectExit() 64 65 //验证消息。 66 verify := runEthkey(t, "verifymessage", address, signature, message) 67 _, matches = verify.ExpectRegexp(` 68 Signature verification successful! 69 Recovered public key: [0-9a-f]+ 70 Recovered address: (0x[0-9a-fA-F]{40}) 71 `) 72 recovered := matches[1] 73 verify.ExpectExit() 74 75 if recovered != address { 76 t.Error("recovered address doesn't match generated key") 77 } 78 }