Coder Social home page Coder Social logo

lukereichold / arweave-swift Goto Github PK

View Code? Open in Web Editor NEW
34.0 5.0 5.0 11.08 MB

A lightweight Swift client for the Arweave blockchain

Home Page: https://www.arweave.org/

License: MIT License

Swift 100.00%
arweave arweave-blockchain arweave-permaweb blockchain swift permaweb ios macos share-extension swiftui async-await

arweave-swift's Introduction

Arweave Client SDK for Swift

GitHub license SPM compatible Twitter

A lightweight Swift client for the Arweave blockchain, providing type safety for interacting with the Arweave API

⚠️ This master branch requires requires Xcode 13 / Swift 5.5. For supporting older versions, see the stable branch.

Installation

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

Demo

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.

Usage

Specify Custom Arweave Node (optional)

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.

Wallets and Keys

Creating a Wallet from an existing JWK keyfile

guard let keyFileData = try? Data(contentsOf: keyFileUrl) else { return }

let wallet = Wallet(jwkFileData: keyFileData)

Get a wallet's public address

wallet.address
/// 1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY

Check wallet balance (async)

All wallet balances are returned using winston units.

let balance = try await wallet.balance()

Convert amounts between AR and winston units

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) // ✅

Fetch the last transaction ID for a given wallet (async)

let lastTxId = try await wallet.lastTransactionId()

Transactions

Transactions are the building blocks of the Arweave permaweb. They can send AR between wallet addresses or store data on the Arweave network.

Create a Data Transaction

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)

Create a wallet-to-wallet Transaction

let targetAddress = Address(address: "someOtherWalletAddress")
let transferAmount = Amount(value: 500, unit: .winston)

let transaction = Transaction(amount: transferAmount, target: targetAddress)

Modifying an existing Transaction

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)

Signing and Submitting a Transaction

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()

⚠️ Modifying a transaction object after signing it will invalidate the signature, this will cause it to be rejected by the network if submitted in that state. Transaction prices are based on the size of the data field, so modifying the data field after a transaction has been created isn't recommended as you'll need to manually update the price.

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.

Get a Transaction status (async)

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))

Fetch Transaction content for a given ID (async)

let tx = try await Transaction.find(exampleTxId)

Fetch Transaction data (async)

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)

Contribute

Contributions welcome. Please check out the issues.

arweave-swift's People

Contributors

lukereichold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

arweave-swift's Issues

Creating wallet from existing key

I'm trying to create a Data object based on the private key of an existing wallet that I have. In javascript if I json.parse this file's contents it does create the wallet. However in swift if I use Data(JSONEncoder().encode("{key}")) it exceptions on the Wallet line.

let walletData = try? Data(JSONEncoder().encode("my key")
let wallet = try Wallet(jwkFileData: walletData!) // fails here

Support v2 transaction format

Xcode 12.0 (12A7209)
iOS 14.0 / 13.7
Arweave 1.0.2

I'm not able to upload data over 10MB size. I was told that tx format v2 is able to handle data size over 10MB but there is no mention of it. How should I use v2? Thanks in advance.

'Tag' init error

Xcode 12.0 (12A7209)
iOS 14.0 / 13.7
Arweave 1.0.2

If I use following code from README, I get error Cannot find 'Tag' in scope.
let tag = Tag(name: "myTag", value: "myValue")

When I try following one, I get error Extra arguments at positions #1, #2 in call .
let tag = Transaction.Tag(name: "myTag", value: "myValue")

Support new Wallet creation

As a consumer of this library, I would like to be able to generate new Wallet instances without having to import a JWK keyfile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.