Skip to content

Commit

Permalink
Merge pull request #3 from mysteriumnetwork/fix/generate-key-method
Browse files Browse the repository at this point in the history
Fix generate key method
  • Loading branch information
tadaskay authored Mar 29, 2023
2 parents 14a578f + 2e58b7d commit 2187629
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4

* Rename `generatePrivateKey` -> `generateKeyPair`. Fix casting.

## 0.3.1

* Fetch WireGuardKit from cocoapods trunk
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# wireguard_dart

A new Flutter plugin project.
A flutter plugin to setup and control VPN connection via [Wireguard](https://www.wireguard.com/) tunnel.

## Getting Started
| | Android | iOS | Linux | macOS | Windows |
|-------------|---------|-------|-------|-------|-------------|
| **Support** | TBD | 15.0+ | TBD | 12+ | TBD |

This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
## Usage

For help getting started with Flutter development, view the
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
To use this plugin, add `wireguard_dart` as a [dependency in your pubspec.yaml file](https://flutter.dev/platform-plugins/).

61 changes: 37 additions & 24 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ class _MyAppState extends State<MyApp> {
debugPrint(_platformVersion);
});

_wireguardDartPlugin.setupTunnel(
bundleId: "network.mysterium.wireguardDartExample.tun");
_wireguardDartPlugin.setupTunnel(bundleId: "network.mysterium.wireguardDartExample.tun");
}

void generateKey() {
_wireguardDartPlugin.generateKeyPair().then((value) => {print(value)});
}

@override
Widget build(BuildContext context) {
const wireguardConfig = """
""";
void handleConnectPressed() =>
{_wireguardDartPlugin.connect(cfg: wireguardConfig)};
void handleConnectPressed() => {_wireguardDartPlugin.connect(cfg: wireguardConfig)};
return MaterialApp(
home: Scaffold(
appBar: AppBar(
Expand All @@ -65,26 +67,37 @@ class _MyAppState extends State<MyApp> {
constraints: const BoxConstraints.expand(),
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text("dududu"),
TextButton(
onPressed: handleConnectPressed,
style: ButtonStyle(
minimumSize: MaterialStateProperty.all<Size>(
const Size(100, 50)),
padding: MaterialStateProperty.all(
const EdgeInsets.fromLTRB(20, 15, 20, 15)),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.blueAccent),
overlayColor: MaterialStateProperty.all<Color>(
Colors.white.withOpacity(0.1))),
child: const Text(
'Connect',
style: TextStyle(color: Colors.white),
))
]),
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text("dududu"),
TextButton(
onPressed: handleConnectPressed,
style: ButtonStyle(
minimumSize: MaterialStateProperty.all<Size>(const Size(100, 50)),
padding: MaterialStateProperty.all(const EdgeInsets.fromLTRB(20, 15, 20, 15)),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueAccent),
overlayColor: MaterialStateProperty.all<Color>(Colors.white.withOpacity(0.1))),
child: const Text(
'Connect',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(height: 20),
TextButton(
onPressed: generateKey,
style: ButtonStyle(
minimumSize: MaterialStateProperty.all<Size>(const Size(100, 50)),
padding: MaterialStateProperty.all(const EdgeInsets.fromLTRB(20, 15, 20, 15)),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueAccent),
overlayColor: MaterialStateProperty.all<Color>(Colors.white.withOpacity(0.1))),
child: const Text(
'Generate Key',
style: TextStyle(color: Colors.white),
),
)
],
),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/SwiftWireguardDartPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SwiftWireguardDartPlugin: NSObject, FlutterPlugin {

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "generatePrivateKey":
case "generateKeyPair":
let privateKey = PrivateKey()
let privateKeyResponse: [String: Any] = [
"privateKey": privateKey.base64Key,
Expand Down
4 changes: 2 additions & 2 deletions lib/wireguard_dart.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'wireguard_dart_platform_interface.dart';

class WireguardDart {
Future<Map<String, String>> generatePrivateKey() {
return WireguardDartPlatform.instance.generatePrivateKey();
Future<Map<String, String>> generateKeyPair() {
return WireguardDartPlatform.instance.generateKeyPair();
}

Future<void> setupTunnel({required String bundleId}) {
Expand Down
8 changes: 3 additions & 5 deletions lib/wireguard_dart_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ class MethodChannelWireguardDart extends WireguardDartPlatform {
final methodChannel = const MethodChannel('wireguard_dart');

@override
Future<Map<String, String>> generatePrivateKey() async {
final Map<String, dynamic> result = await methodChannel.invokeMethod('generatePrivateKey');
return result.cast<String, String>();
Future<Map<String, String>> generateKeyPair() async {
return Map<String, String>.from(await methodChannel.invokeMethod('generateKeyPair'));
}

@override
Future<void> setupTunnel({required String bundleId}) async {
await methodChannel
.invokeMethod<void>('setupTunnel', {'bundleId': bundleId});
await methodChannel.invokeMethod<void>('setupTunnel', {'bundleId': bundleId});
}

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/wireguard_dart_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract class WireguardDartPlatform extends PlatformInterface {
_instance = instance;
}

Future<Map<String, String>> generatePrivateKey() {
throw UnimplementedError('generatePrivateKey() has not been implemented');
Future<Map<String, String>> generateKeyPair() {
throw UnimplementedError('generateKeyPair() has not been implemented');
}

Future<void> setupTunnel({required String bundleId}) {
Expand Down
2 changes: 1 addition & 1 deletion macos/Classes/WireguardDartPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WireguardDartPlugin: NSObject, FlutterPlugin {

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "generatePrivateKey":
case "generateKeyPair":
let privateKey = PrivateKey()
let privateKeyResponse: [String: Any] = [
"privateKey": privateKey.base64Key,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wireguard_dart
description: Wireguard Dart SDK
version: 0.3.1
version: 0.4.0
homepage: https://github.com/mysteriumnetwork/wireguard_dart
repository: https://github.com/mysteriumnetwork/wireguard_dart

Expand Down
6 changes: 3 additions & 3 deletions test/wireguard_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MockWireguardDartPlatform with MockPlatformInterfaceMixin implements Wireg
Future<void> disconnect() => Future.value();

@override
Future<Map<String, String>> generatePrivateKey() {
Future<Map<String, String>> generateKeyPair() {
return Future(() => Map.of({
'privateKey': 'dududu',
'publicKey': 'dududududu',
Expand All @@ -30,12 +30,12 @@ void main() {
expect(initialPlatform, isInstanceOf<MethodChannelWireguardDart>());
});

test('generatePrivateKey', () async {
test('generateKeyPair', () async {
WireguardDart wireguardDartPlugin = WireguardDart();
MockWireguardDartPlatform fakePlatform = MockWireguardDartPlatform();
WireguardDartPlatform.instance = fakePlatform;

final result = await wireguardDartPlugin.generatePrivateKey();
final result = await wireguardDartPlugin.generateKeyPair();
expect(result, isMap);
expect(result, containsPair('privateKey', 'dududu'));
expect(result, containsPair('publicKey', 'dududududu'));
Expand Down

0 comments on commit 2187629

Please sign in to comment.