Skip to content
🎉 Welcome to the new Aptos Docs! Click here to submit feedback!

Swift SDK

There is a Swift SDK for Aptos, built by Alcove here

Installing the Swift SDk

.package(url: "https://github.com/ALCOVE-LAB/aptos-swift-sdk.git", branch: "main")

Using the Swift SDk

Creating a client

You can create a client by importing the aptos-swift-sdk, and createing a Client

import Aptos
 
let client = Aptos(aptosConfig: .devnet)
 

You can configure the network with the AptosConfig.Network, or use a preexisting AptosConfig.devnet, AptosConfig.testnet, or AptosConfig.mainnet

Creating a private key

You can create a new Ed25519 account’s private key by calling Account.generate().

let account = Account.generate()

Derive from private key

 
let privateKey = try Ed25519PrivateKey("myEd25519privatekeystring")
// or
let singleKeyPrivateKey = try Secp256k1PrivateKey(Secp256k1.privateKey)
 
let newAccount: Account.Ed25519Account = try Account.fromPrivateKey(privateKey)
let singleKeyAccount: Account.SingleKeyAccount = try Account.fromPrivateKey(singleKeyPrivateKey)

Derive from path

let path = "m/44'/637'/0'/0'/1"
let mnemonic = "various float stumble..."
let newAccount = try Account.fromDerivationPath(Wallet.path, mnemonic: Wallet.mnemonic)

Funding accounts

You can create and fund an account with a faucet on any network that is not mainnet

let account = Account.generate()
let txn = try await client.faucet.fundAccount(accountAddress: account.accountAddress, amount: 100_000_000)

Sending a transaction

You can send a AptosCoin via a transaction

let txn: TransactionResponse
let senderAccount = Account.generate()
_ = try await aptos.faucet.fundAccount(accountAddress: senderAccount.accountAddress, amount: 100_000_000)
let bob = Account.generate()
// Build transaction
let rawTxn = try await aptos.transaction.build.simple(
    sender: senderAccount.accountAddress,
    data: InputEntryFunctionData(
        function: "0x1::aptos_account::transfer",
        functionArguments: [bob.accountAddress, 100]
    )
)
// Sign 
let authenticator = try await aptos.transaction.sign.transaction(
    signer: senderAccount,
    transaction: rawTxn
)
// Submit 
let response = try await aptos.transaction.submit.simple(
    transaction: rawTxn,
    senderAuthenticator: authenticator
)
// Wait
txn = try await aptos.transaction.waitForTransaction(transactionHash: response.hash)
// Read
let transaction = try await aptos.transaction.getTransactionByHash(txn.hash)
 

Testing

To run the SDK tests, simply run from the root of this repository:

Note: for a better experience, make sure there is no aptos local node process up and running (can check if there is a ?process running on port 8080).

swift test