loc-chain-app/loc_chain_app/lib/util/transaction_manager.dart

79 lines
2.4 KiB
Dart
Raw Normal View History

2021-04-11 05:59:30 +00:00
import 'package:flutter/material.dart';
2021-04-11 06:43:11 +00:00
import 'package:dbcrypt/dbcrypt.dart';
2021-04-11 04:41:42 +00:00
import 'package:flutter_udid/flutter_udid.dart';
2021-04-10 20:26:51 +00:00
import 'package:loc_chain_app/util/keyfile_manager.dart';
2021-04-10 19:55:41 +00:00
class Transaction {
2021-04-10 22:06:05 +00:00
Transaction({required this.hash, required this.pubKey});
2021-04-10 20:26:51 +00:00
final String hash;
2021-04-10 22:06:05 +00:00
final String pubKey;
2021-04-11 06:43:11 +00:00
static Future<Transaction> create(String otherUserId) async {
2021-04-11 04:41:42 +00:00
String id = await FlutterUdid.consistentUdid;
2021-04-10 20:26:51 +00:00
bool idLess = id.compareTo(otherUserId) < 0;
var lesser = idLess ? id : otherUserId;
var greater = idLess ? otherUserId : id;
2021-04-11 06:43:11 +00:00
return Transaction(
hash: DBCrypt()
.hashpw("$lesser-$greater", KeyFileManager.keyPair.privateKey),
pubKey: KeyFileManager.keyPair.publicKey,
);
2021-04-10 20:26:51 +00:00
}
2021-04-10 22:06:05 +00:00
2021-04-11 06:43:11 +00:00
Map<String, dynamic> toJson() => {
"combinedHash": hash,
"publicKey": pubKey,
};
Transaction.fromJson(Map<String, dynamic> json)
: hash = json['combinedHash'],
pubKey = json['publicKey'];
2021-04-10 19:55:41 +00:00
}
2021-04-11 05:59:30 +00:00
class EncounterTransaction {
EncounterTransaction({
required this.combinedHash,
required this.encodedGPSLocation,
required this.partnerPublicKey,
required this.validationSignature,
}) : timestamp = "${DateTime.now().millisecondsSinceEpoch}";
final String combinedHash;
final String timestamp;
final String encodedGPSLocation;
final String partnerPublicKey;
final String validationSignature;
EncounterTransaction.fromJson(Map<String, dynamic> json)
: combinedHash = json['combinedHash'],
timestamp = json['timestamp'],
encodedGPSLocation = json['encodedGPSLocation'],
partnerPublicKey = json['partnerPublicKey'],
validationSignature = json['validationSignature'];
Map<String, dynamic> toJson() => {
"combinedHash": combinedHash,
"timestamp": timestamp,
"encodedGPSLocation": encodedGPSLocation,
"partnerPublicKey": partnerPublicKey,
"validationSignature": validationSignature,
};
}
class ExposureTransaction {
final String clientID;
final String timestamp;
ExposureTransaction({
required this.clientID,
}) : timestamp = "${DateTime.now().millisecondsSinceEpoch}";
ExposureTransaction.fromJson(Map<String, dynamic> json)
: clientID = json['clientID'],
timestamp = json['timestamp'];
Map<String, dynamic> toJson() => {
"clientID": clientID,
"timestamp": timestamp,
};
}