/web3swift

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

Primary LanguageJavaScriptApache License 2.0Apache-2.0

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.


Ready Features

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

  • 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)
  • RLP encoding

  • Batched requests in concurrent mode

  • Base58 encoding scheme

  • Formatting to and from Ethereum Units

  • Comprehensive Unit and Integration Test Coverage

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

Requirements

  • iOS 9.0+ / macOS 10.11+
  • Xcode 9.0+
  • Swift 4.1+

Migration Guides

Communication

When using this lib, please make references to this repo and give your start! :) Nothing makes developers happier than seeing someone else use our work and go wild with it.

If you are using web3swift in your app or know of an app that uses it, please add it to this list.

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 'web3swift'
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:

github "matterinc/web3swift" "carthage"

Run carthage update to build the framework and drag the built web3swift.framework into your Xcode project.

Example Project

You can try lib by running the example project:

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

Popular questions

Is it possible to get 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 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
}

What's next

  • R-Token (Smart Contracts for applying regulatory compliance to tokenized securities issuance and trading)
  • SRC-20 (Swarm protocol that enables the tokenization of assets on the blockchain - Security Tokens)
  • 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)
  • [Objective-C] - a proxy bridge to build your DApp on Objective-C using web3swift
  • [Performance Improvements]
  • [More convenient methods for basic namespaces]
  • Complete Documentation

Credits

Alex Vlasov, @shamatar, alex.m.vlasov@gmail.com

Petr Korolev, @skywinder

Security Disclosure

If you believe you have identified a security vulnerability with web3swift, you should report it as soon as possible via email to Alex Vlasov alex.m.vlasov@gmail.com. Please do not post it to a public issue tracker.

Donations

The Matters 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.

Our Ether wallet address: 0xe22b8979739d724343bd002f9f432f5990879901

Donate

License

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