Merge pull request #678 from bas-vk/feature_635

Support for import/export hex encoded keys
pull/667/head
Jeffrey Wilcke 10 years ago
commit 558683d10c
  1. 3
      cmd/geth/main.go
  2. 13
      crypto/crypto.go

@ -149,8 +149,7 @@ password to file or expose in any other way.
Imports an unencrypted private key from <keyfile> and creates a new account. Imports an unencrypted private key from <keyfile> and creates a new account.
Prints the address. Prints the address.
The keyfile is assumed to contain an unencrypted private key in canonical EC The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
raw bytes format.
The account is saved in encrypted format, you are prompted for a passphrase. The account is saved in encrypted format, you are prompted for a passphrase.

@ -121,7 +121,7 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
// LoadECDSA loads a secp256k1 private key from the given file. // LoadECDSA loads a secp256k1 private key from the given file.
func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
buf := make([]byte, 32) buf := make([]byte, 64)
fd, err := os.Open(file) fd, err := os.Open(file)
if err != nil { if err != nil {
return nil, err return nil, err
@ -130,13 +130,20 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if _, err := io.ReadFull(fd, buf); err != nil { if _, err := io.ReadFull(fd, buf); err != nil {
return nil, err return nil, err
} }
return ToECDSA(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 // SaveECDSA saves a secp256k1 private key to the given file with restrictive
// permissions // permissions
func SaveECDSA(file string, key *ecdsa.PrivateKey) error { func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
return ioutil.WriteFile(file, FromECDSA(key), 0600) k := hex.EncodeToString(FromECDSA(key))
return ioutil.WriteFile(file, []byte(k), 0600)
} }
func GenerateKey() (*ecdsa.PrivateKey, error) { func GenerateKey() (*ecdsa.PrivateKey, error) {

Loading…
Cancel
Save