mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-30 16:43:46 +00:00
Conflicts: Godeps/Godeps.json Godeps/_workspace/src/github.com/expanse-project/ethash/ethash_test.go Makefile VERSION cmd/gexp/blocktestcmd.go cmd/gexp/js.go cmd/gexp/js_test.go cmd/gexp/main.go cmd/utils/cmd.go cmd/utils/flags.go common/natspec/natspec.go common/natspec/natspec_e2e_test.go common/natspec/natspec_e2e_test.go.orig common/path.go common/registrar/registrar.go core/block_processor.go core/block_processor_test.go core/chain_manager.go core/chain_manager_test.go core/chain_util_test.go core/events.go core/execution.go core/filter.go core/state/errors.go core/state/statedb.go core/state_transition.go core/transaction_util.go core/types/bloom9.go core/types/common.go core/types/derive_sha.go core/types/receipt.go core/vm/common.go core/vm/environment.go core/vm/instructions.go core/vm/jit.go core/vm/jit_test.go core/vm/vm.go docker/Dockerfile docker/supervisord.conf eth/downloader/modes.go event/filter/eth_filter.go exp/backend.go exp/downloader/downloader.go exp/downloader/downloader_test.go exp/downloader/peer.go exp/downloader/queue.go exp/gasprice.go exp/handler.go exp/sync.go miner/worker.go rpc/api/admin.go rpc/api/debug.go rpc/api/eth.go rpc/api/eth_args.go rpc/api/miner.go rpc/api/personal.go rpc/comms/ipc.go rpc/comms/ipc_unix.go rpc/comms/ipc_windows.go tests/block_test_util.go tests/files/BlockchainTests/bcUncleTest.json trie/cache.go trie/fullnode.go trie/hashnode.go trie/secure_trie.go trie/shortnode.go trie/trie.go trie/trie_test.go trie/valuenode.go xeth/xeth.go
233 lines
6.5 KiB
Go
233 lines
6.5 KiB
Go
// Copyright 2014 The go-ethereum Authors && Copyright 2015 go-expanse Authors
|
|
// This file is part of the go-expanse library.
|
|
//
|
|
// The go-expanse library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-expanse library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-expanse library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/expanse-project/go-expanse/common"
|
|
"github.com/expanse-project/go-expanse/crypto/randentropy"
|
|
)
|
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
|
ks := NewKeyStorePlain(common.DefaultDataDir())
|
|
pass := "" // not used but required by API
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
k2 := new(Key)
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ks.DeleteKey(k2.Address, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
|
|
pass := "foo"
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
k2 := new(Key)
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ks.DeleteKey(k2.Address, pass) // also to clean up created files
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
|
|
pass := "foo"
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = ks.GetKey(k1.Address, "bar") // wrong passphrase
|
|
if err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase
|
|
if err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ks.DeleteKey(k1.Address, pass) // to clean up
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestImportPreSaleKey(t *testing.T) {
|
|
// file content of a presale key file generated with:
|
|
// python pyethsaletool.py genwallet
|
|
// with password "foo"
|
|
fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
|
|
pass := "foo"
|
|
_, err := ImportPreSaleKey(ks, []byte(fileContent), pass)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Test and utils for the key store tests in the Expanse JSON tests;
|
|
// tests/KeyStoreTests/basic_tests.json
|
|
type KeyStoreTestV3 struct {
|
|
Json encryptedKeyJSONV3
|
|
Password string
|
|
Priv string
|
|
}
|
|
|
|
type KeyStoreTestV1 struct {
|
|
Json encryptedKeyJSONV1
|
|
Password string
|
|
Priv string
|
|
}
|
|
|
|
func TestV3_PBKDF2_1(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("tests/v3_test_vector.json", t)
|
|
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
|
|
}
|
|
|
|
func TestV3_PBKDF2_2(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
testDecryptV3(tests["test1"], t)
|
|
}
|
|
|
|
func TestV3_PBKDF2_3(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
|
|
}
|
|
|
|
func TestV3_PBKDF2_4(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
testDecryptV3(tests["evilnonce"], t)
|
|
}
|
|
|
|
func TestV3_Scrypt_1(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("tests/v3_test_vector.json", t)
|
|
testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
|
|
}
|
|
|
|
func TestV3_Scrypt_2(t *testing.T) {
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
testDecryptV3(tests["test2"], t)
|
|
}
|
|
|
|
func TestV1_1(t *testing.T) {
|
|
tests := loadKeyStoreTestV1("tests/v1_test_vector.json", t)
|
|
testDecryptV1(tests["test1"], t)
|
|
}
|
|
|
|
func TestV1_2(t *testing.T) {
|
|
ks := NewKeyStorePassphrase("tests/v1", LightScryptN, LightScryptP)
|
|
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
|
|
k, err := ks.GetKey(addr, "g")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if k.Address != addr {
|
|
t.Fatal(fmt.Errorf("Unexpected address: %v, expected %v", k.Address, addr))
|
|
}
|
|
|
|
privHex := hex.EncodeToString(FromECDSA(k.PrivateKey))
|
|
expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
|
|
if privHex != expectedHex {
|
|
t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
|
|
}
|
|
}
|
|
|
|
func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
|
|
privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
privHex := hex.EncodeToString(privBytes)
|
|
if test.Priv != privHex {
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
}
|
|
}
|
|
|
|
func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
|
|
privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
privHex := hex.EncodeToString(privBytes)
|
|
if test.Priv != privHex {
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
}
|
|
}
|
|
|
|
func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
|
|
tests := make(map[string]KeyStoreTestV3)
|
|
err := common.LoadJSON(file, &tests)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return tests
|
|
}
|
|
|
|
func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
|
|
tests := make(map[string]KeyStoreTestV1)
|
|
err := common.LoadJSON(file, &tests)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return tests
|
|
}
|
|
|
|
func TestKeyForDirectICAP(t *testing.T) {
|
|
key := NewKeyForDirectICAP(randentropy.Reader)
|
|
if !strings.HasPrefix(key.Address.Hex(), "0x00") {
|
|
t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
|
|
}
|
|
}
|