Added import/exporting of private keys

This commit is contained in:
obscuren 2014-02-18 12:09:26 +01:00
parent 8c8554f558
commit ab7dc92404
2 changed files with 52 additions and 3 deletions

View File

@ -13,6 +13,8 @@ var AddPeer string
var MaxPeer int var MaxPeer int
var GenAddr bool var GenAddr bool
var UseSeed bool var UseSeed bool
var ImportKey string
var ExportKey bool
func Init() { func Init() {
flag.BoolVar(&StartConsole, "c", false, "debug and testing console") flag.BoolVar(&StartConsole, "c", false, "debug and testing console")
@ -21,7 +23,9 @@ func Init() {
flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support") flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
flag.BoolVar(&UseSeed, "seed", true, "seed peers") flag.BoolVar(&UseSeed, "seed", true, "seed peers")
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key") flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
flag.BoolVar(&ExportKey, "export", false, "export private key")
flag.StringVar(&OutboundPort, "p", "30303", "listening port") flag.StringVar(&OutboundPort, "p", "30303", "listening port")
flag.StringVar(&ImportKey, "import", "", "imports the given private key (hex)")
flag.IntVar(&MaxPeer, "x", 5, "maximum desired peers") flag.IntVar(&MaxPeer, "x", 5, "maximum desired peers")
flag.Parse() flag.Parse()

View File

@ -38,8 +38,6 @@ func CreateKeyPair(force bool) {
fmt.Printf(` fmt.Printf(`
Generating new address and keypair. Generating new address and keypair.
Please keep your keys somewhere save. Please keep your keys somewhere save.
Currently Ethereum(G) does not support
exporting keys.
++++++++++++++++ KeyRing +++++++++++++++++++ ++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x addr: %x
@ -54,6 +52,29 @@ pubk: %x
} }
} }
func ImportPrivateKey(prvKey string) {
key := ethutil.FromHex(prvKey)
msg := []byte("tmp")
// Couldn't think of a better way to get the pub key
sig, _ := secp256k1.Sign(msg, key)
pub, _ := secp256k1.RecoverPubkey(msg, sig)
addr := ethutil.Sha3Bin(pub[1:])[12:]
fmt.Printf(`
Importing private key
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
`, addr, key, pub)
keyRing := ethutil.NewValue([]interface{}{key, addr, pub[1:]})
ethutil.Config.Db.Put([]byte("KeyRing"), keyRing.Encode())
}
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
Init() Init()
@ -87,7 +108,31 @@ func main() {
} }
os.Exit(0) os.Exit(0)
} else { } else {
CreateKeyPair(false) if len(ImportKey) > 0 {
fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")
var r string
fmt.Scanln(&r)
for ; ; fmt.Scanln(&r) {
if r == "n" || r == "y" {
break
} else {
fmt.Printf("Yes or no?", r)
}
}
if r == "y" {
ImportPrivateKey(ImportKey)
}
} else {
CreateKeyPair(false)
}
}
if ExportKey {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
keyRing := ethutil.NewValueFromBytes(data)
fmt.Printf("%x\n", keyRing.Get(0).Bytes())
os.Exit(0)
} }
if ShowGenesis { if ShowGenesis {