From 8cddfde837eb85e384cfd440eaa66cdf23d9ed37 Mon Sep 17 00:00:00 2001 From: Tanguy Cizain Date: Thu, 2 Sep 2021 12:03:40 +0200 Subject: [PATCH] Rename getKey -> getPublicKey (#621) * rename getKey to getPublicKey * use publicKey directly in gossipsub * update error messages --- libp2p/crypto/crypto.nim | 13 ++++++++----- libp2p/crypto/ecnist.nim | 6 +++--- libp2p/crypto/ed25519/ed25519.nim | 2 +- libp2p/crypto/rsa.nim | 4 ++-- libp2p/crypto/secp.nim | 4 ++-- libp2p/peerid.nim | 2 +- libp2p/peerinfo.nim | 2 +- libp2p/protocols/pubsub/rpc/message.nim | 7 ++----- libp2p/protocols/secure/noise.nim | 6 ++---- libp2p/protocols/secure/secio.nim | 8 +++----- tests/testcrypto.nim | 8 ++++---- tests/testecnist.nim | 24 ++++++++++++------------ tests/tested25519.nim | 2 +- tests/testidentify.nim | 4 ++-- tests/testpeerid.nim | 2 +- tests/testpeerinfo.nim | 14 +++++++------- tests/testrsa.nim | 6 +++--- 17 files changed, 55 insertions(+), 59 deletions(-) diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index c7705adb3..7a1c4fbd1 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -303,34 +303,37 @@ proc random*(T: typedesc[KeyPair], rng: var BrHmacDrbgContext, else: err(SchemeError) -proc getKey*(key: PrivateKey): CryptoResult[PublicKey] = +proc getPublicKey*(key: PrivateKey): CryptoResult[PublicKey] = ## Get public key from corresponding private key ``key``. case key.scheme of PKScheme.RSA: when supported(PKScheme.RSA): - let rsakey = key.rsakey.getKey() + let rsakey = key.rsakey.getPublicKey() ok(PublicKey(scheme: RSA, rsakey: rsakey)) else: err(SchemeError) of PKScheme.Ed25519: when supported(PKScheme.Ed25519): - let edkey = key.edkey.getKey() + let edkey = key.edkey.getPublicKey() ok(PublicKey(scheme: Ed25519, edkey: edkey)) else: err(SchemeError) of PKScheme.ECDSA: when supported(PKScheme.ECDSA): - let eckey = ? key.eckey.getKey().orError(KeyError) + let eckey = ? key.eckey.getPublicKey().orError(KeyError) ok(PublicKey(scheme: ECDSA, eckey: eckey)) else: err(SchemeError) of PKScheme.Secp256k1: when supported(PKScheme.Secp256k1): - let skkey = key.skkey.getKey() + let skkey = key.skkey.getPublicKey() ok(PublicKey(scheme: Secp256k1, skkey: skkey)) else: err(SchemeError) +proc getKey*(key: PrivateKey): CryptoResult[PublicKey] {.deprecated: "use getPublicKey".} = + key.getPublicKey() + proc toRawBytes*(key: PrivateKey | PublicKey, data: var openarray[byte]): CryptoResult[int] = ## Serialize private key ``key`` (using scheme's own serialization) and store diff --git a/libp2p/crypto/ecnist.nim b/libp2p/crypto/ecnist.nim index c3a43387f..f0cf258aa 100644 --- a/libp2p/crypto/ecnist.nim +++ b/libp2p/crypto/ecnist.nim @@ -245,7 +245,7 @@ proc random*( else: ok(res) -proc getKey*(seckey: EcPrivateKey): EcResult[EcPublicKey] = +proc getPublicKey*(seckey: EcPrivateKey): EcResult[EcPublicKey] = ## Calculate and return EC public key from private key ``seckey``. if isNil(seckey): return err(EcKeyIncorrectError) @@ -272,7 +272,7 @@ proc random*( ## secp521r1). let seckey = ? EcPrivateKey.random(kind, rng) - pubkey = ? seckey.getKey() + pubkey = ? seckey.getPublicKey() key = EcKeyPair(seckey: seckey, pubkey: pubkey) ok(key) @@ -368,7 +368,7 @@ proc toBytes*(seckey: EcPrivateKey, data: var openarray[byte]): EcResult[int] = return err(EcKeyIncorrectError) if seckey.key.curve in EcSupportedCurvesCint: var offset, length: int - var pubkey = ? seckey.getKey() + var pubkey = ? seckey.getPublicKey() var b = Asn1Buffer.init() var p = Asn1Composite.init(Asn1Tag.Sequence) var c0 = Asn1Composite.init(0) diff --git a/libp2p/crypto/ed25519/ed25519.nim b/libp2p/crypto/ed25519/ed25519.nim index 663cee97f..bef26b730 100644 --- a/libp2p/crypto/ed25519/ed25519.nim +++ b/libp2p/crypto/ed25519/ed25519.nim @@ -1682,7 +1682,7 @@ proc random*(t: typedesc[EdKeyPair], rng: var BrHmacDrbgContext): EdKeyPair = res -proc getKey*(key: EdPrivateKey): EdPublicKey = +proc getPublicKey*(key: EdPrivateKey): EdPublicKey = ## Calculate and return ED25519 public key from private key ``key``. copyMem(addr result.data[0], unsafeAddr key.data[32], 32) diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index 6eee3c520..8838cf184 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -225,7 +225,7 @@ proc copy*[T: RsaPKI](key: T): T = result = new RsaSignature result.buffer = key.buffer -proc getKey*(key: RsaPrivateKey): RsaPublicKey = +proc getPublicKey*(key: RsaPrivateKey): RsaPublicKey = ## Get RSA public key from RSA private key. doAssert(not isNil(key)) let length = key.pubk.nlen + key.pubk.elen @@ -245,7 +245,7 @@ proc seckey*(pair: RsaKeyPair): RsaPrivateKey {.inline.} = proc pubkey*(pair: RsaKeyPair): RsaPublicKey {.inline.} = ## Get RSA public key from pair ``pair``. - result = cast[RsaPrivateKey](pair).getKey() + result = cast[RsaPrivateKey](pair).getPublicKey() proc clear*[T: RsaPKI|RsaKeyPair](pki: var T) = ## Wipe and clear EC private key, public key or scalar object. diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index 70f8e9b45..b69661d46 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -141,7 +141,7 @@ proc init*(t: typedesc[SkSignature], data: string): SkResult[SkSignature] = var sig: SkSignature sig.init(data) and ok(sig) -proc getKey*(key: SkPrivateKey): SkPublicKey = +proc getPublicKey*(key: SkPrivateKey): SkPublicKey = ## Calculate and return Secp256k1 `public key` from `private key` ``key``. SkPublicKey(SkSecretKey(key).toPublicKey()) @@ -211,4 +211,4 @@ proc `$`*(key: SkKeyPair): string {.borrow.} proc `==`*(a, b: SkPrivateKey): bool {.borrow.} proc `==`*(a, b: SkPublicKey): bool {.borrow.} proc `==`*(a, b: SkSignature): bool {.borrow.} -proc `==`*(a, b: SkKeyPair): bool {.borrow.} \ No newline at end of file +proc `==`*(a, b: SkKeyPair): bool {.borrow.} diff --git a/libp2p/peerid.nim b/libp2p/peerid.nim index bffc4c4f4..b719d1ed2 100644 --- a/libp2p/peerid.nim +++ b/libp2p/peerid.nim @@ -172,7 +172,7 @@ func init*(t: typedesc[PeerID], pubkey: PublicKey): Result[PeerID, cstring] = func init*(t: typedesc[PeerID], seckey: PrivateKey): Result[PeerID, cstring] = ## Create new peer id from private key ``seckey``. - PeerID.init(? seckey.getKey().orError(cstring("invalid private key"))) + PeerID.init(? seckey.getPublicKey().orError(cstring("invalid private key"))) func match*(pid: PeerID, pubkey: PublicKey): bool = ## Returns ``true`` if ``pid`` matches public key ``pubkey``. diff --git a/libp2p/peerinfo.nim b/libp2p/peerinfo.nim index fcf0dcf0b..14454bceb 100644 --- a/libp2p/peerinfo.nim +++ b/libp2p/peerinfo.nim @@ -143,7 +143,7 @@ proc publicKey*(p: PeerInfo): Option[PublicKey] = elif p.key.isSome: res = p.key else: - let pkeyRes = p.privateKey.getKey() + let pkeyRes = p.privateKey.getPublicKey() if pkeyRes.isOk: res = some(pkeyRes.get()) diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index d99653584..9ae5063e5 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -81,11 +81,8 @@ proc init*( raise (ref LPError)(msg: "Cannot sign message without private key") msg.signature = sign(msg, peer.privateKey).expect("Couldn't sign message!") - msg.key = peer.privateKey - .getKey() - .expect("Expected a Private Key!") - .getBytes() - .expect("Couldn't get Private Key bytes!") + msg.key = peer.privateKey.getPublicKey().expect("Invalid private key!") + .getBytes().expect("Couldn't get public key bytes!") elif sign: raise (ref LPError)(msg: "Cannot sign message without peer info") diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index c475efe48..72b7b07c9 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -602,11 +602,9 @@ proc new*( outgoing: bool = true, commonPrologue: seq[byte] = @[]): T = - let pkBytes = privateKey - .getKey() + let pkBytes = privateKey.getPublicKey() .expect("Expected valid Private Key") - .getBytes() - .expect("Couldn't get Private Key bytes") + .getBytes().expect("Couldn't get public Key bytes") var noise = Noise( rng: rng, diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index 1778b63d3..2ea77b6c8 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -435,16 +435,14 @@ proc new*( T: typedesc[Secio], rng: ref BrHmacDrbgContext, localPrivateKey: PrivateKey): T = - let pkRes = localPrivateKey.getKey() + let pkRes = localPrivateKey.getPublicKey() if pkRes.isErr: - raise newException(Defect, "Can't fetch local private key") + raise newException(Defect, "Invalid private key") let secio = Secio( rng: rng, localPrivateKey: localPrivateKey, - localPublicKey: localPrivateKey - .getKey() - .expect("Can't fetch local private key"), + localPublicKey: pkRes.get(), ) secio.init() secio diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index 9c47c0ca3..e7198470e 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -372,7 +372,7 @@ suite "Key interface test suite": for i in 0..