• This repository has been archived on 13/Aug/2021
  • Stars
    star
    226
  • Rank 176,514 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

No longer maintained. https://community.trustwallet.com/t/trustsdk-is-discontinued/213116

TrustSDK

Version License Platform TrustSDK CI

Getting Started

The TrustSDK lets you sign Ethereum transactions and messages so that you can bulid a native DApp without having to worry about keys or wallets. Follw these instructions to integrate TrustSDK in your native DApp.

Demo

Sign Message and Transaction

Installation

TrustSDK is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TrustSDK'

Run pod install.

Configuration

Follow the next steps to configure TrustSDK in your app.

Schema Configuration

Open Xcode an click on your project. Go to the 'Info' tab and expand the 'URL Types' group. Click on the + button to add a new scheme. Enter a custom scheme name in 'URL Scemes'.

Adding a scheme

Initialization

Open AppDelegate.swift file and initialize TrustSDK inapplication(_:didFinishLaunchingWithOptions:) method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    TrustSDK.initialize(with: TrustSDK.Configuration(scheme: "trustexample"))
    return true
}

Handling Callbacks

Let TrustSDK capture deeplink responses by calling TrustSDK in application(_:open:options:) method:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  return TrustSDK.application(app, open: url, options: options)
}

API

To use TrustSDK you have to import TrustSDK and TrustWalletCore modules.

Sign Transaction

TrustSDK comes with an easy to use generic API to sign transactions. Each blockchain accept a SigningInput object and respond with a SigningOutput that can be broadcasted directly to the node. Each input and output object is a Swift implementation of wallet-core's protobuf messages. To sign an Ethereum transaction you have the following SigningInput:

let input = EthereumSigningInput.with {
    $0.toAddress = "0x3D60643Bf82b928602bce34EE426a7d392157b69"
    $0.chainID = BigInt("1").serialize()!
    $0.nonce = BigInt("464").serialize()!
    $0.gasPrice = BigInt("11500000000").serialize()!
    $0.gasLimit = BigInt("21000").serialize()!
    $0.amount = BigInt("1000000000000000").serialize()!
}

TrustSDK comes with some handy extensions to handle Data and BigInt serialization with ease.

Once you have the input defined, you just have to call the blockchain signer to sign the transaction:

TrustSDK.signers.ethereum.sign(input: input) { result in
  switch result {
  case .success(let output):
      // Handle the signing output
  case .failure(let error):
      // Handle failres like user rejections
  }
}

Sign Messages

To request signing message, you have to encode or hash your message in hex-encoded format first, and then call sign(message:) from TrustSDK.signers, below is an Ethereum example message:

let data = Data("Some message".utf8)
let message = Data("\u{19}Ethereum Signed Message:\n\(data.count)".utf8) + data
let hash = message.sha3(.keccak256)
TrustSDK.signers.ethereum.sign(message: hash) { result in
    switch result {
    case .success(let signature):
        // Handle the signature
    case .failure(let error):
        // Handle failure
    }
}

Get Addresses

To get users addresses, you just need to call getAccounts(for:) directly from TrustSDK and pass an array of CoinType:

TrustSDK.getAccounts(for: [.ethereum, .bitcoin]) { result in
    switch result {
    case .success(let addresses):
        // Handle the address array
    case .failure(let error):
        // Handle failure
    }
}

Wallet Developers

If your wallet already uses TrustWalletCore and want to integrate with TrustSDK you just need to follow the steps below:

Install WalletSDK

Add the following line to your Podfile:

pod 'TrustSDK/Wallet'

Run pod install.

Handling TrustSDK Commands

Import TrustSDK and implement WalletSDKRequestHandler.handle(request:callback:). Commands must handled asyncronously, once finished, your implementation have to call the callback parameter with the command's response.

class WalletSDKRequestHandlerImplementation: WalletSDKRequestHandler {
  func handle(request: WalletSDK.Request, callback: @escaping ((WalletSDK.Response) -> Void)) {
    switch request.command {
    case .getAccounts(let coins):
      // Handle get accoutns command
      let accounts = ...
      callback(.accounts(accounts))
    case .sign(let coin, let input):
      // Handle sign command
      let output = ...
      callback(.sign(coin: coin, output: output))
    }    
    // You can respond with a failure response in case of exception
    callback(.failure(.unknown))
  }
}

On your app initialization method, set the handler implemention WalletSDK.handler then let WalletSDK handle deeplinks by calling it in application(_:open:options:) method:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  return WalletSDK.application(app, open: url, options: options)
}

If you have your app already handles deeplinks, or you have to parse WalletSDK.Request struct by yourself and dispatch is using WalletSDK.dispatch(request:) method.

Supporting Your Wallet

Once you have WalletSDK configured for your wallet, tell dApp developers to set thewalletApp attribute in TrustSDK.Configureation with your wallet's scheme and installURL:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let wallet = WalletApp(
      scheme: "walletscheme",
      installURL: URL(string: "https://apps.apple.com/app/walletapp")!
    )
    TrustSDK.initialize(with: TrustSDK.Configuration(scheme: "trustexample", walletApp: wallet))
    return true
}

Example

Trust SDK includes an example project with the above code. To run the example project clone the repo and run pod install from the Example directory. Open TrustSDK.xcworkspace and run. Make sure that you have Trust Wallet installed on the device or simulator to test the full callback flow.

Author

  • Leone Parise
  • Viktor Radchenko

License

TrustSDK is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

assets

A comprehensive, up-to-date collection of information about several thousands (!) of crypto tokens.
Go
4,539
star
2

wallet-core

Cross-platform, cross-blockchain wallet library.
C++
2,767
star
3

trust-wallet-ios

πŸ“± Trust - Ethereum Wallet and Web3 DApp Browser for iOS
Swift
1,520
star
4

trust-web3-provider

Web3 javascript wrapper provider for iOS and Android platforms.
TypeScript
747
star
5

trust-wallet-android-source

Trust - Ethereum Wallet for Android (Inactive Repository)
Java
385
star
6

blockatlas

Clean and lightweight cross-chain transaction API
Go
368
star
7

tokens

Upload yours token, coin and dApp image to get displayed in the Trust Wallet
JavaScript
345
star
8

developer

Trust Developer documentation: developer.trustwallet.com
337
star
9

trust-ray

☁️ API for the Trust Wallet. Project no longer supported and current version used as source of transactions and address tokens in Blockatlas https://github.com/trustwallet/blockatlas/blob/master/config.yml#L64
TypeScript
185
star
10

TrustSDK-Android

No longer maintained. https://community.trustwallet.com/t/trustsdk-is-discontinued/213116
Kotlin
152
star
11

dapps-browser

DApps browser
JavaScript
131
star
12

trust-keystore

A general-purpose Ethereum keystore for managing wallets.
Swift
119
star
13

wallet-connect-swift

WalletConnect Swift client SDK
Swift
102
star
14

go-libs

Go
91
star
15

wallet-connect-kotlin

Kotlin
90
star
16

watchmarket

Watchmarket is an aggregation and caching service for blockchain market information
Go
86
star
17

trustsdk-react-native

No longer maintained. https://community.trustwallet.com/t/trustsdk-is-discontinued/213116
TypeScript
82
star
18

trust-core

DEPRECATED. Please use wallet-core
Swift
70
star
19

go-primitives

Go
59
star
20

Web3View

Java
52
star
21

trust-api

TypeScript
30
star
22

barz

A Secure, Modular, Upgradeable Smart Contract Wallet enabling mass adoption of Web3
TypeScript
28
star
23

ens-coincodec

Go
25
star
24

wizard-sdk

Wizard-SDK, a TypeScript software development kit, is designed to visualize diverse protocol EIP-712 messages and simulate transactions that users sign daily in the web3.0 environment.
TypeScript
25
star
25

ansible-collection-blockchain

Trust Wallet's Ansible Galaxy Collection of roles to configure blockchain nodes.
Jinja
23
star
26

web-core

TypeScript
16
star
27

dokka

WalletCore Kotlin API documentation
Shell
13
star
28

assets-go-libs

Go
12
star
29

web3-react-trust-wallet

TypeScript
10
star
30

trezor-crypto-ios

πŸ“™ Heavily optimized cryptography algorithms for embedded devices, packaged for iOS.
Ruby
9
star
31

docc

WalletCore Swift API documentation
Shell
2
star
32

multiversx

MultiversX metadata
2
star
33

solana-wallet-standard

2
star