/Zesame

Swift SDK for Zilliqa

Primary LanguageSwiftMIT LicenseMIT

Zesame

Zesame is an unofficial Swift SDK for Zilliqa. It is written in Swift 5.3. This SDK contains cryptographic methods allowing you to create and restore a wallet, sign and broadcast transactions. The cryptographic methods are implemented in EllipticCurveKit. This SDK uses Zilliqas JSON-RPC API

Getting started

SPM will install all dependencies, either when run from terminal, or when Xcode gets opened. To run tests it is recommended to use optimisation flag, otherwise it takes quite a bit of time to run them:

swift test -Xswiftc -O

Protobuf

Zesame uses the JSON-RPC API and not the protobuf API, however, for the packaging of the transaction we need to use protobuf. Please note that protocol buffers is only used for the packaging of the transaction, it is not used at all for any communication with the API. All data is sent as JSON to the JSON-RPC API.

We use the apple/swift-protobuf project for protocol buffers, and it is important to note the two different programs associated with swift protobuf:

Install protobuf

protoc-gen-swift

This program is used only for generating our swift files from our .proto files and we install this program using brew (it can also be manually downloaded and built).

Follow installation instructions using brew here.

brew install swift-protobuf

After this is done we can generate .pb.swift files using

$ protoc --swift_out=. my.proto

SwiftProtobuf library

In order to make use of our generated .pb.swift files we need to include the SwiftProtobuf library, which is installed through SPM.

Use protobuf

Stand in the root of the project and run:

protoc --swift_opt=Visibility=Public --swift_out=. Sources/Zesame/Models/Protobuf/messages.proto

Add the generated file messages.pb.swift to Models folder.

Dependencies

You will find all dependencies inside the Package.swift, but to mention the most important:

EllipticCurveKit

Zesame is dependent on the Elliptic Curve Cryptography of EllipticCurveKit, for the generation of new wallets, restoration of existing ones, the encryption of your private keys into keystores and the signing of your transactions using Schnorr Signatures.

Other

  • RxSwift: The library uses RxSwift for async programming.

  • JSONRPCKit: For consuming the Zilliqa JSON-RPC API.

API

Closure or Rx

This SDK contains two implementations for each method, one that uses Closures(a.k.a. "Callbacks") and one implementation using RxSwift Observables.

Rx

DefaultZilliqaService.shared.rx.getBalance(for: address).subscribe(
    onNext: { print("Balance: \($0.balance)") },
    onError: { print("Failed to get balance, error: \($0)") }
).disposed(by: bag)

Closure

DefaultZilliqaService.shared.getBalance(for: address) {
    switch $0 {
    case .success(let balanceResponse): print("Balance: \(balanceResponse.balance)") 
    case .failure(let error): print("Failed to get balance, error: \(error)")
    }
}

Functions

Have a look at ZilliqaService.swift for an overview of the functions, here is a snapshot of the current functions of the reactive API (each function having a closure counterpart):

public protocol ZilliqaServiceReactive {
    func createNewWallet() -> Observable<Wallet>
    func exportKeystore(from wallet: Wallet, encryptWalletBy passphrase: String) -> Observable<Keystore>
    func importWalletFrom(keyStore: Keystore, encryptedBy passphrase: String) -> Observable<Wallet>

    func getBalance(for address: Address) -> Observable<BalanceResponse>
    func sendTransaction(for payment: Payment, signWith keyPair: KeyPair) -> Observable<TransactionIdentifier>
}

Explorer

While developing it might be useful for you to use the Zilliqa explorer

Donate

This SDK and the foundation EllipticCurveKit its built upon has been developed by the single author Alexander Cyon without paid salary in his free time - approximately a thousand hours of work since May 2018 (see initial commit).

Any donation would be much appreciated:

  • ZIL: zil108t2jdgse760d88qjqmffhe9uy0nk4wvzx404t
  • BTC: 3GarsdAzLpEYbhkryYz1WiZxhtTLLaNJwo
  • ETH: 0xAB8F0137295BFE37f50b581F76418518a91ab8DB
  • NEO: AbbnnCLP26ccSnLooDDcwPLDnfXbVL5skH

License

Zesame is released under the MIT License.