DeFi Wallet Core is an open-source cross-platform library that implements low-level cryptographic wallet functionality for Cosmos SDK-based (such as Crypto.org Chain) and Ethereum-based (such as Cronos) blockchain networks. It is leveraged in DeFi Wallet-related projects as well as in Cronos Play and other projects. The codebase is primarily in Rust and provides cross-language bindings using multiple tools:
- UniFFI is used to generate bindings for both Kotlin (for Android apps) and Swift (for iOS apps); it could also be used to generate bindings for Python and Ruby.
- wasm-bindgen is used to generate bindings for JavaScript and TypeScript to facilitate interactions with the Wasm modules (for web browser extensions or other web-related apps).
- CXX is used to generate bindings for C++.
Naturally, you will need the Rust toolchain installed. Besides that, for UniFFI language bindings, you will need the corresponding language formatters:
brew install ktlint
brew install swiftformat
For the JavaScript-Wasm bindings, you will need the wasm-pack
to be installed:
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
Depending on your target platform, you may also need additional tooling for a given platform (e.g. Android NDK).
You can generate the language bindings for different platforms using the commands below.
cargo run --features=uniffi-bindgen -p defi-wallet-core-common --bin uniffi-bindgen -- generate common/src/common.udl --config common/uniffi.toml --language kotlin --out-dir bindings/android
cargo run --features=uniffi-bindgen -p defi-wallet-core-common --bin uniffi-bindgen -- generate common/src/common.udl --config common/uniffi.toml --language swift --out-dir bindings/ios
wasm-pack build --scope crypto-com bindings/wasm
cargo run --features=uniffi-bindgen -p defi-wallet-core-common --bin uniffi-bindgen -- generate common/src/common.udl --config common/uniffi.toml --language python --out-dir bindings/python
The single proto-build
crate in this repo clones and rebuilds proto files for
all other crates, simply make the required edits in main.rs, then
cd proto-build # enter `proto-build`, so that we can clone repos and build proto files relative to it
cargo run # build proto files and output to `../proto/src/prost` folder
or simply run make proto
.
Building for Android is currently supported on macOS and Linux.
It requires Android SDK (e.g. via Android Studio), Android NDK (the suggestion version is r22), and Java SDK (e.g. OpenJDK) to be installed.
First, you need to set the required environment variables ANDROID_SDK_ROOT
, NDK_HOME
, and JAVA_HOME
. For example:
export ANDROID_SDK_ROOT={DIR}/Android/sdk
export NDK_HOME=$ANDROID_SDK_ROOT/ndk/22.1.7171670
export JAVA_HOME={DIR}/openjdk/17.0.1_1/
Then you can build the project as follows:
make android
Building for iOS is currently supported on macOS.
It requires Xcode 13 or newer to be installed.
Then you can build the project as follows:
make ios
Build bindings/cpp
and example/cpp_example
as follows:
make build_cpp
Build bindings/cpp
and example/cpp_example
, and run the example programs. Please note that errors could happen if devnets (chainmain and cronos) are not running.
make cpp
There are two sample programs for a web app as well as for a web browser extension.
This sample shows how the generated package can be used in JavaScript.
- generate the bindings as above
cd example/js-example
npm install
npm start
This sample shows how the generated package can be used in a web browser extension.
- make wasmweb
cd example/extension-example
npm install
There are several unit and integration tests for different target platforms. Here are the instructions how to run them.
Please install pystarport
, supervisord
, chain-maind v3.3.3
or newer before running this test.
make wasm-tests
Please install nix
before running this test.
make wasm-ci-tests
The recommended way to use wasm-bindgen-test
is with wasm-pack
, since it will handle installing the test runner, installing a WebDriver client for your browser, and informing cargo how to use the custom test runner. However, you can also manage those tasks yourself, if you wish.
It is also possible to use cargo test
instead of wasm-pack
-
Install wasm-bindgen-cli
cargo install wasm-bindgen-cli
-
Install chromdriver (for chrome)
brew install --cask chromedriver
-
Run the test in binding/wasm folder
cargo test
make cpp-ci-tests # build bindings and examples, then run the test
# or
make cpp-tests # run the test without building bindings and examples
Please install nix
before running this test.
make python-tests
make cpp-docs
For a more detailed setup, check the DeFi Wallet Core Cpp Documents Generation Guide