Code Monkey home page Code Monkey logo

web3.swift's Introduction

web3.swift: Ethereum API for Swift

Installation

CocoaPods

Add web3.swift to your Podfile:

pod 'web3.swift'

Then run the following command:

$ pod install

Usage

Getting Started

Create an instance of EthereumAccount with a EthereumKeyStorage provider. This provides a wrapper around your key for web3.swift to use. NOTE We recommend you implement your own KeyStorage provider, instead of relying on the provided EthereumKeyLocalStorage class. This is provided as an example for conformity to the EthereumKeyStorageProtocol.

import web3


let keyStorage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "MY_PASSWORD")

Create an instance of EthereumClient. This will then provide you access to a set of functions for interacting with the Blockchain.

guard let clientUrl = URL("https://an-infura-or-similar-url.com/123") else { return }
let client = EthereumClient(url: clientUrl)

You can then interact with the client methods, such as to get the current gas price:

client.eth_getPrice { (error, currentPrice) in
    print("The current gas price is \(currentPrice)")
}

For more advanced use, you will find support for smart contract parsing. This can be from an ABI JSON definition (e.g. ENS), or by creating strictly typed functions and responses (e.g. ERC20).

Running Tests

The tests will all pass when running against Ropsten. You will need to provide a URL for the blockchain proxy (e.g. on Infura), and a key-pair in TestConfig.swift. Some of the account signing tests will fail, given the signature assertions are against a specific (unprovided) key.

Dependencies

We built web3.swift to be as lightweight as possible. However, given the cryptographic nature of Ethereum, there's a couple of reliable C libraries you will find packaged with this framework:

  • keccac-tiny: An implementation of the FIPS-202-defined SHA-3 and SHAKE functions in 120 cloc (156 lines).
  • secp256k1: For EC operations on curve secp256k1.
  • Tiny AES: A small and portable implementation of the AES ECB, CTR and CBC encryption algorithms.

We also use Apple's own CommonCrypto (via this method) and BigInt via CocoaPod dependency.

Todos

There's some features that have yet to be fully implemented! Not every RPC method is currently supported, and here's some other suggestions we would like to see in the future:

  • ABI encoding support for tuples and arrays
  • Batch support for JSONRPC interface
  • Use a Hex struct for values to be more explicit in expected types
  • Use Truffle for running tests
  • Add support for Swift Package Manager
  • Bloom Filter support
  • Full ERC20 token support of totalSupply, allowance, transfer, approve, transferFrom, and Transfer/Approval events

Contributors

The initial project was crafted by the team at Argent. However, we encorage anyone to help implement new features and to keep this library up-to-date. For features and fixes, simply submit a pull request to the develop branch. Please follow the contributing guidelines.

For bug reports and feature requests, please open an issue.

License

Released under the MIT license.

web3.swift's People

Contributors

darthmike avatar th3m477 avatar gergold avatar

Watchers

Hwee-Boon Yar avatar

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.