Giter Site home page Giter Site logo

Comments (1)

davecgh avatar davecgh commented on May 27, 2024

I'm not familiar with the Cosmos SDK, but looking at the github repo, I do see it listed as a direct dependency in their go.mod, so it appears that it does use it.

For your question, it depends on what format the private key string is in. Assuming it's just the hex-encoded bytes of the private key and is already known to be good, you'd just do the typical hex.DecodeString to decode the string into raw bytes and then call secp256k1.PrivKeyFromBytes to convert it to a private key that you can then convert to a public key via the PubKey method it exposes. From there, you have the public key and can serialize it if you want or otherwise use it however your want.

There is an example in the secp256k1/ecdsa package of doing decoding a hard-coded private key string, using it to sign a message, getting the public key for it, and then verifying the signature+message with the public key:

https://pkg.go.dev/github.com/decred/dcrd/dcrec/secp256k1/[email protected]/ecdsa#example-Sign

Here is a trivial example that does only what you asked and then prints out the serialized compressed public key:

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

func main() {
	privKeyString := "0000000000000000000000000000000000000000000000000000000000000001"
	privKeyBytes, err := hex.DecodeString(privKeyString)
	if err != nil {
		fmt.Println(err)
		return
	}
	privKey := secp256k1.PrivKeyFromBytes(privKeyBytes)
	pubKey := privKey.PubKey()
	fmt.Printf("Serialized Public Key: %x\n", pubKey.SerializeCompressed())
}

Output:

Serialized Public Key: 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798

from dcrd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.