Skip to content

Commit

Permalink
[network] Rename functions in keyTranslator for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Aug 16, 2021
1 parent 6ff89f5 commit 7305d0f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions network/p2p/keyTranslator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func setPubKey(c elliptic.Curve, x *big.Int, y *big.Int) *goecdsa.PublicKey {
// These utility functions convert a Flow crypto key to a LibP2P key (Flow --> LibP2P)

// PrivKey converts a Flow private key to a LibP2P Private key
func PrivKey(fpk fcrypto.PrivateKey) (lcrypto.PrivKey, error) {
func LibP2PPrivKeyFromFlow(fpk fcrypto.PrivateKey) (lcrypto.PrivKey, error) {
// get the signature algorithm
keyType, err := keyType(fpk.Algorithm())
if err != nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ func PrivKey(fpk fcrypto.PrivateKey) (lcrypto.PrivKey, error) {
}

// PublicKey converts a Flow public key to a LibP2P public key
func PublicKey(fpk fcrypto.PublicKey) (lcrypto.PubKey, error) {
func LibP2PPublicKeyFromFlow(fpk fcrypto.PublicKey) (lcrypto.PubKey, error) {
keyType, err := keyType(fpk.Algorithm())
if err != nil {
return nil, err
Expand Down Expand Up @@ -109,7 +109,7 @@ func PublicKey(fpk fcrypto.PublicKey) (lcrypto.PubKey, error) {
// This converts some libp2p PubKeys to a flow PublicKey
// - the supported key types are ECDSA P-256 and ECDSA Secp256k1 public keys,
// - libp2p also supports RSA and Ed25519 keys, which Flow doesn't, their conversion will return an error.
func PublicKeyFromNetwork(lpk lcrypto.PubKey) (fcrypto.PublicKey, error) {
func FlowPublicKeyFromLibP2P(lpk lcrypto.PubKey) (fcrypto.PublicKey, error) {

switch ktype := lpk.Type(); ktype {
case lcrypto_pb.KeyType_ECDSA:
Expand Down
10 changes: 5 additions & 5 deletions network/p2p/keyTranslator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k *KeyTranslatorTestSuite) TestPrivateKeyConversion() {
require.NoError(k.T(), err)

// convert it to a LibP2P private key
lpk, err := PrivKey(fpk)
lpk, err := LibP2PPrivKeyFromFlow(fpk)
require.NoError(k.T(), err)

// get the raw bytes of both the keys
Expand Down Expand Up @@ -91,7 +91,7 @@ func (k *KeyTranslatorTestSuite) TestPublicKeyConversion() {
fpublic := fpk.PublicKey()

// convert the Flow public key to a Libp2p public key
lpublic, err := PublicKey(fpublic)
lpublic, err := LibP2PPublicKeyFromFlow(fpublic)
require.NoError(k.T(), err)

// compare raw bytes of the public keys
Expand Down Expand Up @@ -125,10 +125,10 @@ func (k *KeyTranslatorTestSuite) TestPublicKeyRoundTrip() {
fpublic := fpk.PublicKey()

// convert the Flow public key to a Libp2p public key
lpublic, err := PublicKey(fpublic)
lpublic, err := LibP2PPublicKeyFromFlow(fpublic)
require.NoError(k.T(), err)

fpublic2, err := PublicKeyFromNetwork(lpublic)
fpublic2, err := FlowPublicKeyFromLibP2P(lpublic)
require.NoError(k.T(), err)
require.Equal(k.T(), fpublic, fpublic2)

Expand All @@ -150,7 +150,7 @@ func (k *KeyTranslatorTestSuite) TestPeerIDGenerationIsConsistent() {
fpublic := fpk.PublicKey()

// convert it to the Libp2p Public key
lconverted, err := PublicKey(fpublic)
lconverted, err := LibP2PPublicKeyFromFlow(fpublic)
require.NoError(k.T(), err)

// check that the LibP2P Id generation is deterministic
Expand Down
2 changes: 1 addition & 1 deletion network/p2p/libp2pNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func DefaultLibP2PHost(ctx context.Context, address string, key fcrypto.PrivateK
// DefaultLibP2POptions creates and returns the standard LibP2P host options that are used for the Flow Libp2p network
func DefaultLibP2POptions(address string, key fcrypto.PrivateKey) ([]config.Option, error) {

libp2pKey, err := PrivKey(key)
libp2pKey, err := LibP2PPrivKeyFromFlow(key)
if err != nil {
return nil, fmt.Errorf("could not generate libp2p key: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion network/p2p/libp2pUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func networkingInfo(identity flow.Identity) (string, string, crypto.PubKey, erro
}

// convert the Flow key to a LibP2P key
lkey, err := PublicKey(identity.NetworkPubKey)
lkey, err := LibP2PPublicKeyFromFlow(identity.NetworkPubKey)
if err != nil {
return "", "", nil, fmt.Errorf("could not convert flow key to libp2p key: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion network/p2p/libp2pUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func idsAndPeerInfos(t *testing.T) (flow.IdentityList, []peer.AddrInfo) {
ids[i] = id

// create a libp2p PeerAddressInfo
libp2pKey, err := PublicKey(id.NetworkPubKey)
libp2pKey, err := LibP2PPublicKeyFromFlow(id.NetworkPubKey)
assert.NoError(t, err)
peerID, err := peer.IDFromPublicKey(libp2pKey)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions utils/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DefaultMaxMsgSize = 1024 * 1024 * 16
func X509Certificate(privKey crypto.PrivateKey) (*tls.Certificate, error) {

// convert the Flow crypto private key to a Libp2p private crypto key
libP2PKey, err := p2p.PrivKey(privKey)
libP2PKey, err := p2p.LibP2PPrivKeyFromFlow(privKey)
if err != nil {
return nil, fmt.Errorf("could not convert Flow key to libp2p key: %w", err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func DefaultClientTLSConfig(publicKey crypto.PublicKey) (*tls.Config, error) {
func verifyPeerCertificateFunc(expectedPublicKey crypto.PublicKey) (func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error, error) {

// convert the Flow.crypto key to LibP2P key for easy comparision using LibP2P TLS utils
expectedLibP2PKey, err := p2p.PublicKey(expectedPublicKey)
expectedLibP2PKey, err := p2p.LibP2PPublicKeyFromFlow(expectedPublicKey)
if err != nil {
return nil, fmt.Errorf("failed to generate a libp2p key from a Flow key: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion utils/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCertificateGeneration(t *testing.T) {
require.NoError(t, err)

// convert the test key to a libp2p key for easy comparision
libp2pKey, err := p2p.PrivKey(key)
libp2pKey, err := p2p.LibP2PPrivKeyFromFlow(key)
expectedKey := libp2pKey.GetPublic()
require.NoError(t, err)

Expand Down

0 comments on commit 7305d0f

Please sign in to comment.