Skip to content

Instantly share code, notes, and snippets.

@dcb9
Created December 7, 2017 14:31
Show Gist options
  • Save dcb9/febefaab8370423f7ddbedfa8a8b4c87 to your computer and use it in GitHub Desktop.
Save dcb9/febefaab8370423f7ddbedfa8a8b4c87 to your computer and use it in GitHub Desktop.
restore private key from ethereum, geth test net keystore
package main
import (
"encoding/hex"
"fmt"
"io/ioutil"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
file := `YOUR_DATA_PATH/keystore/UTC-xxx`
password := `YOUR_PASSWORD`
keyjson, err := ioutil.ReadFile(file)
key, err := keystore.DecryptKey(keyjson, password)
if err != nil {
fmt.Printf("json key failed to decrypt: %v\n", err)
}
fmt.Printf("Original PrivateKey %#v\n", key.PrivateKey)
privateKeyBytes := crypto.FromECDSA(key.PrivateKey)
fmt.Printf("privateKeyHexStr %x\n", privateKeyBytes)
privateKey, err := crypto.ToECDSA(privateKeyBytes)
fmt.Printf("Private Key from str %#v err %v \n", privateKey, err)
hexPrivateKey := hex.EncodeToString(privateKeyBytes)
fmt.Println(hexPrivateKey)
neoPrivateKeyBytes, _ := hex.DecodeString(hexPrivateKey)
if string(neoPrivateKeyBytes) == string(privateKeyBytes) {
fmt.Println("neo private key bytes == private key bytes")
} else {
fmt.Println("neo private key bytes != private key bytes")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment