mirror of
https://github.com/hackku21/loc-chain-app.git
synced 2024-10-27 20:34:05 +00:00
p2p transaction logic
This commit is contained in:
parent
f91bc61a8c
commit
68f06e5f05
@ -7,18 +7,32 @@ import 'package:nearby_connections/nearby_connections.dart';
|
|||||||
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
import 'package:loc_chain_app/util/keyfile_manager.dart';
|
||||||
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
||||||
|
|
||||||
class Connect {
|
class Connect {
|
||||||
final serviceId = "com.yourdomain.appname";
|
final serviceId = "com.yourdomain.appname";
|
||||||
final Strategy strategy = Strategy.P2P_STAR;
|
final Strategy strategy = Strategy.P2P_STAR;
|
||||||
late final _userName;
|
late final _userName;
|
||||||
|
final BuildContext? context;
|
||||||
|
|
||||||
Map<String, ConnectionInfo> endpointMap = Map();
|
Map<String, ConnectionInfo> endpointMap = Map();
|
||||||
|
Map<String, Transaction> transactionMap = Map();
|
||||||
|
|
||||||
Connect() {
|
Connect({this.context}) {
|
||||||
SharedPreferences.getInstance()
|
SharedPreferences.getInstance()
|
||||||
.then((s) => _userName = s.getString('userName') ?? '0');
|
.then((s) => _userName = s.getString('userName') ?? '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void showSnackbar(dynamic a) {
|
||||||
|
if (context == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
|
||||||
|
content: Text(a.toString()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
void startConnect() async {
|
void startConnect() async {
|
||||||
// final prefs = await SharedPreferences.getInstance();
|
// final prefs = await SharedPreferences.getInstance();
|
||||||
// final userName = prefs.getString('userName') ?? " ";
|
// final userName = prefs.getString('userName') ?? " ";
|
||||||
@ -27,19 +41,21 @@ class Connect {
|
|||||||
bool advertise = await Nearby().startAdvertising(
|
bool advertise = await Nearby().startAdvertising(
|
||||||
_userName,
|
_userName,
|
||||||
strategy,
|
strategy,
|
||||||
onConnectionInitiated: (String id, ConnectionInfo info) {
|
onConnectionInitiated: onConnectionInit,
|
||||||
// Called whenever a discoverer requests connection
|
|
||||||
//
|
|
||||||
// onConnectionInit
|
|
||||||
},
|
|
||||||
onConnectionResult: (String id, Status status) {
|
onConnectionResult: (String id, Status status) {
|
||||||
// Called when connection is accepted/rejected
|
// Called when connection is accepted/rejected
|
||||||
// if connection is accepted send the transaction
|
// if connection is accepted send the transaction
|
||||||
//
|
endpointMap.forEach((key, value) async {
|
||||||
//
|
String str = await Transaction.generateHash(key);
|
||||||
|
Nearby().sendBytesPayload(key, Uint8List.fromList(str.codeUnits));
|
||||||
|
});
|
||||||
},
|
},
|
||||||
onDisconnected: (String id) {
|
onDisconnected: (String id) {
|
||||||
// Callled whenever a discoverer disconnects from advertiser
|
// Callled whenever a discoverer disconnects from advertiser
|
||||||
|
// delete connection info
|
||||||
|
endpointMap.remove(id);
|
||||||
|
// delete transaction info
|
||||||
|
transactionMap.remove(id);
|
||||||
},
|
},
|
||||||
serviceId: serviceId, // uniquely identifies your app
|
serviceId: serviceId, // uniquely identifies your app
|
||||||
);
|
);
|
||||||
@ -49,6 +65,19 @@ class Connect {
|
|||||||
strategy,
|
strategy,
|
||||||
onEndpointFound: (String id, String userName, String serviceId) {
|
onEndpointFound: (String id, String userName, String serviceId) {
|
||||||
// called when an advertiser is found
|
// called when an advertiser is found
|
||||||
|
Nearby().requestConnection(
|
||||||
|
userName,
|
||||||
|
id,
|
||||||
|
onConnectionInitiated: onConnectionInit,
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
endpointMap.remove(id);
|
||||||
|
showSnackbar(
|
||||||
|
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
||||||
|
},
|
||||||
|
);
|
||||||
},
|
},
|
||||||
onEndpointLost: (String? id) {
|
onEndpointLost: (String? id) {
|
||||||
//called when an advertiser is lost (only if we weren't connected to it )
|
//called when an advertiser is lost (only if we weren't connected to it )
|
||||||
@ -59,9 +88,49 @@ class Connect {
|
|||||||
// platform exceptions like unable to start bluetooth or insufficient permissions
|
// platform exceptions like unable to start bluetooth or insufficient permissions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onConnectionInit(String otherId, ConnectionInfo info) {
|
||||||
|
// Called whenever a discoverer requests connection
|
||||||
|
//
|
||||||
|
// onConnectionInit
|
||||||
|
if (endpointMap.containsKey(otherId)) {
|
||||||
|
Nearby().rejectConnection(otherId);
|
||||||
|
}
|
||||||
|
endpointMap[otherId] = info;
|
||||||
|
Nearby().acceptConnection(
|
||||||
|
otherId,
|
||||||
|
onPayLoadRecieved: (_otherId, payload) async {
|
||||||
|
if (payload.type != PayloadType.BYTES) return;
|
||||||
|
// completed payload from other connection
|
||||||
|
String str = String.fromCharCodes(payload.bytes!);
|
||||||
|
var parts = str.split(':');
|
||||||
|
if (parts.length != 2) {
|
||||||
|
showSnackbar("$_otherId invalid payload: $str");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Store transaction
|
||||||
|
var combinedHash = parts[0];
|
||||||
|
var publicKey = parts[1];
|
||||||
|
transactionMap[_otherId] =
|
||||||
|
Transaction(hash: combinedHash, pubKey: publicKey);
|
||||||
|
// sign combined hash with our private key
|
||||||
|
// upload hash+otherKey to firebase
|
||||||
|
transactionMap.remove(_otherId);
|
||||||
|
},
|
||||||
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
||||||
|
if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
|
||||||
|
print(payloadTransferUpdate.bytesTransferred);
|
||||||
|
} else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
|
||||||
|
print("failed");
|
||||||
|
showSnackbar(endid + ": FAILED to transfer file");
|
||||||
|
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
||||||
|
showSnackbar(
|
||||||
|
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ElevatedButton(
|
// ElevatedButton(
|
||||||
@ -77,10 +146,6 @@ class Connect {
|
|||||||
// },
|
// },
|
||||||
// ),
|
// ),
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// void onConnectionInit(String id, ConnectionInfo info) {
|
// void onConnectionInit(String id, ConnectionInfo info) {
|
||||||
// showModalBottomSheet(
|
// showModalBottomSheet(
|
||||||
// context: context,
|
// context: context,
|
||||||
|
@ -9,12 +9,10 @@ import 'package:loc_chain_app/util/keyfile_manager.dart';
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
class Transaction {
|
class Transaction {
|
||||||
Transaction({required this.hash}) {
|
Transaction({required this.hash, required this.pubKey});
|
||||||
SharedPreferences.getInstance()
|
|
||||||
.then((s) => _id = s.getString('userName') ?? '0');
|
|
||||||
}
|
|
||||||
late final String _id;
|
|
||||||
final String hash;
|
final String hash;
|
||||||
|
final String pubKey;
|
||||||
|
|
||||||
static Future<String> generateHash(String otherUserId) async {
|
static Future<String> generateHash(String otherUserId) async {
|
||||||
String id = await SharedPreferences.getInstance()
|
String id = await SharedPreferences.getInstance()
|
||||||
.then((s) => s.getString('userName') ?? '');
|
.then((s) => s.getString('userName') ?? '');
|
||||||
@ -24,6 +22,9 @@ class Transaction {
|
|||||||
return DBCrypt()
|
return DBCrypt()
|
||||||
.hashpw("$lesser-$greater", KeyFileManager.keyPair.privateKey);
|
.hashpw("$lesser-$greater", KeyFileManager.keyPair.privateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String> generateP2PPayload(String otherUserId) async =>
|
||||||
|
"${await generateHash(otherUserId)}:${KeyFileManager.keyPair.publicKey}";
|
||||||
}
|
}
|
||||||
|
|
||||||
class TransactionsDBManager {
|
class TransactionsDBManager {
|
||||||
|
Loading…
Reference in New Issue
Block a user