Skip to content

Commit

Permalink
complete docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Jul 8, 2021
1 parent 089626e commit d18eaf9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 29 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/sodium_libs_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,24 @@ jobs:
- uses: ./.github/actions/dependency_clean
with:
package: ${{ env.PACKAGE_NAME }}
- name: restore prebuilt binaries (android)
- name: restore cached prebuilt binaries (android)
id: cache-libsodium-bins
if: matrix.platform == 'android'
uses: actions/cache@v2
with:
path: packages/sodium_libs/android/src/main/jniLibs
key: libsodium-${{ matrix.platform }}-${{ hashFiles('libsodium_version.json') }}
- name: check cache was restored (android)
if: matrix.platform == 'android' && steps.cache-libsodium-bins.outputs.cache-hit != 'true'
key: libsodium-android-${{ hashFiles('libsodium_version.json') }}
- name: check cache was restored
if: steps.cache-libsodium-bins.outputs.cache-hit != 'true'
run: |
echo 'Failed to restore android binaries from cache!'
echo '::error::Failed to restore binaries from cache!'
exit 1
- run: flutter pub get
- name: store credentials
run: |
mkdir -p ~/.pub-cache
echo '${{ secrets.PUB_DEV_CREDENTIALS }}' > ~/.pub-cache/credentials.json
file ~/.pub-cache/credentials.json
du -sh melos.yaml
- run: dart run tool/publish.dart --force
- name: clean up credentials
if: always()
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
[![Continous Integration for package sodium](https://github.com/Skycoder42/libsodium_dart_bindings/actions/workflows/sodium_ci.yaml/badge.svg)](https://github.com/Skycoder42/libsodium_dart_bindings/actions/workflows/sodium_ci.yaml)
[![Pub Version](https://img.shields.io/pub/v/sodium)](https://pub.dev/packages/sodium)

[![Continous Integration for package sodium_libs](https://github.com/Skycoder42/libsodium_dart_bindings/actions/workflows/sodium_libs_ci.yaml/badge.svg)](https://github.com/Skycoder42/libsodium_dart_bindings/actions/workflows/sodium_libs_ci.yaml)
[![Pub Version](https://img.shields.io/pub/v/sodium_libs)](https://pub.dev/packages/sodium_libs)

This repository is a multi package repository for dart bindings of
[libsodium](https://libsodium.gitbook.io/doc/). It consists of the following
packages. Please check the READMEs of the specific packages for more details on
Expand All @@ -11,5 +14,8 @@ If you just landed here and don't know where to start, simply read the
[sodium README](packages/sodium), as that is the primary package of this
repository.

- **[sodium](packages/sodium)**: Dart bindings for libsodium, supporting both the VM and JS without flutter
dependencies.
- **[sodium](packages/sodium)**: Dart bindings for libsodium, supporting both
the VM and JS without flutter dependencies.
- **[sodium_libs](packages/sodium_libs)**: Flutter Companion-Package to
[sodium](packages/sodium) that provides the low-level libsodium binaries for
easy use.
16 changes: 13 additions & 3 deletions packages/sodium_libs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Flutter Companion-Package to [sodium](https://pub.dev/packages/sodium) that
provides the low-level libsodium binaries for easy use.

## Table of contents
- [Features](#features)
- [Installation](#installation)
* [Platform requirements](#platform-requirements)
+ [Linux](#linux)
+ [Windows](#windows)
+ [Web](#web)
- [Usage](#usage)
- [Documentation](#documentation)

<small><i><a href='http://ecotrust-canada.github.io/markdown-toc/'>Table of contents generated with markdown-toc</a></i></small>

## Features
- Extends [sodium](https://pub.dev/packages/sodium) for Flutter with binaries
Expand All @@ -26,7 +36,7 @@ Simply add `sodium_libs` to your `pubspec.yaml` and run `pub get` (or
In addition to installing the package, you will also have to install operating
system specific tools for some platforms:

### Linux
#### Linux
You have to install [libsodium](https://github.com/jedisct1/libsodium) on your
system. How you do this depends on your distribution:
- Arch/Manjaro: `[sudo] pacman -S libsodium`
Expand All @@ -36,7 +46,7 @@ system. How you do this depends on your distribution:
When bundeling the application for release, remember to also include the
`libsodium.so` into the deployment package.

### Windows
#### Windows
Since the plugin downloads the binaries at build time, it needs
[minisign](https://jedisct1.github.io/minisign/) to validate their integrity.
The easiest way to install minisign is via
Expand All @@ -46,7 +56,7 @@ The easiest way to install minisign is via
choco install minisign
```

### Web
#### Web
The web setup differs slightly from the others. Instead of just installing some
system library, you need to add
[`sodium.js`](https://github.com/jedisct1/libsodium.js) to each project. You can
Expand Down
61 changes: 44 additions & 17 deletions packages/sodium_libs/lib/src/sodium_init.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:sodium/sodium.dart' as sodium;
import 'package:synchronized/synchronized.dart';

import 'platforms/stub_platforms.dart'
if (dart.library.ffi) 'platforms/io_platforms.dart'
Expand All @@ -15,28 +16,54 @@ import 'sodium_platform.dart';
abstract class SodiumInit {
static const _expectedVersion = sodium.SodiumVersion(10, 3, '1.0.18');

static final _instanceLock = Lock();
static sodium.Sodium? _instance;

const SodiumInit._(); // coverage:ignore-line

static void registerPlugins() {
/// Ensures that the correct platform plugin is registered
///
/// This method is automatically called by [init] and usually does not need
/// to be called manually. However, If you are working with [SodiumPlatform],
/// You should call this method to make sure the correct
/// [SodiumPlatform.instance] is available.
///
/// **Note:** This method only applies to Dart-VM targets. On the web, the
/// registration happens automatically.
static void ensurePlatformRegistered() {
if (!SodiumPlatform.isRegistered) {
Platforms.registerPlatformPlugin();
}
}

static Future<sodium.Sodium> init() async {
WidgetsFlutterBinding.ensureInitialized();
registerPlugins();
final instance = await SodiumPlatform.instance.loadSodium();
if (!kReleaseMode) {
if (instance.version < _expectedVersion) {
// ignore: avoid_print
print(
'WARNING: The embedded libsodium is outdated! '
'Expected $_expectedVersion, but was ${instance.version}}. '
'${SodiumPlatform.instance.updateHint}',
);
}
}
return instance;
}
/// Creates a new [Sodium] instance and initializes it
///
/// Internally, this method ensures the correct [SodiumPlatform] is available
/// and then uses [SodiumPlatform.loadSodium] to create an instance.
///
/// In addition, when not running in release mode, it also performs a version
/// check on the library to ensure you are using the correct native binary on
/// platforms, where the binary is fetched dynamically.
///
/// **Note:** Calling this method multiple times will always return the same
/// instance.
static Future<sodium.Sodium> init() => _instanceLock.synchronized(() async {
if (_instance != null) {
return _instance!;
}
WidgetsFlutterBinding.ensureInitialized();
ensurePlatformRegistered();
_instance = await SodiumPlatform.instance.loadSodium();
if (!kReleaseMode) {
if (_instance!.version < _expectedVersion) {
// ignore: avoid_print
print(
'WARNING: The embedded libsodium is outdated! '
'Expected $_expectedVersion, but was ${_instance!.version}}. '
'${SodiumPlatform.instance.updateHint}',
);
}
}
return _instance!;
});
}
3 changes: 2 additions & 1 deletion packages/sodium_libs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ dependencies:
sdk: flutter
js: ^0.6.3
meta: ^1.3.0
plugin_platform_interface: ^2.0.0
plugin_platform_interface: ^2.0.1
sodium: ^1.0.0
synchronized: ^3.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit d18eaf9

Please sign in to comment.