Skip to content

Commit

Permalink
Jl/caip multichain/fix wallet eip155 eth account assignment (#27769)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Sets and reads eth accounts from/to `wallet:eip155` scope 

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27769?quickstart=1)

## **Related issues**

Fixes: MetaMask/MetaMask-planning#3485

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
jiexi authored Oct 10, 2024
1 parent 7221bb6 commit 947dcd7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ describe('CAIP-25 eth_accounts adapters', () => {
notifications: [],
accounts: ['eip155:100:0x100'],
},
'wallet:eip155': {
methods: [],
notifications: [],
accounts: ['wallet:eip155:0x5'],
},
},
isMultichainOrigin: false,
});

expect(ethAccounts).toStrictEqual(['0x1', '0x2', '0x4', '0x3', '0x100']);
expect(ethAccounts).toStrictEqual([
'0x1',
'0x2',
'0x4',
'0x3',
'0x100',
'0x5',
]);
});
});

Expand Down Expand Up @@ -87,6 +99,10 @@ describe('CAIP-25 eth_accounts adapters', () => {
notifications: [],
accounts: ['eip155:100:0x100'],
},
'wallet:eip155': {
methods: [],
notifications: [],
},
},
isMultichainOrigin: false,
};
Expand Down Expand Up @@ -128,6 +144,15 @@ describe('CAIP-25 eth_accounts adapters', () => {
notifications: [],
accounts: ['eip155:100:0x1', 'eip155:100:0x2', 'eip155:100:0x3'],
},
'wallet:eip155': {
methods: [],
notifications: [],
accounts: [
'wallet:eip155:0x1',
'wallet:eip155:0x2',
'wallet:eip155:0x3',
],
},
},
isMultichainOrigin: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import {
ScopeString,
} from '../scope';

const isEip155ScopeString = (scopeString: ScopeString) => {
const { namespace, reference } = parseScopeString(scopeString);

return (
namespace === KnownCaipNamespace.Eip155 ||
(namespace === KnownCaipNamespace.Wallet &&
reference === KnownCaipNamespace.Eip155)
);
};

export const getEthAccounts = (caip25CaveatValue: Caip25CaveatValue) => {
const ethAccounts: string[] = [];
const sessionScopes = mergeScopes(
Expand All @@ -21,12 +31,9 @@ export const getEthAccounts = (caip25CaveatValue: Caip25CaveatValue) => {

Object.entries(sessionScopes).forEach(([_, { accounts }]) => {
accounts?.forEach((account) => {
const {
address,
chain: { namespace },
} = parseCaipAccountId(account);
const { address, chainId } = parseCaipAccountId(account);

if (namespace === KnownCaipNamespace.Eip155) {
if (isEip155ScopeString(chainId)) {
ethAccounts.push(address);
}
});
Expand All @@ -42,9 +49,7 @@ const setEthAccountsForScopesObject = (
const updatedScopesObject: ScopesObject = {};

Object.entries(scopesObject).forEach(([scopeString, scopeObject]) => {
const { namespace } = parseScopeString(scopeString);

if (namespace !== KnownCaipNamespace.Eip155) {
if (!isEip155ScopeString(scopeString as ScopeString)) {
updatedScopesObject[scopeString as ScopeString] = scopeObject;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/run-api-specs-multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async function main() {
notifications: ['eth_subscription'],
},
'wallet:eip155': {
accounts: [],
accounts: [`wallet:eip155:${ACCOUNT_1}`],
methods: walletEip155Methods,
notifications: [],
},
Expand Down

0 comments on commit 947dcd7

Please sign in to comment.