From 8f552d667a98866d3f35df2d4c063c05760c2078 Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:34:14 -0800 Subject: [PATCH 1/6] Added PeerID and Multiaddr interop tests --- .../MultiaddrPeerIDTests.swift | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift diff --git a/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift b/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift new file mode 100644 index 0000000..73c69a2 --- /dev/null +++ b/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the swift-libp2p open source project +// +// Copyright (c) 2022-2025 swift-libp2p project authors +// Licensed under MIT +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of swift-libp2p project authors +// +// SPDX-License-Identifier: MIT +// +//===----------------------------------------------------------------------===// + +import XCTest + +@testable import LibP2PCore + +// These Multiaddr <-> PeerID tests are located here in swift-libp2p-core because +// this is the first package in our stack that depends on the two of them +final class MultiaddrPeerIDTests: XCTestCase { + + // Make sure we can extract a PeerID from a Multiaddr + func testGetPeerID() throws { + // B58 String + let ma1 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN") + let peerID1 = try ma1.getPeerIDActual() + + // B58 String + let ma2 = try Multiaddr("/ip4/139.178.91.71/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN") + let peerID2 = try ma2.getPeerIDActual() + + // CID String + let ma3 = try Multiaddr( + "/dnsaddr/bootstrap.libp2p.io/p2p/bafzbeiagwnqiviaae5aet2zivwhhsorg75x2wka2pu55o7grr23ulx5kxm" + ) + let peerID3 = try ma3.getPeerIDActual() + + XCTAssertEqual(peerID1, peerID2) + XCTAssertEqual(peerID1, peerID3) + + // Embedded Public Key + let ma4 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d") + let peerID4 = try ma4.getPeerIDActual() + + XCTAssertEqual(peerID4.type, .isPublic) + + // Throw when no PeerID is present + XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerIDActual()) + } + + func testGetPeerIDEmbeddedEd25519PublicKey() throws { + let ma1 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d") + + let embeddedKeyInBytes = try BaseEncoding.decode(ma1.getPeerID()!, 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() + + 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() + + XCTAssertEqual(peerID3.type, .idOnly) + + XCTAssertEqual(peerID1, peerID3) + } +} From 7c0c43c887c665eeae8f29c9a488f2953ecbc64d Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:35:37 -0800 Subject: [PATCH 2/6] Removed extractPublicKey now that PeerID supports embedded keys and implemented a temporary method for testing that will be reanamed and made public later --- Sources/LibP2PCore/Peer/Peer.swift | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/Sources/LibP2PCore/Peer/Peer.swift b/Sources/LibP2PCore/Peer/Peer.swift index 4be0d9d..bd0a484 100644 --- a/Sources/LibP2PCore/Peer/Peer.swift +++ b/Sources/LibP2PCore/Peer/Peer.swift @@ -44,22 +44,12 @@ public struct PeerInfo { } } -extension PeerID { - func extractPublicKey() -> PeerID? { - if self.type == .isPublic || self.type == .isPrivate { return self } - switch self.keyPair?.keyType { - case .ed25519: - return try? PeerID(cid: self.cidString) - default: - return nil +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 { + throw NSError(domain: "No CID present in Multiaddr", code: 0) } + return try PeerID(cid: cid) } } - -//extension Multiaddr { -// func getPeerID() -> PeerID { -// self. -// } -//} - -//extension PeerID.Key.RawPublicKey: PublicKey { } From 2cd049a68d83860d4acccc0c011f1a321639d4cf Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:36:07 -0800 Subject: [PATCH 3/6] No longer using extractPublicKey method --- Sources/LibP2PCore/Routing/Routing.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/LibP2PCore/Routing/Routing.swift b/Sources/LibP2PCore/Routing/Routing.swift index f1db9f9..26940d8 100644 --- a/Sources/LibP2PCore/Routing/Routing.swift +++ b/Sources/LibP2PCore/Routing/Routing.swift @@ -91,9 +91,9 @@ func keyForPublicKey(id: PeerID) -> String { } func getPublicKey(_ store: ValueStore, peer: PeerID, on: EventLoop) -> EventLoopFuture { - /// extractPublicKey will simply check if the Peer has a pubkey in it's keypair or if the pubkey is embedded in it's ID (in the case of Ed25519 keys) - if let pubKey = peer.extractPublicKey() { - return on.makeSucceededFuture(pubKey) + /// If the PeerID has a public key, just return it + if peer.keyPair?.publicKey != nil { + return on.makeSucceededFuture(peer) } /// If we have a DHT as our routing system, use optimized fetcher @@ -101,10 +101,10 @@ func getPublicKey(_ store: ValueStore, peer: PeerID, on: EventLoop) -> EventLoop return dht.getPublicKey(peerID: peer.cidString) } - let key = keyForPublicKey(id: peer) - + /// TODO: Implement ValueStore protocol ... return on.makeFailedFuture(RoutingErrors.notFound) - /// TODO: Figure out how to handle this... + + //let key = keyForPublicKey(id: peer) //return store.getValue(key: key).flatMapThrowing { pkval -> PublicKey in // try PublicKey(fromMarshaledValue: pkval) //} From a650a1d7f4b0652bdfdc40696f4d492a12e99e5b Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:44:11 -0800 Subject: [PATCH 4/6] Set our package versions --- Package.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 65e2be7..d6c9d0b 100644 --- a/Package.swift +++ b/Package.swift @@ -32,16 +32,16 @@ let package = Package( // Dependencies declare other packages that this package depends on. // Swift NIO for all things networking - .package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), + .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")), // LibP2P Peer Identities - .package(url: "https://github.com/swift-libp2p/swift-peer-id.git", from: "0.0.1"), + .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", from: "0.0.1"), + .package(url: "https://github.com/swift-libp2p/swift-multiaddr.git", .upToNextMinor(from: "0.0.1")), // Logging - .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")), // Swift Protobuf //.package(url: "https://github.com/apple/swift-protobuf.git", .exact("1.19.0")), From a33c7e22025921f7dab8da733af3570281294dde Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 22:14:19 -0800 Subject: [PATCH 5/6] formatted --- Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift b/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift index 73c69a2..6ec8d06 100644 --- a/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift +++ b/Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift @@ -19,7 +19,7 @@ import XCTest // These Multiaddr <-> PeerID tests are located here in swift-libp2p-core because // this is the first package in our stack that depends on the two of them final class MultiaddrPeerIDTests: XCTestCase { - + // Make sure we can extract a PeerID from a Multiaddr func testGetPeerID() throws { // B58 String @@ -35,16 +35,16 @@ final class MultiaddrPeerIDTests: XCTestCase { "/dnsaddr/bootstrap.libp2p.io/p2p/bafzbeiagwnqiviaae5aet2zivwhhsorg75x2wka2pu55o7grr23ulx5kxm" ) let peerID3 = try ma3.getPeerIDActual() - + XCTAssertEqual(peerID1, peerID2) XCTAssertEqual(peerID1, peerID3) - + // Embedded Public Key let ma4 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d") let peerID4 = try ma4.getPeerIDActual() - + XCTAssertEqual(peerID4.type, .isPublic) - + // Throw when no PeerID is present XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerIDActual()) } From f1fd89ed70e73c43bd86f123efc5034e5b555698 Mon Sep 17 00:00:00 2001 From: Brandon <32753167+btoms20@users.noreply.github.com> Date: Wed, 5 Mar 2025 22:14:44 -0800 Subject: [PATCH 6/6] formatted --- Sources/LibP2PCore/Routing/Routing.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LibP2PCore/Routing/Routing.swift b/Sources/LibP2PCore/Routing/Routing.swift index 26940d8..4767f75 100644 --- a/Sources/LibP2PCore/Routing/Routing.swift +++ b/Sources/LibP2PCore/Routing/Routing.swift @@ -103,7 +103,7 @@ func getPublicKey(_ store: ValueStore, peer: PeerID, on: EventLoop) -> EventLoop /// TODO: Implement ValueStore protocol ... return on.makeFailedFuture(RoutingErrors.notFound) - + //let key = keyForPublicKey(id: peer) //return store.getValue(key: key).flatMapThrowing { pkval -> PublicKey in // try PublicKey(fromMarshaledValue: pkval)