Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Wallet can switch between hosted and local node #1419

Merged
merged 66 commits into from
Oct 24, 2019

Conversation

annakaz
Copy link
Contributor

@annakaz annakaz commented Oct 21, 2019

Description

Allow wallet users to switch between a local and a hosted node.

Handle storing the private key in web3.eth.personal (with local node) vs in the WalletKit Celo provider (with hosted node)

Tested

Tested on device on alfajores.
Build that starts in zeroSync mode:
sent tx, turned zeroSync mode off and confirmed that geth starts, sent tx, turned zeroSync mode off and confirmed that geth stops, sent tx

Build that starts in ultralight mode:
sent tx, turned on zeroSync mode, confirmed that geth turns off, sent tx

More testing to be done by the QA team

Other changes

  • Move verification and invite to generators in order to access zeroSyncMode state

Related issues

Backwards compatibility

Not with pervious code with ZeroSync mode encoded as DEFAULT_SYNC_MODE=-1.
There are now Separate parameters (DEFAULT_SYNC_MODE and ZERO_SYNC_INITIALLY) in .env file

Copy link
Contributor

@jmrossy jmrossy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress! See questions below

@@ -4,23 +4,23 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it did the same for me, i had to manually revert it. But it's nbd feel free to leave in this PR

DEFAULT_SYNC_MODE=5
ZERO_SYNC_INITIALLY=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what this config is for exactly? My first guess would be it's whether or not the app should use zero sync when it starts, but that would conflict with the DEFAULT_SYNC_MODE config we already have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFAULT_SYNC_MODE now specifies the sync mode for when geth is turned on
Because geth will turn on and off, DEFAULT_SYNC_MODE can't encode whether the app should use zeroSync initially. Added a comment in the .env file

throw new Error('addLocalAccount can only be called in Zero sync mode')
// Mutates web3 with new provider
export function switchWeb3ProviderForSyncMode(zeroSync: boolean) {
if (zeroSync) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a bit more logging here? Unless you already have it elsewhere where it's called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already some logging where it's called but I added logging for each provider switch

// Note that if geth is running it saves the key using web3.personal.
const privateKey = String(key)
account = getAccountAddressFromPrivateKey(privateKey)
yield savePrivateKeyToLocalDisk(account, privateKey, pincode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me nervous. We allowed it for zerosync mode, which I half-assumed was just a temporary thing. but if we're now going to permanently store the user's private key (encrypted by their pincode which could be poor and/or lost), I think we should have a broader discussion about it first. Maybe we already have but I missed it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for flagging this. After looking into it, geth also stores the private keys in a file encrypted with the password (for which we use the same pincode), so I will keep this as is for now.
See https://celo-org.slack.com/archives/CL7BVQPHB/p1571763622020000

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

// TODO(anna) implement switching geth on/off, changing web3 provider
return true
// Stores account and private key in web3 keystore using web3.eth.personal
export function* addAccountToWeb3Keystore(key: string, currentAccount: string, pincode: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason pincode isn't a string?


export function* switchZeroSyncMode(action: SetIsZeroSyncAction) {
Logger.debug(TAG + '@switchZeroSyncMode', ` to: ${action.zeroSyncMode}`)
yield call(getConnectedAccount) // Ensure web3 connected before switching provider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wait for us to be connected first?

export function* switchToGethFromZeroSync() {
yield call(initGethSaga)

setZeroSyncMode(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you missing a yield put here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah thanks for catching this


export function* switchToZeroSyncFromGeth() {
Logger.debug(TAG + 'Switching to zeroSync from geth..')
setZeroSyncMode(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

'Importing account from private key to web3 keystore'
)
const pincode = yield call(getPincode)
const privateKey: string = yield readPrivateKeyFromLocalDisk(currentAccount, pincode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: yield call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

packages/mobile/src/web3/saga.ts Show resolved Hide resolved
@jmrossy jmrossy assigned annakaz and unassigned jmrossy Oct 22, 2019
Copy link
Contributor

@jmrossy jmrossy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code changes lgtm. See my question below about the default value for ZERO_SYNC_ENABLED_INITIALLY and see CI tests, which seem to have failed.

# -1 == ZeroSync, 5 == Ultralight, see src/geth/consts.ts for more info
# If ZERO_SYNC_ENABLED_INITIALLY, local geth will not run initially.
# If toggled on, it will use DEFAULT_SYNC_MODE. See src/geth/consts.ts for more info
ZERO_SYNC_ENABLED_INITIALLY=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying the this. My next question is then: should this be true? My understanding from my chat with Connor about this a while back was that we'd keep using geth by default during development, so we could catch bugs early (since prod still uses geth)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point for usually using geth during development. I'll have this be false for all env files for now.

// Note that if geth is running it saves the key using web3.personal.
const privateKey = String(key)
account = getAccountAddressFromPrivateKey(privateKey)
yield savePrivateKeyToLocalDisk(account, privateKey, pincode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@jmrossy jmrossy dismissed their stale review October 23, 2019 12:17

Most issues addressed

@codecov
Copy link

codecov bot commented Oct 23, 2019

Codecov Report

Merging #1419 into master will decrease coverage by 0.38%.
The diff coverage is 39.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1419      +/-   ##
==========================================
- Coverage   65.86%   65.48%   -0.39%     
==========================================
  Files         271      271              
  Lines        8062     8144      +82     
  Branches      559      560       +1     
==========================================
+ Hits         5310     5333      +23     
- Misses       2639     2697      +58     
- Partials      113      114       +1
Flag Coverage Δ
#mobile 65.48% <39.25%> (-0.39%) ⬇️
Impacted Files Coverage Δ
packages/mobile/src/account/Account.tsx 63.41% <ø> (ø) ⬆️
packages/mobile/src/geth/consts.ts 100% <ø> (ø) ⬆️
packages/mobile/src/web3/reducer.ts 42.85% <0%> (-7.15%) ⬇️
packages/mobile/src/account/CeloLite.tsx 100% <100%> (ø) ⬆️
packages/mobile/src/invite/saga.ts 80.53% <100%> (+0.39%) ⬆️
packages/mobile/src/geth/networkConfig.ts 100% <100%> (ø) ⬆️
packages/mobile/src/app/ErrorMessages.ts 100% <100%> (ø) ⬆️
packages/mobile/src/web3/saga.ts 42.39% <18.96%> (-7.61%) ⬇️
packages/mobile/src/transactions/send.ts 24.48% <35.29%> (+3.55%) ⬆️
packages/mobile/src/app/saga.ts 67.18% <44.44%> (-3.73%) ⬇️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3a69de8...a615862. Read the comment docs.

@annakaz annakaz assigned annakaz and unassigned cmcewen Oct 24, 2019
@annakaz annakaz added the automerge Have PR merge automatically when checks pass label Oct 24, 2019
@celo-ci-bot-user celo-ci-bot-user merged commit 1c82ae6 into master Oct 24, 2019
@celo-ci-bot-user celo-ci-bot-user deleted the annakaz/zero-sync-toggle-logic branch October 24, 2019 16:48
aaronmgdr added a commit that referenced this pull request Oct 24, 2019
* master:
  [Wallet] Wallet can switch between hosted and local node (#1419)
  [Wallet] Prevent error from Avatar when name is missing (#1454)
  [Wallet] Show splash screen until JS is ready on iOS (#1453)
  Use new segment api keys used by both iOS and Android (#1452)
  [Wallet] Don't log all props, which includes i18n (#1445)
  [Helm] Updated the helm package to deploy the upgraded blockscout version (#1129)
  Tiny copy change (#1429)
  [contractkit] SortedOraclesWrapper + tests (#1405)
  [wallet] Refactor leftover thunk to sagas (#1388)
  [Wallet] Fix repeated QR code scanning and related navigation issues (#1439)
  [Wallet] Show the currency values with correct rounding. (#1435)
  [Wallet] Fix firebase initialization error on iOS after reinstalling the app (#1423)
  [Wallet] Use exit on iOS since we can't restart like Android (#1424)
  [Wallet] Update local currency styles and layout (#1325)
  Reset pincode cache if unlock fails (#1430)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Have PR merge automatically when checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Users SBAT select a hosted node setup when in Low Connectivity
6 participants