Skip to content

horizontalsystems/StellarKit.Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StellarKit.Swift

StellarKit.Swift is a native (Swift) toolkit for Stellar blockchain. It's implemented and used by Unstoppable Wallet, a multi-currency crypto wallet.

Installation

Swift Package Manager

Swift Package Manager is a dependency manager for Swift projects. You can install it with the following command:

dependencies: [
    .package(url: "https://github.com/horizontalsystems/StellarKit.Swift.git", .upToNextMajor(from: "1.0.8"))
]

Prerequisites

  • Xcode 15.0+
  • Swift 5.5+
  • iOS 16+

Quick Start

1. Create and Prepare StellarKit

let stellarKit = try Kit.instance(
    accountId: "STELLAR_ACCOUNT_ID",
    testNet: false,
    walletId: "WALLET_UNIQUE_ID", // any unique id, used to distinguish multiple StellarKit instances
    minLogLevel: .error
)

stellarKit.sync()

Any time later kit can be synced manually by calling sync() method.

2. Get Data from StellarKit

2.1. Sync States

// Get account sync state
stellarKit.syncState
stellarKit.syncStatePublisher

// Get operation sync state
stellarKit.operationSyncState
stellarKit.operationSyncStatePublisher

Sync state is enum with the following cases:

enum SyncState {
    case synced
    case syncing
    case notSynced(error: Error)
}

2.2. Account

Account is optional. If account has synced sync state, but is nil - this means that account does not yet exist in blockchain.

// Get currently synced account
stellarKit.account
stellarKit.accountPublisher

Account and Asset structures:

struct Account {
    let subentryCount: UInt
    let assetBalanceMap: [Asset: AssetBalance]
}

enum Asset {
    case native
    case asset(code: String, issuer: String)
}

struct AssetBalance {
    let asset: Asset
    let balance: Decimal
    let limit: Decimal?
}

2.3. Receive Address

// Get receive address
stellarKit.receiveAddress

3. Listening to Operation Stream

Starts listening to operation stream and handle automatic syncing of kit when any new operation is received

stellarKit.startListener()
stellarKit.stopListener()

4. Send Transaction

In order to send transactions you need a valid KeyPair

4.1. Prepare Asset

let nativeAsset = Asset.native
let asset = Asset.asset(code: "CODE", issuer: "ISSUER")

4.2. Supported Operations

let paymentOperation = try stellarKit.paymentOperation(
    asset: asset, // or nativeAsset
    destinationAccountId: "destination_account_id",
    amount: 1.23
)
let createAccountOperation = try stellarKit.createAccountOperation(
    destinationAccountId: "destination_account_id",
    amount: 1.23
)
let changeTrustOperation = try stellarKit.changeTrustOperation(
    asset: asset,
    limit: nil // nil for unlimited or any other amount
)

4.3. Send Transaction

let memo = "memo_here"

let txId = try await StellarKit.Kit.send(
    operations: [paymentOperation],
    memo: memo,
    keyPair: keyPair,
    testNet: false
)

Example Project

All features of the library are used in example project located in Demo App folder. It can be referred as a starting point for usage of the library.

License

The StellarKit.Swift toolkit is open source and available under the terms of the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages