How to pem encode an ethereum privateKey with go? - ethereum

I have the following code:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
return address
}
Now I want to pem encode the publicKey and privatekey and save:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
x509EncodedPri, _ := x509.MarshalECPrivateKey(privKey)
block := pem.Block{
Type: "ECDSA Private Key",
Headers: nil,
Bytes: x509EncodedPri,
}
file, err := os.Create("eccPrivate.pem")
if err != nil {
log.Fatal(err)
}
defer file.Close()
pem.Encode(file, &block)
return address
}
I found it empty:
$ cat eccPrivate.pem
-----BEGIN ECDSA Private Key-----
-----END ECDSA Private Key-----
I know I should use ecdsa.GenerateKey(elliptic.P256(), rand.Reader) to generate the private key, but I end up with a different address than crypto.GenerateKey().
If anyone can tell me where I am going wrong, it would be greatly appreciated.

Related

Golang how to pass username and password as JSON body to Post API

Using a post API request I want to receive a JWT token for the response.
For that, I want to send the username and password as JSON body input to the API. The username and password should be taken dynamically from the user as a JSON payload.
Currently, I am hardcoding the user name and password using strings.NewReader which is not suitable as this is a web application and many users will be using it.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
)
var authToken string
type authorization struct {
AuthorizationToken string `json:"authorizationToken"`
ExpiresTimestamp string `json:"expiresTimestamp"`
}
func main() {
enverr := godotenv.Load(".env")
if enverr != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
token()
}
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.NewReader(`
{
"name" : "testuser",
"pwd" : "testpwd",
"hName" : "hname"
}
`)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
if err != nil {
panic(err)
}
defer response.Body.Close()
content, _ := ioutil.ReadAll(response.Body)
var result authorization
if err := json.Unmarshal(content, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
}
authToken := result.AuthorizationToken
fmt.Println(PrettyPrint(authToken))
}
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
You can use http requests body (for POST request)
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
c := Credentials{}
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
err: = decoder.Decode(&c)
if err!=nil{
// handle it
}
// use it
fmt.Println(c.Username, c.Password)
If you are intending to distribute the application as an executable (aka command line in the Unix world), you can either:
Provide the username and password as program arguments, os.Args (omitting unchanged code):
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", os.Args[2], -1), "USER", os.Args[1], -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}
You should then be able to run your program (once compiled): ./filename user password
Or as better alternative, having sensitive data involved, have the username and password input from standard input with password being echoed (hiding the characters):
import (
// ...
"syscall"
"golang.org/x/term"
)
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username := reader.ReadString('\n') // handle error
fmt.Print("Enter password: ")
pwdbytes := term.ReadPassword(int(syscall.Stdin))
password := string(pwdbytes)
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", password, -1), "USER", username, -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}

Parse Ethereum private key in string in Go

I am trying really hard to convert Ethereum private keys BIP44 in string format to a type that can (*ecdsa.PrivateKey) be used by rest of the code.
import (
"crypto/x509"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
const (
privateKey2 string = "0xbacd06016aea4280e14efd7182ba18cd98bf11701943d3d47d76b04bb7baad19"
)
func main() {
_, err = x509.ParsePKCS8PrivateKey(firstKey)
if err != nil {
fmt.Println("Cannot parse private key")
}
}
Here is how a private key can be converted to Ethereum address
You need to import one additional package "crypto/ecdsa" and also remove "0x" from the private key.
privateKey, err := crypto.HexToECDSA(privateKey2)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

How to modify a return json

I'm taking the data from the github API with golang, however, I want to send to the front only the necessary data (id, name, url, language and description) instead of just returning everything that the github API gives me.
func GetAllReposStars(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
params := mux.Vars(r)
username := params["username"]
res, err := http.Get("https://api.github.com/users/" + username + "/starred")
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
log.Printf("Body: %s\n", body)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
json.NewEncoder(w)
}
You can define a type that has only the keys you need and decode the response from the GitHub API into a variable of that type so that only the keys you need are kept, then write that variable to the response.
For example:
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
type RelevantRepoData struct {
Id int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Language string `json:"language"`
Description string `json:"description"`
}
func GetAllReposStars(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
params := mux.Vars(r)
username := params["username"]
res, err := http.Get("https://api.github.com/users/" + username + "/starred")
var repoData RelevantRepoData
err = json.NewDecoder(res.Body).Decode(&repoData)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(repoData)
}
If you want to send some specific fields to front-end but in future want to handle them on backend, you can use json:"-" in your struct
e.g :
type RelevantRepoData struct {
Irrelevant string `json:"-"`
Id int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Language string `json:"language"`
Description string `json:"description"`
}
When you return this object, the fields with json:"-" will be ignored but you can still use them on backend.

How to marshal an ECC public or private key

Hello i'am trying to unmarshal object that contain ECC public key
but i go umarshaling error saying that can't unmarshal object properly.
i'am tring to do the followeing :
var privateKey *ecdsa.PrivateKey
var publicKey ecdsa.PublicKey
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("Failed to generate ECDSA key: %s\n", err)
}
publicKey = privateKey.PublicKey
marshalledKey, err := json.Marshal(publicKey)
if err != nil {
panic(err)
}
var unmarshalledKey ecdsa.PublicKey
err2 := json.Unmarshal(marshalledKey, &unmarshalledKey)
if err2 != nil {
panic(err2)
}
and tthe error returned from (err2) is :
panic: json: cannot unmarshal object into Go struct field PublicKey.Curve of type elliptic.Curve
and there is no way in either eliptoc or 509x function to umarshal and the curve value is alwayes null
i found the answer finally and i will post it in case somone need it.
all we need is just to creat another struct, lets name it "retrieve" and declare the value it wil read from the unmarshal.
now the ECC public key contain 3 values "curve" and will append it as json:"Curve", "Y" ans will append it as json:"Y" and we will do the same for "X"
and it will work like this :
type retrieve struct {
CurveParams *elliptic.CurveParams `json:"Curve"`
MyX *big.Int `json:"X"`
MyY *big.Int `json:"Y"`
}
//UnmarshalECCPublicKey extract ECC public key from marshaled objects
func UnmarshalECCPublicKey(object []byte) (pub ecdsa.PublicKey) {
var public ecdsa.PublicKey
rt := new(retrieve)
errmarsh := json.Unmarshal(object, &rt)
if errmarsh != nil {
fmt.Println("err at UnmarshalECCPublicKey()")
panic(errmarsh)
}
public.Curve = rt.Key.CurveParams
public.X = rt.Key.MyX
public.Y = rt.Key.MyY
mapstructure.Decode(public, &pub)
fmt.Println("Unmarshalled ECC public key : ", pub)
return
}
we will pass the the marshalled public key to UnmarshalECCPublicKey() and it will return the correct unmarshaled obj
so the coorect use will be :
var privateKey *ecdsa.PrivateKey
var publicKey ecdsa.PublicKey
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("Failed to generate ECDSA key: %s\n", err)
}
publicKey = privateKey.PublicKey
marshalledKey, err := json.Marshal(publicKey)
if err != nil {
panic(err)
}
var unmarshalledKey ecdsa.PublicKey
unmarshalledKey = UnmarshalECCPublicKey(marshalledKey)
and that should do it, u will get the ECC public unmarshalled successfully.

How to validate a signature signed with PKCS8 private key?

I'm developing a program with Go language for validating signatures.
It receives parameters(public key, signature, plaintext)
and the signature is signed by user's private key in PKCS#8.
I tried to use the function VerifyPKCS1v15 in the package x509
but it didn't work for me.
I guess it may due to the function is oriented for signatures made by pkcs1 private keys.
can anybody help me?
I wanna know if there is any way to validate signatures using pkcs8 public key?
this is my code
func main() {
var plainTest = "P0000000025300000100000000001000026720180705140842"
var hashVal = "15b47c1d79b0be2aae36a05bcd8644af7bfe3dd4e0c23e2b78692fc900998fca"
var signatureStr = "WWFCZsD3BhakkCaLAcTPxMvd3Pom1Glhgcc+xhR7tIDBLvkVk/LtxV+2nHw6b9u0Dcla8U4vUR7KH8zpUS7fNJD9yPDDWxH5PYiw4jQTjziiLHSUpuaGbf8N1Y2jKPXvzq1ZFaEAqCirLSmt5KyD3gQ22ysHgYA2vH44zzBApcxYXVbzLbCIGAR5aL/mvYt7uWsh4FX8dQ49v9SqIm/rRBGEbsscF4HpQApy8VqRGvq6EbwrPCfMcpwIbBHdDUR0mneaNg9GH4hozfMC08SZtAMGDk8J/NQway1FisrjpUeZfMe/hANDH1LmfrbThKDgB7WIpDryCXMTsBKjrqyArQ=="
var pubKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA17hWIujBfiqrd4o0JCEn6N1mzv94VM9LiVHoldvPRDEwCXbeoSebzZElvhkJsPl08o68g1BgRC4LpaGQDcVzwyFMs4DnXEDLapZQpTzkmXaSIYIRYER/U1OgdW5Cq2do/eTrylWdloqWuz5JL2vIr4GFycnEduYVSzFmAqucCvgGEFUxwFxtZ95BVsxfKOt7eFCJWoS0iR2/If5EMG9F6KG6DtDUWg6awN2mIbhm8fqxSF48ehCkPCN4s4YkcUlkmGYEetdBCxbaUh9/S960XjQBK3MXbLIJLgRLoEAdWJ2v6IjaEsw7dQAaMti3QOPr0x7TyHlS7rz/lyjlJjaXEQIDAQAB"
publicKeyBase64, err := base64.StdEncoding.DecodeString(pubKeyStr)
if err != nil {
fmt.Println("base64 error : " + err.Error())
}
fmt.Println("publicKeyBase64: ")
fmt.Println(string(publicKeyBase64))
pub, err := x509.ParsePKIXPublicKey(publicKeyBase64)
if err != nil {
fmt.Println("failed to parse DER encoded public key: " + err.Error())
}
switch pub := pub.(type) {
case *rsa.PublicKey:
fmt.Println("pub is of type RSA:", pub)
case *dsa.PublicKey:
fmt.Println("pub is of type DSA:", pub)
case *ecdsa.PublicKey:
fmt.Println("pub is of type ECDSA:", pub)
default:
panic("unknown type of public key")
}
publicKey, isRSAPublicKey := pub.(*rsa.PublicKey)
if !isRSAPublicKey {
fmt.Println("Public key parsed is not an RSA public key")
}
signatureBytes, _ := hex.DecodeString(signatureStr)
fmt.Println("signatureBytes : " + string(signatureBytes))
validateBytes := sha256.Sum256([]byte(plainTest))
fmt.Println("validateBytes : " + string(validateBytes[:]))
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, validateBytes[:], signatureBytes)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Printf("ok")
}
}
I got the same problem, it turns out my publicKey is wrong.
I mean not the format, but the key is not paired with the private key.
I change to another pair of key, and the code worked.