Skip to content

Resources

Baah Kusi edited this page Oct 21, 2021 · 3 revisions

TrustDart

A flutter wallet library based on trust wallet core. This document will serve as log to capture all research, findings and decisions made in the library.

Overview & Resources

Building flutter plugins tutorial lists

Initializing the Project.

  • In dart this project is a plugin. See the difference here πŸ‘‰ https://flutter.dev/docs/development/packages-and-plugins/using-packages.
  • A git repository was already created for the project.
  • Hence to initialize it, the following command was used.
  • flutter create --org africa.ejara --template=plugin --platforms=android,ios --android-language kotlin --ios-language swift --project-name trustdart .

Adding trust wallet core dependencies

Trust wallet usefull links

Write platform specific code for both ios and android

The plugin uses;

Android NDK Setup

Api of Wallet Library

Here we describe the api of the trustdart wallet library. Basically any crypto has the following functionalities;

  • Wallet management
    • Creating a new multi-coin wallet
    • Importing a multi-coin wallet
  • Address derivation (receiving)
    • Generating the default address for a coin
    • Generating an address using a custom derivation path (expert)
  • Transaction building (e.g. for sending)

Hence from these the following methods would be exposed

  • createWallet
  • importWalletFromMnemonic
  • generateAddressForCoin
  • validateAddressForCoin
  • buildAndSignTransaction

CoinType Functions Hints

The trustwallet library seems to have a cointype object that when imported one can access all coins. For example here is how its imported in kotlin android https://github.com/trustwallet/wallet-core/blob/4a4eb20f7f6f21ea92cec5736dc46544ba2a6685/samples/android/app/src/main/java/com/trust/walletcore/example/MainActivity.kt#L8 , and here is an example usage https://github.com/trustwallet/wallet-core/blob/4a4eb20f7f6f21ea92cec5736dc46544ba2a6685/samples/android/app/src/main/java/com/trust/walletcore/example/MainActivity.kt#L62 .

This file here https://github.com/trustwallet/wallet-core/blob/master/src/Coin.cpp contains methods one can call directly on the cointype, for example deriveAddress https://github.com/trustwallet/wallet-core/blob/4a4eb20f7f6f21ea92cec5736dc46544ba2a6685/src/Coin.cpp#L208 . For example CoinType.BITCOIN.deriveAddress.

Sometime it the above rule does not apply. For example with validateAddress https://github.com/trustwallet/wallet-core/blob/4a4eb20f7f6f21ea92cec5736dc46544ba2a6685/src/Coin.cpp#L208 would be invoked in kotlin as CoinType.BITCOIN.validate. Basically just try the first word or the sensible ones.

Kotlin Reflection

Since there is no formal documentation, one has to resort to means like reflection to determine all the members of a class.

Just add the following dependency to your build.gradle file

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

Now you can use reflection during development just to figure out members of an object. Here is an example to figure out members of PrivateKey class.

val privateKey = wallet.getKey(CoinType.TEZOS, path)
val opJson =  JSONObject(txData).toString();
println(privateKey::class.members)
for (prop in privateKey::class.members) {
  println("${prop.name}")
}

Here is the output

I/System.out(13648): [fun wallet.core.jni.PrivateKey.sign(kotlin.ByteArray!, wallet.core.jni.Curve!): kotlin.ByteArray!, fun wallet.core.jni.PrivateKey.getPublicKeySecp256k1(kotlin.Boolean): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.signAsDER(kotlin.ByteArray!, wallet.core.jni.Curve!): kotlin.ByteArray!, fun wallet.core.jni.PrivateKey.getPublicKeyEd25519Extended(): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.getPublicKeyEd25519Blake2b(): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.getPublicKeyCurve25519(): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.signSchnorr(kotlin.ByteArray!, wallet.core.jni.Curve!): kotlin.ByteArray!, fun wallet.core.jni.PrivateKey.getPublicKeyNist256p1(): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.data(): kotlin.ByteArray!, fun wallet.core.jni.PrivateKey.getPublicKeyEd25519(): wallet.core.jni.PublicKey!, fun wallet.core.jni.PrivateKey.getSharedKey(wallet.core.jni.PublicKey!, wallet.core.jni.Curve!): kotlin.ByteArray!, var wallet.core.jni.PrivateKey.nativeHandle: kotlin.Long, fun wallet.core.jni.PrivateKey.hashCode(): kotlin.Int, fun wallet.core.jni.PrivateKey.equals(kotlin.Any?): kotlin.Boolean, fun wallet.core.jni.PrivateKey.toString(): kotlin.String, fun createFromNative(kotlin.Long): wallet.core.jni.PrivateKey!, fun isValid(kotlin.ByteArray!, wallet.core.jni.Curve!): kotlin.Boolean, fun nativeCreate(): kotlin.Long, fun nativeCreateCopy(wallet.core.jni.PrivateKey!): kotlin.Long, fun nativeCreateWithData(kotlin.ByteArray!): kotlin.Long, fun nativeDelete(kotlin.Long): kotlin.Unit]
I/System.out(13648): sign
I/System.out(13648): getPublicKeySecp256k1
I/System.out(13648): signAsDER
I/System.out(13648): getPublicKeyEd25519Extended
I/System.out(13648): getPublicKeyEd25519Blake2b
I/System.out(13648): getPublicKeyCurve25519
I/System.out(13648): signSchnorr
I/System.out(13648): getPublicKeyNist256p1
I/System.out(13648): data
I/System.out(13648): getPublicKeyEd25519
I/System.out(13648): getSharedKey
I/System.out(13648): nativeHandle
I/System.out(13648): hashCode
I/System.out(13648): equals
I/System.out(13648): toString
I/System.out(13648): createFromNative
I/System.out(13648): isValid
I/System.out(13648): nativeCreate
I/System.out(13648): nativeCreateCopy
I/System.out(13648): nativeCreateWithData
I/System.out(13648): nativeDelete

For IoS (using xcode) there was no need for this since method completion was working perfectly as well as links to methods. For Android (android studio) method completion wasn't working for some unknown reason.

Build Transactions

  • For android these set of tests provide a good pointer for building transactions

https://github.com/trustwallet/wallet-core/tree/master/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains

  • For ios

https://github.com/trustwallet/wallet-core/tree/master/swift/Tests/Blockchains

Tezos

Ethereum

All Relevant Links