Skip to content

PeerID Spec Conformance #22

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

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Sources/LibP2PCrypto/Keys/KeyPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,48 @@ extension LibP2PCrypto.Keys {
public init(marshaledPrivateKey str: String, base: BaseEncoding) throws {
try self.init(marshaledPrivateKey: BaseEncoding.decode(str, as: base).data)
}

/// Instantiate a KeyPair from a marshaled private key
/// https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md
public init(marshaledPrivateKey data: Data) throws {
let proto = try PrivateKey(contiguousBytes: data)
switch proto.type {
case .rsa:
try self.init(privateKey: RSAPrivateKey(marshaledData: proto.data))

case .ed25519:
try self.init(privateKey: Curve25519.Signing.PrivateKey(marshaledData: proto.data))

switch proto.data.count {
case 32:
try self.init(privateKey: Curve25519.Signing.PrivateKey(marshaledData: proto.data))
case 64:
// [private key bytes][public key bytes]
// Ensure we can derive the attached public key
let privkey = try Curve25519.Signing.PrivateKey(marshaledData: proto.data.prefix(32))
guard privkey.publicKey.rawRepresentation == proto.data.suffix(32) else {
throw NSError(
domain: "Invalid private key protobuf encoding -> unable to validate public key",
code: 0
)
}
try self.init(privateKey: privkey)
case 96:
// [private key][public key][public key]
// Ensure the two pubkeys match and we can derive the attached public key
let parts = Array(proto.data.chunks(ofCount: 32))
guard parts[1] == parts[2] else {
throw NSError(domain: "Invalid private key protobuf encoding -> pubkeys dont match", code: 0)
}
let privkey = try Curve25519.Signing.PrivateKey(marshaledData: parts[0])
guard privkey.publicKey.rawRepresentation == parts[1] else {
throw NSError(
domain: "Invalid private key protobuf encoding -> unable to validate public key",
code: 0
)
}
try self.init(privateKey: privkey)
default:
throw NSError(domain: "Invalid private key protobuf encoding -> invalid data payload", code: 0)
}
case .secp256K1:
try self.init(privateKey: Secp256k1PrivateKey(marshaledData: proto.data))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/LibP2PCrypto/Keys/Types/Secp256k1/Secp256k1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension Secp256k1PublicKey: CommonPublicKey {
public func marshal() throws -> Data {
var publicKey = PublicKey()
publicKey.type = .secp256K1
publicKey.data = self.rawRepresentation
publicKey.data = try Data(self.compressPublicKey())
return try publicKey.serializedData()
}

Expand Down
Loading
Loading