Handle different contracts with the same address on different networks

This commit is contained in:
Victor Baranov 2018-12-04 21:23:21 +03:00
parent 3e3371a4b8
commit c65b7c2275
5 changed files with 265 additions and 252 deletions

View File

@ -56,7 +56,7 @@ AccountDetailScreen.prototype.render = function () {
this.props.dispatch(actions.showAddSuggestedTokenPage())
}
const currentKeyring = getCurrentKeyring(props.address, props.keyrings, props.identities)
const currentKeyring = getCurrentKeyring(props.address, network, props.keyrings, props.identities)
return (

View File

@ -24,7 +24,15 @@ class AccountDropdowns extends Component {
renderAccounts () {
const { identities, selected, keyrings, network } = this.props
const accountOrder = keyrings.reduce((list, keyring) => list.concat(keyring.accounts), [])
const simpleAddressType = 'Simple Address'
const accountOrder = keyrings.reduce((list, keyring) => {
if (keyring.type === simpleAddressType && keyring.network === network) {
list = list.concat(keyring.accounts)
} else if (keyring.type !== simpleAddressType) {
list = list.concat(keyring.accounts)
}
return list
}, [])
return accountOrder.map((address, index) => {
const identity = identities[address]
@ -33,7 +41,7 @@ class AccountDropdowns extends Component {
}
const isSelected = identity.address === selected
const keyring = getCurrentKeyring(address, keyrings, identities)
const keyring = getCurrentKeyring(address, network, keyrings, identities)
// display contract acc only for network where it was created
if (ifContractAcc(keyring)) {
@ -111,7 +119,7 @@ class AccountDropdowns extends Component {
}
ifHardwareAcc (address) {
const keyring = getCurrentKeyring(address, this.props.keyrings, this.props.identities)
const keyring = getCurrentKeyring(address, this.props.network, this.props.keyrings, this.props.identities)
if (keyring && keyring.type.search('Hardware') !== -1) {
return true
}
@ -343,7 +351,7 @@ class AccountDropdowns extends Component {
if (!isNaN(this.props.network)) {
const { selected, network, keyrings, identities } = this.props
if (network !== prevProps.network) {
const keyring = getCurrentKeyring(selected, keyrings, identities)
const keyring = getCurrentKeyring(selected, this.props.network, keyrings, identities)
if (ifContractAcc(keyring)) {
if (keyring.network !== this.props.network) {
const firstKeyring = keyrings[0]

View File

@ -293,12 +293,17 @@ function countSignificantDecimals (val, len) {
*
* returns {object} keyring object corresponding to unlocked address
**/
function getCurrentKeyring (address, keyrings, identities) {
function getCurrentKeyring (address, network, keyrings, identities) {
const identity = identities[address]
const simpleAddress = identity && identity.address.substring(2).toLowerCase()
const keyring = keyrings && keyrings.find((kr) => {
return kr.accounts.includes(simpleAddress) ||
kr.accounts.includes(address)
if (kr.type === 'Simple Address') {
return kr.network === network && (kr.accounts.includes(simpleAddress) ||
kr.accounts.includes(address))
} else {
return kr.accounts.includes(simpleAddress) ||
kr.accounts.includes(address)
}
})
return keyring

480
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,7 +29,7 @@ const keyrings = [
},
]
describe('getCurrentKeyring(address, keyrings, identities) function', () => {
describe('getCurrentKeyring(address, keyrings, network, identities) function', () => {
const address = '0xb55e278d3e8ff77ec95749b51b526a236502b6fe'
const identities = {
'0x99a22ce737b6a48f44cad6331432ce98693cad07': {name: 'Account 1', address: '0x99a22ce737b6a48f44cad6331432ce98693cad07'},
@ -44,11 +44,11 @@ describe('getCurrentKeyring(address, keyrings, identities) function', () => {
'0x99a22ce737b6a48f44cad6331432ce98693cad07',
'0xa37bd195eebfc4ccd02529125b3e691fb6fe3a53',
],
}, getCurrentKeyring(address, keyrings, identities))
}, getCurrentKeyring(address, 1, keyrings, identities))
})
it('returns keyring matched to address', () => {
assert.deepEqual(null, getCurrentKeyring('0x9053a0Fe25fc45367d06B2e04528BDb4c1A03eaB', keyrings, identities))
it('doesn\'t return keyring matched to address', () => {
assert.deepEqual(null, getCurrentKeyring('0x9053a0Fe25fc45367d06B2e04528BDb4c1A03eaB', 1, keyrings, identities))
})
})