Skip to content

Commit 462eddc

Browse files
authored
Merge pull request #4 from swift-libp2p/multiaddr-update
Multiaddr Update
2 parents 1dafdd7 + aef5689 commit 462eddc

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let package = Package(
3838
.package(url: "https://github.com/swift-libp2p/swift-peer-id.git", .upToNextMinor(from: "0.1.0")),
3939

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

4343
// Logging
4444
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),

Sources/LibP2PCore/Network/Connection.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ extension Connection {
139139
}
140140

141141
public var expectedRemotePeer: PeerID? {
142-
//print("LibP2PCore:Connection:ERROR: FIXME!!!")
143-
if let cid = self.remoteAddr?.getPeerID() {
144-
return try? PeerID(cid: cid)
145-
}
146-
return nil
142+
try? self.remoteAddr?.getPeerID()
147143
}
148144
}
149145

Sources/LibP2PCore/Peer/Peer.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import PeerID
3434
// var addr:[Multiaddr] { get }
3535
//}
3636

37+
/// A peer (PeerID) and their known addresses (Multiaddr)
3738
public struct PeerInfo {
3839
public let peer: PeerID
3940
public let addresses: [Multiaddr]
@@ -45,9 +46,10 @@ public struct PeerInfo {
4546
}
4647

4748
extension Multiaddr {
48-
// TODO: Rename this to getPeerID once https://github.com/swift-libp2p/swift-multiaddr/issues/14 is addressed
49-
func getPeerIDActual() throws -> PeerID {
50-
guard let cid = self.getPeerID() else {
49+
/// Attempts to extract a PeerID from the Multiaddr if one is present
50+
/// - 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.
51+
public func getPeerID() throws -> PeerID {
52+
guard let cid = self.getPeerIDString() else {
5153
throw NSError(domain: "No CID present in Multiaddr", code: 0)
5254
}
5355
return try PeerID(cid: cid)

Tests/LibP2PCoreTests/MultiaddrPeerIDTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,46 @@ final class MultiaddrPeerIDTests: XCTestCase {
2424
func testGetPeerID() throws {
2525
// B58 String
2626
let ma1 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
27-
let peerID1 = try ma1.getPeerIDActual()
27+
let peerID1 = try ma1.getPeerID()
2828

2929
// B58 String
3030
let ma2 = try Multiaddr("/ip4/139.178.91.71/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
31-
let peerID2 = try ma2.getPeerIDActual()
31+
let peerID2 = try ma2.getPeerID()
3232

3333
// CID String
3434
let ma3 = try Multiaddr(
3535
"/dnsaddr/bootstrap.libp2p.io/p2p/bafzbeiagwnqiviaae5aet2zivwhhsorg75x2wka2pu55o7grr23ulx5kxm"
3636
)
37-
let peerID3 = try ma3.getPeerIDActual()
37+
let peerID3 = try ma3.getPeerID()
3838

3939
XCTAssertEqual(peerID1, peerID2)
4040
XCTAssertEqual(peerID1, peerID3)
4141

4242
// Embedded Public Key
4343
let ma4 = try Multiaddr("/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWAfPDpPRRRBrmqy9is2zjU5srQ4hKuZitiGmh4NTTpS2d")
44-
let peerID4 = try ma4.getPeerIDActual()
44+
let peerID4 = try ma4.getPeerID()
4545

4646
XCTAssertEqual(peerID4.type, .isPublic)
4747

4848
// Throw when no PeerID is present
49-
XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerIDActual())
49+
XCTAssertThrowsError(try Multiaddr("/dnsaddr/bootstrap.libp2p.io/").getPeerID())
5050
}
5151

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

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

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

6161
XCTAssertEqual(peerID1, peerID2)
6262
XCTAssertEqual(peerID1.type, .isPublic)
6363
XCTAssertEqual(peerID2.type, .isPublic)
6464

6565
let ma3 = try Multiaddr("/ip4/139.178.91.71/tcp/4001/p2p/QmPoHmYtUt8BU9eiwMYdBfT6rooBnna5fdAZHUaZASGQY8")
66-
let peerID3 = try ma3.getPeerIDActual()
66+
let peerID3 = try ma3.getPeerID()
6767

6868
XCTAssertEqual(peerID3.type, .idOnly)
6969

0 commit comments

Comments
 (0)