Skip to content
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let package = Package(
.package(url: "https://github.com/swift-libp2p/swift-peer-id.git", .upToNextMinor(from: "0.1.0")),

// LibP2P Multiaddr
.package(url: "https://github.com/swift-libp2p/swift-multiaddr.git", .upToNextMinor(from: "0.0.1")),
.package(url: "https://github.com/swift-libp2p/swift-multiaddr.git", .upToNextMinor(from: "0.1.0")),

// Logging
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),
Expand Down
6 changes: 1 addition & 5 deletions Sources/LibP2PCore/Network/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ extension Connection {
}

public var expectedRemotePeer: PeerID? {
//print("LibP2PCore:Connection:ERROR: FIXME!!!")
if let cid = self.remoteAddr?.getPeerID() {
return try? PeerID(cid: cid)
}
return nil
try? self.remoteAddr?.getPeerID()
}
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/LibP2PCore/Peer/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import PeerID
// var addr:[Multiaddr] { get }
//}

/// A peer (PeerID) and their known addresses (Multiaddr)
public struct PeerInfo {
public let peer: PeerID
public let addresses: [Multiaddr]
Expand All @@ -45,9 +46,10 @@ public struct PeerInfo {
}

extension Multiaddr {
// TODO: Rename this to getPeerID once https://github.com/swift-libp2p/swift-multiaddr/issues/14 is addressed
func getPeerIDActual() throws -> PeerID {
guard let cid = self.getPeerID() else {
/// Attempts to extract a PeerID from the Multiaddr if one is present
/// - Note: The returned PeerID is usually only an ID and doesn't contain a key pair. In some instances (ED25519 keys) a public key might be recoverable.
public func getPeerID() throws -> PeerID {
guard let cid = self.getPeerIDString() else {
throw NSError(domain: "No CID present in Multiaddr", code: 0)
}
return try PeerID(cid: cid)
Expand Down
16 changes: 8 additions & 8 deletions Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,46 @@ final class MultiaddrPeerIDTests: XCTestCase {
func testGetPeerID() throws {
// B58 String
let ma1 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
let peerID1 = try ma1.getPeerIDActual()
let peerID1 = try ma1.getPeerID()

// B58 String
let ma2 = try Multiaddr("/ip4/139.178.91.71/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
let peerID2 = try ma2.getPeerIDActual()
let peerID2 = try ma2.getPeerID()

// CID String
let ma3 = try Multiaddr(
"/dnsaddr/bootstrap.libp2p.io/p2p/bafzbeiagwnqiviaae5aet2zivwhhsorg75x2wka2pu55o7grr23ulx5kxm"
)
let peerID3 = try ma3.getPeerIDActual()
let peerID3 = try ma3.getPeerID()

XCTAssertEqual(peerID1, peerID2)
XCTAssertEqual(peerID1, peerID3)

// Embedded Public Key
let ma4 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d")
let peerID4 = try ma4.getPeerIDActual()
let peerID4 = try ma4.getPeerID()

XCTAssertEqual(peerID4.type, .isPublic)

// Throw when no PeerID is present
XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerIDActual())
XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerID())
}

func testGetPeerIDEmbeddedEd25519PublicKey() throws {
let ma1 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d")

let embeddedKeyInBytes = try BaseEncoding.decode(ma1.getPeerID()!, as: .base58btc)
let embeddedKeyInBytes = try BaseEncoding.decode(ma1.getPeerIDString()!, as: .base58btc)
let peerID1 = try PeerID(fromBytesID: embeddedKeyInBytes.data.bytes)

let ma2 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d")
let peerID2 = try ma2.getPeerIDActual()
let peerID2 = try ma2.getPeerID()

XCTAssertEqual(peerID1, peerID2)
XCTAssertEqual(peerID1.type, .isPublic)
XCTAssertEqual(peerID2.type, .isPublic)

let ma3 = try Multiaddr("/ip4/139.178.91.71/tcp/4001/p2p/QmPoHmYtUt8BU9eiwMYdBfT6rooBnna5fdAZHUaZASGQY8")
let peerID3 = try ma3.getPeerIDActual()
let peerID3 = try ma3.getPeerID()

XCTAssertEqual(peerID3.type, .idOnly)

Expand Down
Loading