Last active
October 9, 2019 17:30
-
-
Save klivin/45ae7889ba54668436f797f05cbab4f4 to your computer and use it in GitHub Desktop.
WIP user-friendly protocol for XYO Hackathon developers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// XyProtocol.swift | |
// SampleiOS | |
// | |
// Created by Kevin Weiler on 10/7/19. | |
// Copyright © 2019 XYO Network. All rights reserved. | |
// | |
import Foundation | |
import XyBleSdk | |
import sdk_core_swift | |
import CoreLocation | |
import sdk_objectmodel_swift | |
enum AddressFormat { | |
case hex | |
case ethereumHex | |
} | |
struct ParseOptions { | |
var addressFormat: AddressFormat = .hex | |
} | |
protocol BoundWitnessParseable { | |
var boundWitness: XyoBoundWitness {get set} | |
var options: ParseOptions {get set} | |
init(_boundWitness: XyoBoundWitness, _options: ParseOptions?) | |
// Throws if you can't get heuristic from BW for generic type | |
// ExpectedHeuristic will be CLLocation, Data, String, or Int based on BoundWitness fetters | |
func heuristic<ExpectedHeuristic>(forKey: String) -> ExpectedHeuristic | |
// Addresses default to hex format | |
func address(index: UInt) -> String | |
func allAddresses() -> [String] | |
// In hex | |
func signature(index: UInt) -> String | |
func allSignatures(format: String) -> [String] | |
func asJson() -> [String: Any] | |
func bytes() -> Data | |
func hash() -> String | |
} | |
enum BoundWitnessHeuristicType { | |
case location // CLLocation | |
case data // Data | |
case string // String | |
case int // Int | |
} | |
protocol SentinelHeuristicFetcher { | |
func getHeuristicType() -> BoundWitnessHeuristicType | |
func getHeuristicKey() -> String | |
// ExpectedHeuristic will be CLLocation, Data, String, or Int based on getHeuristicType() | |
// Will throw if not as specified | |
func getHeuristic<ExpectedHeuristicType>() -> ExpectedHeuristicType | |
} | |
protocol SentinelDelegate { | |
func sentinelScan(detected devices: [XYBluetoothDevice], family: XYDeviceFamily) | |
func sentinelScan(detected device: XYBluetoothDevice, rssi: Int, family: XYDeviceFamily) | |
func boundWitness(didSucceed withBoundWitness: BoundWitnessParseable, withDevice: XYBluetoothDevice) | |
func boundWitness(didFail withError: Error) | |
} | |
extension SentinelDelegate { | |
func sentinelScan(detected device: XYBluetoothDevice, rssi: Int, family: XYDeviceFamily) { | |
//this is a empty implementation to allow this method to be optional | |
} | |
func boundWitnessSuccess(boundWitness: BoundWitnessParseable, withDevice: XYBluetoothDevice) { | |
//this is a empty implementation to allow this method to be optional | |
} | |
func boundWitnessFail(withError: Error) { | |
//this is a empty implementation to allow this method to be optional | |
} | |
} | |
typealias BoundWitnessCallback = (_ boundWitness: BoundWitnessParseable?, _ device: XYBluetoothDevice?, _ error: Error?) -> () | |
protocol SentinelProtocol { | |
var delegate: SentinelDelegate {get} | |
static func buildSentinel(withDelegate: SentinelDelegate) -> SentinelClient | |
/// Configures origin chain and sets up heuristic getters | |
func configure(heuristicFetchers: [String : SentinelHeuristicFetcher]?) | |
func startScanningForDevices() | |
func stopScanningForDevices() | |
func doBoundWitness(withDevice: XYBluetoothDevice) | |
func doBoundWitnessWithSelf() | |
func doBoundWitness(withDevice: XYBluetoothDevice, withCompletion: BoundWitnessCallback) | |
func doBoundWitnessWithSelf(withCompletion: BoundWitnessCallback) | |
func originChainLength() -> UInt | |
func boundWitnessAtIndex(index: UInt) -> BoundWitnessParseable | |
// Hex formatted long form | |
func originChainAddress() -> String | |
/// Private vars, but here for clarity: | |
var originChain : XyoOriginChainCreator { get } | |
var device : XYBluetoothDevice {get} | |
} | |
protocol BridgeDelegate { | |
func bridgeConnection(didFail withError: Error) | |
func bridgeConnection(didChangeState state: BridgeStatus) | |
func bridging(didSucceed withBoundWitnesses: [BoundWitnessParseable]) | |
func bridging(didFail withError: Error) | |
} | |
protocol BridgeProtocol: SentinelProtocol { | |
var archivistUrl: NSURL {get} | |
var bridgeStatus: BridgeStatus {get} | |
var pendingBoundWitnesses: [BoundWitnessParseable] {get} | |
var delegate: BridgeDelegate {get} | |
static func buildBridge(withDelegate: BridgeDelegate) -> BridgeClient | |
func configure(archivist: NSURL, heuristicFetchers: [String : SentinelHeuristicFetcher]?, autoBridge: Bool) | |
func bridgePendingToArchivist() | |
} | |
enum BridgeStatus: Int { | |
case none | |
case connecting | |
case connected | |
case disconnected | |
} | |
class SentinelClient: SentinelProtocol { | |
private init(_device: XYBluetoothDevice) { | |
} | |
static func buildSentinel(withDelegate: SentinelDelegate) -> SentinelClient { | |
} | |
///...TODO | |
} | |
class BridgeClient: SentinelClient, BridgeProtocol { | |
///...TODO | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment