Giter Site home page Giter Site logo

trustsdk-ios's Introduction

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.

trustsdk-ios's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

trustsdk-ios's Issues

no such module

User reported:

thanks for TrustSDK this is great, want to integrate it in iOS project but got problems like no TrustSDK "no such module", have tried all type of general solution but no luck,

followed this link : https://github.com/TrustWallet/TrustSDK-iOS

please help me how can i integrate it in my project (need steps).

Pod::Spec.new do |s| s.name = 'TrustSDK' s.version = '1.4.7' s.summary = 'Trust Wallet SDK' s.homepage = 'https://github.com/TrustWallet/TrustSDK-iOS' s.license = { :type => 'MIT', :file => 'LICENSE' } s.authors = { 'Leone Parise' => 'leoneparise', 'Viktor Radchenko' => 'vikmeup' } s.source = { :git => 'https://github.com/TrustWallet/TrustSDK-iOS.git', :tag => s.version.to_s } s.ios.deployment_target = '11.0' s.swift_version = '5.1' s.default_subspec = 'Client' s.subspec 'Client' do |cs| cs.resource_bundles = { 'TrustSDK' => ['TrustSDK/Resources/**/*.xcassets', 'TrustSDK/Resources/**/*.strings'] } cs.source_files = 'TrustSDK/Classes/Client/**/*' cs.dependency 'TrustWalletCore/Types' cs.dependency 'BigInt' end s.subspec 'Wallet' do |cs| cs.source_files = 'TrustSDK/Classes/Wallet/**/*' cs.dependency 'TrustSDK/Client' end end

.

HH

Impossible to verify the signature

Good afternoon.

I've tried to verify the signature, but, unfortunately, I couldn't do that (I get 'false').

Signature created by: Trust.signMessage()

I've used this approach for verifying:

let hash = Crypto.hash(messageData)
let isValid = Crypto.verify(signature: signed, message: hash, publicKey: myWalletAddress.data(using: .utf8)!)

And two more things:

  1. signature verification is success on the https://etherscan.io/verifySig
  2. signatures, created for the same message on Android and iOS, are different

Questions on the functional

Good afternoon.

Tell me, please, how can I decode signedTransaction data to Transaction object?

On Android I've done like this:

val tx = Geth.newTransactionFromRLP(Numeric.hexStringToByteArray(response.result))

Thank you in advance.

pod install giving issue.

On pod install getting error : "Unable to find a specification for TrezorCrypto (~> 0.0.8) depended upon by TrustCore"

Confirming transaction problem.

The DApp browser has been removed from the iOS version of Trust Wallet, so I went through all the steps to connect to pancakeswap on safari web browser to my trust wallet, but when I swap tokens on exchange.pancakeswap.finance it takes me back to my trust wallet to confirm the transaction, but there’s no option it gives me to confirm the transaction, it just takes me to my trust wallet main page. Please help.
Thank you.

EthereumTransaction.ContractGeneric example

Hello!

Trying to sign eth contract method call with TrustSDK/WalletCore, then try to sign this data with sdk_sign URL Scheme. TrustWallet responds with the sign_error. Any example of how we can sign a contract call correctly?

    let signerInput = EthereumSigningInput.with {
      $0.toAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
      $0.chainID = BigInt("1").serialize()!
      $0.nonce = BigInt("477").serialize()!
      $0.gasPrice = BigInt("2112000000").serialize()!
      $0.gasLimit = BigInt("21000").serialize()!
      $0.privateKey = wallet.getKeyForCoin(coin: .ethereum).data
      $0.transaction = EthereumTransaction.with {
        $0.contractGeneric = EthereumTransaction.ContractGeneric.with {
          $0.data = Data(hexString:"ad5c4648")!
          $0.amount = BigInt("0").serialize()!
        }
      }
    }

If we use erc20 transfer – it's all ok, but no success with ContractGeneric.

error: `'init' is inaccessible due to 'internal' protection level`

I am using TrustWalletCore(wallet-core) instead of TrustCore(trust-core) in TrustSDK-iOS

In TrustCore use EthereumTransaction for Transaction.
In TrustWalletCore I use EthereumSigner for Transaction.

Following is my code

TrustSDKObjC.swift

@objc
  public func signTransaction(_ gasPrice: String, _ gasLimit: UInt64, _ address: String, amount: String, success: @escaping (Data) -> Void, failure: @escaping (NSError) -> Void) {
    /*
      let transaction = EthereumTransaction(
          nonce: 0,
          gasPrice: BigInt(gasPrice)!,
          gasLimit: BigInt(gasLimit),
          to: EthereumAddress(string: address)!,
          amount: BigInt(amount)!,
          payload: .none
      )
      */
      
      let transaction = EthereumSigner (
          gasPrice: gasPrice.data(using: .utf8)!,
          gasLimit: gasLimit,
          to: EthereumAddress(string: address)!,
          amount: amount.data(using: .utf8)!,
          payload: .none
      )

      signTransaction(transaction) { result in
          switch result {
          case .success(let signedMesssage):
              success(signedMesssage)
          case .failure(let error):
              failure(error as NSError)
          }
      }
  }

But getting following error

Sources/TrustSDK/ObjCWrapper/TrustSDKObjC.swift

error: 'init' is inaccessible due to 'internal' protection level
note: 'init' declared here

Compile errors ... EthereumTransaction, EthereumAddress

Hi.... thanks for making this library...

In the example project, I get the following compile errors:

/TrustSDK-iOS/Sources/TrustSDK/Commands/SignTransactionCommand.swift:15:29: Use of undeclared type 'EthereumTransaction'
/TrustSDK-iOS/Sources/TrustSDK/Commands/SignTransactionCommand.swift:30:30: Use of undeclared type 'EthereumTransaction'
/TrustSDK-iOS/Sources/TrustSDK/Commands/SignTransactionCommand.swift:78:48: Use of undeclared type 'EthereumTransaction'
/TrustSDK-iOS/Sources/TrustSDK/ObjCWrapper/TrustSDKObjC.swift:27:27: Use of unresolved identifier 'EthereumTransaction'
/TrustSDK-iOS/Sources/TrustSDK/ObjCWrapper/TrustSDKObjC.swift:31:17: Use of unresolved identifier 'EthereumAddress'
/TrustSDK-iOS/Sources/TrustSDK/ObjCWrapper/TrustSDKObjC.swift:48:65: Use of unresolved identifier 'EthereumAddress'

Wallet Connect doesn't work properly for IOS

Description
Although Trust has Wallet connect (nice!), it doesn't work if you close the app.

When the Wallet Trust App is in the background or closed, and you try to use the Dapp, the Dapp will send the transaction to be signed by the wallet. The issue is that if the app is not opened this message is lost. Furthermore, if you open later Trust wallet Dapp on top of not getting the messages sent while it was inactive, it doesn't get any new messages either.

My best guess is that, once the websocket is broken, it doesn't recover and recreate a new websocket using the topic id.

Other wallets, like Metamask allow the app to be in the background

How to reproduce

  1. Go to any Dapp, like Uniswap
  2. Connect using Wallet Connect
  3. Try to swap 2 tokens, see how the transaction is shown in Trust ✅
  4. Close the app
  5. Open the app, see how the transaction is NOT shown in Trust automatically ❌
  6. Try to swap 2 tokens again, see how the transaction is NOT shown in Trust

Incorrect assign to transaction payload field

Hi, I have an issue when I try to send transaction with a "payload" field. For Example, when I send transaction with some data to TrustWallet:

var transaction = Transaction(gasPrice: BigInt(21), gasLimit: 21000, to: address)
transaction.amount = amount
transaction.payload = Data(hexString: "0x8f834227000000000000000000000000000000005224") //some data

trustSDK.signTransaction(transaction) { [weak self] signedTransaction in
    let alert = UIAlertController(title: "Signed Transaction", message: signedTransaction.hexString, 
        preferredStyle: .alert)
    alert.addAction(.init(title: "OK", style: .default, handler: nil))
    self?.present(alert, animated: true, completion: nil)
}

it doesn't display at screen:
image

I think it happened because transaction data is base64 encoded string and TrustWalletSDK can't parse it correctly. If change this code (in source TrustWalletSDK - > TrustWalletSDK.swift -> handleSignTransaction function):

 transaction.payload = components.queryParameterValue(for: "data").flatMap({ Data(hexString: $0) })

for something like this:

let data = components.queryParameterValue(for: "data").flatMap({ String($0) })!
let dataEncoded = Data(base64Encoded: data)!
transaction.payload = dataEncoded

now it's work correct:
image

!

!

Not working as expected

The SDK implementation or even the example is not working on my iPhone XR. It opens the Trust Wallet app but does nothing.

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.