GithubHelp home page GithubHelp logo

dangercz / siaus-ios Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 18.37 MB

Demo project demonstrating downloads & uploads on the Sia network directly from your mobile device.

License: GNU General Public License v3.0

Objective-C 5.52% C 0.32% Swift 94.16%

siaus-ios's Introduction

SiaUs-iOS

This repository demonstrates how to use the us mobile bindings to create a simple iOS app that stores data on Sia. The app scans a file contract from a QR code, connects to a shard server, and uses the Sia renter-host protocol to upload and download an image chosen from the user's photo library.

Prerequisites

This project was created with Xcode 10.2 and doesn't have any pods or dependencies. All resources you need are in the project already, so it works out of the box.

However, in order to use the app, you will need a file contract and a shard server to connect to. The us-bindings repo has instructions for obtaining a file contract. As for the shard server, you can run one yourself or connect to a public instance.

You can also find more details in this article that this project is part of.

Updating and importing the framework

This repo includes Us.framework, which is generated from the us-bindings repo by the gomobile tool. You may want to rebuild this framework if the us mobile bindings are updated with bug fixes or new functionality. To do so, run these commands:

go get -u lukechampine.com/us-bindings/gomobile
gomobile bind -target=ios lukechampine.com/us-bindings/gomobile

This will generate a new Us.framework, which you should place in SiaUs-iOS / SiaUs / Frameworks in Xcode. Alternatively, you can delete the existing framework and drag the new version into Xcode. You will need to do this each time you rebuild the framework.

Running the app

Once you have Us.framework in Xcode, you can build and run the app in simulator. You can then scan the contract QR code you generated previously and enter the address of the shard server you are using. You can also simply hard-code the contract and server values in the app source code, which you can find in Constants.swift.

Next, enter a name for your file, select the image from your library, and hit Upload. After a moment, the upload will complete. You can then download the file, causing it to appear at the bottom of the screen. Note that the app will ignore any touches during the upload/download.

Using the us Bindings

The us bindings do not provide much functionality yet, but they are sufficient for a simple app like this. We need to do three things: import a file contract, connect to hosts, and upload/download a file.

Importing contracts

Contracts are created with the UsContract constructor, which accepts a 96-byte array. Contracts are typically provided as a 192-byte hexadecimal string, so we need to handle that as well:

let contract = UsContract(Data(fromHexEncodedString: contractValue)

Connecting to hosts

A host set is a group of hosts that will collectively store a file. Each host has an associated contract. We also make use of our shard server to resolve the host's public keys to their IP addresses. First, we initialize an empty host set with the address of our shard server:

let hostSet = UsNewHostSet(shardServer, &error)

Then, we connect to the host by adding our contract:

try hostSet?.addHost(contract)

If your app uses multiple contracts, you should call addHost on each of them.

Creating the virtual filesystem

With the host set ready, we can create a virtual filesystem that stores files on Sia. The virtual filesystem needs to store a piece of metadata for each file we upload to Sia, so we pass it a root directory in addition to the host set:

let fileSystem = UsFileSystem.init(rootFolder, hs: host)

This directory should be the app's document directory; otherwise, you may get a permissions error when the filesystem attempts to save its metadata. You will need this metadata to download your files later, so consider syncing it across the user's devices.

It is very important that you call close on the filesystem before quitting the app. This method flushes any unwritten data to Sia hosts and saves the corresponding metadata to local storage. Failing to call close may cause data loss!

Once you have a filesystem, uploading and downloading are straightforward:

try fileSystem?.upload(fileName, data: fileData, minHosts: 1)
let data = try fileSystem?.download(fileName)

The only interesting parameter here is minHosts, which controls the redundancy of the file. If you have six contracts and minHosts = 2, then you will be able to download the file from any two hosts; i.e. the file will have 3x redundancy.

That's it! Now you're ready to start using Sia in your own mobile app. :)

siaus-ios's People

Contributors

dangercz avatar lukechampine avatar

Stargazers

Riccardo Pagnozzi avatar Leo avatar

Watchers

James Cloos avatar  avatar

Forkers

tumbalemark

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.