This Swift SDK provides iOS-friendly bindings for the Dash Platform, wrapping the rs-sdk-ffi crate with idiomatic Swift interfaces.
See also: iOS Simulator MCP usage and Codex config in IOS_SIMULATOR_MCP.md.
- Identity Management: Create, fetch, and manage Dash Platform identities
- Data Contracts: Define and deploy structured data schemas
- Document Operations: Create, fetch, and update documents
- Credit Transfers: Transfer credits between identities
- Put to Platform: Multiple options for state transitions (instant lock, chain lock, with/without wait)
- iOS 13.0+
- Xcode 12.0+
- Swift 5.3+
- Build the Rust library:
cd packages/swift-sdk
cargo build --release- The build will generate a static library that can be linked with your iOS project.
- Add the generated library to your Xcode project
- Import the Swift module:
import SwiftDashSDKswift_dash_identity_fetch- Fetch an identity by IDswift_dash_identity_get_info- Get identity informationswift_dash_identity_put_to_platform_with_instant_lock- Put identity with instant lockswift_dash_identity_put_to_platform_with_instant_lock_and_wait- Put and wait for confirmationswift_dash_identity_put_to_platform_with_chain_lock- Put identity with chain lockswift_dash_identity_put_to_platform_with_chain_lock_and_wait- Put and wait for confirmationswift_dash_identity_transfer_credits- Transfer credits between identities
swift_dash_data_contract_fetch- Fetch a data contract by IDswift_dash_data_contract_create- Create a new data contractswift_dash_data_contract_get_info- Get contract information as JSONswift_dash_data_contract_get_schema- Get schema for a document typeswift_dash_data_contract_put_to_platform- Put contract to platformswift_dash_data_contract_put_to_platform_and_wait- Put and wait for confirmation
swift_dash_document_create- Create a new documentswift_dash_document_fetch- Fetch a document by IDswift_dash_document_get_info- Get document informationswift_dash_document_put_to_platform- Put document to platformswift_dash_document_put_to_platform_and_wait- Put and wait for confirmationswift_dash_document_purchase_to_platform- Purchase document from platformswift_dash_document_purchase_to_platform_and_wait- Purchase and wait for confirmation
swift_dash_sdk_init- Initialize the SDK libraryswift_dash_sdk_create- Create an SDK instanceswift_dash_sdk_destroy- Destroy an SDK instanceswift_dash_sdk_get_network- Get the configured networkswift_dash_sdk_get_version- Get SDK version
swift_dash_signer_create_test- Create a test signer for developmentswift_dash_signer_destroy- Destroy a signer instance
// Initialize the SDK
swift_dash_sdk_init()
// Create SDK configuration
let config = swift_dash_sdk_config_testnet() // or mainnet/local
// Create SDK instance
let sdk = swift_dash_sdk_create(config)
// Create a test signer (for development)
let signer = swift_dash_signer_create_test()
// Clean up when done
defer {
swift_dash_signer_destroy(signer)
swift_dash_sdk_destroy(sdk)
}let identityId = "your_identity_id_here"
if let identity = swift_dash_identity_fetch(sdk, identityId) {
// Get identity information
if let info = swift_dash_identity_get_info(identity) {
print("Balance: \(info.pointee.balance)")
print("Revision: \(info.pointee.revision)")
// Clean up
swift_dash_identity_info_free(info)
}
}var settings = swift_dash_put_settings_default()
settings.timeout_ms = 60000
// Put with instant lock
if let result = swift_dash_identity_put_to_platform_with_instant_lock(
sdk, identity, publicKeyId, signer, &settings
) {
// Process result
let data = Data(bytes: result.pointee.data, count: result.pointee.len)
// Clean up
swift_dash_binary_data_free(result)
}
// Put with instant lock and wait for confirmation
if let confirmedIdentity = swift_dash_identity_put_to_platform_with_instant_lock_and_wait(
sdk, identity, publicKeyId, signer, &settings
) {
// Identity is confirmed on platform
}let recipientId = "recipient_identity_id"
let amount: UInt64 = 50000
if let result = swift_dash_identity_transfer_credits(
sdk, identity, recipientId, amount, publicKeyId, signer, &settings
) {
print("Transferred: \(result.pointee.amount) credits")
print("To: \(String(cString: result.pointee.recipient_id))")
// Clean up
swift_dash_transfer_credits_result_free(result)
}let ownerId = "identity_that_owns_contract"
let schema = """
{
"$formatVersion": "0",
"ownerId": "\(ownerId)",
"documents": {
"message": {
"type": "object",
"properties": {
"content": {
"type": "string",
"maxLength": 280
},
"timestamp": {
"type": "integer"
}
},
"required": ["content", "timestamp"],
"additionalProperties": false
}
}
}
"""
if let contract = swift_dash_data_contract_create(sdk, ownerId, schema) {
// Put contract to platform
if let result = swift_dash_data_contract_put_to_platform(
sdk, contract, publicKeyId, signer, &settings
) {
// Contract deployed
swift_dash_binary_data_free(result)
}
}let contractId = "contract_id_here"
if let contract = swift_dash_data_contract_fetch(sdk, contractId) {
// Get contract information
if let info = swift_dash_data_contract_get_info(contract) {
let infoString = String(cString: info)
print("Contract info: \(infoString)")
free(info)
}
}let documentData = """
{
"content": "Hello, Dash Platform!",
"timestamp": \(Date().timeIntervalSince1970 * 1000),
"author": "dashuser"
}
"""
if let document = swift_dash_document_create(
sdk, contract, ownerId, "message", documentData
) {
// Put document to platform
if let result = swift_dash_document_put_to_platform(
sdk, document, publicKeyId, signer, &settings
) {
// Document created on platform
swift_dash_binary_data_free(result)
}
}let documentType = "message"
let documentId = "document_id_here"
if let document = swift_dash_document_fetch(
sdk, contract, documentType, documentId
) {
// Get document information
if let info = swift_dash_document_get_info(document) {
print("Document ID: \(String(cString: info.pointee.id))")
print("Owner: \(String(cString: info.pointee.owner_id))")
print("Type: \(String(cString: info.pointee.document_type))")
print("Revision: \(info.pointee.revision)")
swift_dash_document_info_free(info)
}
}Configure how state transitions are submitted:
var settings = swift_dash_put_settings_default()
// Timeouts
settings.connect_timeout_ms = 30000 // Connection timeout
settings.timeout_ms = 60000 // Request timeout
settings.wait_timeout_ms = 120000 // Wait for confirmation timeout
// Retry behavior
settings.retries = 3 // Number of retries
settings.ban_failed_address = true // Ban addresses that fail
// Fee management
settings.user_fee_increase = 10 // Increase fee by 10%
// Security
settings.allow_signing_with_any_security_level = false
settings.allow_signing_with_any_purpose = falseThe SDK uses manual memory management. Always free allocated resources:
// Free binary data
swift_dash_binary_data_free(binaryData)
// Free info structures
swift_dash_identity_info_free(identityInfo)
swift_dash_document_info_free(documentInfo)
swift_dash_transfer_credits_result_free(transferResult)
// Free strings
free(cString)
// Destroy handles
swift_dash_sdk_destroy(sdk)
swift_dash_signer_destroy(signer)All functions that can fail return optional values. Always check for nil:
guard let sdk = swift_dash_sdk_create(config) else {
print("Failed to create SDK")
return
}
guard let identity = swift_dash_identity_fetch(sdk, identityId) else {
print("Failed to fetch identity")
return
}The Swift SDK uses compilation verification and Swift integration testing:
# Verify compilation
cargo build -p swift-sdk
# Run unit tests
cargo test -p swift-sdk --lib
# Check symbol exports
nm -g target/debug/libswift_sdk.a | grep swift_dash_For comprehensive testing, integrate the compiled library into an iOS project with XCTest suites.
Here's a complete example:
import SwiftDashSDK
class DashPlatformService {
private var sdk: OpaquePointer?
private var signer: OpaquePointer?
init() {
swift_dash_sdk_init()
let config = swift_dash_sdk_config_testnet()
sdk = swift_dash_sdk_create(config)
signer = swift_dash_signer_create_test()
}
deinit {
if let signer = signer {
swift_dash_signer_destroy(signer)
}
if let sdk = sdk {
swift_dash_sdk_destroy(sdk)
}
}
func createMessage(content: String, authorId: String) async throws {
guard let sdk = sdk, let signer = signer else {
throw DashError.notInitialized
}
// Fetch contract
let contractId = "your_contract_id"
guard let contract = swift_dash_data_contract_fetch(sdk, contractId) else {
throw DashError.contractNotFound
}
// Create document
let timestamp = Int(Date().timeIntervalSince1970 * 1000)
let documentData = """
{
"content": "\(content)",
"timestamp": \(timestamp),
"author": "\(authorId)"
}
"""
guard let document = swift_dash_document_create(
sdk, contract, authorId, "message", documentData
) else {
throw DashError.documentCreationFailed
}
// Put to platform
var settings = swift_dash_put_settings_default()
settings.timeout_ms = 60000
guard let result = swift_dash_document_put_to_platform(
sdk, document, 0, signer, &settings
) else {
throw DashError.platformSubmissionFailed
}
defer { swift_dash_binary_data_free(result) }
// Success!
print("Message created successfully")
}
}
enum DashError: Error {
case notInitialized
case contractNotFound
case documentCreationFailed
case platformSubmissionFailed
}To build the library:
cargo build --release -p swift-sdkThis will generate both static and dynamic libraries that can be linked with iOS applications.
- Build the library using the command above
- Include the generated header file in your Xcode project
- Link against the generated library
- Use the C functions directly from Swift
The underlying FFI is thread-safe, but individual handles should not be shared across threads without proper synchronization.
This SDK follows the same license as the Dash Platform project.