From b3a3fdf9a447bd2b3f862380d87c675138da78e7 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 8 Apr 2015 23:03:47 +0200 Subject: [PATCH 1/2] Support for import/export hex encoded keys, closes #635 --- cmd/geth/main.go | 3 +-- common/bytes.go | 12 ++++++++++++ crypto/crypto.go | 6 +++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 9437f8eb4a..02964dadff 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -149,8 +149,7 @@ password to file or expose in any other way. Imports an unencrypted private key from and creates a new account. Prints the address. -The keyfile is assumed to contain an unencrypted private key in canonical EC -raw bytes format. +The keyfile is assumed to contain an unencrypted private key in hexadecimal format. The account is saved in encrypted format, you are prompted for a passphrase. diff --git a/common/bytes.go b/common/bytes.go index 5bdacd810a..ba180ac94d 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -147,6 +147,18 @@ func Hex2Bytes(str string) []byte { return h } +func HexBytes2Bytes(d []byte) []byte { + r := make([]byte, hex.DecodedLen(len(d))) + hex.Decode(r, d) + return r +} + +func Bytes2HexBytes(d []byte) []byte { + r := make([]byte, hex.EncodedLen(len(d))) + hex.Encode(r, d) + return r +} + func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) { if len(str) > 1 && str[0:2] == "0x" && !strings.Contains(str, "\n") { ret = Hex2Bytes(str[2:]) diff --git a/crypto/crypto.go b/crypto/crypto.go index 7d1d51fa6c..6e3ffbd4aa 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -121,7 +121,7 @@ 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, 32) + buf := make([]byte, 64) fd, err := os.Open(file) if err != nil { return nil, err @@ -130,13 +130,13 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { if _, err := io.ReadFull(fd, buf); err != nil { return nil, err } - return ToECDSA(buf), nil + return ToECDSA(common.HexBytes2Bytes(buf)), nil } // SaveECDSA saves a secp256k1 private key to the given file with restrictive // permissions func SaveECDSA(file string, key *ecdsa.PrivateKey) error { - return ioutil.WriteFile(file, FromECDSA(key), 0600) + return ioutil.WriteFile(file, common.Bytes2HexBytes(FromECDSA(key)), 0600) } func GenerateKey() (*ecdsa.PrivateKey, error) { From ef393da9334adcf99187c0825df025596ae41fb3 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Thu, 9 Apr 2015 10:59:37 +0200 Subject: [PATCH 2/2] removed utility function and implemented hex conversation in crypto functions --- common/bytes.go | 12 ------------ crypto/crypto.go | 11 +++++++++-- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index ba180ac94d..5bdacd810a 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -147,18 +147,6 @@ func Hex2Bytes(str string) []byte { return h } -func HexBytes2Bytes(d []byte) []byte { - r := make([]byte, hex.DecodedLen(len(d))) - hex.Decode(r, d) - return r -} - -func Bytes2HexBytes(d []byte) []byte { - r := make([]byte, hex.EncodedLen(len(d))) - hex.Encode(r, d) - return r -} - func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) { if len(str) > 1 && str[0:2] == "0x" && !strings.Contains(str, "\n") { ret = Hex2Bytes(str[2:]) diff --git a/crypto/crypto.go b/crypto/crypto.go index 6e3ffbd4aa..9865c87c4e 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -130,13 +130,20 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { if _, err := io.ReadFull(fd, buf); err != nil { return nil, err } - return ToECDSA(common.HexBytes2Bytes(buf)), nil + + key, err := hex.DecodeString(string(buf)) + if err != nil { + return nil, err + } + + return ToECDSA(key), nil } // SaveECDSA saves a secp256k1 private key to the given file with restrictive // permissions func SaveECDSA(file string, key *ecdsa.PrivateKey) error { - return ioutil.WriteFile(file, common.Bytes2HexBytes(FromECDSA(key)), 0600) + k := hex.EncodeToString(FromECDSA(key)) + return ioutil.WriteFile(file, []byte(k), 0600) } func GenerateKey() (*ecdsa.PrivateKey, error) {