Skip to content
Snippets Groups Projects
Commit 9671c573 authored by vjrj's avatar vjrj
Browse files

Correct cplus calls

parent 58e013ea
No related branches found
No related tags found
No related merge requests found
...@@ -736,30 +736,26 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async { ...@@ -736,30 +736,26 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async {
'version': 2, 'version': 2,
'issuer': pubKey, 'issuer': pubKey,
'title': name + userNameSuffix, 'title': name + userNameSuffix,
'geoPoint': null,
'time': DateTime.now().millisecondsSinceEpoch ~/ 'time': DateTime.now().millisecondsSinceEpoch ~/
1000, // current time in seconds 1000, // current time in seconds
'tags': <String>[], 'tags': <String>[],
}; };
final String userProfileJson = jsonEncode(userProfile); signAndHash(userProfile, wallet);
final String signature = wallet.sign(userProfileJson);
userProfile['hash'] = calculateHash(userProfileJson);
userProfile['signature'] = signature;
// Convert the user profile data into a JSON string again, now including hash and signature // Convert the user profile data into a JSON string again, now including hash and signature
final String userProfileJsonWithHashAndSignature = jsonEncode(userProfile); final String userProfileJsonWithHashAndSignature = jsonEncode(userProfile);
// Prepare the request headers
final Map<String, String> headers = <String, String>{
'Content-Type': 'application/json',
};
if (userName != null) { if (userName != null) {
// User exists, update the user profile logger('User exists, update the user profile');
final http.Response updateResponse = await _requestWithRetry( final http.Response updateResponse = await _requestWithRetry(
NodeType.cesiumPlus, '/user/profile/_update', false, true, NodeType.cesiumPlus,
'/user/profile/$pubKey/_update?pubkey=$pubKey',
false,
true,
httpType: HttpType.post, httpType: HttpType.post,
headers: headers, headers: _defCPlusHeaders(),
body: userProfileJsonWithHashAndSignature); body: userProfileJsonWithHashAndSignature);
if (updateResponse.statusCode == 200) { if (updateResponse.statusCode == 200) {
logger('User profile updated successfully.'); logger('User profile updated successfully.');
...@@ -769,11 +765,11 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async { ...@@ -769,11 +765,11 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async {
logger('Response body: ${updateResponse.body}'); logger('Response body: ${updateResponse.body}');
} }
} else if (userName == null) { } else if (userName == null) {
// User does not exist, create a new user profile logger('User does not exist, create a new user profile');
final http.Response createResponse = await _requestWithRetry( final http.Response createResponse = await _requestWithRetry(
NodeType.cesiumPlus, '/user/profile', false, false, NodeType.cesiumPlus, '/user/profile', false, false,
httpType: HttpType.post, httpType: HttpType.post,
headers: headers, headers: _defCPlusHeaders(),
body: userProfileJsonWithHashAndSignature); body: userProfileJsonWithHashAndSignature);
if (createResponse.statusCode == 200) { if (createResponse.statusCode == 200) {
...@@ -785,6 +781,21 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async { ...@@ -785,6 +781,21 @@ Future<void> createOrUpdateCesiumPlusUser(String name) async {
} }
} }
Map<String, String> _defCPlusHeaders() {
return <String, String>{
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=UTF-8',
};
}
void signAndHash(Map<String, dynamic> userProfile, CesiumWallet wallet) {
final String userProfileJson = jsonEncode(userProfile);
final String hash = calculateHash(userProfileJson);
final String signature = wallet.sign(hash);
userProfile['hash'] = hash;
userProfile['signature'] = signature;
}
Future<String?> getCesiumPlusUser(String pubKey) async { Future<String?> getCesiumPlusUser(String pubKey) async {
final Contact c = await getProfile(pubKey, true); final Contact c = await getProfile(pubKey, true);
return c.name; return c.name;
...@@ -803,14 +814,13 @@ Future<bool> deleteCesiumPlusUser() async { ...@@ -803,14 +814,13 @@ Future<bool> deleteCesiumPlusUser() async {
1000, // current time in seconds 1000, // current time in seconds
}; };
final String userProfileJson = jsonEncode(userProfile); signAndHash(userProfile, wallet);
final String signature = wallet.sign(userProfileJson);
userProfile['hash'] = calculateHash(userProfileJson);
userProfile['signature'] = signature;
final http.Response delResponse = await _requestWithRetry( final http.Response delResponse = await _requestWithRetry(
NodeType.cesiumPlus, '/user/profile/_delete', false, false, NodeType.cesiumPlus, '/history/delete', false, false,
httpType: HttpType.post); httpType: HttpType.post,
headers: _defCPlusHeaders(),
body: jsonEncode(userProfile));
return delResponse.statusCode == 200; return delResponse.statusCode == 200;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment