Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

trustwallet/trustsdk-react-native

Repository files navigation

@trustwallet/rn-sdk

npm version CI

@trustwallet/rn-sdk is Trust Wallet's react native SDK, it allows you to request accounts, sign messages and transactions.

Installation

npm i @trustwallet/rn-sdk @trustwallet/wallet-core

Configuring Android

Make sure you have set up intent-filter for your app (documentation here)

The example app settings:

<activity
  android:name=".MainActivity"
  android:launchMode="singleTask"
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="trust-rn-example"/>
    </intent-filter>
</activity>

Configuring iOS

Make sure you have set up url scheme for your app (Open Xcode an click on your project. Go to the 'Info' tab and expand the 'URL Types' group).

The example app settings:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>trust-rn-example</string>
    </array>
  </dict>
</array>
// iOS 9.x or newer
#import <React/RCTLinkingManager.h>

- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

// If your app is using Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
}

Example

Checkout the example typescript project in example folder.

git clone git@github.com:TrustWallet/react-native-trust-sdk.git
cd react-native-trust-sdk/example
npm install && npm start

Run iOS

react-native run-ios

Run Android

react-native run-android
iOS demo Android tx demo
demo ios gif demo android gif

Usage

import the package:

import TrustWallet, {CoinType} from '@trustwallet/rn-sdk'

initialize an instance, e.g. in componentDidMount:

const wallet = new TrustWallet('<your_app_scheme>://');

request ETH/BNB accounts:

wallet.requestAccounts([CoinType.ethereum, CoinType.binance])
.then((accounts) => {
  Alert.alert('Accounts', accounts.join('\n'))
}).catch(error => {
  Alert.alert('Error', JSON.stringify(error))
})

sign an Ethereum message:

const message = utils.keccak256(this.ethereumMessage("Some message"))
wallet.signMessage(message, CoinType.ethereum)
.then((result) => {
  Alert.alert('Signature', result)
}).catch(error => {
  Alert.alert('Error', JSON.stringify(error))
})

sign an Ethereum transaction:

// tx should comply TW.Ethereum.Proto.ISigningInput from @trustwallet/wallet-core
const tx = {
  toAddress: '0x728B02377230b5df73Aa4E3192E89b6090DD7312',
  chainId: Buffer.from('0x01', 'hex'),
  nonce: this.serializeBigInt('447'),
  gasPrice: this.serializeBigInt('2112000000'),
  gasLimit: this.serializeBigInt('21000'),
  amount: this.serializeBigInt('100000000000000')
}
wallet.signTransaction(tx, CoinType.ethereum, send)
.then(result =>{
  Alert.alert('Transaction', result)
}).catch(error => {
  Alert.alert('Error', JSON.stringify(error))
})

clean up all resolve handlers, e.g. incomponentWillUnmount:

wallet.cleanup();

Contributing

You are welcome! Create pull requests and help to improve the package.

License

MIT