- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.4k
 
Use account identities as default petnames #21956
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            101 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      0451133
              
                snapshot
              
              
                dbrans a284586
              
                Added tests
              
              
                dbrans 7ca6069
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans a640669
              
                Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
              
              
                dbrans d49abe1
              
                Added migration
              
              
                dbrans a18c044
              
                Updated migrations/index.js
              
              
                dbrans 165de83
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 2c64dbe
              
                snapshot
              
              
                dbrans 0025d41
              
                Abstract out the generic and name bridge code
              
              
                dbrans 19c59f7
              
                Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
              
              
                dbrans a7b71f6
              
                Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
              
              
                dbrans 8b89c3e
              
                snapshot
              
              
                dbrans 0bed4cf
              
                snapshot
              
              
                dbrans 717bfcd
              
                snapshot
              
              
                dbrans 9486486
              
                snapshot
              
              
                dbrans eff695d
              
                snapshot
              
              
                dbrans c20a7ff
              
                petnames bridge refactor
              
              
                dbrans 52f474d
              
                Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
              
              
                dbrans 2e15a4c
              
                Fixed migration
              
              
                dbrans 1dbfa17
              
                snapshot
              
              
                dbrans c32db5d
              
                Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
              
              
                dbrans b24fece
              
                snapshot
              
              
                dbrans 6bb5f70
              
                remove .vscode/launch.json from repo
              
              
                dbrans 0ce6a38
              
                remove .vscode/launch.json from repo
              
              
                dbrans efe6e02
              
                fix import type in ppom-middleware
              
              
                dbrans b91c0d8
              
                Added launch.json configuration to debug Jest
              
              
                dbrans caaed66
              
                Added launch.json configuration to debug Jest
              
              
                dbrans 06be366
              
                fix lint
              
              
                dbrans e28fa77
              
                update to name-controller v4
              
              
                dbrans 6e763af
              
                removing launch.json
              
              
                dbrans 85e4a61
              
                Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
              
              
                dbrans c9d589a
              
                Incorporate preview build of https://github.com/MetaMask/core/pull/3705
              
              
                dbrans 4fb15c7
              
                yarn dedupe
              
              
                dbrans cc6be8e
              
                Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
              
              
                dbrans fd1bc14
              
                yarn lavamoat:auto
              
              
                dbrans 599b9c0
              
                Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
              
              
                dbrans 4f5014a
              
                Fixing type errors related to SetNameRequest changes
              
              
                dbrans 6bd5953
              
                Update LavaMoat policies
              
              
                metamaskbot c491ad5
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 90dcdaa
              
                revert .gitignore to develop
              
              
                dbrans e77780f
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans cf857ca
              
                remove preview build resolution
              
              
                dbrans 6f098d3
              
                remove preview build resolution continued
              
              
                dbrans d3aa3f6
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 1e6a44b
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 7903808
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 27bf4e1
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 6c4f7ee
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 944f119
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans f75ecdc
              
                snapshot
              
              
                dbrans 75507cd
              
                Use origin in AbstractPetnamesBridge and subclasses
              
              
                dbrans f05c1c7
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 8f90773
              
                fix error
              
              
                dbrans 08aad20
              
                fixing useName.test
              
              
                dbrans d09047f
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans f6adbfe
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans aada840
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 84783b3
              
                remove mmcore preview build
              
              
                dbrans c5be096
              
                update yarn.lock
              
              
                dbrans c6b30f7
              
                update to [email protected]
              
              
                dbrans 968b9e4
              
                yarn dedupe
              
              
                dbrans c2178ae
              
                added tests for fallback variation to useName.tests.ts
              
              
                dbrans 368f659
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 032e327
              
                updating tests to mock messenger.registerInitialEventPayload
              
              
                dbrans dae99b9
              
                yarn.lock
              
              
                dbrans cd3fd78
              
                update BaseControllerV2 to 4.2.0
              
              
                dbrans 3dd4ee1
              
                yarn dedupe
              
              
                dbrans 4f50649
              
                addressing comments
              
              
                dbrans bf62b2b
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 687a3df
              
                name is cleared but there are still proposed names – fallback variati…
              
              
                dbrans fb1c7b0
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 1609115
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 304c966
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans a6a8649
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 980d506
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans ec0e5ba
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 3b9bd13
              
                Account list e2e test: split account details e2e tests into multiple …
              
              
                chloeYue b218fcd
              
                Part of #20163 for file ui\components\app\terms-of-use-popup\terms-of…
              
              
                PrgrmrHarshShukla 0e1c5e7
              
                FEAT: Update what's new and settings copy to communicate additional n…
              
              
                segun 74b7886
              
                fix: Blockaid What's New os theme -> dark mode (#22649)
              
              
                digiwand 174a084
              
                UX: Send Flow: Add 'Sent to Contract' warning to new send page (#22551)
              
              
                darkwing 9a07b71
              
                feat: check for ledger error codes in the message and display a more …
              
              
                montelaidev a6c017d
              
                chore: Upgrade `@metamask/controller-utils` to `v8.0.1` (#22500)
              
              
                Gudahtt 30b272e
              
                feat: Enable ppom on other chains (#22633)
              
              
                segun 02a53aa
              
                Add imToken Wallet support with EIP-4527 (#21804)
              
              
                makoshan f21d35c
              
                feat: Add PPOM Validation to normal send. (#22321)
              
              
                segun 35d352e
              
                feat: hardcode svg network (#22643)
              
              
                salimtb 957dc30
              
                feat: update UI elements to use `getSelectedInternalAccount` selector…
              
              
                montelaidev 24e09cb
              
                fix: Trezor connect - update to v9 in trezor-usb-permissions (#22591)
              
              
                martykan 35726ff
              
                Update Gas API URL (#22544)
              
              
                dan437 49219f1
              
                E2E test: Improve fill method in driver (#22652)
              
              
                chloeYue def6447
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 38eaac7
              
                normalizing (lowercase) addresses in abstract bridge
              
              
                dbrans 932798c
              
                fixing useName to return fallback if variation has no name
              
              
                dbrans 0d5e81d
              
                adding origin to migration
              
              
                dbrans 02f0b31
              
                fixing tests
              
              
                dbrans 726f30e
              
                fix lint
              
              
                dbrans 325fb58
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans f38a919
              
                readability
              
              
                dbrans e6763ef
              
                Merge branch 'develop' into dbrans/petnames-accountids
              
              
                dbrans 30c30b4
              
                reverting app/scripts/lib/keyring-snaps-permissions.test.ts
              
              
                dbrans File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            153 changes: 153 additions & 0 deletions
          
          153 
        
  app/scripts/lib/setupAccountLabelsPetnamesBridge.test.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,153 @@ | ||
| import { NameController } from '@metamask/name-controller'; | ||
| import setupAccountLabelsPetnamesBridge, { | ||
| PertinentPreferencesController, | ||
| PertinentState, | ||
| ACCOUNT_LABEL_NAME_TYPE, | ||
| ACCOUNT_LABEL_VARIATION, | ||
| } from './setupAccountLabelsPetnamesBridge'; | ||
| 
     | 
||
| const ADDRESS_MOCK = '0xabc'; | ||
| const NAME_MOCK = 'testName'; | ||
| const NAME_2_MOCK = 'testName2'; | ||
| 
     | 
||
| interface MockPreferencesController | ||
| extends jest.Mocked<PertinentPreferencesController> { | ||
| store: jest.Mocked<PertinentPreferencesController['store']>; | ||
| } | ||
| 
     | 
||
| function createPreferencesControllerMock( | ||
| identities: PertinentState['identities'] = {}, | ||
| ): MockPreferencesController { | ||
| return { | ||
| store: { | ||
| getState: jest.fn(() => ({ identities })), | ||
| subscribe: jest.fn(), | ||
| }, | ||
| }; | ||
| } | ||
| 
     | 
||
| function createNameControllerMock( | ||
| state: any = {}, | ||
| ): jest.Mocked<NameController> { | ||
| return { | ||
| state: { | ||
| names: { | ||
| ethereumAddress: state, | ||
| }, | ||
| }, | ||
| setName: jest.fn(), | ||
| } as any; | ||
| } | ||
| 
     | 
||
| describe('setupAccountLabelsPetnamesBridge', () => { | ||
| let preferencesControllerDefault: MockPreferencesController; | ||
| let nameControllerDefault: NameController; | ||
| 
     | 
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| 
     | 
||
| preferencesControllerDefault = createPreferencesControllerMock(); | ||
| nameControllerDefault = createNameControllerMock(); | ||
| }); | ||
| 
     | 
||
| it('adds entry when account id entry added', () => { | ||
| setupAccountLabelsPetnamesBridge( | ||
| preferencesControllerDefault, | ||
| nameControllerDefault, | ||
| ); | ||
| 
     | 
||
| preferencesControllerDefault.store.subscribe.mock.calls[0][0]({ | ||
| identities: { | ||
| [ADDRESS_MOCK]: { | ||
| address: ADDRESS_MOCK, | ||
| name: NAME_MOCK, | ||
| }, | ||
| }, | ||
| }); | ||
| 
     | 
||
| expect(nameControllerDefault.setName).toHaveBeenCalledTimes(1); | ||
| expect(nameControllerDefault.setName).toHaveBeenCalledWith({ | ||
| value: ADDRESS_MOCK, | ||
| type: ACCOUNT_LABEL_NAME_TYPE, | ||
| variation: ACCOUNT_LABEL_VARIATION, | ||
| name: NAME_MOCK, | ||
| }); | ||
| }); | ||
| 
     | 
||
| it('updates entry when account id is updated', () => { | ||
| nameControllerDefault = createNameControllerMock({ | ||
| [ADDRESS_MOCK]: { | ||
| [ACCOUNT_LABEL_VARIATION]: { | ||
| name: NAME_MOCK, | ||
| proposedNames: {}, | ||
| }, | ||
| }, | ||
| }); | ||
| 
     | 
||
| preferencesControllerDefault = createPreferencesControllerMock({ | ||
| [ADDRESS_MOCK]: { | ||
| address: ADDRESS_MOCK, | ||
| name: NAME_MOCK, | ||
| }, | ||
| }); | ||
| 
     | 
||
| setupAccountLabelsPetnamesBridge( | ||
| preferencesControllerDefault, | ||
| nameControllerDefault, | ||
| ); | ||
| 
     | 
||
| preferencesControllerDefault.store.subscribe.mock.calls[0][0]({ | ||
| identities: { | ||
| [ADDRESS_MOCK]: { | ||
| address: ADDRESS_MOCK, | ||
| name: NAME_2_MOCK, | ||
| }, | ||
| }, | ||
| }); | ||
| 
     | 
||
| expect(nameControllerDefault.setName).toHaveBeenCalledTimes(1); | ||
| expect(nameControllerDefault.setName).toHaveBeenCalledWith({ | ||
| value: ADDRESS_MOCK, | ||
| type: ACCOUNT_LABEL_NAME_TYPE, | ||
| name: NAME_2_MOCK, | ||
| sourceId: undefined, | ||
| variation: ACCOUNT_LABEL_VARIATION, | ||
| }); | ||
| }); | ||
| 
     | 
||
| it('deletes entry when address book entry is deleted', () => { | ||
| nameControllerDefault = createNameControllerMock({ | ||
| [ADDRESS_MOCK]: { | ||
| [ACCOUNT_LABEL_VARIATION]: { | ||
| name: NAME_MOCK, | ||
| proposedNames: {}, | ||
| } as any, | ||
| }, | ||
| }); | ||
| 
     | 
||
| preferencesControllerDefault = createPreferencesControllerMock({ | ||
| [ADDRESS_MOCK]: { | ||
| address: ADDRESS_MOCK, | ||
| name: NAME_MOCK, | ||
| }, | ||
| }); | ||
| 
     | 
||
| setupAccountLabelsPetnamesBridge( | ||
| preferencesControllerDefault, | ||
| nameControllerDefault, | ||
| ); | ||
| 
     | 
||
| preferencesControllerDefault.store.subscribe.mock.calls[0][0]({ | ||
| identities: {}, | ||
| }); | ||
| 
     | 
||
| expect(nameControllerDefault.setName).toHaveBeenCalledTimes(1); | ||
| expect(nameControllerDefault.setName).toHaveBeenCalledWith({ | ||
| value: ADDRESS_MOCK, | ||
| type: ACCOUNT_LABEL_NAME_TYPE, | ||
| name: null, | ||
| sourceId: undefined, | ||
| variation: ACCOUNT_LABEL_VARIATION, | ||
| }); | ||
| }); | ||
| }); | ||
  
    
      This file contains hidden or 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,105 @@ | ||
| import { NameController, NameType } from '@metamask/name-controller'; | ||
| import { cloneDeep } from 'lodash'; | ||
| import log from 'loglevel'; | ||
| 
     | 
||
| type IdentityEntry = { | ||
| address: string; | ||
| name: string; | ||
| }; | ||
| 
     | 
||
| export type PertinentState = { | ||
                
      
                  matthewwalsh0 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| identities: { [address: string]: IdentityEntry }; | ||
| }; | ||
| 
     | 
||
| export type PertinentPreferencesController = { | ||
                
      
                  matthewwalsh0 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| store: { | ||
| getState: () => PertinentState; | ||
| subscribe: (callback: (state: PertinentState) => void) => void; | ||
| }; | ||
| }; | ||
| 
     | 
||
| const MAINNET_CHAIN_ID = '0x1'; | ||
                
      
                  matthewwalsh0 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| export const ACCOUNT_LABEL_NAME_TYPE = NameType.ETHEREUM_ADDRESS; | ||
| export const ACCOUNT_LABEL_VARIATION = MAINNET_CHAIN_ID; | ||
                
      
                  dbrans marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| /** | ||
| * Groups the entries in the old and new entries arrays into added, updated and | ||
| * deleted entries, like a patch. | ||
| * | ||
| * @param oldEntries - The last seen IdentityEntries in the preferences controller. | ||
| * @param newEntries - The new IdentityEntries. | ||
| */ | ||
| function groupEntries( | ||
                
      
                  matthewwalsh0 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| oldEntries: IdentityEntry[], | ||
| newEntries: IdentityEntry[], | ||
| ): { | ||
| added: IdentityEntry[]; | ||
| updated: IdentityEntry[]; | ||
| deleted: IdentityEntry[]; | ||
| } { | ||
| const added = newEntries.filter( | ||
| (newEntry) => | ||
| !oldEntries.some((oldEntry) => oldEntry.address === newEntry.address), | ||
| ); | ||
| 
     | 
||
| const updated = newEntries.filter((newEntry) => | ||
| oldEntries.some( | ||
| (oldEntry) => | ||
| oldEntry.address === newEntry.address && | ||
| oldEntry.name !== newEntry.name, | ||
| ), | ||
| ); | ||
| 
     | 
||
| const deleted = oldEntries.filter( | ||
| (oldEntry) => | ||
| !newEntries.some((newEntry) => newEntry.address === oldEntry.address), | ||
| ); | ||
| 
     | 
||
| return { added, updated, deleted }; | ||
| } | ||
| 
     | 
||
| /** | ||
| * Sets up a bridge between the account labels in the preferences controller and | ||
| * the petnames in the name controller. | ||
| * | ||
| * @param preferencesController - The preferences controller to listen to. | ||
| * @param nameController - The name controller to update. | ||
| */ | ||
| export default function setupAccountLabelsPetnamesBridge( | ||
                
      
                  dbrans marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| preferencesController: PertinentPreferencesController, | ||
| nameController: NameController, | ||
| ) { | ||
| const { identities } = preferencesController.store.getState(); | ||
| 
     | 
||
| let oldEntries = Object.values(identities); | ||
| 
     | 
||
| preferencesController.store.subscribe(async (state) => { | ||
| const newEntries = Object.values(state.identities); | ||
| 
     | 
||
| const { added, updated, deleted } = groupEntries(oldEntries, newEntries); | ||
| 
     | 
||
| for (const entry of [...added, ...updated]) { | ||
| nameController.setName({ | ||
| type: ACCOUNT_LABEL_NAME_TYPE, | ||
| variation: ACCOUNT_LABEL_VARIATION, | ||
| value: entry.address, | ||
| name: entry.name, | ||
| }); | ||
| 
     | 
||
| log.debug('Updated petname following account label update', entry); | ||
| } | ||
| 
     | 
||
| for (const entry of deleted) { | ||
| nameController.setName({ | ||
| type: ACCOUNT_LABEL_NAME_TYPE, | ||
| variation: ACCOUNT_LABEL_VARIATION, | ||
| value: entry.address, | ||
| name: null, | ||
| }); | ||
| 
     | 
||
| log.debug('Removed petname following account label removal', entry); | ||
| } | ||
| 
     | 
||
| oldEntries = cloneDeep(newEntries); | ||
| }); | ||
| } | ||
  
    
      This file contains hidden or 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
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.