A lightweight Swift client for the Arweave blockchain, providing type safety for interacting with the Arweave API
master
branch requires requires Xcode 13 / Swift 5.5. For supporting older versions, see the stable branch.
To install via Swift Package Manager, add Arweave
to your Package.swift
file. Alternatively, add it to your Xcode project directly.
let package = Package(
...
dependencies: [
.package(url: "https://github.com/lukereichold/arweave-swift.git", from: "1.1.0")
],
...
)
Then import Arweave
wherever you’d like to use it:
import Arweave
See the included demo app, written in SwiftUI, which dynamically creates a Wallet
object from an existing Arweave JWK keyfile and uses an iOS share extension to create and submit a new data transaction containing the data of a given page in Safari.
Arweave.baseUrl = URL(string: "https://myArweaveNode.net:443")!
Craft a custom URL
to specify host, port, and protocol of target node. All subsequent requests will automatically be made to this custom host.
guard let keyFileData = try? Data(contentsOf: keyFileUrl) else { return }
let wallet = Wallet(jwkFileData: keyFileData)
wallet.address
/// 1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY
All wallet balances are returned using winston units.
let balance = try await wallet.balance()
var transferAmount = Amount(value: 1, unit: .AR)
let amtInWinston = transferAmount.converted(to: .winston)
XCTAssertEqual(amtInWinston.value, 1000000000000, accuracy: 0e-12) // ✅
transferAmount = Amount(value: 2, unit: .winston)
let amtInAR = transferAmount.converted(to: .AR)
XCTAssertEqual(amtInAR.value, 0.000000000002, accuracy: 0e-12) // ✅
let lastTxId = try await wallet.lastTransactionId()
Transactions are the building blocks of the Arweave permaweb. They can send AR between wallet addresses or store data on the Arweave network.
Data transactions are used to store data on the Arweave permaweb and can contain any arbitrary data.
let data = "<h1>Hello World!</h1>".data(using: .utf8)!
let transaction = Transaction(data: data)
let targetAddress = Address(address: "someOtherWalletAddress")
let transferAmount = Amount(value: 500, unit: .winston)
let transaction = Transaction(amount: transferAmount, target: targetAddress)
Metadata can be optionally added to transactions through tags, these are simple key/value attributes that can be used to document the contents of a transaction or provide related data.
let tag = Transaction.Tag(name: "myTag", value: "myValue")
transaction.tags.append(tag)
The data and wallet-to-wallet transaction initializers above simply create an unsigned Transaction
object. To be submitted to the network, however, each Transaction
must first be signed.
let transaction = Transaction(data: data)
let signedTx = try await transaction.sign(with: wallet)
try await signed.commit()
The transaction ID is a hash of the transaction signature, so a transaction ID can't be known until its contents are finalized and it has been signed.
let txStatus = try await Transaction.status(of: exampleTxId)
/// Arweave.Transaction.Status.accepted(data: Arweave.Transaction.Status.Data(block_height: 502761, block_indep_hash: "V6pCKSyeQiqICWKM2G_zkQ8SCA_WKnZoVGOD8eKFV_xozoWS9xPFgncxnMWjtFao", number_of_confirmations: 8655))
let tx = try await Transaction.find(exampleTxId)
We can get the transaction data (represented as a base64 URL encoded string) for a given transaction ID without having to fetch the entire Transaction object.
let txData = try await Transaction.data(for: exampleTxId)
Contributions welcome. Please check out the issues.