Skip to content

Commit

Permalink
Cleanup legacy code and update cordova docs (#219)
Browse files Browse the repository at this point in the history
* mark ffi delete functions as unsafe

* remove wallet rindex module

since it's unused and basically obsolete

* remove legacy unused utxo functionality

* rebuild genesis binary before generating json for cordova

* ignore exist status when deleting the plugins before installing

Helps in the case the script was interrupted after deleting the plugin
but before installing the new version. If there is an error it would
most probably show up in the following install.

* cordova test: rebuild genesis json info before installing

* remove utxo keys from keys.json

* use python3 binary in test script

* remove utxo keys argument from cordova

* remove unused outdated electron/browser cordova platform

* remove outdated getting_started.md file

* update cordova readme

* add updated genesis test vector

* add cbindgen to the cordova requirements

* join the cordova docs in a single readme

* regen wallet.h

* do not require jcli in the ci integration test build

since the tests don't run, there is not much point in regenerating the
assets, and not installing jcli could save some time.
  • Loading branch information
ecioppettini authored Jun 15, 2022
1 parent 432a891 commit f1cd62a
Show file tree
Hide file tree
Showing 52 changed files with 334 additions and 3,349 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ jobs:
--directory test_app \
--platform android \
--no-cargo-build \
--no-regen-test-vectors \
-- full
env:
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- *breaking change*: wallet_spending_counter replaced by wallet_spending_counters
- *breaking change*: wallet_set_state now takes an array of spending counters
- *breaking change* wallet_vote now takes a lane as an argument
- *breaking change*: wallet_vote now takes a lane as an argument
- *breaking change*: wallet_import_keys now takes a single argument with the
account key.

## [0.8.0-alpha1]

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ This includes bindings for Android Kotlin already packaged in a AAR package.
| -------- | :-------: |
| android ||
| ios ||
| electron ||

### iOS

Expand Down Expand Up @@ -91,6 +90,9 @@ You can find the main rust libraries at the top level of this repository. These
are the core elements and offer prime support for all the different `bindings`
implemented in the `bindings` directory.

For the Cordova plugin, check out the [readme in the plugin's
directory](bindings/wallet-cordova/README.md).

## Code formatting

In order to avoid long lasting discussions and arguments about how code should
Expand All @@ -104,9 +106,9 @@ when manipulating raw pointers.

## Documentation

- [Getting Started](doc/getting_started.md)
- [Wallet Cryptography and Encoding](doc/CRYPTO.md)
- [Enhanced Mnemonic Encoding (EME)](doc/EME.md)
- [Cordova plugin](bindings/wallet-cordova/README.md)

[rust-lang]: https://www.rust-lang.org/
[Jörmungandr]: https://input-output-hk.github.io/jormungandr
Expand Down
17 changes: 5 additions & 12 deletions bindings/wallet-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,9 @@ pub type EncryptingVoteKeyPtr = *mut EncryptingVoteKey;
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_import_keys(
account_key: *const u8,
utxo_keys: *const u8,
utxo_keys_len: usize,
wallet_out: *mut WalletPtr,
) -> ErrorPtr {
let r = wallet_import_keys(
account_key,
utxo_keys as *const [u8; 64],
utxo_keys_len,
wallet_out as *mut *mut WalletRust,
);
let r = wallet_import_keys(account_key, wallet_out as *mut *mut WalletRust);

r.into_c_api() as ErrorPtr
}
Expand Down Expand Up @@ -526,7 +519,7 @@ pub unsafe extern "C" fn iohk_jormungandr_wallet_delete_buffer(ptr: *mut u8, len
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub extern "C" fn iohk_jormungandr_wallet_delete_error(error: ErrorPtr) {
pub unsafe extern "C" fn iohk_jormungandr_wallet_delete_error(error: ErrorPtr) {
wallet_delete_error(error as *mut ErrorRust)
}

Expand All @@ -539,7 +532,7 @@ pub extern "C" fn iohk_jormungandr_wallet_delete_error(error: ErrorPtr) {
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub extern "C" fn iohk_jormungandr_wallet_delete_settings(settings: SettingsPtr) {
pub unsafe extern "C" fn iohk_jormungandr_wallet_delete_settings(settings: SettingsPtr) {
wallet_delete_settings(settings as *mut SettingsRust)
}

Expand All @@ -552,7 +545,7 @@ pub extern "C" fn iohk_jormungandr_wallet_delete_settings(settings: SettingsPtr)
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub extern "C" fn iohk_jormungandr_wallet_delete_wallet(wallet: WalletPtr) {
pub unsafe extern "C" fn iohk_jormungandr_wallet_delete_wallet(wallet: WalletPtr) {
wallet_delete_wallet(wallet as *mut WalletRust)
}

Expand All @@ -565,7 +558,7 @@ pub extern "C" fn iohk_jormungandr_wallet_delete_wallet(wallet: WalletPtr) {
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub extern "C" fn iohk_jormungandr_wallet_delete_proposal(proposal: ProposalPtr) {
pub unsafe extern "C" fn iohk_jormungandr_wallet_delete_proposal(proposal: ProposalPtr) {
wallet_delete_proposal(proposal as *mut ProposalRust)
}

Expand Down
2 changes: 0 additions & 2 deletions bindings/wallet-c/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,6 @@ ErrorPtr iohk_jormungandr_wallet_id(WalletPtr wallet,
*
*/
ErrorPtr iohk_jormungandr_wallet_import_keys(const uint8_t *account_key,
const uint8_t *utxo_keys,
uintptr_t utxo_keys_len,
WalletPtr *wallet_out);

/**
Expand Down
38 changes: 0 additions & 38 deletions bindings/wallet-cordova/BUILD.md

This file was deleted.

206 changes: 117 additions & 89 deletions bindings/wallet-cordova/README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,119 @@
# Usage

## Examples

### Wallet conversion

Minimal wallet conversion example in plain javascript

``` js
const mnemonics = 'neck bulb teach illegal soul cry monitor claw amount boring provide village rival draft stone';
const BLOCK0 = hexStringToBytes('0052000000000369000000000000000000000000fd8b6f5c9d824dbaffe3c10435db4c3522323a238fcbd17457569e89dd6dfcd4000000000000000000000000000000000000000000000000000000000000000000a60000000e0088000000005e922c7000410100c200010398000000000000000a000000000000000200000000000000640104000000b401411404040000a8c00208000000000000006402440001900001840000006405810104c800005af3107a40000521020000000000000064000000000000000d0000000000000013000000010000000302e0e57ceb3b2832f07e2ef051e772b62a837f7a486c35e38f51bf556bd3abcd8eca016f00010500000000000f4240004c82d818584283581c0992e6e3970dd01055ba919cff5b670a6813f41c588eb701231e3cf0a101581e581c4bff51e6e1bcf245c7bcb610415fad427c2d8b87faca8452215970f6001a660a147700000000000186a0004c82d818584283581c3657ed91ad2f25ad3ebc4faec404779f8dafafc03fa181743c76aa61a101581e581cd7c99cfa13e81ca55d026fe0395124646e39b188c475fb276525975d001ab75977f20000000000002710002b82d818582183581cadff678b11b127aef0c296e88bfb4769c905284716c23e5d63278787a0001a63f679c70000000000000001004c82d818584283581c4baebf60011d051b02143a3417514fed6f25c8c03d2253025aa2ed5fa101581e581c4bff51e6e1bcf245c7bcb5104c7ca9ed201e1b1a6c6dfbe93eadeece001a318972700000000000000064002b82d818582183581cadff678b11b127aef0c296e88bfb4769c905284716c23e5d63278787a0001a63f679c7014e00010500000000000f4240002b82d818582183581c783fd3008d0d8fb4532885481360cb6e97dc7801c8843f300ed69a56a0001a7d83a21d0000000000002710002b82d818582183581cadff678b11b127aef0c296e88bfb4769c905284716c23e5d63278787a0001a63f679c70000000000000001002b82d818582183581c783fd3008d0d8fb4532885481360cb6e97dc7801c8843f300ed69a56a0001a7d83a21d0000000000000064004c82d818584283581cffd85f20cf3f289fd091e0b033285ecad725496bc57035a504b84a10a101581e581c4bff51e6e1bcf245c7bcb4105299a598c50eabacdd0f72815c016da7001a57f9068f00000000000003f2004c82d818584283581c847329097386f263121520fc9c364047b436298b37c9148a15efddb4a101581e581cd7c99cfa13e81ce17f4221e0aed54c08625a0a8c687d9748f462a6b2001af866b8b9');

function hexStringToBytes(string) {
const bytes = [];
for (let c = 0; c < string.length; c += 2) {
bytes.push(parseInt(string.substr(c, 2), 16));
}
return Uint8Array.from(bytes);
}

document.addEventListener('deviceready', onDeviceReady, false)

function onDeviceReady() {
var lib = cordova.require('wallet-cordova-plugin.wallet');
// or
// var wallet = window.wallet;

function handleError(error) {
console.error('error: ' + error);
}

function getTransactionsFromConversion(conversion) {
lib.conversionTransactionsSize(conversion, function(size) {
const transactions = [];
for (var i = 0; i < size; i++) {
lib.conversionTransactionsGet(conversion, i, function(tx) {
transactions.push(tx);

if (transactions.length == size) {
// do something with the transactions, for example, POST to jormungandr
console.log(transactions);
}
}, handleError)
}
}, handleError)
}

lib.walletRestore(mnemonics, function(wallet) {
lib.walletRetrieveFunds(wallet, BLOCK0, function(settings) {
lib.walletTotalFunds(wallet, function(retrievedFunds) {
console.log('retrieved: ' + retrievedFunds + ' funds from block0');
lib.walletConvert(wallet, settings, getTransactionsFromConversion, handleError)
}, handleError);
}, handleError)
}, handleError);
}
# Getting started

The javascript documentation for this module can be generated with jsdoc by
running:

```bash
npm install
npm run doc
```

The generated files can be found at the `doc` directory, and can be read by
opening `index.html` with a web browser.

At the moment the best source for examples are the javascript
[tests](tests/src/main.js).

# Development

## Getting started

The [official cordova
documentation](https://cordova.apache.org/docs/en/11.x/guide/hybrid/plugins/index.html)
is the best place to start.

## Requirements

### General

As a baseline, Node.js and the cordova cli are required. Since the process of
running the tests involves creating an application. The documentation at
[installing-the-cordova-cli](https://cordova.apache.org/docs/en/11.x/guide/cli/index.html#installing-the-cordova-cli)
can be used as a guide. Check out also the [Android
documentation](https://cordova.apache.org/docs/en/11.x/guide/platforms/android/index.html)
and the [iOS
documentation](https://cordova.apache.org/docs/en/11.x/guide/platforms/ios/plugin.html)
for requirements specific to the platform you are going to be developing for.

Additionally, python3 is required to run the helper scripts.


`jcli` is required to generate the genesis file that it is used in
the test-vectors, installation instructions can be found in the [jormungandr's
repository](https://github.com/input-output-hk/jormungandr). It's recommended
that the `jcli` version is built with the same version of `chain-libs` that is
used to build the plugin (which can be found in the Cargo.lock file), although
it's not strictly necessary as long as the genesis binary encoding is
compatible.

### Android

- [cross](https://github.com/cross-rs/cross) is currently used for building the
native libraries for Android.
- [uniffi-bindgen](https://github.com/mozilla/uniffi-rs). The version must be the same one that is used in the `wallet-uniffi` crate. This can be found [here](../wallet-uniffi/Cargo.toml).


### iOS

The ios rust platforms:

- `rustup target add x86_64-apple-ios`
- `rustup target add aarch64-apple-ios`

[cbindgen](https://github.com/eqrion/cbindgen) is necessary for regenerating the
C header, which is then used from the [Objetive C code](src/ios/WalletPlugin.m) in this package. Since the
latest version is in source control, this is only needed if the core API
changes. The [regen_header.sh](../bindings/wallet-c/regen_header.sh) script can
be used to do this.

## Overview

The core of the plugin is written in rust, and ffi is used to bridge that to
either Objective-C or Kotlin, depending on the platform.

The [wallet.js](www/wallet.js) file has the top level Javascript api for the
plugin users, which is mostly a one-to-one mapping to the API of the
wallet-core rust crate.

The iOS part of the plugin is backed by the [wallet-c](../wallet-c/wallet.h)
package, while the Android support is provided via the
[wallet-uniffi](../wallet-uniffi/src/lib.udl) package. Both are also thin
wrappers over **wallet-core**.

## Build

[build_jni.py](scripts/build_jni.py) in the `scripts` directory will compile the
Android native libraries, generate the Kotlin bindings, and copy those to this
package in the `src/android` directory.

[build_ios.py](scripts/build_ios.py) in the `scripts` directory will compile the
iOS native libraries, and copy those along the C header to this package.

`npm pack` can be used to make a distributable version of the plugin as an npm
package.

## Running the tests

The *tests* directory contains a Cordova plugin with [js
tests](tests/src/main.js), we use
[cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework)
as a test harness.

The [test.py](scripts/test.py) script can be used to build
the plugin and setup the test harness. For example, the following command will

- create a cordova application at the `~/cdvtest/hello` directory. The cdvtest directory must not exist, as the script will not overwrite it.
- install the cordova-plugin-test-framework.
- build the native libraries for the android platform, and copy those to
src/android/libs.
- build the wallet-uniffi kotlin bindings for the native library.
- install the plugin at this directory.
- install the plugin in the tests directory.
- run the test application if there is an emulator or device available.

```bash
python3 test.py --platform android -d ~/cdvtest --cargo-build --run android full
```

## Electron quirks

At the moment, the plugin requires that node integrations are enabled in the app.


# Implemented API's per platform

| | Android | Ios | Electron/browser |
| ---------------------------- | ------- | --- | ---------------- |
| CONVERSION_DELETE ||||
| CONVERSION_IGNORED ||||
| CONVERSION_TRANSACTIONS_GET ||||
| CONVERSION_TRANSACTIONS_SIZE ||||
| PENDING_TRANSACTIONS_DELETE || ||
| PENDING_TRANSACTIONS_GET || ||
| PENDING_TRANSACTIONS_SIZE || ||
| PROPOSAL_DELETE ||||
| PROPOSAL_NEW ||||
| PROPOSAL_NEW_PRIVATE ||||
| PROPOSAL_NEW_PUBLIC ||||
| SETTINGS_DELETE ||||
| SYMMETRIC_CIPHER_DECRYPT ||||
| WALLET_CONFIRM_TRANSACTION || ||
| WALLET_CONVERT ||||
| WALLET_DELETE ||||
| WALLET_ID ||||
| WALLET_IMPORT_KEYS ||||
| WALLET_PENDING_TRANSACTIONS || ||
| WALLET_RESTORE ||||
| WALLET_RETRIEVE_FUNDS ||||
| WALLET_SET_STATE ||||
| WALLET_TOTAL_FUNDS ||||
| WALLET_VOTE ||||
The `reload-plugin` and `reload-tests` commands can be used if only one of
those was modified, to avoid having to redo the whole process.
32 changes: 0 additions & 32 deletions bindings/wallet-cordova/TESTING.md

This file was deleted.

Loading

0 comments on commit f1cd62a

Please sign in to comment.