From 69e03da94ad5a4a8fdc3022e7808b0155d3c826a Mon Sep 17 00:00:00 2001 From: Mark Beamer Jr Date: Wed, 18 Nov 2020 01:30:50 -0500 Subject: [PATCH] Add extraction of PrivateKey from pem --- schema/keys/keys.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/schema/keys/keys.go b/schema/keys/keys.go index c9cdc67..c742d46 100644 --- a/schema/keys/keys.go +++ b/schema/keys/keys.go @@ -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) +}