Go-ethereum: how convert PrivateKey to string

Created on 24 Aug 2016  路  3Comments  路  Source: ethereum/go-ethereum

System information

Geth version: geth version
OS & Version: Windows/Linux/OSX
Commit hash : (if develop)

Expected behaviour

I has a keystore file and its password, I want export it's privateKey, to backup and sendTransaction use ethereumjs-tx, but I don't know how to,

package main

import (
    "fmt"
    "github.com/ethereum/go-ethereum/accounts"
    "io/ioutil"
)


func main() {


    file := "/Users/john/Library/Ethereum/testnet/keystore/UTC--2016-08-15T15-55-53.113310954Z--5f56bba9ee673d0c7bccf4be1a78b9db3a51114d"
    password := "123123"

    keyjson, err := ioutil.ReadFile(file)

    key, err := accounts.DecryptKey(keyjson, password)
    if err != nil {
        fmt.Println("json key failed to decrypt: %v", err)
    }

    fmt.Println(key.PrivateKey) 


}

output:

&{{0xc820010fc0 81583740361756840836121323789587338945461422073887594179255540915112063238155 21797972115651064114086597227887646793489837261366149234119939453580822821536} 94774382518166634573449883147278946376407637768700369139633360135918296102392}

Actual behaviour

I want get the privateKey: like this

var privateKey = 'dc099880ee7d2b16e956b33cbd8b71d7cf5fdb85a3d14aea434572936a0afbd7';
//  some javascript use ethereumjs-tx

Steps to reproduce the behaviour

Backtrace

[backtrace]

Most helpful comment

fmt.Printf("%x", key.PrivateKey.D.Bytes())

or

fmt.Printf("%x", crypto.FromECDSA(key.PrivateKey))

All 3 comments

Hi, this is not per se an issue on the go-ethereum client but I'll try to help you: the PrivateKey object that you print out is of type https://golang.org/pkg/crypto/ecdsa/#PrivateKey so the last part of what you print out (94774382518166634573449883147278946376407637768700369139633360135918296102392) is the private key as integer, which you can convert to hex and you should get what you're looking for.

fmt.Printf("%x", key.PrivateKey.D.Bytes())

or

fmt.Printf("%x", crypto.FromECDSA(key.PrivateKey))

@karalabe @Hellsegga Thanks.

Was this page helpful?
0 / 5 - 0 ratings