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

Fix path to derive accounts in HD wallets for RSK, ETC #317

Merged
merged 9 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [#323](https://github.com/poanetwork/nifty-wallet/pull/323) - (Chore) update Mainnet tokens metadata
- [#322](https://github.com/poanetwork/nifty-wallet/pull/322) - (Fix) Update explorers links
- [#318](https://github.com/poanetwork/nifty-wallet/pull/318) - (Fix) pasting of ABI for contract type account
- [#317](https://github.com/poanetwork/nifty-wallet/pull/317) - (Fix) path to derive accounts in HD wallets for RSK, ETC

## 4.11.10 Tue Feb 04 2020

Expand Down
94 changes: 7 additions & 87 deletions app/scripts/controllers/network/createPocketClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,16 @@ const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/b
const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware')
const createPocketMiddleware = require('json-rpc-pocket')
const BlockTracker = require('eth-block-tracker')
const { getNetworkID } = require('../../../../old-ui/app/util')
const devid = 'DEVVGQ8VfHgBBet8CyowHcN'
const {
ROPSTEN,
ROPSTEN_CODE,
ROPSTEN_CHAINID,
RINKEBY_CODE,
RINKEBY_CHAINID,
RINKEBY,
KOVAN,
KOVAN_CODE,
KOVAN_CHAINID,
MAINNET,
MAINNET_CODE,
MAINNET_CHAINID,
ETH_TICK,
POA_SOKOL,
POA_CODE,
POA_CHAINID,
POA_TICK,
POA,
DAI,
DAI_CODE,
DAI_CHAINID,
GOERLI_TESTNET,
GOERLI_TESTNET_CODE,
GOERLI_TESTNET_CHAINID,
POA_SOKOL_CODE,
POA_SOKOL_CHAINID,
} = require('./enums')

module.exports = createPocketClient

function createPocketClient ({ network }) {
const networkIDs = getNetworkIds({ network })
const networkID = getNetworkID({ network })
const pocketMiddleware = createPocketMiddleware(devid, {
netID: networkIDs.netId,
network: networkIDs.ticker,
netID: networkID.netId,
network: networkID.ticker,
})
const pocketProvider = providerFromMiddleware(pocketMiddleware)
const blockTracker = new BlockTracker({ provider: pocketProvider })
Expand All @@ -61,64 +34,11 @@ function createPocketClient ({ network }) {
return { networkMiddleware, blockTracker }
}

function getNetworkIds ({ network }) {
let chainId
let netId
let ticker
switch (network) {
case MAINNET:
netId = MAINNET_CODE.toString()
chainId = MAINNET_CHAINID
ticker = ETH_TICK
break
case ROPSTEN:
netId = ROPSTEN_CODE.toString()
chainId = ROPSTEN_CHAINID
ticker = ETH_TICK
break
case RINKEBY:
netId = RINKEBY_CODE.toString()
chainId = RINKEBY_CHAINID
ticker = ETH_TICK
break
case KOVAN:
netId = KOVAN_CODE.toString()
chainId = KOVAN_CHAINID
ticker = ETH_TICK
break
case GOERLI_TESTNET:
netId = GOERLI_TESTNET_CODE.toString()
chainId = GOERLI_TESTNET_CHAINID
ticker = ETH_TICK
break
case POA:
netId = POA_CODE.toString()
chainId = POA_CHAINID
ticker = POA_TICK
break
case DAI:
netId = DAI_CODE.toString()
chainId = DAI_CHAINID
ticker = POA_TICK
break
case POA_SOKOL:
netId = POA_SOKOL_CODE.toString()
chainId = POA_SOKOL_CHAINID
ticker = POA_TICK
break
default:
throw new Error(`createPocketClient - unknown network "${network}"`)
}
return {
chainId, netId, ticker,
}
}

function createNetworkAndChainIdMiddleware ({ network }) {
const networkIds = getNetworkIds({network})
const networkID = getNetworkID({network})

return createScaffoldMiddleware({
eth_chainId: networkIds.chainId,
net_version: networkIds.netId,
eth_chainId: networkID.chainId,
net_version: networkID.netId,
})
}
14 changes: 12 additions & 2 deletions app/scripts/controllers/network/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ const LOCALHOST = 'localhost'

const ETH_TICK = 'ETH'
const POA_TICK = 'POA'
const RSK_TICK = 'RBTC'
const CLASSIC_TICK = 'ETC'

const MAINNET_CHAINID = '0X01'
const MAINNET_CHAINID = '0x01'
const ROPSTEN_CHAINID = '0x03'
const RINKEBY_CHAINID = '0x04'
const KOVAN_CHAINID = '0x2a'
const GOERLI_TESTNET_CHAINID = '0X5'
const GOERLI_TESTNET_CHAINID = '0x5'
const POA_CHAINID = '0x63'
const DAI_CHAINID = '0x64'
const POA_SOKOL_CHAINID = '0x4D'
const RSK_CHAINID = '0x1E'
const RSK_TESTNET_CHAINID = '0x1F'
const CLASSIC_CHAINID = '0x3D'

const POA_CODE = 99
const DAI_CODE = 100
Expand Down Expand Up @@ -68,6 +73,8 @@ module.exports = {
POA,
POA_TICK,
ETH_TICK,
RSK_TICK,
CLASSIC_TICK,
MAINNET_CHAINID,
ROPSTEN_CHAINID,
RINKEBY_CHAINID,
Expand All @@ -76,6 +83,8 @@ module.exports = {
POA_CHAINID,
DAI_CHAINID,
POA_SOKOL_CHAINID,
RSK_CHAINID,
RSK_TESTNET_CHAINID,
DAI,
POA_SOKOL,
MAINNET,
Expand All @@ -96,6 +105,7 @@ module.exports = {
KOVAN_CODE,
GOERLI_TESTNET_CODE,
CLASSIC_CODE,
CLASSIC_CHAINID,
RSK_CODE,
RSK_TESTNET_CODE,
POA_DISPLAY_NAME,
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ module.exports = class NetworkController extends EventEmitter {
//

_switchNetwork (opts) {
const previousNetworkID = this.getNetworkState()
this.setNetworkState('loading')
this._configureProvider(opts)
this.emit('networkDidChange', opts.type)
this.emit('networkDidChange', opts.type, previousNetworkID)
}

_configureProvider (opts) {
Expand Down
19 changes: 17 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const ethUtil = require('ethereumjs-util')
const sigUtil = require('eth-sig-util')
const { importTypes } = require('../../old-ui/app/accounts/import/enums')
const { LEDGER, TREZOR } = require('../../old-ui/app/components/connect-hardware/enum')
const { ifPOA, ifRSK } = require('../../old-ui/app/util')
const { ifPOA, ifRSK, getNetworkID } = require('../../old-ui/app/util')

const {
CLASSIC_CODE,
Expand Down Expand Up @@ -155,9 +155,24 @@ module.exports = class MetamaskController extends EventEmitter {
})

// ensure accountTracker updates balances after network change
this.networkController.on('networkDidChange', () => {
this.networkController.on('networkDidChange', (newType, previousNetworkIDStr) => {
this.accountTracker._updateAccounts()
this.detectTokensController.restartTokenDetection()

const previousNetworkID = parseInt(previousNetworkIDStr, 10)
const nextNetwork = getNetworkID({network: newType})
const nextNetworkID = parseInt(nextNetwork && nextNetwork.netId, 10)

if (nextNetworkID !== previousNetworkID) {
const isPreviousETC = previousNetworkID === CLASSIC_CODE
const isPreviousRSK = ifRSK(previousNetworkID)
const isNextETC = nextNetworkID === CLASSIC_CODE
const isNextRSK = ifRSK(nextNetworkID)
if (isPreviousETC || isPreviousRSK || isNextETC || isNextRSK) {
this.forgetDevice(LEDGER, false)
this.forgetDevice(TREZOR, false)
}
}
})

// key mgmt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AccountsDropdownItemView extends Component {
identity: PropTypes.object.isRequired,
actions: PropTypes.objectOf(PropTypes.func),
closeMenu: PropTypes.func,
network: PropTypes.string,
}

constructor (props) {
Expand Down Expand Up @@ -117,7 +118,7 @@ class AccountsDropdownItemView extends Component {
this.props.actions.showAccountDetail(address)
if (ifHardwareAcc(keyring)) {
if (isLedger(keyring.type)) {
const hdPaths = getHdPaths()
const hdPaths = getHdPaths(this.props.network)
return new Promise((resolve, reject) => {
this.props.actions.connectHardwareAndUnlockAddress(LEDGER, hdPaths[1].value, address)
.then(_ => resolve())
Expand Down
4 changes: 2 additions & 2 deletions old-ui/app/components/connect-hardware/account-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class AccountList extends Component {
}

renderHdPathSelector = () => {
const { onPathChange, selectedPath } = this.props
const { onPathChange, selectedPath, network } = this.props

const options = getHdPaths()
const options = getHdPaths(network)
return (
<div>
<h3 className="hw-connect__hdPath__title">Select HD Path</h3>
Expand Down
27 changes: 22 additions & 5 deletions old-ui/app/components/connect-hardware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ class ConnectHardwareForm extends Component {

async checkIfUnlocked () {
[TREZOR, LEDGER].forEach(async device => {
const unlocked = await this.props.checkHardwareStatus(device, this.props.defaultHdPaths[device])
const hdPath = this._setHdPath(device)
const unlocked = await this.props.checkHardwareStatus(device, hdPath)
if (unlocked) {
this.setState({unlocked: true})
this.getPage(device, 0, this.props.defaultHdPaths[device])
this.getPage(device, 0, hdPath)
}
})
}
Expand All @@ -66,8 +67,10 @@ class ConnectHardwareForm extends Component {
return null
}

const hdPath = this._setHdPath(device)

// Default values
this.getPage(device, 0, this.props.defaultHdPaths[device])
this.getPage(device, 0, hdPath)
}

onPathChange = (path) => {
Expand Down Expand Up @@ -201,7 +204,7 @@ class ConnectHardwareForm extends Component {
return (
<AccountList
onPathChange={this.onPathChange}
selectedPath={this.props.defaultHdPaths[this.state.device]}
selectedPath={this._setHdPath(this.state.device)}
device={this.state.device}
accounts={this.state.accounts}
selectedAccount={this.state.selectedAccount}
Expand Down Expand Up @@ -238,6 +241,18 @@ class ConnectHardwareForm extends Component {
</div>
)
}

_setHdPath (device) {
let hdPath
const {network, customHdPaths, defaultHdPaths} = this.props
const networkInteger = parseInt(network, 10)
if (customHdPaths.hasOwnProperty(networkInteger)) {
hdPath = customHdPaths[networkInteger][device]
} else {
hdPath = defaultHdPaths[device]
}
return hdPath
}
}

ConnectHardwareForm.propTypes = {
Expand All @@ -257,6 +272,7 @@ ConnectHardwareForm.propTypes = {
accounts: PropTypes.object,
address: PropTypes.string,
defaultHdPaths: PropTypes.object,
customHdPaths: PropTypes.object,
}

const mapStateToProps = state => {
Expand All @@ -266,7 +282,7 @@ const mapStateToProps = state => {
const accounts = getMetaMaskAccounts(state)
const numberOfExistingAccounts = Object.keys(identities).length
const {
appState: { defaultHdPaths },
appState: { defaultHdPaths, customHdPaths },
} = state

return {
Expand All @@ -275,6 +291,7 @@ const mapStateToProps = state => {
address: selectedAddress,
numberOfExistingAccounts,
defaultHdPaths,
customHdPaths,
}
}

Expand Down
Loading