substrate_sign_flutter 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 60

Substrate Sign Flutter Plugin #

Flutter plugin for using Substrate signing functions offline.

This plugin use Dart ffi module to bind Rust function.

Now works on Android, check this thread to get update for ios

More details about using rust in flutter please check this great article

Usage #

Introduce SURI

SURI refers to Secret URI, it is with format of:

"$seedPhrase//$derivationPath///derivationPassword"

for example:

SURI bottom drive obey lake curtain smoke basket hold race lonely fit walk//kusama/1///000000.

Functions

randomPhrase: Generate random seed phrase

String randomPhrase(int digits)

substrateAddress: Generate substrate address with provided suri(Secret URI).

String substrateAddress(String seed, int prefix) 

substrateSign: Sign hex string with the provided seed

String substrateSign(String suri, String message)

encryptData: encrypt data with ethsign

String encryptData(String data, String password) 

decryptData: decrypt data with ethsign

String decryptData(String data, String password)

Functions with seed pointer

By using seed phrase, we could keep seed phrase in the protected Rust runtime and avoid it to be exposed to the dart Runtime and avoid it to keep in the app state. To interact with seed reference functions user need give the seed pointer as an argument.

//get address with seed pointer
String substrateAddressWithRef(int seedRef, String suriSuffix, int prefix)

//sign hex data with seed pointer
String substrateSignWithRef(int seedRef, String suriSuffix, String message)

//decrypt the cipher text and return the seed pointer
int decryptDataWithRef(String data, String password)

//destroy the pointer, deallocate the memory assigned to the seed
void destroyDataRef(int ref)

NOTICE: here suriSuffix refer to the combination of derivation path and derivation password like //some_path/1///123456 or only path but without password like //another_path

Develop #

To customize the library or add more functions. First run

./scripts/init.sh

to setup required toolchains, you will also need Android NDK for generating android native code, download it here and pointer environment variable NDK_HOME to it.

After customizing the rust code, run

./scripts/build.sh

to generate the glue code and binaries for Rust, Flutter, iOS and Android.

Test #

Now use integration test for testing the native binding functions.

cd example && flutter drive --target=test_driver/app.dart

License #

GNU 3.0

0.1.1 #

  • Update Description to be more accurate.

0.1.0 #

  • Fix parsing bug on substrate signing related method

0.0.1 #

  • initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:substrate_sign_flutter/substrate_sign_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _testSeed = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String testSeed;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      testSeed = randomPhrase(12);
    } on PlatformException {
      testSeed = 'Failed to get seed.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _testSeed = testSeed;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text('Genrated Seed: $_testSeed\n'),
              FlatButton(
                child: const Text('Press'),
                onPressed: () {
//                  String seed = "${randomPhrase(12)}//kusama///";
                  String seed = "daring phrase labor alien treat divert trick analyst shrug idle run program//kusama///";
                  String suriSuffix = "//kusama";
                  String seedPhrase = "daring phrase labor alien treat divert trick analyst shrug idle run program";
                  print("response is $seed");
                  String message = "49800000100005301025a4a03f84a19cf8ebda40e62358c592870691a9cf456138bb4829969d10fe9699c0400ff3c36776005aec2f32a34c109dc791a82edef980eec3be80da938ac9bcc68217202286bee7503000015040000b0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe19d84dae3af90c5c78ff8eaed58b0db1ff8600c4426d4e0969a905ab5fe2d2e50";
                  String signedResult = substrateSign(seed, message);
                  print("signed result is $signedResult");
                  String address = substrateAddress(seed, 2);
                  print("substrate address is $address");
                  String encrypted = encryptData(seed, "000000");
                  print("encrypted data is $encrypted");
                  String decrypted = decryptData(encrypted, "000000");
                  print("decrypted seed is $decrypted");

                  String encryptedSeedPhrase = encryptData(seedPhrase, "000000");
                  int seedRef = decryptDataWithRef(encryptedSeedPhrase, "000000");
                  print("decrypted seed ref is $seedRef");
                  String signedWithRefResult = substrateSignWithRef(seedRef, suriSuffix, message);
                  print("signed with ref result is $signedWithRefResult");
                  String addressWithRef = substrateAddressWithRef(seedRef, suriSuffix, 2);
                  print("generate address with ref is $addressWithRef");
                  rustDestroyDataRef(seedRef);
                },
              ),
            ],
          )
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  substrate_sign_flutter: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:substrate_sign_flutter/substrate_sign_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
20
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:substrate_sign_flutter/substrate_sign_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Web

Because:

  • package:substrate_sign_flutter/substrate_sign_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Windows

Because:

  • package:substrate_sign_flutter/substrate_sign_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform macOS

Because:

  • package:substrate_sign_flutter/substrate_sign_flutter.dart that declares support for platforms: Android, iOS

Package not compatible with SDK dart

Because:

  • substrate_sign_flutter that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
ffi ^0.1.3 0.1.3
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test