Skip to content

Commit 9ef52fb

Browse files
committed
Now using channel and routing message handlers. Basic ping/pong functionality.
1 parent 720fd31 commit 9ef52fb

15 files changed

+2614
-123
lines changed

FFI-Bridging-Header.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef FFI_Bridging_Header_h
1010
#define FFI_Bridging_Header_h
1111

12+
#import "rust_types.h"
1213
#import "ldk_ffi.h"
1314

1415
#endif /* FFI_Bridging_Header_h */

Swift Rust FFI.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
076BCB88240E56F4002FFD79 /* Swift_Rust_FFITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076BCB87240E56F4002FFD79 /* Swift_Rust_FFITests.swift */; };
2121
076BCB98240E5935002FFD79 /* libresolv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 076BCB97240E592E002FFD79 /* libresolv.tbd */; };
2222
07FC1B5B241A350C00C4E6D8 /* Handshake.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07FC1B5A241A350C00C4E6D8 /* Handshake.swift */; };
23+
2DD11656DBCC13B29E23D4DC /* ChannelMessageHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD119B94B1681AF880B6CDB /* ChannelMessageHandler.swift */; };
2324
2DD1176232492006BCF8AAAF /* Peer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD1191AF10E5CEBB4B8B4A1 /* Peer.swift */; };
25+
2DD1184BFB34D666CC554401 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD11FCA822BF0F8F231EA44 /* Logger.swift */; };
2426
2DD11E689A19E6FE3660C28D /* CustomPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD11A892D5A07AE47006E98 /* CustomPeer.swift */; };
2527
9CC87346C07A1FC6013EC06F /* Pods_Swift_Rust_FFI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 769F94D0AF02F1B815B4006B /* Pods_Swift_Rust_FFI.framework */; };
2628
/* End PBXBuildFile section */
@@ -58,8 +60,12 @@
5860
076BCB9B240E6047002FFD79 /* librust_swift_ffi.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = librust_swift_ffi.a; path = ../../cargo/target/universal/release/librust_swift_ffi.a; sourceTree = "<group>"; };
5961
076BCB9D24117ECE002FFD79 /* libbindings.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libbindings.a; path = "../../../rust-lightning/target/universal/release/libbindings.a"; sourceTree = "<group>"; };
6062
07FC1B5A241A350C00C4E6D8 /* Handshake.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Handshake.swift; sourceTree = "<group>"; };
63+
2DD110AA3B0A133662005BE6 /* rust_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rust_types.h; sourceTree = "<group>"; };
6164
2DD1191AF10E5CEBB4B8B4A1 /* Peer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Peer.swift; sourceTree = "<group>"; };
65+
2DD119B94B1681AF880B6CDB /* ChannelMessageHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChannelMessageHandler.swift; sourceTree = "<group>"; };
6266
2DD11A892D5A07AE47006E98 /* CustomPeer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomPeer.swift; sourceTree = "<group>"; };
67+
2DD11EBA6418725D4FEA7893 /* lightning.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lightning.h; sourceTree = "<group>"; };
68+
2DD11FCA822BF0F8F231EA44 /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
6369
769F94D0AF02F1B815B4006B /* Pods_Swift_Rust_FFI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Swift_Rust_FFI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6470
99D1A1263F0FB934A614F5D4 /* Pods-Swift Rust FFI.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Swift Rust FFI.release.xcconfig"; path = "Target Support Files/Pods-Swift Rust FFI/Pods-Swift Rust FFI.release.xcconfig"; sourceTree = "<group>"; };
6571
C0B8CA82EE150BB6FD2501CC /* Pods-Swift Rust FFI.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Swift Rust FFI.debug.xcconfig"; path = "Target Support Files/Pods-Swift Rust FFI/Pods-Swift Rust FFI.debug.xcconfig"; sourceTree = "<group>"; };
@@ -92,6 +98,8 @@
9298
0721E06524649C49001ED069 /* PeerManager.swift */,
9399
0721E063246327A9001ED069 /* RawLDKTypes.swift */,
94100
2DD1191AF10E5CEBB4B8B4A1 /* Peer.swift */,
101+
2DD11FCA822BF0F8F231EA44 /* Logger.swift */,
102+
2DD119B94B1681AF880B6CDB /* ChannelMessageHandler.swift */,
95103
);
96104
path = LDK;
97105
sourceTree = "<group>";
@@ -107,6 +115,8 @@
107115
076BCB6E240E56F0002FFD79 /* Products */,
108116
076BCB92240E5760002FFD79 /* Frameworks */,
109117
FD542169B65FC2B60F4F1487 /* Pods */,
118+
2DD11EBA6418725D4FEA7893 /* lightning.h */,
119+
2DD110AA3B0A133662005BE6 /* rust_types.h */,
110120
);
111121
sourceTree = "<group>";
112122
};
@@ -329,6 +339,8 @@
329339
076BCB75240E56F0002FFD79 /* ContentView.swift in Sources */,
330340
2DD1176232492006BCF8AAAF /* Peer.swift in Sources */,
331341
2DD11E689A19E6FE3660C28D /* CustomPeer.swift in Sources */,
342+
2DD1184BFB34D666CC554401 /* Logger.swift in Sources */,
343+
2DD11656DBCC13B29E23D4DC /* ChannelMessageHandler.swift in Sources */,
332344
);
333345
runOnlyForDeploymentPostprocessing = 0;
334346
};

Swift Rust FFI/ContentView.swift

+2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ struct ContentView: View {
3636
// self.peer.contentView = self
3737
// self.peer.createPeerManager()
3838
// self.peer.createNode();
39+
3940
Experimentation.setupPeerManager()
41+
4042
}) {
4143
Text("Test")
4244
}

Swift Rust FFI/CustomPeer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CustomPeer : Peer {
1515
self.tcpClient.connect(timeout: 5)
1616
}
1717

18+
1819
override func sendDataCallback(data: Data) -> UInt {
1920
self.tcpClient.send(data: data)
2021
print("Sent data to peer:", [UInt8](data))
@@ -36,4 +37,5 @@ class CustomPeer : Peer {
3637
self.receiveData(data: Data(response))
3738
}
3839
}
40+
3941
}

Swift Rust FFI/Experimentation.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import SwiftSocket
1212
class Experimentation {
1313

1414
public static var contentView: ContentView?
15-
15+
private static var peer: Peer?
16+
17+
1618
static func setupPeerManager() {
1719

1820
let privateKey = Data.init(base64Encoded: "ERERERERERERERERERERERERERERERERERERERERERE=")!;
1921
let ephemeralPrivateKey = Data.init(base64Encoded: "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI=")!;
2022
let remotePublicKey = Data.init(base64Encoded: "Ao11AN1MEmhdH1aLTCtQSOhTS4czGfOo2qYStGkTLsf3")!;
2123
let alexPublicKey = Data.init(base64Encoded: "AnRVrvhFPZL0cGtWC2FSfMIX3fFNpBdw6O1mBxkKGFG4")!;
24+
let localPublicKey = Data.init(base64Encoded: "Aq8eftKnVZAP2NZAgdF8nyJ4caoyJV5wFIB8Xpi2LyQF")!;
2225

2326
let peerManager = PeerManager(privateKey: privateKey, ephemeralSeed: ephemeralPrivateKey)
2427

28+
29+
2530
// a connection to be discarded
2631
peerManager.initiateOutboundConnection(remotePublicKey: remotePublicKey)
2732

@@ -35,10 +40,21 @@ class Experimentation {
3540
let tcpClient = TCPClient(address: "testnet-lnd.yalls.org", port: 9735)
3641
let peer = CustomPeer(tcpClient: tcpClient)
3742
peer.name = "Alex"
43+
44+
// print("Creating local peer")
45+
// let tcpClient = TCPClient(address: "127.0.0.1", port: 1337)
46+
// let peer = CustomPeer(tcpClient: tcpClient)
47+
// peer.name = "Local"
48+
49+
self.peer = peer;
3850
peerManager.initiateOutboundConnection(remotePublicKey: alexPublicKey, peer: peer)
3951

52+
53+
4054
}
4155

56+
/*
57+
4258
static func beginHandshake() {
4359

4460
DispatchQueue.global(qos: .background).async {
@@ -110,6 +126,7 @@ class Experimentation {
110126
contentView.logText = contentView.logText + "\n" + entry + "\n"
111127
}
112128
}
129+
*/
113130

114131
}
115132

Swift Rust FFI/Handshake.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@
66
// Copyright © 2020 Arik Sosman. All rights reserved.
77
//
88

9+
910
import Foundation
1011

1112
enum HandshakeError : Error {
1213
case other(message: String)
1314
}
1415

1516
class Handshake {
16-
17+
18+
1719
private var cHandshake: OpaquePointer;
1820
private var cConduit: OpaquePointer?;
1921

2022
init(cHandshake: OpaquePointer){
2123
self.cHandshake = cHandshake;
2224
}
23-
25+
26+
/*
2427
func process_act(actData: Data) throws -> Data {
2528
let rawActDataPointer = (actData as NSData).bytes.assumingMemoryBound(to: UInt8.self);
2629
let dataArgument = LDKBufferArgument(data: rawActDataPointer, length: UInt(actData.count));
@@ -80,5 +83,6 @@ class Handshake {
8083

8184
return plaintext
8285
}
86+
*/
8387

8488
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// Created by Arik Sosman on 5/13/20.
3+
// Copyright (c) 2020 Arik Sosman. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
class ChannelMessageHandler {
9+
10+
var cMessageHandler: LDKChannelMessageHandler?
11+
12+
init(){
13+
14+
let instance = RawLDKTypes.instanceToPointer(instance: self);
15+
16+
func handleOpenChannel(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, initFeatures: LDKInitFeatures, openChannel: UnsafePointer<LDKOpenChannel>?) -> Void {
17+
print("opened channel");
18+
}
19+
func handleAcceptChannel(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, initFeatures: LDKInitFeatures, acceptChannel: UnsafePointer<LDKAcceptChannel>?) -> Void {
20+
print("accepted channel");
21+
}
22+
func handleFundingCreated(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, fundingCreated: UnsafePointer<LDKFundingCreated>?) -> Void {
23+
print("funding created");
24+
}
25+
func handleFundingSigned(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, fundingSigned: UnsafePointer<LDKFundingSigned>?) -> Void {
26+
print("funding signed");
27+
}
28+
func handleFundingLocked(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, fundingLocked: UnsafePointer<LDKFundingLocked>?) -> Void {
29+
print("funding locked");
30+
}
31+
func handleShutdown(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, shutdown: UnsafePointer<LDKShutdown>?) -> Void {
32+
print("shut down");
33+
}
34+
func handleClosingSigned(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, closingSigned: UnsafePointer<LDKClosingSigned>?) -> Void {
35+
print("closing signed");
36+
}
37+
func handleUpdateAddHTLC(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, updateAddHTLC: UnsafePointer<LDKUpdateAddHTLC>?) -> Void {
38+
print("added HTLC");
39+
}
40+
func handleUpdateFulfillHTLC(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, updateFulfillHTLC: UnsafePointer<LDKUpdateFulfillHTLC>?) -> Void {
41+
print("fulfilled HTLC");
42+
}
43+
func handleUpdateFailHTLC(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, updateFailHTLC: UnsafePointer<LDKUpdateFailHTLC>?) -> Void {
44+
print("failed HTLC");
45+
}
46+
func handleUpdateFailMalformedHTLC(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, updateFailMalformedHTLC: UnsafePointer<LDKUpdateFailMalformedHTLC>?) -> Void {
47+
print("failed malformed HTLC");
48+
}
49+
func handleCommitmentSigned(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, commitmentSigned: UnsafePointer<LDKCommitmentSigned>?) -> Void {
50+
print("commitment signed");
51+
}
52+
func handleRevokeAndAck(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, revokeAndAck: UnsafePointer<LDKRevokeAndACK>?) -> Void {
53+
print("revoked and acked");
54+
}
55+
func handleUpdateFee(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, updateFee: UnsafePointer<LDKUpdateFee>?) -> Void {
56+
print("updated fee");
57+
}
58+
func handleAnnouncementSignatures(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, announcementSignatures: UnsafePointer<LDKAnnouncementSignatures>?) -> Void {
59+
print("announcement signatures");
60+
}
61+
62+
func peerDisconnected(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, no_connection_possible: Bool) -> Void {
63+
print("peer disconnected");
64+
}
65+
func peerConnected(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, init: UnsafePointer<LDKInit>?) -> Void {
66+
print("peer connected");
67+
}
68+
69+
func handleChannelReestablish(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, channelReestablish: UnsafePointer<LDKChannelReestablish>?) -> Void {
70+
print("channel reestablished");
71+
}
72+
func handleError(instancePointer: UnsafeRawPointer?, publicKey: LDKPublicKey, error: UnsafePointer<LDKErrorMessage>?) -> Void {
73+
print("some sort of error");
74+
}
75+
76+
let eventsProvider = LDKMessageSendEventsProvider(this_arg: instance)
77+
78+
self.cMessageHandler = LDKChannelMessageHandler(
79+
this_arg: instance,
80+
handle_open_channel: handleOpenChannel,
81+
handle_accept_channel: handleAcceptChannel,
82+
handle_funding_created: handleFundingCreated,
83+
handle_funding_signed: handleFundingSigned,
84+
handle_funding_locked: handleFundingLocked,
85+
handle_shutdown: handleShutdown,
86+
handle_closing_signed: handleClosingSigned,
87+
handle_update_add_htlc: handleUpdateAddHTLC,
88+
handle_update_fulfill_htlc: handleUpdateFulfillHTLC,
89+
handle_update_fail_htlc: handleUpdateFailHTLC,
90+
handle_update_fail_malformed_htlc: handleUpdateFailMalformedHTLC,
91+
handle_commitment_signed: handleCommitmentSigned,
92+
handle_revoke_and_ack: handleRevokeAndAck,
93+
handle_update_fee: handleUpdateFee,
94+
handle_announcement_signatures: handleAnnouncementSignatures,
95+
peer_disconnected: peerDisconnected,
96+
peer_connected: peerConnected,
97+
handle_channel_reestablish: handleChannelReestablish,
98+
handle_error: handleError,
99+
MessageSendEventsProvider: eventsProvider)
100+
101+
}
102+
103+
}

Swift Rust FFI/LDK/Logger.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by Arik Sosman on 5/12/20.
3+
// Copyright (c) 2020 Arik Sosman. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
class Logger {
9+
10+
var cLogger: LDKLogger?;
11+
12+
init(){
13+
func logCallback(pointer: UnsafeRawPointer?, buffer: UnsafePointer<Int8>?) -> Void {
14+
// let instance: Logger = RawLDKTypes.pointerToInstance(pointer: pointer!)
15+
print("something got logged!");
16+
}
17+
18+
self.cLogger = LDKLogger(this_arg: RawLDKTypes.instanceToPointer(instance: self), log: logCallback);
19+
}
20+
21+
}

Swift Rust FFI/LDK/Peer.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ class Peer {
1313
final var cSocketDescriptor: OpaquePointer?
1414
final var manager: PeerManager?
1515
var name: String?
16-
16+
17+
1718
func sendDataCallback(data: Data) -> UInt {
1819
let plaintextBytes = [UInt8](data)
19-
print("plaintext bytes:", plaintextBytes)
20+
print("sending plaintext bytes:", plaintextBytes)
2021
return 4
2122
}
2223

2324
final func receiveData(data: Data) {
25+
print("received data from other end:", [UInt8](data))
2426
self.manager?.receiveData(peer: self, data: data)
2527
}
2628

@@ -29,4 +31,5 @@ class Peer {
2931
print("socket descriptor", self.name, "destroyed")
3032
}
3133

34+
3235
}

0 commit comments

Comments
 (0)