Add extraction of PrivateKey from pem

This commit is contained in:
Mark Beamer Jr 2020-11-18 01:30:50 -05:00
parent b3f7657c1b
commit 69e03da94a
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973

View file

@ -4,6 +4,7 @@ import (
"crypto/elliptic"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"github.com/lbryio/lbry.go/v2/extras/errors"
@ -16,6 +17,14 @@ type publicKeyInfo struct {
PublicKey asn1.BitString
}
//This type provides compatibility with the btcec package
type ecPrivateKey struct {
Version int
PrivateKey []byte
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
}
func PublicKeyToDER(publicKey *btcec.PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
@ -47,3 +56,12 @@ func GetPublicKeyFromBytes(pubKeyBytes []byte) (*btcec.PublicKey, error) {
pubkeyBytes1 := []byte(PKInfo.PublicKey.Bytes)
return btcec.ParsePubKey(pubkeyBytes1, btcec.S256())
}
//Returns a btec.Private key object if provided a correct secp256k1 encoded pem.
func ExtractKeyFromPem(pm string) (*btcec.PrivateKey, *btcec.PublicKey) {
byta := []byte(pm)
blck, _ := pem.Decode(byta)
var ecp ecPrivateKey
asn1.Unmarshal(blck.Bytes, &ecp)
return btcec.PrivKeyFromBytes(btcec.S256(), ecp.PrivateKey)
}