Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modp2p): websocket transport with TLS #3560

Merged
merged 14 commits into from
Aug 6, 2024
28 changes: 16 additions & 12 deletions nodebuilder/p2p/tls.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package p2p

import (
cfg "crypto/tls"
"crypto/tls"
"os"
"path/filepath"

"github.com/libp2p/go-libp2p"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
Expand All @@ -11,28 +12,31 @@ import (
)

const (
cert = "/cert.pem"
key = "/key.pem"
cert = "cert.pem"
key = "key.pem"
)

var tlsPath = "TLS_PATH"

// enableWss checks whether `tlsPath` is not empty and creates a certificates
// to enable a websocket transport.
func enableWss() (libp2p.Option, bool, error) {
path := os.Getenv(tlsPath)
exist := utils.Exists(path+cert) && utils.Exists(path+key)
certPath := filepath.Join(path, cert)
keyPath := filepath.Join(path, key)

exist := utils.Exists(certPath) && utils.Exists(keyPath)
if !exist {
return libp2p.Transport(ws.New), exist, nil
}

var certificates []cfg.Certificate
if path != "" {
cert, err := cfg.LoadX509KeyPair(path+cert, path+key)
if err != nil {
return nil, false, err
}
certificates = append(certificates, cert)
var certificates []tls.Certificate
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, false, err
}
config := &cfg.Config{MinVersion: cfg.VersionTLS12, Certificates: certificates}
certificates = append(certificates, cert)
config := &tls.Config{MinVersion: tls.VersionTLS12, Certificates: certificates}

return libp2p.Transport(ws.New, ws.WithTLSConfig(config)), true, nil
}
3 changes: 2 additions & 1 deletion state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (ca *CoreAccessor) SubmitPayForBlob(
if response != nil && response.Code != 0 {
err = errors.Join(err, sdkErrors.ABCIError(response.Codespace, response.Code, response.Logs.String()))
}
fmt.Println("TX HASH ", response.TxHash)
return unsetTx(response), err
}
return nil, fmt.Errorf("failed to submit blobs after %d attempts: %w", maxRetries, lastErr)
Expand Down Expand Up @@ -594,7 +595,7 @@ func (ca *CoreAccessor) setupTxClient(ctx context.Context, keyName string) (*use
}
ca.defaultSignerAddress = addr
return user.SetupTxClient(ctx, ca.keyring, ca.coreConn, encCfg,
user.WithDefaultAccount(keyName), user.WithDefaultAddress(addr),
user.WithDefaultAccount(keyName),
)
}

Expand Down
Loading