Page MenuHomePhabricator

[Cashtab] Patch error when activating saved wallet for first time
ClosedPublic

Authored by bytesofman on Dec 4 2022, 05:01.

Details

Summary

T2824

Cashtab wallets are created into savedWallets without a state key, as this is created and populated in update. However, update is only called on active wallets. When a wallet is first activated, it is running through loadStoredWallet, which expects a defined state.

This function allows loadStoredWallet to handle undefined as an input.

Test Plan

npm test
npm start
create a new wallet
activate it
note no error msgs in dev console
activate a stored wallet that already has state
note no error msgs in dev console
Confirm you can use the wallet

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

emack requested changes to this revision.Dec 4 2022, 06:28
emack added a subscriber: emack.
emack added inline comments.
web/cashtab/src/utils/cashMethods.js
875 ↗(On Diff #36940)

is there a reason this attribute is used to track whether it's a BigNumber, rather than using BigNumber.isBigNumber(x)? Doesn't it run the risk of introducing a future bug where the type changes but the attribute remains as true?

This revision now requires changes to proceed.Dec 4 2022, 06:28
web/cashtab/src/utils/cashMethods.js
875 ↗(On Diff #36940)

edit: just realized this was legacy code. A quick search on the repo shows it's not used anywhere else other than another mock, so perhaps a separate diff to remove this?

bytesofman added inline comments.
web/cashtab/src/utils/cashMethods.js
875 ↗(On Diff #36940)

Here's a summary of what's going on

  1. Cashtab stores token balances in wallet.state as a BigNumber, at wallet.state.tokens[].balance
  2. When wallet is saved to indexedDb using localForage, the BigNumber type is not preserved
  3. So, when wallet is loaded from localStorage, you have something that looks like
wallet.state.tokens[0].balance = {
    "s": 1,
    "e": 0,
    "c": [
        1
    ]
}

We aren't checking whether or not this is a BigNumber (BigNumber.isBigNumber(x)) -- we are converting this to a BigNumber, since we know that's what it is supposed to be. Unfortunately, new BigNumber(wallet.state.tokens[0].balance) (the above value) does not work unless thisTokenBalance._isBigNumber = true is added first.

To your point, it should be removed -- but the way to do this is to make sure wallet.state.tokens[].balance is always stored as a string and not as a BigNumber, and always convert to BigNumber when you are doing calculations.

There's a longstanding task for getting this fix in. Since we are looking at some wallet upgrades, now is probably the right time to do it and I'll add it to my workflow: T2614

However, I think it's worth landing this diff first since it patches a bug. There are some other wallet upgrades in the pipeline that will make this function obsolete or drastically different, but it's still worth getting the bug fixed now.

Use Cashtab standard typeof walletStateFromStorage !== 'undefined' instead of === undefined

This revision is now accepted and ready to land.Dec 4 2022, 11:25