From dbc0f84d58a0ea83662ab670c64200a906494efb Mon Sep 17 00:00:00 2001 From: Zachary Atkins Date: Sat, 10 Apr 2021 17:44:39 -0500 Subject: [PATCH] fix p2p transactions --- loc_chain_app/lib/util/bluetooth.dart | 26 +++++++++++++------ .../lib/util/transaction_manager.dart | 5 ++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/loc_chain_app/lib/util/bluetooth.dart b/loc_chain_app/lib/util/bluetooth.dart index f204128..d16fb82 100644 --- a/loc_chain_app/lib/util/bluetooth.dart +++ b/loc_chain_app/lib/util/bluetooth.dart @@ -42,13 +42,15 @@ class Connect { _userName, strategy, onConnectionInitiated: onConnectionInit, - onConnectionResult: (String id, Status status) { + onConnectionResult: (String id, Status status) async { // Called when connection is accepted/rejected // 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)); - }); + if (status != Status.CONNECTED) { + return; + } + // Connected to other device, send combined hash and pub key + String str = await Transaction.generateHash(id); + Nearby().sendBytesPayload(id, Uint8List.fromList(str.codeUnits)); }, onDisconnected: (String id) { // Callled whenever a discoverer disconnects from advertiser @@ -69,8 +71,15 @@ class Connect { userName, id, onConnectionInitiated: onConnectionInit, - onConnectionResult: (id, status) { - showSnackbar(status); + onConnectionResult: (String id, Status status) async { + // Called when connection is accepted/rejected + // if connection is accepted send the transaction + if (status != Status.CONNECTED) { + return; + } + // Connected to other device, send combined hash and pub key + String str = await Transaction.generateHash(id); + Nearby().sendBytesPayload(id, Uint8List.fromList(str.codeUnits)); }, onDisconnected: (id) { endpointMap.remove(id); @@ -114,8 +123,9 @@ class Connect { transactionMap[_otherId] = Transaction(hash: combinedHash, pubKey: publicKey); // sign combined hash with our private key + showSnackbar('Received $_otherId payload: $combinedHash'); + print('Received $_otherId payload: $combinedHash:$publicKey'); // upload hash+otherKey to firebase - transactionMap.remove(_otherId); }, onPayloadTransferUpdate: (endid, payloadTransferUpdate) { if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) { diff --git a/loc_chain_app/lib/util/transaction_manager.dart b/loc_chain_app/lib/util/transaction_manager.dart index db8d623..4a85379 100644 --- a/loc_chain_app/lib/util/transaction_manager.dart +++ b/loc_chain_app/lib/util/transaction_manager.dart @@ -19,8 +19,9 @@ class Transaction { bool idLess = id.compareTo(otherUserId) < 0; var lesser = idLess ? id : otherUserId; var greater = idLess ? otherUserId : id; - return DBCrypt() - .hashpw("$lesser-$greater", KeyFileManager.keyPair.privateKey); + // return DBCrypt() + // .hashpw("$lesser-$greater", KeyFileManager.keyPair.privateKey); + return "$lesser-$greater"; } Future generateP2PPayload(String otherUserId) async =>