Skip to content

Commit

Permalink
update certificate creation for ed25519, update makefile
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Anderson <[email protected]>
  • Loading branch information
dmikey committed May 6, 2024
1 parent 3ea9ce1 commit 0e6087d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
15 changes: 0 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ run-head:
--rest-api :8081
@echo "\n✅ Head Node is running!\n"


.PHONY: run-worker
run-worker:
@echo "\n🚀 Launching Worker Node...\n"
./dist/b7s --peer-db /tmp/b7s/head-peer-db \
--function-db /tmp/b7s/head-fdb \
--log-level debug \
--port 9527 \
--role head \
--workspace /tmp/debug/head \
--private-key ./configs/testkeys/ident1/priv.bin \
--rest-api :8081
@echo "\n✅ Worker Node is running!\n"


.PHONY: run-worker
run-worker:
@echo "\n🚀 Launching Worker Node...\n"
Expand Down
8 changes: 5 additions & 3 deletions host/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package host
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
Expand All @@ -27,12 +28,13 @@ func convertLibp2pPrivKeyToCryptoPrivKey(privKey libp2pcrypto.PrivKey) (crypto.P
return x509.ParsePKCS1PrivateKey(rawKey)
case libp2pcrypto.ECDSA:
return x509.ParseECPrivateKey(rawKey)
case libp2pcrypto.Ed25519:
return ed25519.PrivateKey(rawKey), nil
default:
return nil, fmt.Errorf("unsupported key type for X.509 conversion")
}
}

// Generate an X.509 certificate using a generic crypto.PrivateKey
func generateX509Certificate(privKey crypto.PrivateKey) (tls.Certificate, error) {
// Define certificate template
template := &x509.Certificate{
Expand All @@ -45,7 +47,6 @@ func generateX509Certificate(privKey crypto.PrivateKey) (tls.Certificate, error)
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
}

// Extract the public key from the private key
pubKey := publicKey(privKey)

// Create the certificate
Expand All @@ -63,13 +64,14 @@ func generateX509Certificate(privKey crypto.PrivateKey) (tls.Certificate, error)
return cert, nil
}

// Extract the public key from a generic crypto.PrivateKey
func publicKey(priv crypto.PrivateKey) crypto.PublicKey {
switch key := priv.(type) {
case *rsa.PrivateKey:
return &key.PublicKey
case *ecdsa.PrivateKey:
return &key.PublicKey
case ed25519.PrivateKey:
return key.Public().(ed25519.PublicKey)
default:
panic("unsupported key type")
}
Expand Down
4 changes: 3 additions & 1 deletion host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func New(log zerolog.Logger, address string, port uint, options ...func(*Config)

// Read private key, if provided.
var key crypto.PrivKey
var err error

if cfg.PrivateKey != "" {
key, err := readPrivateKey(cfg.PrivateKey)
key, err = readPrivateKey(cfg.PrivateKey)
if err != nil {
return nil, fmt.Errorf("could not read private key: %w", err)
}
Expand Down

0 comments on commit 0e6087d

Please sign in to comment.