Home > database >  I cannot generate the ECDSA public key from her private key sequentially
I cannot generate the ECDSA public key from her private key sequentially

Time:04-08

I cannot generate the public keys in sequence.

output :./keysgo.go:33:33: cannot use PrivateKey (type []byte) as type string in argument to Public

Thank you so much for your help

important to keep this part:

func Public(PrivateKey string) (publicKey string) {
    var e ecdsa.PrivateKey
    e.D, _ = new(big.Int).SetString(PrivateKey, 16)
    e.PublicKey.Curve = secp256k1.S256()
    e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
    return fmt.Sprintf("%x", elliptic.MarshalCompressed(secp256k1.S256(), e.X, e.Y))

i tried this

package main 

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "fmt"
    "math/big"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
   
    
    
)
func Public(PrivateKey string) (publicKey string) {
    var e ecdsa.PrivateKey
    e.D, _ = new(big.Int).SetString(PrivateKey, 16)
    e.PublicKey.Curve = secp256k1.S256()
    e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
    return fmt.Sprintf("%x", elliptic.MarshalCompressed(secp256k1.S256(), e.X, e.Y))

}

func main() {
        count, one := big.NewInt(1), big.NewInt(1)
    count.SetString("9404625697166532776746648320380374280100293470930272690489102837043110636674",10)
        PrivateKey := make([]byte, 32)
   for {
        count.Add(count, one)
        copy(PrivateKey[32-len(count.Bytes()):], count.Bytes())
        
        fmt.Printf("%x\n",Public(PrivateKey))
        

    }   
    
}   

}

CodePudding user response:

You define PrivateKey as a byte slice:

PrivateKey := make([]byte, 32)

The function Public is defined to take a string as its arguments:

func Public(PrivateKey string) (publicKey string) {...}

Thus Public(PrivateKey) cannot work, because privateKey is the wrong type.

I am not quite sure what the code is attempting to do, but maybe Public(string(PrivateKey)) is what you need?

CodePudding user response:

Thank you very much for your kind reply

I tried Public(string(PrivateKey))

the program's response

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x4b47f9]

goroutine 1 [running]:
math/big.(*Int).Bytes(...)
        /usr/local/go/src/math/big/int.go:453

main.Public({0xc00001e1a0, 0x20})
        /root/Desktop/tuttogoprova/adressgoprova.go:17  0x99
main.main()
        /root/Desktop/tuttogoprova/adressgoprova.go:37  0xd3
exit status 2
  •  Tags:  
  • go
  • Related