-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address book entries by chainId (#7205)
- Loading branch information
1 parent
e44a228
commit e86cebd
Showing
10 changed files
with
433 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,5 @@ module.exports = [ | |
require('./034'), | ||
require('./035'), | ||
require('./036'), | ||
require('./037'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.