AlgorandKit

A Swift library to enable lightweight interaction with the Algorand blockchain.

Embed this library to easily create and share Algorand Payment Prompt URIs and stylized QR codes.

AlgorandKit_screenshot_device

Requirements

  • Xcode 12.0 or higher.
  • iOS 13.0 or higher.

Installation

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

To integrate AlgorandKit into your Xcode project using Xcode 12, specify it in File > Swift Packages > Add Package Dependency...:

https://github.com/computerbluemonday/AlgorandKit

Or, manually add the dependency to your Package.swift:

.package(url: https://github.com/computerbluemonday/AlgorandKit, from: "1.1.0")

AlgorandURI

A utility for constructing Algorand Payment Prompt URIs, as defined by the specification:

https://developer.algorand.org/docs/reference/payment_prompts/

Per the spec, this URI represents "a standardized way for applications and websites
to send requests and information through deeplinks, QR codes, etc." On iOS, these URIs
can be handled by the official Algorand Wallet app to delegate transaction processing on Algorand:

https://developer.algorand.org/articles/payment-prompts-with-algorand-mobile-wallet/

AlgorandURI is an immutable Swift struct which enforces business logic rules through its
interface definition upon initialization. All URIs constructed via AlgorandURI should be
semantically valid. (AlgorandURI does not validate addresses or transaction validity.)

The object definition lends itself to quick visual inspection in the debugger -- for instance:

▿ AlgorandURI
  - uriScheme : "algorand"
  ▿ receiver : Receiver
    - address : "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE"
    ▿ label : Optional<String>
      - some : "Bob"
  ▿ assetAmount : AssetAmount
    ▿ algo : 1 element
      - microAlgos : 50
  ▿ note : Optional<note>
    ▿ some : note
      ▿ readonly : 1 element
        - xnote : "Transfer"

A suite of test cases is included for regression verification.

Usage Examples

let algoURI = AlgorandURI(
      receiver: AlgorandURI.Receiver(address: "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE", label: "Barclay"),
      assetAmount: .algo(microAlgos: 100),
      note: .readonly(xnote: "Transporter Insurance"))
      
let asaURI = AlgorandURI(
      receiver: AlgorandURI.Receiver(address: "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE", label: "Lanel"),
      assetAmount: .ASA(assetID: 1000000, amount: 150),
      note: .readonly(xnote: "Spaceship"))

if let url = algoURI.url() {
  if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
  }
}

AlgorandQRCode

Utilities for generating raw and stylized Algorand QR codes from AlgorandURI instances:

  • AlgorandURI (extension)
    -- Generate a raw QR code UIImage from an AlgorandURI
  • AlgorandQRCodeBadge
    -- Create a scalable, (optionally) stylized and branded Algorand QR code badge in SwiftUI

Although not explicitly provided as part of this library, see AlgorandQRCodeTests for examples of detecting
Algorand QR Codes using Apple's Vision framework.

Usage Examples

// Create an AlgorandURI to receiver Barclay for 100 microAlgos, with the readonly note "Transporter Insurance"
let algoURI = AlgorandURI(
  receiver: AlgorandURI.Receiver(address: "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE", label: "Barclay"),
  assetAmount: .algo(microAlgos: 100),
  note: .readonly(xnote: "Transporter Insurance"))

// Generate a raw QR code UIImage for this transaction URI
let qrCodeImage = algoURI.generateQRCode()

// Instantiate a stylized SwiftUI QR code badge
AlgorandQRCodeBadge(
  uri:AlgorandURI(receiver: AlgorandURI.Receiver(address: "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE", label: nil), assetAmount: nil, note: nil),
  qrCodeColor: UIColor.systemTeal,
  frameConfiguration: .framed(color: UIColor.darkGray),
  includeLogo: true)

Sample Screenshots

algo-qr-screenshot-1

algo-qr-screenshot-2

algo-qr-screenshot-3

UIActivityViewController / AirDrop

Support for sharing AlgorandURIs with other devices or services via UIActivityViewController.
Defaults to a set of sharing destinations including AirDrop, SMS, Mail, Copy/Paste; configurable to other sharing destinations by the caller.

Usage Examples

// Create an AlgorandURI to receiver Data for 400 microAlgos.
let algoURI = AlgorandURI(
  receiver: AlgorandURI.Receiver(address: "4AOJ5QITUBWZGO4K5AF77H5GED5A4QDBB6DOQGI63QE2GU6KD2XNETBBJE", label: "Data"),
  assetAmount: .algo(microAlgos: 400),
  note: .readonly(xnote: "Cat food."))

// Present UIActivityViewController to share AlgorandURI via AirDrop, SMS, etc. (SwiftUI)
if let av = algoURI.activityViewController(excludedActivityTypes: nil) {
  UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}

Sample Screenshots

algo-airdrop-algos

algo-share-sheet

algo-receive-airdrop

AlgoSampleApp

AlgoSampleApp is a standalone iOS Application project which demonstrates importing AlgorandKit, and specifically demos AlgorandURI's UIActivityViewController / AirDrop support. This can also serve as a test harness for exploring other AlgorandKit functionality.

To build, simply open AlgoSampleApp.xcodeproj included in the AlgoSampleApp directory.

GitHub

https://github.com/computerbluemonday/AlgorandKit