Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/BitcoinDevShop/pln
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Jul 1, 2022
2 parents 062cf6f + e1ef2fb commit 81cf2a7
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cargo run -- --development-mode=true --network=regtest --bitcoind-rpc-host=loca
Paul

```
cargo run -- --development-mode=true --network=regtest --bitcoind-rpc-host=localhost --bitcoind-rpc-port=18443 --bitcoind-rpc-username=admin1 --bitcoind-rpc-password=123 --data-dir=./.data
cargo run -- --development-mode=true --network=regtest --bitcoind-rpc-host=localhost --bitcoind-rpc-port=18443 --bitcoind-rpc-username=polaruser --bitcoind-rpc-password=polarpass --data-dir=./.data
```

The GRPC should run on `5401`
Expand Down
10 changes: 8 additions & 2 deletions mobile/lib/data/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:flutter/material.dart';
import 'package:pln/generated/pln.pbgrpc.dart';
import 'package:riverpod/riverpod.dart';

// ignore: depend_on_referenced_packages
import 'package:fixnum/fixnum.dart' show Int64;
import 'package:pln/grpc.dart';

import '../grpc.dart';

Expand Down Expand Up @@ -41,7 +43,9 @@ class Channel {
}

class ChannelNotifier extends StateNotifier<Channel?> {
ChannelNotifier() : super(null);
ChannelNotifier(this.ref) : super(null);

final Ref ref;

Timer? _timer;

Expand All @@ -51,6 +55,7 @@ class ChannelNotifier extends StateNotifier<Channel?> {
}

openChannel() async {
final plnClient = ref.read(plnClientProvider)!;
try {
debugPrint("creating channel...");
final response = await plnClient.openChannel(OpenChannelRequest(
Expand All @@ -66,6 +71,7 @@ class ChannelNotifier extends StateNotifier<Channel?> {
}

checkChannelStatus() async {
final plnClient = ref.read(plnClientProvider)!;
try {
final response =
await plnClient.getChannel(GetChannelRequest(id: state?.id));
Expand Down Expand Up @@ -103,5 +109,5 @@ class ChannelNotifier extends StateNotifier<Channel?> {
}

final channelProvider = StateNotifierProvider<ChannelNotifier, Channel?>((ref) {
return ChannelNotifier();
return ChannelNotifier(ref);
});
41 changes: 41 additions & 0 deletions mobile/lib/data/prefs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

// ignore: constant_identifier_names
const String PLN_GRPC_ENDPOINT = "PLN_GRPC_ENDPOINT";

class PrefsStateNotifier extends StateNotifier<String?> {
PrefsStateNotifier(this.prefs)
: super(prefs.get(PLN_GRPC_ENDPOINT) as String?);

// String _federationCode;
SharedPreferences prefs;

/// Updates the value asynchronously.
Future<void> update(String? value) async {
if (value != null) {
await prefs.setString(PLN_GRPC_ENDPOINT, value);
} else {
await prefs.remove(PLN_GRPC_ENDPOINT);
}
super.state = value;
}

/// Do not use the setter for state.
/// Instead, use `await update(value).`
@override
set state(String? value) {
assert(false,
"Don't use the setter for state. Instead use `await update(value)`.");
Future(() async {
await update(value);
});
}
}

StateNotifierProvider<PrefsStateNotifier, String?> createPrefProvider({
required SharedPreferences Function(Ref) prefs,
}) {
return StateNotifierProvider<PrefsStateNotifier, String?>(
(ref) => PrefsStateNotifier(prefs(ref)));
}
14 changes: 9 additions & 5 deletions mobile/lib/data/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ int btcToSats(btc) {
}

class SendNotifier extends StateNotifier<Send?> {
SendNotifier() : super(null);
SendNotifier(this.ref) : super(null);

final Ref ref;

createSendFromInvoice(String invoice) async {
final req = Bolt11PaymentRequest(invoice);
debugPrint("amount: ${(req.amount.toDouble() * 100000000).toInt()}");

var description = "";
req.tags.forEach((TaggedField t) {
print("${t.type}: ${t.data}");
for (var t in req.tags) {
debugPrint("${t.type}: ${t.data}");
if (t.type == "description") {
description = t.data;
}
});
}
state = Send(
invoice: invoice,
amountSats: btcToSats(req.amount),
Expand All @@ -64,6 +66,7 @@ class SendNotifier extends StateNotifier<Send?> {
}

pay() async {
final plnClient = ref.read(plnClientProvider)!;
try {
debugPrint("paying...");
final response = await plnClient.sendPayment(
Expand All @@ -77,6 +80,7 @@ class SendNotifier extends StateNotifier<Send?> {
}

checkPaymentStatus() async {
final plnClient = ref.read(plnClientProvider)!;
try {
debugPrint("checking status for ${state?.invoice}");
final response = await plnClient
Expand All @@ -98,5 +102,5 @@ class SendNotifier extends StateNotifier<Send?> {
}

final sendProvider = StateNotifierProvider<SendNotifier, Send?>((ref) {
return SendNotifier();
return SendNotifier(ref);
});
47 changes: 38 additions & 9 deletions mobile/lib/grpc.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:grpc/grpc.dart';
import 'package:pln/generated/pln.pbgrpc.dart';
import 'package:pln/main.dart';

final _channel = ClientChannel(
'localhost',
port: 5401,
options: ChannelOptions(
credentials: const ChannelCredentials.insecure(),
codecRegistry: CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
),
);
final plnClientProvider =
StateNotifierProvider<PlnClient, ManagerClient?>((ref) {
return PlnClient(ref);
});

final plnClient = ManagerClient(_channel);
class PlnClient extends StateNotifier<ManagerClient?> {
PlnClient(this.ref) : super(null);

final Ref ref;

Future<void> restartClient() async {
debugPrint("restarting grpc client");

try {
final prefs = ref.read(prefProvider);

final uri = Uri.parse(prefs ?? "http://localhost:5401");

// TODO is this all I need from the URI?
final channel = ClientChannel(uri.host + uri.path,
port: uri.port,
options: ChannelOptions(
credentials: const ChannelCredentials.insecure(),
codecRegistry:
CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
));
state = ManagerClient(channel);
} catch (e) {
throw ("couldn't connect");
}
}

ManagerClient? get() {
return state;
}
}
14 changes: 13 additions & 1 deletion mobile/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pln/data/prefs.dart';
import 'package:pln/router.dart';
import 'package:pln/theme.dart';
import 'package:shared_preferences/shared_preferences.dart';

late SharedPreferences prefs;

final prefProvider = createPrefProvider(
prefs: (_) => prefs,
);

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

prefs = await SharedPreferences.getInstance();

void main() {
runApp(const ProviderScope(child: MyApp()));
}

Expand Down
16 changes: 11 additions & 5 deletions mobile/lib/pln_appbar.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'constants.dart';

Expand Down Expand Up @@ -28,11 +29,16 @@ class PlnAppBar extends StatelessWidget implements PreferredSizeWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(title,
style: Theme.of(context)
.textTheme
.headline1
?.copyWith(color: home ? blue : black)),
home
? GestureDetector(
onTap: () => context.go("/welcome"),
child: Text(title,
style: Theme.of(context)
.textTheme
.headline1
?.copyWith(color: blue)),
)
: Text(title, style: Theme.of(context).textTheme.headline1),
backAction != null
? InkWell(
onTap: backAction,
Expand Down
23 changes: 13 additions & 10 deletions mobile/lib/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter/material.dart';
import 'package:pln/main.dart';
import 'package:pln/screens/channel_status.dart';
import 'package:pln/screens/welcome.dart';

import './screens/channel.dart';
import 'screens/channel_fund.dart';
Expand All @@ -14,7 +16,7 @@ import 'screens/send_status.dart';

/// Caches and Exposes a [GoRouter]
final routerProvider = Provider<GoRouter>((ref) {
final router = RouterNotifier();
final router = RouterNotifier(ref);

return GoRouter(
debugLogDiagnostics: true, // For demo purposes
Expand All @@ -25,22 +27,23 @@ final routerProvider = Provider<GoRouter>((ref) {
});

class RouterNotifier extends ChangeNotifier {
// final Ref _ref;
final Ref _ref;

// /// This implementation exploits `ref.listen()` to add a simple callback that
// /// calls `notifyListeners()` whenever there's change onto a desider provider.
// RouterNotifier(this._ref) {
// _ref.listen<String?>(
// prefProvider,
// (_, __) => notifyListeners(),
// );
// }
/// This implementation exploits `ref.listen()` to add a simple callback that
/// calls `notifyListeners()` whenever there's change onto a desider provider.
RouterNotifier(this._ref) {
_ref.listen<String?>(
prefProvider,
(_, __) => notifyListeners(),
);
}

String? _redirectLogic(GoRouterState state) {
return null;
}

List<GoRoute> get _routes => [
GoRoute(path: "/welcome", builder: (context, state) => const Welcome()),
GoRoute(path: "/", builder: (context, state) => const Home(), routes: [
GoRoute(
path: "send",
Expand Down
Loading

0 comments on commit 81cf2a7

Please sign in to comment.