SDK 核心库 Swift 3.1.7

SDK 核心库 Swift 3.1.7

XYO FoundationCarter HarrisonDeploy 维护。



  • XY - 持续的企业

logo

SDK 核心库 Swift

Test Coverage Carthage compatible

XYO Foundation 提供了这部分源代码,以便进一步了解 XYO 协议及其可能的用途。我们继续维护此软件以供开发人员教育使用。此源代码不适用于生产环境。

目录

注意 此 SDK 的最新版本包括从 sdk-objectmodel-swift 导入的对象模型。

README.md 文档是集成 XYO 核心SDK到您的项目时可能需要的常用方法的概述。

有关易于使用的入门集成指南,请参阅我们的 示例应用程序指南

一个库,可以执行所有基本的 XYO 网络函数。这包括创建起源链、维护起源链、与其他节点进行协商以及其他基本功能。该库具有高度抽象的模块,因此所有操作都将与任何加密、存储、网络等兼容。

创建起源块的 XYO 协议在 XYO 黄皮书 中指定。其中,介绍了 XYO 网络上节点创建绑定见证者的行为。注意,此行为并不与传输层、加密算法或哈希算法中的任何特定技术限制相关。

入门

Cocoapods

请注意,当前CocoaPods只支持iOS。

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

需要 CocoaPods 1.6.0.beta.2+。

要使用 CocoaPods 将其集成到 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'sdk-core-swift', '3.1.6'
end

然后,运行以下命令

$ pod install

进行测试运行

pod try sdk-core-swift

原始链

最常见的通过创建一个原始链创建者对象来访问此库的接口。通过一个原始链创建者对象可以创建并维护一个原始链。

// this object will be used to hash items within the node
let hasher = XyoSha256()

// this is used as a key value store 
let storage = XyoInMemoryStorage()

// this is used as a place to store all of the bound witnesses/origin blocks
let chainRepo = XyoStorageOriginBlockRepository(storage: storage, hasher: hasher)

// this is used to save the state of the node (keys, index, previous hash)
let stateRepo = XyoStorageOriginStateRepository(storage: storage)

// this holds the state and the chain repository together
let configuration = XyoRepositoryConfiguration(originState: stateRepo, originBlock: chainRepo)

// the node to interface with creating an origin chain
let node = XyoOriginChainCreator(hasher: hasher, repositoryConfiguration: configuration)

在创建一个节点后,通常会添加一个签名者,并创建一个创世区块。

// creates a signer with a random private key
let signer = XyoSecp256k1Signer()
    
// adds the signer to the node
node.originState.addSigner(signer: signer)

// creates a origin block with its self (genesis block if this is the first block you make)
try node.selfSignOriginChain()

在创建创世区块后,您的原始链正式开始。记住,所有状态都存储在通过节点构建的状态存储库(XyoOriginChainStateRepository)和区块存储库(XyoOriginBlockRepository)中。这两个存储库都很高级,可以根据需要实现。此库自带了一个关键值存储数据库的实现(XyoStorageOriginBlockRepository)和(XyoStorageOriginChainStateRepository)。

XyoStorageProvider 接口定义了简单键值存储的方法。此库附带一个默认的内存键值存储实现(XyoInMemoryStorage)。

创建原始区块

在创建了一个节点之后,可以使用它来使用其他节点创建原始区块。与其他节点通信的过程已经通过使用管道(例如tcp,ble,内存)抽象化,该管道处理所有传输逻辑。此接口定义为 XyoNetworkPipe。此库自带了一个内存管道和一个tcp管道。

使用tcp管道

 // this defines who to create a tcp pipe with
let tcpPeer = XyoTcpPeer(ip: "myarchivist.com", port: 11000)

// prepares a socket tcp to communicate with the other node
let socket = XyoTcpSocket.create(peer: tcpPeer)

// wraps the socket to comply to the pipe interface
let pipe = XyoTcpSocketPipe(socket: socket, initiationData: nil)

// wraps the pipe to preform standard communications
let handler = XyoNetworkHandler(pipe: pipe)

node.boundWitness(handler: handler, procedureCatalogue: XyoProcedureCatalogue) { (boundWitness, error) in
    
}

使用内存管道

let pipeOne = XyoMemoryPipe()
let pipeTwo = XyoMemoryPipe()

pipeOne.other = pipeTwo
pipeTwo.other = pipeOne

let handlerOne = XyoNetworkHandler(pipe: pipeOne)
let handlerTwo = XyoNetworkHandler(pipe: pipeTwo)

nodeOne.boundWitness(handler: handlerOne, procedureCatalogue: TestInteractionCatalogueCaseOne()) { (result, error) in
    // this should complete first
}

nodeTwo.boundWitness(handler: handlerTwo, procedureCatalogue: TestInteractionCatalogueCaseOne()) { (result, error) in
    // this should complete second
}

更多示例和桥接交互可以在 这里 找到

蓝牙

客户机和服务器蓝牙 swift 管道可以在 这里 找到。

其他

只要遵循定义的接口 这里,就可以实现其他网络管道。

约束见证

将自定义数据添加到约束见证

要将自定义数据添加到约束见证,可以创建一个XyoHeuristicGetter。

public struct MyCustomData: XyoHeuristicGetter {
    public func getHeuristic() -> XyoObjectStructure? {
        if (conditionIsMet) {
            let myData = getDataSomehow()
            return myData
        }
        
        return nil
    }
}

创建getter后,可以通过调用以下方式将其添加到节点:

 let myDataForBoundWitness = MyCustomData()
 node.addHeuristic (key: "MyData", getter : myDataForBoundWitness)

节点监听器

向节点添加监听器

 struct MyListener : XyoNodeListener {
    /// This function will be called every time a bound witness has started
    func onBoundWitnessStart() {
        // update UI
    }
    
    /// This function is called when a bound witness starts, but fails due to an error
    func onBoundWitnessEndFailure() {
        // update UI
    }
    
    /// This function is called when the node discovers a new origin block, this is typicaly its new blocks
    /// that it is creating, but will be called when a bridge discovers new blocks.
    /// - Parameter boundWitness: The boundwitness just discovered
    func onBoundWitnessDiscovered(boundWitness : XyoBoundWitness) {
        // update UI
    }
    
    /// This function is called every time a bound witness starts and complets successfully.
    /// - Parameter boundWitness: The boundwitness just completed
    func onBoundWitnessEndSuccess(boundWitness : XyoBoundWitness) {
        // update UI
    }
}

您可以添加以下内容来向节点添加监听器:

  let listener = MyListener()
  myNode.addListener(key: "MyListener", listener : listener)
  

TCP节点

TCP节点示例

以下代码是一个示例,展示了一个节点10次与服务器绑定见证。

let storage = XyoInMemoryStorage()
let blocks = XyoStrageProviderOriginBlockRepository(storageProvider: storage,
                                                    hasher: XyoSha256())
let state = XyoStorageOriginChainStateRepository(storage: storage)
let conf = XyoRepositoryConfiguration(originState: state, originBlock: blocks)
let node = XyoRelayNode(hasher: XyoSha256(),
                        repositoryConfiguration: conf,
                        queueRepository: XyoStorageBridgeQueueRepository(storage: storage))

node.originState.addSigner(signer: XyoSecp256k1Signer())

for i in 0..9 {
    let peer = XyoTcpPeer(ip: "alpha-peers.xyo.network", port: 11000)
    let socket = XyoTcpSocket.create(peer: peer)
    let pipe = XyoTcpSocketPipe(socket: socket, initiationData: nil)
    let handler = XyoNetworkHandler(pipe: pipe)

    let data = UInt32(XyoProcedureCatalogueFlags.TAKE_ORIGIN_CHAIN | XyoProcedureCatalogueFlags.GIVE_ORIGIN_CHAIN)
    node.boundWitness(handler: handler, procedureCatalogue: XyoFlagProcedureCatalogue(forOther: data, withOther: data)) { (result, error) in

    }
}

维护者

  • Carter Harrison
  • Arie Trouw
  • Kevin Weiler

许可证

查看LICENSE文件以获取许可证详情。

致谢

🔥❄️XYO制作