Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
0451133
snapshot
dbrans Nov 23, 2023
a284586
Added tests
dbrans Dec 4, 2023
7ca6069
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Dec 4, 2023
a640669
Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
dbrans Dec 4, 2023
d49abe1
Added migration
dbrans Dec 4, 2023
a18c044
Updated migrations/index.js
dbrans Dec 4, 2023
165de83
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Dec 6, 2023
2c64dbe
snapshot
dbrans Dec 6, 2023
0025d41
Abstract out the generic and name bridge code
dbrans Dec 11, 2023
19c59f7
Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
dbrans Dec 11, 2023
a7b71f6
Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
dbrans Dec 13, 2023
8b89c3e
snapshot
dbrans Dec 14, 2023
0bed4cf
snapshot
dbrans Dec 14, 2023
717bfcd
snapshot
dbrans Dec 15, 2023
9486486
snapshot
dbrans Dec 18, 2023
eff695d
snapshot
dbrans Dec 18, 2023
c20a7ff
petnames bridge refactor
dbrans Dec 19, 2023
52f474d
Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
dbrans Dec 19, 2023
2e15a4c
Fixed migration
dbrans Dec 19, 2023
1dbfa17
snapshot
dbrans Dec 20, 2023
c32db5d
Merge remote-tracking branch 'origin/develop' into dbrans/petnames-ac…
dbrans Jan 3, 2024
b24fece
snapshot
dbrans Jan 3, 2024
6bb5f70
remove .vscode/launch.json from repo
dbrans Jan 3, 2024
0ce6a38
remove .vscode/launch.json from repo
dbrans Jan 3, 2024
efe6e02
fix import type in ppom-middleware
dbrans Jan 3, 2024
b91c0d8
Added launch.json configuration to debug Jest
dbrans Jan 4, 2024
caaed66
Added launch.json configuration to debug Jest
dbrans Jan 4, 2024
06be366
fix lint
dbrans Jan 4, 2024
e28fa77
update to name-controller v4
dbrans Jan 4, 2024
6e763af
removing launch.json
dbrans Jan 4, 2024
85e4a61
Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
dbrans Jan 4, 2024
c9d589a
Incorporate preview build of https://github.com/MetaMask/core/pull/3705
dbrans Jan 4, 2024
4fb15c7
yarn dedupe
dbrans Jan 4, 2024
cc6be8e
Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
dbrans Jan 4, 2024
fd1bc14
yarn lavamoat:auto
dbrans Jan 4, 2024
599b9c0
Merge branch 'dbrans/name-controller-v4' into dbrans/petnames-accountids
dbrans Jan 4, 2024
4f5014a
Fixing type errors related to SetNameRequest changes
dbrans Jan 4, 2024
6bd5953
Update LavaMoat policies
metamaskbot Jan 4, 2024
c491ad5
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 5, 2024
90dcdaa
revert .gitignore to develop
dbrans Jan 5, 2024
e77780f
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 5, 2024
cf857ca
remove preview build resolution
dbrans Jan 5, 2024
6f098d3
remove preview build resolution continued
dbrans Jan 5, 2024
d3aa3f6
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 8, 2024
1e6a44b
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 8, 2024
7903808
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 8, 2024
27bf4e1
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 9, 2024
6c4f7ee
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 9, 2024
944f119
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 10, 2024
f75ecdc
snapshot
dbrans Jan 11, 2024
75507cd
Use origin in AbstractPetnamesBridge and subclasses
dbrans Jan 11, 2024
f05c1c7
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 11, 2024
8f90773
fix error
dbrans Jan 11, 2024
08aad20
fixing useName.test
dbrans Jan 11, 2024
d09047f
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 11, 2024
f6adbfe
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 12, 2024
aada840
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 15, 2024
84783b3
remove mmcore preview build
dbrans Jan 15, 2024
c5be096
update yarn.lock
dbrans Jan 15, 2024
c6b30f7
update to [email protected]
dbrans Jan 15, 2024
968b9e4
yarn dedupe
dbrans Jan 15, 2024
c2178ae
added tests for fallback variation to useName.tests.ts
dbrans Jan 15, 2024
368f659
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 15, 2024
032e327
updating tests to mock messenger.registerInitialEventPayload
dbrans Jan 15, 2024
dae99b9
yarn.lock
dbrans Jan 15, 2024
cd3fd78
update BaseControllerV2 to 4.2.0
dbrans Jan 15, 2024
3dd4ee1
yarn dedupe
dbrans Jan 15, 2024
4f50649
addressing comments
dbrans Jan 16, 2024
bf62b2b
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 17, 2024
687a3df
name is cleared but there are still proposed names – fallback variati…
dbrans Jan 18, 2024
fb1c7b0
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 18, 2024
1609115
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 18, 2024
304c966
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 18, 2024
a6a8649
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 19, 2024
980d506
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 20, 2024
ec0e5ba
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 24, 2024
3b9bd13
Account list e2e test: split account details e2e tests into multiple …
chloeYue Jan 24, 2024
b218fcd
Part of #20163 for file ui\components\app\terms-of-use-popup\terms-of…
PrgrmrHarshShukla Jan 24, 2024
0e1c5e7
FEAT: Update what's new and settings copy to communicate additional n…
segun Jan 24, 2024
74b7886
fix: Blockaid What's New os theme -> dark mode (#22649)
digiwand Jan 24, 2024
174a084
UX: Send Flow: Add 'Sent to Contract' warning to new send page (#22551)
darkwing Jan 24, 2024
9a07b71
feat: check for ledger error codes in the message and display a more …
montelaidev Jan 24, 2024
a6c017d
chore: Upgrade `@metamask/controller-utils` to `v8.0.1` (#22500)
Gudahtt Jan 24, 2024
30b272e
feat: Enable ppom on other chains (#22633)
segun Jan 25, 2024
02a53aa
Add imToken Wallet support with EIP-4527 (#21804)
makoshan Jan 25, 2024
f21d35c
feat: Add PPOM Validation to normal send. (#22321)
segun Jan 25, 2024
35d352e
feat: hardcode svg network (#22643)
salimtb Jan 25, 2024
957dc30
feat: update UI elements to use `getSelectedInternalAccount` selector…
montelaidev Jan 25, 2024
24e09cb
fix: Trezor connect - update to v9 in trezor-usb-permissions (#22591)
martykan Jan 25, 2024
35726ff
Update Gas API URL (#22544)
dan437 Jan 25, 2024
49219f1
E2E test: Improve fill method in driver (#22652)
chloeYue Jan 25, 2024
def6447
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 25, 2024
38eaac7
normalizing (lowercase) addresses in abstract bridge
dbrans Jan 26, 2024
932798c
fixing useName to return fallback if variation has no name
dbrans Jan 26, 2024
0d5e81d
adding origin to migration
dbrans Jan 26, 2024
02f0b31
fixing tests
dbrans Jan 26, 2024
726f30e
fix lint
dbrans Jan 26, 2024
325fb58
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 26, 2024
f38a919
readability
dbrans Jan 26, 2024
e6763ef
Merge branch 'develop' into dbrans/petnames-accountids
dbrans Jan 26, 2024
30c30b4
reverting app/scripts/lib/keyring-snaps-permissions.test.ts
dbrans Jan 26, 2024
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
153 changes: 153 additions & 0 deletions app/scripts/lib/setupAccountLabelsPetnamesBridge.test.ts
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_CHAIN_ID,
} 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_CHAIN_ID,
name: NAME_MOCK,
});
});

it('updates entry when account id is updated', () => {
nameControllerDefault = createNameControllerMock({
[ADDRESS_MOCK]: {
[ACCOUNT_LABEL_CHAIN_ID]: {
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_CHAIN_ID,
});
});

it('deletes entry when address book entry is deleted', () => {
nameControllerDefault = createNameControllerMock({
[ADDRESS_MOCK]: {
[ACCOUNT_LABEL_CHAIN_ID]: {
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_CHAIN_ID,
});
});
});
105 changes: 105 additions & 0 deletions app/scripts/lib/setupAccountLabelsPetnamesBridge.ts
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';

export type IdentityEntry = {
address: string;
name: string;
};

export type PertinentState = {
identities: { [address: string]: IdentityEntry };
};

export type PertinentPreferencesController = {
store: {
getState: () => PertinentState;
subscribe: (callback: (state: PertinentState) => void) => void;
};
};

const MAINNET_CHAIN_ID = '0x1';
export const ACCOUNT_LABEL_NAME_TYPE = NameType.ETHEREUM_ADDRESS;
export const ACCOUNT_LABEL_CHAIN_ID = MAINNET_CHAIN_ID;

/**
* 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(
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(
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_CHAIN_ID,
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_CHAIN_ID,
value: entry.address,
name: null,
});

log.debug('Removed petname following account label removal', entry);
}

oldEntries = cloneDeep(newEntries);
});
}
6 changes: 6 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ import { keyringSnapPermissionsBuilder } from './lib/keyring-snaps-permissions';
///: BEGIN:ONLY_INCLUDE_IF(petnames)
import { SnapsNameProvider } from './lib/SnapsNameProvider';
import { AddressBookPetnamesBridge } from './lib/AddressBookPetnamesBridge';
import setupAccountLabelsPetnamesBridge from './lib/setupAccountLabelsPetnamesBridge';
///: END:ONLY_INCLUDE_IF

///: BEGIN:ONLY_INCLUDE_IF(blockaid)
Expand Down Expand Up @@ -1775,6 +1776,11 @@ export default class MetamaskController extends EventEmitter {
allowedEvents: ['NameController:stateChange'],
}),
}).init();

setupAccountLabelsPetnamesBridge(
this.preferencesController,
this.nameController,
);
///: END:ONLY_INCLUDE_IF

// ensure accountTracker updates balances after network change
Expand Down
Loading