Initial staking package with TLS certificate loading

- LoadTLSCertFromBytes for parsing PEM-encoded certs
- LoadTLSCertFromFiles for loading from disk
This commit is contained in:
Zach Kelling
2025-12-24 14:18:02 -08:00
commit 90335e0b2d
2 changed files with 40 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
module github.com/luxfi/staking
go 1.25.5
+37
View File
@@ -0,0 +1,37 @@
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package staking
import (
"crypto/tls"
"crypto/x509"
"fmt"
)
// LoadTLSCertFromBytes parses a TLS certificate from PEM-encoded key and cert bytes.
func LoadTLSCertFromBytes(keyBytes, certBytes []byte) (*tls.Certificate, error) {
cert, err := tls.X509KeyPair(certBytes, keyBytes)
if err != nil {
return nil, fmt.Errorf("failed creating cert: %w", err)
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, fmt.Errorf("failed parsing cert: %w", err)
}
return &cert, nil
}
// LoadTLSCertFromFiles loads a TLS certificate from key and cert files.
func LoadTLSCertFromFiles(keyPath, certPath string) (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, fmt.Errorf("failed parsing cert: %w", err)
}
return &cert, nil
}