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

address book entries by chainId #7205

Merged
merged 5 commits into from
Sep 21, 2019
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
56 changes: 56 additions & 0 deletions app/scripts/migrations/037.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const version = 37
const clone = require('clone')
const {
util,
} = require('gaba')

/**
* The purpose of this migration is to update the address book state
* to the new schema with chainId as a key.
* and to add the isEns flag to all entries
*/
module.exports = {
version,
migrate: async function (originalVersionedData) {
const versionedData = clone(originalVersionedData)
versionedData.meta.version = version
const state = versionedData.data
versionedData.data = transformState(state)
return versionedData
},
}

function transformState (state) {

if (state.AddressBookController) {
const ab = state.AddressBookController.addressBook

const chainIds = new Set()
const newAddressBook = {}

// add all of the chainIds to a set
for (const item in ab) {
chainIds.add(ab[item].chainId)
}

// fill the chainId object with the entries with the matching chainId
for (const id of chainIds.values()) {
// make an empty object entry for each chainId
newAddressBook[id] = {}
for (const address in ab) {
if (ab[address].chainId === id) {

ab[address].isEns = false
if (util.normalizeEnsName(ab[address].name)) {
ab[address].isEns = true
}
newAddressBook[id][address] = ab[address]
}
}
}

state.AddressBookController.addressBook = newAddressBook
}

return state
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ module.exports = [
require('./034'),
require('./035'),
require('./036'),
require('./037'),
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"extensionizer": "^1.0.1",
"fast-json-patch": "^2.0.4",
"fuse.js": "^3.2.0",
"gaba": "^1.6.0",
"gaba": "^1.7.0",
"human-standard-token-abi": "^2.0.0",
"jazzicon": "^1.2.0",
"json-rpc-engine": "^5.1.4",
Expand Down
16 changes: 10 additions & 6 deletions test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,17 @@
"currentCurrency": "usd",
"nativeCurrency": "ETH",
"conversionRate": 556.12,
"addressBook": [
{
"address": "0xc42edfcc21ed14dda456aa0756c153f7985d8813",
"name": "",
"chainId": 4
"addressBook": {
"4": {
"0xc42edfcc21ed14dda456aa0756c153f7985d8813": {
"address": "0xc42edfcc21ed14dda456aa0756c153f7985d8813",
"chainId": "4",
"isEns": false,
"memo": "",
"name": ""
}
}
],
},
"selectedTokenAddress": "0x108cf70c7d384c552f42c07c41c0e1e46d77ea0d",
"unapprovedMsgs": {},
"unapprovedMsgCount": 0,
Expand Down
121 changes: 121 additions & 0 deletions test/unit/migrations/037-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const assert = require('assert')
const migration37 = require('../../../app/scripts/migrations/037')

describe('migration #37', () => {
it('should update the version metadata', (done) => {
const oldStorage = {
'meta': {
'version': 36,
},
'data': {},
}

migration37.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.meta, {
'version': 37,
})
done()
})
.catch(done)
})

it('should transform old state to new format', (done) => {
const oldStorage = {
'meta': {},
'data': {
'AddressBookController': {
'addressBook': {
'0x1De7e54679bfF0c23856FbF547b2394e723FCA91': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA91',
chainId: '4',
memo: '',
name: 'account 3',
},
'0x32Be343B94f860124dC4fEe278FDCBD38C102D88': {
address: '0x32Be343B94f860124dC4fEe278FDCBD38C102D88',
chainId: '4',
memo: '',
name: 'account 2',
},
// there are no repeated addresses by the current implementation
'0x1De7e54679bfF0c23856FbF547b2394e723FCA93': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA93',
chainId: '2',
memo: '',
name: 'account 2',
},
},
},
},
}

migration37.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.data.AddressBookController.addressBook, {
'4': {
'0x1De7e54679bfF0c23856FbF547b2394e723FCA91': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA91',
chainId: '4',
isEns: false,
memo: '',
name: 'account 3',
},
'0x32Be343B94f860124dC4fEe278FDCBD38C102D88': {
address: '0x32Be343B94f860124dC4fEe278FDCBD38C102D88',
chainId: '4',
isEns: false,
memo: '',
name: 'account 2',
},
},
'2': {
'0x1De7e54679bfF0c23856FbF547b2394e723FCA93': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA93',
chainId: '2',
isEns: false,
memo: '',
name: 'account 2',
},
},
})
done()
})
.catch(done)
})

it('ens validation test', (done) => {
const oldStorage = {
'meta': {},
'data': {
'AddressBookController': {
'addressBook': {
'0x1De7e54679bfF0c23856FbF547b2394e723FCA91': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA91',
chainId: '4',
memo: '',
name: 'metamask.eth',
},
},
},
},
}

migration37.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.data.AddressBookController.addressBook, {
'4': {
'0x1De7e54679bfF0c23856FbF547b2394e723FCA91': {
address: '0x1De7e54679bfF0c23856FbF547b2394e723FCA91',
chainId: '4',
isEns: true,
memo: '',
name: 'metamask.eth',
},
},
})
done()
})
.catch(done)
})
})
14 changes: 8 additions & 6 deletions ui/app/pages/send/tests/send-selectors-test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ module.exports = {
'address': '0xd85a4b6a394794842887b8284293d69163007bbb',
},
},
'addressBook': [
{
'address': '0x06195827297c7a80a443b6894d3bdb8824b43896',
'name': 'Address Book Account 1',
'chainId': '3',
'addressBook': {
'3': {
'0x06195827297c7a80a443b6894d3bdb8824b43896': {
'address': '0x06195827297c7a80a443b6894d3bdb8824b43896',
'name': 'Address Book Account 1',
'chainId': '3',
},
},
],
},
'tokens': [
{
'address': '0x1a195821297c7a80a433b6894d3bdb8824b43896',
Expand Down
8 changes: 4 additions & 4 deletions ui/app/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ function conversionRateSelector (state) {

function getAddressBook (state) {
const network = state.metamask.network
const addressBookEntries = Object.values(state.metamask.addressBook)
.filter(entry => entry.chainId && entry.chainId.toString() === network)

return addressBookEntries
if (!state.metamask.addressBook[network]) {
return []
}
return Object.values(state.metamask.addressBook[network])
}

function getAddressBookEntry (state, address) {
Expand Down
16 changes: 10 additions & 6 deletions ui/app/selectors/tests/selectors-test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ module.exports = {
'address': '0xd85a4b6a394794842887b8284293d69163007bbb',
},
},
'addressBook': [
{
'address': '0x06195827297c7a80a443b6894d3bdb8824b43896',
'name': 'Address Book Account 1',
'chainId': '3',
'addressBook': {
'3': {
'0x06195827297c7a80a443b6894d3bdb8824b43896': {
'address': '0x06195827297c7a80a443b6894d3bdb8824b43896',
'chainId': '3',
'isEns': false,
'memo': '',
'name': 'Address Book Account 1',
},
},
],
},
'tokens': [
{
'address': '0x1a195821297c7a80a433b6894d3bdb8824b43896',
Expand Down
8 changes: 5 additions & 3 deletions ui/app/selectors/tests/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ describe('selectors', () => {
getAddressBook(mockState),
[
{
address: '0x06195827297c7a80a443b6894d3bdb8824b43896',
name: 'Address Book Account 1',
chainId: '3',
'address': '0x06195827297c7a80a443b6894d3bdb8824b43896',
'chainId': '3',
'isEns': false,
'memo': '',
'name': 'Address Book Account 1',
},
],
)
Expand Down
Loading