Skip to content

Commit

Permalink
Merge pull request #1 from mysteriumnetwork/gen-priv-key
Browse files Browse the repository at this point in the history
Generate private key
  • Loading branch information
tadaskay authored Mar 28, 2023
2 parents 584c7e5 + 783cd73 commit cb3e69f
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 7 deletions.
Binary file removed .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dart.lineLength": 150,
"[dart]": {
"editor.rulers": [150]
}
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.3.0

* `generatePrivateKey` support for iOS/macOS

## 0.2.0

* iOS/macOS support

## 0.1.0

* Initial release
7 changes: 7 additions & 0 deletions ios/Classes/SwiftWireguardDartPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class SwiftWireguardDartPlugin: NSObject, FlutterPlugin {

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "generatePrivateKey":
let privateKey = PrivateKey()
let privateKeyResponse: [String: Any] = [
"privateKey": privateKey.base64Key,
"publicKey": privateKey.publicKey.base64Key,
]
result(privateKeyResponse)
case "setupTunnel":
Self.logger.debug("handle setupTunnel")
if let args = call.arguments as? Dictionary<String, Any>,
Expand Down
4 changes: 4 additions & 0 deletions lib/wireguard_dart.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'wireguard_dart_platform_interface.dart';

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

Future<void> setupTunnel({required String bundleId}) {
return WireguardDartPlatform.instance.setupTunnel(bundleId: bundleId);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/wireguard_dart_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class MethodChannelWireguardDart extends WireguardDartPlatform {
@visibleForTesting
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>();
}

@override
Future<void> setupTunnel({required String bundleId}) async {
await methodChannel
Expand Down
4 changes: 4 additions & 0 deletions lib/wireguard_dart_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ abstract class WireguardDartPlatform extends PlatformInterface {
_instance = instance;
}

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

Future<void> setupTunnel({required String bundleId}) {
throw UnimplementedError('setupTunnel() has not been implemented');
}
Expand Down
Binary file removed macos/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions macos/Classes/WireguardDartPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public class WireguardDartPlugin: NSObject, FlutterPlugin {

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "generatePrivateKey":
let privateKey = PrivateKey()
let privateKeyResponse: [String: Any] = [
"privateKey": privateKey.base64Key,
"publicKey": privateKey.publicKey.base64Key,
]
result(privateKeyResponse)
case "setupTunnel":
if let args = call.arguments as? Dictionary<String, Any>,
let argBundleId = args["bundleId"] as? String {
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.1.0
version: 0.3.0
homepage: https://github.com/mysteriumnetwork/wireguard_dart
repository: https://github.com/mysteriumnetwork/wireguard_dart

Expand Down
24 changes: 18 additions & 6 deletions test/wireguard_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:wireguard_dart/wireguard_dart_method_channel.dart';
import 'package:wireguard_dart/wireguard_dart_platform_interface.dart';
import 'package:wireguard_dart/wireguard_dart.dart';

class MockWireguardDartPlatform with MockPlatformInterfaceMixin implements WireguardDartPlatform {
@override
Expand All @@ -12,6 +13,14 @@ class MockWireguardDartPlatform with MockPlatformInterfaceMixin implements Wireg

@override
Future<void> disconnect() => Future.value();

@override
Future<Map<String, String>> generatePrivateKey() {
return Future(() => Map.of({
'privateKey': 'dududu',
'publicKey': 'dududududu',
}));
}
}

void main() {
Expand All @@ -21,11 +30,14 @@ void main() {
expect(initialPlatform, isInstanceOf<MethodChannelWireguardDart>());
});

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

// expect(await wireguardDartPlugin.setupTunnel());
// });
final result = await wireguardDartPlugin.generatePrivateKey();
expect(result, isMap);
expect(result, containsPair('privateKey', 'dududu'));
expect(result, containsPair('publicKey', 'dududududu'));
});
}

0 comments on commit cb3e69f

Please sign in to comment.