Merge pull request #6939 from brave/backup-words

Adds back recovery keys api's
This commit is contained in:
Brian R. Bondy
2020-10-23 18:20:57 -04:00
committed by GitHub
19 changed files with 111 additions and 6 deletions
+27
View File
@@ -195,6 +195,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetPaymentId(ledger::type::BraveWalletPtr wallet);
void GetWalletPassphrase(const base::ListValue* args);
void OnGetWalletPassphrase(const std::string& pass);
// RewardsServiceObserver implementation
void OnFetchPromotions(
brave_rewards::RewardsService* rewards_service,
@@ -473,6 +477,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getPaymentId",
base::BindRepeating(&RewardsDOMHandler::GetPaymentId,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getWalletPassphrase",
base::BindRepeating(&RewardsDOMHandler::GetWalletPassphrase,
base::Unretained(this)));
}
void RewardsDOMHandler::Init() {
@@ -1895,6 +1902,26 @@ void RewardsDOMHandler::OnGetPaymentId(ledger::type::BraveWalletPtr wallet) {
base::Value(payment_id));
}
void RewardsDOMHandler::GetWalletPassphrase(const base::ListValue* args) {
if (!rewards_service_) {
return;
}
rewards_service_->GetWalletPassphrase(
base::Bind(&RewardsDOMHandler::OnGetWalletPassphrase,
weak_factory_.GetWeakPtr()));
}
void RewardsDOMHandler::OnGetWalletPassphrase(const std::string& passphrase) {
if (!web_ui()->CanCallJavascript()) {
return;
}
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.walletPassphrase",
base::Value(passphrase));
}
} // namespace
BraveRewardsPageUI::BraveRewardsPageUI(content::WebUI* web_ui,
@@ -242,6 +242,9 @@ class MockRewardsService : public RewardsService {
MOCK_METHOD1(StartProcess,
void(brave_rewards::StartProcessCallback));
MOCK_METHOD1(GetWalletPassphrase,
void(brave_rewards::GetWalletPassphraseCallback));
};
class AdsServiceTest : public testing::Test {
@@ -133,6 +133,8 @@ using GetBraveWalletCallback =
using StartProcessCallback =
base::OnceCallback<void(ledger::type::Result result)>;
using GetWalletPassphraseCallback = base::Callback<void(const std::string&)>;
class RewardsService : public KeyedService {
public:
RewardsService();
@@ -358,6 +360,8 @@ class RewardsService : public KeyedService {
virtual void StartProcess(StartProcessCallback callback) = 0;
virtual void GetWalletPassphrase(GetWalletPassphraseCallback callback) = 0;
protected:
base::ObserverList<RewardsServiceObserver> observers_;
@@ -3373,5 +3373,14 @@ void RewardsServiceImpl::StartProcess(StartProcessCallback callback) {
StartLedger(std::move(callback));
}
void RewardsServiceImpl::GetWalletPassphrase(
GetWalletPassphraseCallback callback) {
if (!Connected()) {
std::move(callback).Run("");
return;
}
bat_ledger_->GetWalletPassphrase(callback);
}
} // namespace brave_rewards
@@ -333,6 +333,8 @@ class RewardsServiceImpl : public RewardsService,
void StartProcess(StartProcessCallback callback) override;
void GetWalletPassphrase(GetWalletPassphraseCallback callback) override;
// Testing methods
void SetLedgerEnvForTesting();
void PrepareLedgerEnvForTesting();
@@ -75,7 +75,8 @@ export const defaultState: Rewards.State = {
rate: 0
},
initializing: true,
paymentId: ''
paymentId: '',
recoveryKey: ''
}
const cleanData = (state: Rewards.State) => {
@@ -332,3 +332,9 @@ export const onPaymentId = (paymentId: string) => action(types.ON_PAYMENT_ID, {
export const setFirstLoad = (firstLoad: boolean) => action(types.SET_FIRST_LOAD, {
firstLoad
})
export const getWalletPassphrase = () => action(types.GET_WALLET_PASSPHRASE)
export const onWalletPassphrase = (passphrase: string) => action(types.ON_WALLET_PASSPHRASE, {
passphrase
})
@@ -246,6 +246,10 @@ window.cr.define('brave_rewards', function () {
getActions().onPaymentId(paymentId)
}
function walletPassphrase (passphrase: string) {
getActions().onWalletPassphrase(passphrase)
}
return {
initialize,
rewardsParameters,
@@ -290,7 +294,8 @@ window.cr.define('brave_rewards', function () {
countryCode,
initialized,
completeReset,
paymentId
paymentId,
walletPassphrase
}
})
@@ -72,6 +72,9 @@ class PageWallet extends React.Component<Props, State> {
}
onModalBackupOpen = () => {
if (this.props.rewardsData.recoveryKey.length === 0) {
this.actions.getWalletPassphrase()
}
this.actions.onModalBackupOpen()
}
@@ -767,7 +770,8 @@ class PageWallet extends React.Component<Props, State> {
const {
balance,
ui,
pendingContributionTotal
pendingContributionTotal,
recoveryKey
} = this.props.rewardsData
const { total } = balance
const { emptyWallet, modalBackup, onlyAnonWallet } = ui
@@ -815,7 +819,7 @@ class PageWallet extends React.Component<Props, State> {
modalBackup
? <ModalBackupRestore
activeTabId={this.state.activeTabId}
backupKey={''}
backupKey={recoveryKey}
showBackupNotice={this.showBackupNotice()}
onTabChange={this.onModalBackupTabChange}
onClose={this.onModalBackupClose}
@@ -92,5 +92,7 @@ export const enum types {
ON_COMPLETE_RESET = '@@rewards/ON_COMPLETE_RESET',
GET_PAYMENT_ID = '@@rewards/GET_PAYMENT_ID',
ON_PAYMENT_ID = '@@rewards/ON_PAYMENT_ID',
SET_FIRST_LOAD = '@@rewards/SET_FIRST_LOAD'
SET_FIRST_LOAD = '@@rewards/SET_FIRST_LOAD',
GET_WALLET_PASSPHRASE = '@@rewards/GET_WALLLET_PASSPHRASE',
ON_WALLET_PASSPHRASE = '@@rewards/ON_WALLLET_PASSPHRASE'
}
@@ -56,6 +56,7 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
getCurrentBalanceReport()
ui.modalBackup = false
ui.emptyWallet = false
state.recoveryKey = ''
}
state = {
@@ -196,6 +197,24 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
state.monthlyReportIds = action.payload
break
}
case types.GET_WALLET_PASSPHRASE: {
chrome.send('brave_rewards.getWalletPassphrase')
break
}
case types.ON_WALLET_PASSPHRASE: {
const value = action.payload.passphrase
if (value && value.length > 0) {
state = { ...state }
let ui = state.ui
state.recoveryKey = value
state = {
...state,
ui
}
}
break
}
}
return state
@@ -75,7 +75,8 @@ export const defaultState: Rewards.State = {
rate: 0
},
initializing: true,
paymentId: ''
paymentId: '',
recoveryKey: ''
}
const cleanData = (state: Rewards.State) => {
+1
View File
@@ -58,6 +58,7 @@ declare namespace Rewards {
pendingContributions: PendingContribution[]
pendingContributionTotal: number
reconcileStamp: number
recoveryKey: string
recurringList: Publisher[]
recurringLoad: boolean
safetyNetFailed?: boolean
@@ -1149,4 +1149,8 @@ void BatLedgerImpl::GetBraveWallet(GetBraveWalletCallback callback) {
_1));
}
void BatLedgerImpl::GetWalletPassphrase(GetWalletPassphraseCallback callback) {
std::move(callback).Run(ledger_->GetWalletPassphrase());
}
} // namespace bat_ledger
@@ -245,6 +245,8 @@ class BatLedgerImpl :
void GetBraveWallet(GetBraveWalletCallback callback) override;
void GetWalletPassphrase(GetWalletPassphraseCallback callback) override;
private:
// workaround to pass base::OnceCallback into std::bind
template <typename Callback>
@@ -156,6 +156,8 @@ interface BatLedger {
GetEventLogs() => (array<ledger.mojom.EventLog> logs);
GetBraveWallet() => (ledger.mojom.BraveWallet? wallet);
GetWalletPassphrase() => (string passphrase);
};
interface BatLedgerClient {
+2
View File
@@ -384,6 +384,8 @@ class LEDGER_EXPORT Ledger {
virtual void GetEventLogs(GetEventLogsCallback callback) = 0;
virtual void GetBraveWallet(GetBraveWalletCallback callback) = 0;
virtual std::string GetWalletPassphrase() const = 0;
};
} // namespace ledger
@@ -813,4 +813,13 @@ void LedgerImpl::GetBraveWallet(GetBraveWalletCallback callback) {
callback(wallet()->GetWallet());
}
std::string LedgerImpl::GetWalletPassphrase() const {
const auto brave_wallet = wallet()->GetWallet();
if (!brave_wallet) {
return "";
}
return wallet()->GetWalletPassphrase(brave_wallet->Clone());
}
} // namespace ledger
@@ -347,6 +347,8 @@ class LedgerImpl : public ledger::Ledger {
void GetBraveWallet(GetBraveWalletCallback callback) override;
std::string GetWalletPassphrase() const override;
// end ledger.h
void OnAllDone(const type::Result result, ledger::ResultCallback callback);