Skip to content
35 changes: 35 additions & 0 deletions cmd/geth/accountcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -88,6 +89,40 @@ Path of the secret key file: .*UTC--.+--[0-9a-f]{40}
`)
}

func TestAccountImport(t *testing.T) {
success := "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n"
failureTemplate := "Fatal: Failed to load the private key: expected 64 bytes, got %v\n"
tests := []struct {
key string
result string
}{
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", result: success},
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n", result: success},
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\r\n", result: success},
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1", result: fmt.Sprintf(failureTemplate, 65)},
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefx", result: fmt.Sprintf(failureTemplate, 65)},
{key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\n\n", result: fmt.Sprintf(failureTemplate, 67)},
}
for _, test := range tests {
importAccountWithExpect(t, test.key, test.result)
}
}

func importAccountWithExpect(t *testing.T, key string, expected string) {
dir := tmpdir(t)
keyfile := filepath.Join(dir, "key.prv")
if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil {
t.Error(err)
}
passwordFile := filepath.Join(dir, "password.txt")
if err := ioutil.WriteFile(passwordFile, []byte("foobar"), 0644); err != nil {
t.Error(err)
}
geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile)
defer geth.ExpectExit()
geth.Expect(expected)
}

func TestAccountNewBadRepeat(t *testing.T) {
geth := runGeth(t, "account", "new", "--lightkdf")
defer geth.ExpectExit()
Expand Down
18 changes: 13 additions & 5 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package crypto

import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
Expand Down Expand Up @@ -166,15 +166,23 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {

// LoadECDSA loads a secp256k1 private key from the given file.
func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
buf := make([]byte, 64)
fd, err := os.Open(file)
stat, err := os.Stat(file)
if err != nil {
return nil, err
}
defer fd.Close()
if _, err := io.ReadFull(fd, buf); err != nil {
size := stat.Size()
// Allow two extra chars for possible line ending to be checked later
if size < 64 || size > 66 {
return nil, fmt.Errorf("expected 64 bytes, got %v", size)
}
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
buf = bytes.TrimSpace(buf)
if len(buf) != 64 {
return nil, fmt.Errorf("expected 64 bytes, got %v", len(buf))
}

key, err := hex.DecodeString(string(buf))
if err != nil {
Expand Down