github.com/theQRL/go-zond@v0.2.1/cmd/zondkey/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  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/theQRL/go-zond/accounts/keystore"
    25  	"github.com/theQRL/go-zond/cmd/utils"
    26  	"github.com/theQRL/go-zond/common"
    27  )
    28  
    29  func TestMessageSignVerify(t *testing.T) {
    30  	tmpdir := t.TempDir()
    31  
    32  	keyfile := filepath.Join(tmpdir, "the-keyfile")
    33  	message := "test message"
    34  
    35  	// Create the key.
    36  	generate := runZondkey(t, "generate", "--lightkdf", keyfile)
    37  	generate.Expect(`
    38  !! Unsupported terminal, password will be echoed.
    39  Password: {{.InputLine "foobar"}}
    40  Repeat password: {{.InputLine "foobar"}}
    41  `)
    42  	generate.ExpectRegexp(`Address: (Z[0-9a-fA-F]{40})\n`)
    43  	generate.ExpectExit()
    44  
    45  	// Sign a message.
    46  	sign := runZondkey(t, "signmessage", keyfile, message)
    47  	sign.Expect(`
    48  !! Unsupported terminal, password will be echoed.
    49  Password: {{.InputLine "foobar"}}
    50  `)
    51  	_, matches := sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
    52  	signature := matches[1]
    53  	sign.ExpectExit()
    54  
    55  	// Read key from file.
    56  	keyjson, err := os.ReadFile(keyfile)
    57  	if err != nil {
    58  		utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfile, err)
    59  	}
    60  
    61  	// Decrypt key with passphrase.
    62  	key, err := keystore.DecryptKey(keyjson, "foobar")
    63  	if err != nil {
    64  		utils.Fatalf("Error decrypting key: %v", err)
    65  	}
    66  
    67  	// Verify the message.
    68  	publicKey := key.Dilithium.GetPK()
    69  	verify := runZondkey(t, "verifymessage", signature, common.Bytes2Hex(publicKey[:]), message)
    70  	verify.ExpectRegexp(`
    71  Signature verification successful!
    72  `)
    73  	verify.ExpectExit()
    74  }