/web3swift

Elegant Web3js functionality in Swift. Native ABI parsing and smart contract interactions.

Primary LanguageSwiftApache License 2.0Apache-2.0

Important notice:

This library will not be actively updated by its owners in the nearest future.

Hello everyone, I, Anton Grigorev, was the main contributor to this library for a while. However, since June I am too immersed in other projects and cannot actively support this library. I sincerely apologize if this negatively affected your work. If web3swift is important to you and you want to help develop it, I will be happy to review and accept your PRs.

Sincerely, Anton Grigorev (@baldyash)

matter-github-swift

web3swift

Build Status Swift Platform CocoaPods Compatible Carthage Compatible License support Stackoverflow

web3swift is your toolbelt for any kind interactions with Ethereum network.

Design Decisions

  • Not every JSON RPC function is exposed yet, and priority is given to the ones required for mobile devices
  • Functionality was focused on serializing and signing transactions locally on the device to send raw transactions to Ethereum network
  • Requirements for password input on every transaction are indeed a design decision. Interface designers can save user passwords with the user's consent
  • Public function for private key export is exposed for user convenience but marked as UNSAFE_ :) Normal workflow takes care of EIP155 compatibility and proper clearing of private key data from memory

Projects that using web3swift

If you are using this library in your project, please add a link to this repo.

Nothing makes developers happier than seeing someone else use our work and go wild with it.

Installation

  • CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ sudo gem install cocoapods

To integrate web3swift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

target '<Your Target Name>' do
    use_frameworks!
    pod 'web3.swift.pod', '~> 2.2.0'
end

Then, run the following command:

$ pod install
  • Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate web3swift into your Xcode project using Carthage, specify it in your Cartfile. Create an empty Cartfile with the touch command and open it:

$ touch Cartfile
$ open -a Xcode Cartfile

Add the following line to the Cartfile and save it:

github "matter-labs/web3swift" "master"

Run carthage update to build the framework. By default, Carthage performs checkouts and builds in a new directory 'Carthage' in the same location as your Cartfile. Open this directory, go to 'Build' directory, choose iOS or macOS directory and use the framework from the chosen directory in your Xcode project.

Requirements

  • iOS 9.0+ / macOS 10.11+
  • Xcode 10.2+
  • Swift 5.0+

Migration Guides


Documentation

Full source please look at Documentation folder

Here are quick references for basic features:

Example

You can try lib by running the example project:

  • Clone the repo: git clone https://github.com/matter-labs/web3swift.git
  • Move to the repo: cd web3swift/Example/web3swiftExample
  • Install Dependencies: pod install
  • Open: open ./web3swiftExample.xcworkspace

FAQ

Is it possible to get a Mnemonic Phrase (Seed Phrase) from Private key using web3swift?

In web3swift there is no backward conversion from Private key to Mnemonic Phrase. Also it is theoretically impossible to recover a phrase from a Private key. After Seed Phrase is converted to some initial entropy the “master key is derived” and the initial entropy is discarded.

The simplest solution is to encrypt the phrase using users pincode and save it in some other secure keystore. Mnemonic Phrase is very sensitive data and you must be very careful to let the user get it. Our advise if you want to show it to a user - ask to save a Passphrase when creating BIP32Keystore.

How to interact with custom smart-contract with web3swift?

For example: you want to interact with smart-contract and all you know is - its address (address example: 0xfa28eC7198028438514b49a3CF353BcA5541ce1d).

You can get the ABI of your contract directly from Remix IDE (Solution)

Then you should use contract address and ABI in creating contract object. In example we use Infura Mainnet:

let yourContractABI: String = <CONTRACT JSON ABI>
let toEthereumAddress: EthereumAddress = <DESTINATION ETHEREUM ADDRESS>
let abiVersion: Int = <ABI VERSION NUMBER>

let contract = Web3.InfuraMainnetWeb3().contract(yourContractABI, at: toEthereumAddress, abiVersion: abiVersion)

Here is the example how you should call some contract method:

let method: String = <CONTRACT METHOD NAME>
let parameters: [AnyObject] = <PARAMETERS>
let extraData: Data = <DATA>
let transactionOptions: TransactionOptions = <OPTIONS>

let transaction = contract.read(method, parameters: parameters, extraData: extraData, transactionOptions: transactionOptions)

Here is the example how you should send transaction to some contract method:

let method: String = <CONTRACT METHOD NAME>
let parameters: [AnyObject] = <PARAMETERS>
let extraData: Data = <DATA>
let transactionOptions: TransactionOptions = <OPTIONS>

let transaction = contract.write(method, parameters: parameters, extraData: extraData, transactionOptions: transactionOptions)

How to set test local node? You can write something like that:

func setLocalNode(port: Int = 8545) -> Web3? {
    guard let web3 = Web3(url: URL(string: "http://127.0.0.1:\(port)")!) else { return nil }
    return web3
}

Credits

Alex Vlasov, @shamatar

Petr Korolev, @skywinder

Anton Grigorev, @baldyash

What we have already done

  • Swift implementation of web3.js functionality ⚡

  • Interaction with remote node via JSON RPC 💭

  • Local keystore management (geth compatible)

  • Smart-contract ABI parsing 📖

  • ABI deconding (V2 is supported with return of structures from public functions. Part of 0.4.22 Solidity compiler)

  • Ethereum Name Service (ENS) support - a secure & decentralised way to address resources both on and off the blockchain using simple, human-readable names

  • Interactions (read/write to Smart contracts) 🔄

  • Complete Infura Ethereum API support, patial Websockets API support

  • Parsing TxPool content into native values (ethereum addresses and transactions) - easy to get pending transactions

  • Event loops functionality

  • Supports Web3View functionality - WKWebView with injected "web3" provider

  • Possibility to add or remove "middleware" that intercepts, modifies and even cancel transaction workflow on stages "before assembly", "after assembly"and "before submission"

  • Literally following the standards:

    • BIP32 HD Wallets: Deterministic Wallet
    • BIP39 (Seed phrases)
    • BIP44 (Key generation prefixes)
    • EIP-20 (A standard interface for tokens - ERC-20)
    • EIP-67 (Standard URI scheme with metadata, value and byte code)
    • EIP-155 (Replay attacks protection) enforced!
    • EIP-681 (A standard way of representing various transactions, especially payment requests in Ethers and ERC-20 tokens as URLs)
    • EIP-721 (A standard interface for non-fungible tokens, also known as deeds - ERC-721)
    • EIP-165 (Standard Interface Detection, also known as ERC-165)
    • EIP-777 (New Advanced Token Standard, also known as ERC-777)
    • EIP-820 (Pseudo-introspection Registry Contract, also known as ERC-820)
    • EIP-888 (MultiDimensional Token Standard, also known as ERC-888)
    • EIP-1400 (Security Token Standard, also known as ERC-1400)
    • EIP-1410 (Partially Fungible Token Standard, also known as ERC-1410)
    • EIP-1594 (Core Security Token Standard, also known as ERC-1594)
    • EIP-1643 (Document Management Standard, also known as ERC-1643)
    • EIP-1644 (Controller Token Operation Standard, also known as ERC-1644)
    • EIP-1633 (Re-Fungible Token, also known as ERC-1633)
    • EIP-721x (An extension of ERC721 that adds support for multi-fungible tokens and batch transfers, while being fully backward-compatible, also known as ERC-721x)
    • EIP-1155 (Multi Token Standard, also known as ERC-1155)
    • EIP-1376 (Service-Friendly Token, also known as ERC-1376)
    • ST-20 - ST-20 token is an Ethereum-based token implemented on top of the ERC-20 protocol that adds the ability for tokens to control transfers based on specific rules
  • RLP encoding

  • Batched requests in concurrent mode

  • Base58 encoding scheme

  • Formatting to and from Ethereum Units

  • Comprehensive Unit and Integration Test Coverage

Future steps

  • Objective-C - a proxy bridge to build your DApp on Objective-C using web3swift
  • Complete Documentation (https://web3swift.github.io/web3swift)
  • Modularity with the basic Web3 subspec/SPM (the most basic functions like transaction signing and interacting with an http rpc server) and other modules with additional functionality
  • R-Token - Smart Contracts for applying regulatory compliance to tokenized securities issuance and trading
  • Support IPFS via Infura public IPFS gateway
  • Support more blockchains - Ripple, Bitcoin, EOS, etc.
  • Performance Improvements
  • More convenient methods for basic namespaces

Contribute

Donate

Security Disclosure

If you believe you have identified a security vulnerability with web3swift, you should report it as soon as possible via email to hello@matter-labs.io. Please do not post it to a public issue tracker.

Matter Labs are charged with open-sourсe and do not require money for using their web3swift lib. We want to continue to do everything we can to move the needle forward. If you use any of our libraries for work, see if your employers would be interested in donating. Any amount you can donate today to help us reach our goal would be greatly appreciated.

License

web3swift is available under the Apache License 2.0 license. See the LICENSE for details.