Skip to content

Commit

Permalink
Merge pull request brave#1565 from brave/resolve-bad-state-60
Browse files Browse the repository at this point in the history
Fixes wallet bad state
  • Loading branch information
NejcZdovc authored Feb 5, 2019
2 parents 7b855c1 + 8d9de80 commit b581115
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 40 deletions.
27 changes: 23 additions & 4 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void SetBackupCompleted(const base::ListValue* args);
void OnGetWalletPassphrase(const std::string& pass);
void OnGetContributionAmount(double amount);
void OnGetAddresses(const std::map<std::string, std::string>& addresses);
void OnGetAddresses(const std::string func_name,
const std::map<std::string, std::string>& addresses);
void OnGetNumExcludedSites(const std::string& publisher_id, uint32_t num);
void OnGetAutoContributeProps(
int error_code,
Expand All @@ -97,6 +98,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnIsWalletCreated(bool created);
void GetPendingContributionsTotal(const base::ListValue* args);
void OnGetPendingContributionsTotal(double amount);
void GetAddressesForPaymentId(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -244,6 +246,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getPendingContributionsTotal",
base::BindRepeating(&RewardsDOMHandler::GetPendingContributionsTotal,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getAddressesForPaymentId",
base::BindRepeating(&RewardsDOMHandler::GetAddressesForPaymentId,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -530,22 +535,26 @@ void RewardsDOMHandler::GetReconcileStamp(const base::ListValue* args) {
}

void RewardsDOMHandler::OnGetAddresses(
const std::string func_name,
const std::map<std::string, std::string>& addresses) {
if (web_ui()->CanCallJavascript()) {
if (web_ui()->CanCallJavascript() && (
func_name == "addresses" || func_name == "addressesForPaymentId")) {
base::DictionaryValue data;
data.SetString("BAT", addresses.at("BAT"));
data.SetString("BTC", addresses.at("BTC"));
data.SetString("ETH", addresses.at("ETH"));
data.SetString("LTC", addresses.at("LTC"));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.addresses", data);
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards." + func_name, data);
}
}

void RewardsDOMHandler::GetAddresses(const base::ListValue* args) {
if (rewards_service_)
rewards_service_->GetAddresses(base::Bind(
&RewardsDOMHandler::OnGetAddresses, weak_factory_.GetWeakPtr()));
&RewardsDOMHandler::OnGetAddresses,
weak_factory_.GetWeakPtr(),
"addresses"));
}

void RewardsDOMHandler::OnAutoContributePropsReady(
Expand Down Expand Up @@ -893,6 +902,16 @@ void RewardsDOMHandler::OnRewardsMainEnabled(
}
}

void RewardsDOMHandler::GetAddressesForPaymentId(
const base::ListValue* args) {
if (rewards_service_) {
rewards_service_->GetAddressesForPaymentId(base::Bind(
&RewardsDOMHandler::OnGetAddresses,
weak_factory_.GetWeakPtr(),
"addressesForPaymentId"));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class RewardsService : public KeyedService {
virtual void GetRewardsMainEnabled(
const GetRewardsMainEnabledCallback& callback) const = 0;

virtual void GetAddressesForPaymentId(
const GetAddressesCallback& callback) = 0;

void AddObserver(RewardsServiceObserver* observer);
void RemoveObserver(RewardsServiceObserver* observer);

Expand Down
12 changes: 12 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2362,4 +2362,16 @@ void RewardsServiceImpl::OnRestorePublishersInternal(
}
}

void RewardsServiceImpl::GetAddressesForPaymentId(
const GetAddressesCallback& callback) {
if (!Connected()) {
return;
}

bat_ledger_->GetAddressesForPaymentId(
base::BindOnce(&RewardsServiceImpl::OnGetAddresses,
AsWeakPtr(),
callback));
}

} // namespace brave_rewards
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class RewardsServiceImpl : public RewardsService,
void GetRewardsMainEnabled(
const GetRewardsMainEnabledCallback& callback) const override;

void GetAddressesForPaymentId(const GetAddressesCallback& callback) override;

// Testing methods
void SetLedgerEnvForTesting();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const onReconcileStamp = (stamp: number) => action(types.ON_RECONCILE_STA
stamp
})

export const onAddresses = (addresses: Record<string, string>) => action(types.ON_ADDRESSES, {
export const onAddresses = (addresses: Record<Rewards.AddressesType, string>) => action(types.ON_ADDRESSES, {
addresses
})

Expand Down Expand Up @@ -165,3 +165,10 @@ export const onPendingContributionTotal = (amount: number) => action(types.ON_PE
export const onRewardsEnabled = (enabled: boolean) => action(types.ON_REWARDS_ENABLED, {
enabled
})

export const getAddressesForPaymentId = () => action(types.GET_ADDRESSES_FOR_PAYMENT_ID)

export const onAddressesForPaymentId = (addresses: Record<Rewards.AddressesType, string>) =>
action(types.ON_ADDRESSES_FOR_PAYMENT_ID, {
addresses
})
9 changes: 7 additions & 2 deletions components/brave_rewards/resources/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ window.cr.define('brave_rewards', function () {
getActions().onReconcileStamp(stamp)
}

function addresses (addresses: Record<string, string>) {
function addresses (addresses: Record<Rewards.AddressesType, string>) {
getActions().onAddresses(addresses)
}

Expand Down Expand Up @@ -143,6 +143,10 @@ window.cr.define('brave_rewards', function () {
getActions().onRewardsEnabled(enabled)
}

function addressesForPaymentId (addresses: Record<Rewards.AddressesType, string>) {
getActions().onAddressesForPaymentId(addresses)
}

return {
initialize,
walletCreated,
Expand All @@ -167,7 +171,8 @@ window.cr.define('brave_rewards', function () {
adsData,
pendingContributionTotal,
onPendingContributionSaved,
rewardsEnabled
rewardsEnabled,
addressesForPaymentId
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class SettingsPage extends React.Component<Props, {}> {
if (!this.props.rewardsData.ui.addressCheck) {
this.actions.getAddresses()
}

// one time check (legacy fix)
if (!this.props.rewardsData.ui.paymentIdCheck) {
// https://github.com/brave/brave-browser/issues/3060
this.actions.getAddressesForPaymentId()
// https://github.com/brave/brave-browser/issues/3061
this.actions.getWalletPassphrase()
}
}

componentWillUnmount () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export const enum types {
GET_RECONCILE_STAMP = '@@rewards/GET_RECONCILE_STAMP',
GET_PENDING_CONTRIBUTION_TOTAL = '@@rewards/GET_PENDING_CONTRIBUTION_TOTAL',
ON_PENDING_CONTRIBUTION_TOTAL = '@@rewards/ON_PENDING_CONTRIBUTION_TOTAL',
ON_REWARDS_ENABLED = '@@rewards/ON_REWARDS_ENABLED'
ON_REWARDS_ENABLED = '@@rewards/ON_REWARDS_ENABLED',
GET_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/GET_ADDRESSES_FOR_PAYMENT_ID',
ON_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/ON_ADDRESSES_FOR_PAYMENT_ID'
}
88 changes: 57 additions & 31 deletions components/brave_rewards/resources/ui/reducers/wallet_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ const createWallet = (state: Rewards.State) => {
return state
}

const saveAddresses = (state: Rewards.State, addresses: Record<Rewards.AddressesType, string>) => {
if (!addresses) {
return state
}

state = { ...state }
state.addresses = {
BAT: {
address: addresses.BAT,
qr: null
},
BTC: {
address: addresses.BTC,
qr: null
},
ETH: {
address: addresses.ETH,
qr: null
},
LTC: {
address: addresses.LTC,
qr: null
}
}

generateQR(addresses)

return state
}

const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State, action) => {
switch (action.type) {
case types.CREATE_WALLET:
Expand Down Expand Up @@ -128,30 +158,15 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
}
case types.ON_ADDRESSES:
{
if (!action.payload.addresses) {
break
}

state = { ...state }
state.addresses = {
BAT: {
address: action.payload.addresses.BAT,
qr: null
},
BTC: {
address: action.payload.addresses.BTC,
qr: null
},
ETH: {
address: action.payload.addresses.ETH,
qr: null
},
LTC: {
address: action.payload.addresses.LTC,
qr: null
}
state = saveAddresses(state, action.payload.addresses)
let ui = state.ui
ui.addressCheck = true

state = {
...state,
ui
}
generateQR(action.payload.addresses)
break
}
case types.ON_QR_GENERATED:
Expand Down Expand Up @@ -210,16 +225,7 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
}
case types.GET_ADDRESSES:
{
state = { ...state }
let ui = state.ui
ui.addressCheck = true

chrome.send('brave_rewards.getAddresses')

state = {
...state,
ui
}
break
}
case types.GET_RECONCILE_STAMP:
Expand All @@ -238,6 +244,26 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
state.pendingContributionTotal = action.payload.amount
break
}
case types.GET_ADDRESSES_FOR_PAYMENT_ID:
{
chrome.send('brave_rewards.getAddressesForPaymentId')
break
}
case types.ON_ADDRESSES_FOR_PAYMENT_ID:
{
state = { ...state }

state = saveAddresses(state, action.payload.addresses)

let ui = state.ui
ui.paymentIdCheck = true

state = {
...state,
ui
}
break
}
}

return state
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/resources/ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export const defaultState: Rewards.State = {
recoveryKey: '',
reconcileStamp: 0,
ui: {
addressCheck: false,
addressCheck: true,
emptyWallet: true,
modalBackup: false,
paymentIdCheck: true,
walletCorrupted: false,
walletImported: false,
walletRecoverySuccess: null,
Expand Down
1 change: 1 addition & 0 deletions components/definitions/rewards.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ declare namespace Rewards {
addressCheck: boolean
emptyWallet: boolean
modalBackup: boolean
paymentIdCheck: boolean
walletRecoverySuccess: boolean | null
walletServerProblem: boolean
walletCorrupted: boolean
Expand Down
18 changes: 18 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,22 @@ void BatLedgerImpl::HasSufficientBalanceToReconcile(
std::move(callback).Run(ledger_->HasSufficientBalanceToReconcile());
}

// static
void BatLedgerImpl::OnAddressesForPaymentId(
CallbackHolder<GetAddressesForPaymentIdCallback>* holder,
std::map<std::string, std::string> addresses) {
if (holder->is_valid())
std::move(holder->get()).Run(mojo::MapToFlatMap(addresses));
delete holder;
}

void BatLedgerImpl::GetAddressesForPaymentId(
GetAddressesForPaymentIdCallback callback) {
// delete in OnAddressesForPaymentId
auto* holder = new CallbackHolder<GetAddressesForPaymentIdCallback>(
AsWeakPtr(), std::move(callback));
ledger_->GetAddressesForPaymentId(
std::bind(BatLedgerImpl::OnAddressesForPaymentId, holder, _1));
}

} // namespace bat_ledger
7 changes: 7 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class BatLedgerImpl : public mojom::BatLedger,
void HasSufficientBalanceToReconcile(
HasSufficientBalanceToReconcileCallback callback) override;

void GetAddressesForPaymentId(
GetAddressesForPaymentIdCallback callback) override;

private:
// workaround to pass base::OnceCallback into std::bind
template <typename Callback>
Expand All @@ -143,6 +146,10 @@ class BatLedgerImpl : public mojom::BatLedger,
CallbackHolder<GetPublisherBannerCallback>* holder,
std::unique_ptr<ledger::PublisherBanner> banner);

static void OnAddressesForPaymentId(
CallbackHolder<GetAddressesForPaymentIdCallback>* holder,
std::map<std::string, std::string> addresses);

std::unique_ptr<BatLedgerClientMojoProxy> bat_ledger_client_mojo_proxy_;
std::unique_ptr<ledger::Ledger> ledger_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ interface BatLedger {
GetRewardsMainEnabled() => (bool main_enabled);

HasSufficientBalanceToReconcile() => (bool sufficient);

GetAddressesForPaymentId() => (map<string, string> addresses);
};

interface BatLedgerClient {
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ LEDGER_EXPORT struct PaymentData {


using PublisherBannerCallback = std::function<void(std::unique_ptr<ledger::PublisherBanner> banner)>;
using WalletAddressesCallback = std::function<void(std::map<std::string, std::string> addresses)>;

class LEDGER_EXPORT Ledger {
public:
Expand Down Expand Up @@ -205,6 +206,8 @@ class LEDGER_EXPORT Ledger {

virtual uint64_t GetBootStamp() const = 0;
virtual bool HasSufficientBalanceToReconcile() = 0;

virtual void GetAddressesForPaymentId(ledger::WalletAddressesCallback callback) = 0;
};

} // namespace ledger
Expand Down
Loading

0 comments on commit b581115

Please sign in to comment.