Skip to content

Commit

Permalink
Android connects so sync through time limited bip words
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Mar 30, 2022
1 parent 9299e84 commit 1948418
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 26 deletions.
18 changes: 17 additions & 1 deletion android/java/org/chromium/chrome/browser/BraveSyncWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ public void run() {
}
};

public String GetCodephrase() {
public String GetPureWords() {
return BraveSyncWorkerJni.get().getSyncCodeWords(mNativeBraveSyncWorker);
}

public String GetTimeLimitedWordsFromPure(String pureWords) {
return BraveSyncWorkerJni.get().getTimeLimitedWordsFromPure(pureWords);
}

public void SaveCodephrase(String codephrase) {
BraveSyncWorkerJni.get().saveCodeWords(mNativeBraveSyncWorker, codephrase);
}
Expand All @@ -159,6 +163,14 @@ public String GetSeedHexFromQrJson(String jsonQr) {
return BraveSyncWorkerJni.get().getSeedHexFromQrJson(jsonQr);
}

public int GetWordsValidationResult(String timeLimitedWords) {
return BraveSyncWorkerJni.get().getWordsValidationResult(timeLimitedWords);
}

public String GetPureWordsFromTimeLimited(String timeLimitedWords) {
return BraveSyncWorkerJni.get().getPureWordsFromTimeLimited(timeLimitedWords);
}

public void RequestSync() {
BraveSyncWorkerJni.get().requestSync(mNativeBraveSyncWorker);
}
Expand Down Expand Up @@ -204,6 +216,10 @@ interface Natives {
String getQrDataJson(String seedHex);
int getQrCodeValidationResult(String jsonQr);
String getSeedHexFromQrJson(String jsonQr);
int getWordsValidationResult(String timeLimitedWords);
String getPureWordsFromTimeLimited(String timeLimitedWords);
String getTimeLimitedWordsFromPure(String pureWords);

void saveCodeWords(long nativeBraveSyncWorker, String passphrase);

void finalizeSyncSetup(long nativeBraveSyncWorker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,31 @@ private void setSyncText(String title, String message, TextView textView) {
textView.setText(formatedText);
}

private String getWordsValidationString(String words) {
int validationResult = getBraveSyncWorker().GetWordsValidationResult(words);
Log.v(TAG, "validationResult is " + validationResult);
switch (validationResult) {
case 0:
// kValid, empty string indicates there is no error
return "";
case 2:
// kVersionDeprecated
return getResources().getString(R.string.brave_sync_code_from_deprecated_version);
case 3:
// kExpired
return getResources().getString(R.string.brave_sync_code_expired);
case 4:
// kValidForTooLong
return getResources().getString(R.string.brave_sync_code_valid_for_too_long);

default:
// These three different types of errors have the same message
// kWrongWordsNumber
// kNotValidPureWords
return getResources().getString(R.string.brave_sync_wrong_code_error);
}
}

/** OnClickListener for the clear button. We show an alert dialog to confirm the action */
@Override
public void onClick(View v) {
Expand All @@ -570,7 +595,7 @@ public void onClick(View v) {
setJoinExistingChainLayout();
} else if (mStartNewChainButton == v) {
// Creating a new chain
GetCodephrase();
GetPureWords();
setNewChainLayout();
seedWordsReceived(mCodephrase);
} else if (mMobileButton == v) {
Expand Down Expand Up @@ -615,27 +640,18 @@ public void onClick(View v) {
.replace(" ", " ")
.replace("\n", " ")
.split(" ");
if (BIP39_WORD_COUNT != words.length) {
Log.e(TAG, "Confirm code words - wrong words count " + words.length);
onSyncError(getResources().getString(R.string.brave_sync_word_count_error));
return;
}

String hexString = getBraveSyncWorker().GetSeedHexFromWords(
TextUtils.join(" ", words));
if (hexString == null || hexString.isEmpty()) {
String trimmedWords = TextUtils.join(" ", words);
String validationError = getWordsValidationString(trimmedWords);
if (!validationError.isEmpty()) {
Log.e(TAG, "Confirm code words - wrong codephrase");
onSyncError(getResources().getString(R.string.brave_sync_wrong_code_error));
onSyncError(validationError);
return;
}

String codephraseCandidate = TextUtils.join(" ", words);
// Validate the code words with GetSeedHexFromWords
String seedHex = getBraveSyncWorker().GetSeedHexFromWords(codephraseCandidate);
if (null == seedHex || seedHex.isEmpty()) {
onSyncError(getResources().getString(R.string.brave_sync_wrong_code_error));
return;
}
String codephraseCandidate =
getBraveSyncWorker().GetPureWordsFromTimeLimited(trimmedWords);
assert codephraseCandidate != null && !codephraseCandidate.isEmpty();

showFinalSecurityWarning(FinalWarningFor.CODE_WORDS, () -> {
// We have the confirmation from user
Expand Down Expand Up @@ -1219,9 +1235,9 @@ private void setNewChainLayout() {
}

private String mCodephrase;
public String GetCodephrase() {
public String GetPureWords() {
if (mCodephrase == null || mCodephrase.isEmpty()) {
mCodephrase = getBraveSyncWorker().GetCodephrase();
mCodephrase = getBraveSyncWorker().GetPureWords();
}
return mCodephrase;
}
Expand Down Expand Up @@ -1267,7 +1283,7 @@ private void setAddMobileDeviceLayout() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String seedHex = getBraveSyncWorker().GetSeedHexFromWords(GetCodephrase());
String seedHex = getBraveSyncWorker().GetSeedHexFromWords(GetPureWords());
if (null == seedHex || seedHex.isEmpty()) {
// Give up, seed must be valid
Log.e(TAG, "setAddMobileDeviceLayout seedHex is empty");
Expand Down Expand Up @@ -1361,9 +1377,12 @@ private void setAddLaptopLayout() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String codePhrase = GetCodephrase();
String codePhrase = GetPureWords();
assert codePhrase != null && !codePhrase.isEmpty();
mBraveSyncAddDeviceCodeWords.setText(codePhrase);
String timeLimitedWords =
getBraveSyncWorker().GetTimeLimitedWordsFromPure(codePhrase);
assert timeLimitedWords != null && !timeLimitedWords.isEmpty();
mBraveSyncAddDeviceCodeWords.setText(timeLimitedWords);
}
});
}
Expand Down
42 changes: 42 additions & 0 deletions browser/android/brave_sync_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "brave/components/brave_sync/qr_code_data.h"
#include "brave/components/brave_sync/qr_code_validator.h"
#include "brave/components/brave_sync/sync_service_impl_helper.h"
#include "brave/components/brave_sync/time_limited_words.h"
#include "brave/components/sync/driver/brave_sync_service_impl.h"

#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -351,6 +352,47 @@ int JNI_BraveSyncWorker_GetQrCodeValidationResult(
brave_sync::QrCodeDataValidator::ValidateQrDataJson(str_json_qr));
}

int JNI_BraveSyncWorker_GetWordsValidationResult(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& time_limited_words) {
std::string str_time_limited_words =
base::android::ConvertJavaStringToUTF8(time_limited_words);
DCHECK(!str_time_limited_words.empty());
std::string pure_words_stub;
return static_cast<int>(brave_sync::TimeLimitedWords::Validate(
str_time_limited_words, &pure_words_stub));
}

static base::android::ScopedJavaLocalRef<jstring>
JNI_BraveSyncWorker_GetPureWordsFromTimeLimited(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& time_limited_words) {
std::string str_time_limited_words =
base::android::ConvertJavaStringToUTF8(time_limited_words);
DCHECK(!str_time_limited_words.empty());

std::string pure_words;
auto validation_result = brave_sync::TimeLimitedWords::Validate(
str_time_limited_words, &pure_words);
DCHECK_EQ(validation_result, brave_sync::WordsValidationResult::kValid);

return base::android::ConvertUTF8ToJavaString(env, pure_words);
}

static base::android::ScopedJavaLocalRef<jstring>
JNI_BraveSyncWorker_GetTimeLimitedWordsFromPure(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& pure_words) {
std::string str_pure_words =
base::android::ConvertJavaStringToUTF8(pure_words);
DCHECK(!str_pure_words.empty());

std::string time_limited_words =
brave_sync::TimeLimitedWords::GenerateForNow(str_pure_words);

return base::android::ConvertUTF8ToJavaString(env, time_limited_words);
}

static base::android::ScopedJavaLocalRef<jstring>
JNI_BraveSyncWorker_GetSeedHexFromQrJson(
JNIEnv* env,
Expand Down
3 changes: 0 additions & 3 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,6 @@ until they verify, or until 90 days have passed.
<message name="IDS_BRAVE_SYNC_WORD_COUNT_TEXT" desc="Text for Brave Sync word count message.">
Word count: <ph name="WORD_COUNT">%1$d</ph>
</message>
<message name="IDS_BRAVE_SYNC_WORD_COUNT_ERROR" desc="Text for Brave Sync word count error.">
Incorrect number of words
</message>
<message name="IDS_BRAVE_SYNC_WRONG_CODE_ERROR" desc="Text for wrong Brave Sync code error.">
Wrong sync code
</message>
Expand Down

0 comments on commit 1948418

Please sign in to comment.