Skip to content
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

feature/node-identity #84

Draft
wants to merge 34 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c15698a
merged, adding some basic stuff to pass identity to transport
decanus Oct 11, 2019
40ebd84
addition
decanus Oct 13, 2019
09a710f
merged
decanus Oct 14, 2019
6ea3757
started working on identity
decanus Oct 16, 2019
5dcf8ac
Update Node.swift
decanus Oct 16, 2019
bff46dd
fixes
decanus Oct 16, 2019
13151a5
Update Node.swift
decanus Oct 16, 2019
bef95a9
Update CoreBluetoothTransport.swift
decanus Oct 16, 2019
82a0829
Update Transport.swift
decanus Oct 16, 2019
168ecd8
Update Transport.swift
decanus Oct 16, 2019
03885a4
Merge branch 'feature/node-identity' of https://github.com/ultralight…
decanus Oct 16, 2019
58ffd84
Update Transport.swift
decanus Oct 16, 2019
0ea8473
Update Transport.swift
decanus Oct 16, 2019
3c50c6e
Merge branch 'feature/node-identity' of https://github.com/ultralight…
decanus Oct 21, 2019
9c621f9
Curve25519 keys
decanus Oct 21, 2019
7192573
split
decanus Oct 21, 2019
df2ad21
merged
decanus Oct 21, 2019
17a0acd
started
decanus Oct 21, 2019
91b5a9b
updates
decanus Oct 21, 2019
a022722
large changes, could lead to race conditions
decanus Oct 21, 2019
85baf0b
code style fixes
decanus Oct 21, 2019
f7a7376
removed
decanus Oct 21, 2019
471fc14
updates
decanus Oct 21, 2019
c66ab44
updated
decanus Oct 21, 2019
735fd8e
fixes
decanus Oct 21, 2019
7a7493b
latest xcode
decanus Oct 21, 2019
90df88e
updated
decanus Oct 21, 2019
25229e9
fix
decanus Oct 21, 2019
96474fb
fix
decanus Oct 21, 2019
19ed83a
cleaned up send
decanus Oct 21, 2019
1aedade
updated
decanus Oct 22, 2019
23eb097
updated
decanus Oct 22, 2019
ae164e4
change
decanus Oct 22, 2019
e37e58f
updates
decanus Oct 22, 2019
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
99 changes: 99 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (3.0.1)
activesupport (4.2.11.1)
i18n (~> 0.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
atomos (0.1.3)
claide (1.0.3)
cocoapods (1.7.5)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.7.5)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 1.2.2, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-stats (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.3.1, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored2 (~> 3.1)
escape (~> 0.0.4)
fourflusher (>= 2.3.0, < 3.0)
gh_inspector (~> 1.0)
molinillo (~> 0.6.6)
nap (~> 1.0)
ruby-macho (~> 1.4)
xcodeproj (>= 1.10.0, < 2.0)
cocoapods-core (1.7.5)
activesupport (>= 4.0.2, < 6)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
cocoapods-deintegrate (1.0.4)
cocoapods-downloader (1.2.2)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.0)
cocoapods-stats (1.1.0)
cocoapods-trunk (1.4.0)
nap (>= 0.8, < 2.0)
netrc (~> 0.11)
cocoapods-try (1.1.0)
colored2 (3.1.2)
concurrent-ruby (1.1.5)
escape (0.0.4)
ffi (1.11.1)
fourflusher (2.3.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
jazzy (0.11.1)
cocoapods (~> 1.5)
mustache (~> 1.1)
open4
redcarpet (~> 3.4)
rouge (>= 2.0.6, < 4.0)
sassc (~> 2.1)
sqlite3 (~> 1.3)
xcinvoke (~> 0.3.0)
liferaft (0.0.6)
minitest (5.11.3)
molinillo (0.6.6)
mustache (1.1.0)
nanaimo (0.2.6)
nap (1.1.0)
netrc (0.11.0)
open4 (1.3.4)
redcarpet (3.5.0)
rouge (2.0.7)
ruby-macho (1.4.0)
sassc (2.2.1)
ffi (~> 1.9)
sqlite3 (1.4.1)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
xcinvoke (0.3.0)
liferaft (~> 0.0.6)
xcodeproj (1.12.0)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.2.6)
xcpretty (0.3.0)
rouge (~> 2.0.7)

PLATFORMS
ruby

DEPENDENCIES
jazzy (= 0.11.1)
xcpretty

BUNDLED WITH
1.17.2
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// swift-tools-version:5.0
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
name: "UB",
platforms: [
.macOS(.v10_13),
.macOS(.v10_15),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Using the UB within your own project is kept simple. Initialize a `Node`, and as
```swift
import UB

let node = Node()
let node = Node(key: Curve25519.Signing.PrivateKey())
node.delegate = self

node.add(transport: CoreBluetoothTransport())
Expand Down
73 changes: 51 additions & 22 deletions Sources/UB/Node.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import SwiftProtobuf
import CryptoKit

// @todo figure out architecture to support new forwarding algorithm.

Expand All @@ -8,11 +9,22 @@ public class Node {
/// The known transports for the node.
public private(set) var transports = [String: Transport]()

/// The known peers for a node.
public private(set) var peers = [Addr: Peer]()

/// The nodes delegate.
public weak var delegate: NodeDelegate?

/// The nodes private key.
private let key: Curve25519.Signing.PrivateKey

/// Initializes a node.
public init() {}
///
/// - Parameters:
/// - key: The private key for the node.
public init(key: Curve25519.Signing.PrivateKey) {
self.key = key
}

/// Adds a new transport to the list of known transports.
///
Expand All @@ -27,7 +39,7 @@ public class Node {

transports[id] = transport
transports[id]?.delegate = self
transport.listen()
transport.listen(identity: UBID(key.publicKey.rawRepresentation))
}

/// Removes a transport from the list of known transports.
Expand Down Expand Up @@ -55,29 +67,31 @@ public class Node {
return
}

transports.forEach { _, transport in
let peers = transport.peers

// @todo ensure that messages are delivered?
// what this does is try to send a message to an exact target or broadcast it to all peers
if message.recipient.count != 0 {
if peers.contains(where: { $0.id == message.recipient }) {
return transport.send(message: data, to: message.recipient)
}
}

// what this does is send a message to anyone that implements a specific service
if message.service.count != 0 {
let filtered = peers.filter { $0.services.contains { $0 == message.service } }
if filtered.count > 0 {
let sends = flood(message, data: data, transport: transport, peers: filtered)
if sends > 0 {
return
}
if message.recipient.count != 0 {
if let peer = peers[message.recipient] {
// @todo ensure we actually had > 0 transports to send to.
return peer.transports.forEach { id, addr in
guard let transport = transports[id] else { return }
transport.send(message: data, to: addr)
}
}
_ = flood(message, data: data, transport: transport, peers: peers)
}

// @todo figure this out
// transports.forEach { _, transport in
// let peers = transport.peers
// // what this does is send a message to anyone that implements a specific service
// if message.service.count != 0 {
// let filtered = peers.filter { $0.services.contains { $0 == message.service } }
// if filtered.count > 0 {
// let sends = flood(message, data: data, transport: transport, peers: filtered)
// if sends > 0 {
// return
// }
// }
// }
// _ = flood(message, data: data, transport: transport, peers: peers)
// }
}

private func flood(_ message: Message, data: Data, transport: Transport, peers: [Peer]) -> Int {
Expand Down Expand Up @@ -113,4 +127,19 @@ extension Node: TransportDelegate {

delegate?.node(self, didReceiveMessage: Message(protobuf: packet, from: from))
}

public func transport(_ transport: Transport, didConnectToPeer id: Addr, withAddr addr: Addr) {
if peers[id] == nil {
peers[id] = Peer(id: id, services: [UBID]())
}

guard let peer = peers[id] else { return }

peer.transports[String(describing: transport)] = addr
}

public func transport(_ transport: Transport, didDisconnectFromPeer id: Addr) {
guard let peer = peers[id] else { return }
peer.transports.removeValue(forKey: String(describing: transport))
}
}
7 changes: 6 additions & 1 deletion Sources/UB/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import Foundation

// @todo clean this up properly, currently very rough for testing purposes.

// @todo set transport addresses, allowing a node to select which transports to send on.

/// Represents the nodes a transport can communicate with.
public class Peer {
/// The peers id.
/// The peers identifier.
public let id: Addr

/// The services a peer knows.
public let services: [UBID]

/// A list of peer addresses for a given transport.
public var transports = [String: Addr]()

/// Initializes a peer with a specified id and list of known services.
///
/// - Parameters:
Expand Down
Loading