Skip to content

Latest commit

 

History

History
179 lines (139 loc) · 6.19 KB

README.md

File metadata and controls

179 lines (139 loc) · 6.19 KB

Cardano.swift

🐧 linux: ready GitHub license Build Status GitHub release SPM compatible CocoaPods version Platform macOS | iOS | Linux

Swift SDK for Cardano network (Alonzo).

Installation

Cardano.swift uses cardano-serialization-lib Rust library inside.

Cardano.swift deploys to macOS 10.12, iOS 11 and Linux. It has been tested on the latest OS releases only however, as the module uses very few platform-provided APIs, there should be very few issues with earlier versions.

Setup instructions:

  • Swift Package Manager: Add this to the dependency section of your Package.swift manifest:

    .package(url: "https://github.com/tesseract-one/Cardano.swift.git", from: "0.1.0")

    SPM build provides 2 targets: Cardano and CardanoBlockfrost (networking library).

  • CocoaPods: Put this in your Podfile:

    pod 'Cardano.swift/Cardano/Binary', '~> 0.1.0'
    pod 'Cardano.swift/Blockfrost' # networking
  • CocoaPods with Rust part built from sources:

    If you want to build Rust part from sources add this in your Podfile:

    pod 'Cardano.swift/Cardano/Build', '~> 0.1.0'
    pod 'Cardano.swift/Blockfrost' # networking

    And install Rust targets:

    rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios aarch64-apple-darwin x86_64-apple-darwin
  • Linux:

    Linux supported through SPM. For build you have to build Rust library manually. You can build it using rust/scripts/build_binary_linux.sh script from this repository.

    rust/scripts/build_binary_linux.sh "SOME_INSTALL_PATH"

    And provide path to it as parameters to SPM for build.

    swift build -Xlinker -L"SOME_INSTALL_PATH/lib" -Xcc -I"SOME_INSTALL_PATH/include"

Usage

Library provides a set of high-level APIs for standard tasks. Lets try to send ADA to another address on the test net. We will use Blockfrost API for simplicity.

Initialize library

import Foundation
import Cardano
// Needed for SPM only
import CardanoBlockfrost

// We need Keychain with our keys first to sign our transactions
let keychain = try Keychain(mnemonic: ["your", "mnemonic", "phrase"])

// Generate first account in keychain
try keychain.addAccount(index: 0)

// Create main Cardano object
let cardano = try! Cardano(blockfrost: "your-project-key",
                           info: .testnet,
                           signer: keychain)

//Sync addresses with network and call our sendAda method
cardano.addresses.fetch { _ in
  sendAda(cardano: cardano)
}

Send ADA

func sendAda(cardano: Cardano) {
  // Get our account from fetched accounts
  let account = try cardano.addresses.fetchedAccounts()[0]

  // Create recepient address
  let to = try Address(bech32: "addr_test1qzdj7sr6ymlqmrpvvd5qg9ct55kx6kcev67u33uc9grgm3dc4rwdulp233ujjmc09g446unlhtt0ekdqds2t2qccxxmspd22lj")

  // Send ADA
  cardano.send.ada(to: to, lovelace: 10000000, from: account) { res in
    switch res {
      case .failure(let err): print("TX Failed: \(err)")
      case .success(let txId): print("TX Id: \(txId)")
    }
  }
}

Addresses and UTXOs

We have two helper interfaces for Address and UTXO management. They can be obtained as addresses and utxos from Cardano object.

Address Manager

var account: Account? = nil
// Get list of accounts from Signer
cardano.addresses.accounts() { res in
  account = try res.get()[0]
}

// Fetch list of addresses from the network for provided accounts
cardano.addresses.fetch(for: [account]) { _ in }

// Get list and fetch addresses (bot methods together)
cardano.addresses.fetch() { _ in }

// Get list of fetched accounts
let accounts = cardano.addresses.fetchedAccounts()

// Get list of fetched addresses for Account
let addresses = try cardano.addresses.get(cached: account)

// Create new address for the Account
let address = try cardano.addresses.new(for: account, change: false)

UTXO provider

// Obtain list of UTXOs from the network for addresses
cardano.utxos.get(for: [address]).next { res, next in
  let utxos = try! res.get()
  print("UTXOs: \(utxos), has more: \(next != nil)")
}
// Obtain list of UTXOs from the network for transaction
cardano.utxos.get(for: try! TransactionHash(hex: "0x..")).next { res, next in
  let utxos = try! res.get()
  print("UTXOs: \(utxos), has more: \(next != nil)")
}

Custom Transaction

TransactionBuilder class can be used for building custom transactions.

let info = NetworkApiInfo.testnet

var builder = try! TransactionBuilder(linearFee: info.linearFee,
                                      minimumUtxoVal: info.minimumUtxoVal,
                                      poolDeposit: info.poolDeposit,
                                      keyDeposit: info.keyDeposit,
                                      maxValueSize: info.maxValueSize,
                                      maxTxSize: info.maxTxSize)
// Add tx information
builder.addInput(....)
builder.addOutput(....)

// Build TX
let tx = try builder.build()

// Sign and submit
cardano.tx.signAndSubmit(tx: tx, addresses: [/* used addresses in tx*/]) { res in
  print("Result: \(res)")
}

Further Development

Further development plans based on requests from the users. If you need more APIs for your dApp - create Issue and we will add them.

License

Cardano.swift can be used, distributed and modified under the Apache 2.0 license.