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

feat: create merger and factory for the Caip25Permission #5283

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 0 additions & 6 deletions eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,6 @@
"@typescript-eslint/no-unsafe-enum-comparison": 4,
"jsdoc/tag-lines": 4
},
"packages/multichain/src/scope/transform.ts": {
"jsdoc/tag-lines": 3
},
"packages/multichain/src/scope/types.ts": {
"jsdoc/tag-lines": 1
},
"packages/multichain/src/scope/validation.ts": {
"jsdoc/tag-lines": 2
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
KnownWalletNamespaceRpcMethods,
KnownWalletRpcMethods,
} from '../scope/constants';
import { mergeScopes } from '../scope/transform';
import { mergeNormalizedScopes } from '../scope/transform';
import type {
InternalScopesObject,
NonWalletKnownCaipNamespace,
Expand Down Expand Up @@ -94,7 +94,7 @@ export const getSessionScopes = (
'requiredScopes' | 'optionalScopes'
>,
) => {
return mergeScopes(
return mergeNormalizedScopes(
getNormalizedScopesObject(caip25CaveatValue.requiredScopes),
getNormalizedScopesObject(caip25CaveatValue.optionalScopes),
);
Expand Down
295 changes: 294 additions & 1 deletion packages/multichain/src/caip25Permission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('caip25EndowmentBuilder', () => {
describe('caip25CaveatBuilder', () => {
const findNetworkClientIdByChainId = jest.fn();
const listAccounts = jest.fn();
const { validator } = caip25CaveatBuilder({
const { validator, merger } = caip25CaveatBuilder({
findNetworkClientIdByChainId,
listAccounts,
});
Expand Down Expand Up @@ -698,4 +698,297 @@ describe('caip25CaveatBuilder', () => {
}),
).toBeUndefined();
});

describe('permission merger', () => {
describe('optionalScopes', () => {
it.each<{
description: string;
rightValue: Caip25CaveatValue;
expectedMergedValue: Caip25CaveatValue;
expectedDiff: Caip25CaveatValue;
}>([
{
description:
'incremental request existing scope with a new account - should return merged scope with existing chain and both accounts',
rightValue: {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xbeef'],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedMergedValue: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead', 'eip155:1:0xbeef'] },
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedDiff: {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xbeef'],
},
},
isMultichainOrigin: false,
requiredScopes: {},
},
},
{
description:
'incremental request a whole new scope without accounts - should return merged scope with previously existing chain and accounts, plus new requested chain with no accounts',
rightValue: {
optionalScopes: {
'eip155:10': {
accounts: [],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedMergedValue: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead'] },
'eip155:10': {
accounts: [],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedDiff: {
optionalScopes: {
'eip155:10': {
accounts: [],
},
},
isMultichainOrigin: false,
requiredScopes: {},
},
},
{
description:
'incremental request a whole new scope with accounts - should return merged scope with previously existing chain and accounts, plus new requested chain with new account',
rightValue: {
optionalScopes: {
'eip155:10': {
accounts: ['eip155:10:0xbeef'],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedMergedValue: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead'] },
'eip155:10': { accounts: ['eip155:10:0xbeef'] },
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedDiff: {
optionalScopes: {
'eip155:10': {
accounts: ['eip155:10:0xbeef'],
},
},
isMultichainOrigin: false,
requiredScopes: {},
},
},
{
description:
'incremental request an existing scope with new accounts, and whole new scope with accounts - should return merged scope with previously existing chain and accounts, plus new requested chain with new accounts',
rightValue: {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xdead', 'eip155:1:0xbeef'],
},
'eip155:10': {
accounts: ['eip155:10:0xdead', 'eip155:10:0xbeef'],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedMergedValue: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead', 'eip155:1:0xbeef'] },
'eip155:10': {
accounts: ['eip155:10:0xdead', 'eip155:10:0xbeef'],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedDiff: {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xbeef'],
},
'eip155:10': {
accounts: ['eip155:10:0xdead', 'eip155:10:0xbeef'],
},
},
isMultichainOrigin: false,
requiredScopes: {},
},
},
{
description:
'incremental request an existing scope with new accounts, and 2 whole new scope with accounts - should return merged scope with previously existing chain and accounts, plus new requested chains with new accounts',
rightValue: {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xdead', 'eip155:1:0xbadd'],
},
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedMergedValue: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead', 'eip155:1:0xbadd'] },
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
requiredScopes: {},
isMultichainOrigin: false,
},
expectedDiff: {
optionalScopes: {
'eip155:1': { accounts: ['eip155:1:0xbadd'] },
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
isMultichainOrigin: false,
requiredScopes: {},
},
},
])(
'$description',
async ({ rightValue, expectedMergedValue, expectedDiff }) => {
const initLeftValue: Caip25CaveatValue = {
optionalScopes: {
'eip155:1': {
accounts: ['eip155:1:0xdead'],
},
},
requiredScopes: {},
isMultichainOrigin: false,
};

const [newValue, diff] = merger(initLeftValue, rightValue);

expect(newValue).toStrictEqual(
expect.objectContaining(expectedMergedValue),
);
expect(diff).toStrictEqual(expect.objectContaining(expectedDiff));
},
);
});

describe('requiredScopes', () => {
it('incremental request an existing scope with new accounts, and 2 whole new scope with accounts - should return merged scope with previously existing chain and accounts, plus new requested chains with new accounts', () => {
const initLeftValue: Caip25CaveatValue = {
requiredScopes: {
'eip155:1': {
accounts: ['eip155:1:0xdead'],
},
},
optionalScopes: {},
isMultichainOrigin: false,
};

const rightValue: Caip25CaveatValue = {
requiredScopes: {
'eip155:1': {
accounts: ['eip155:1:0xdead', 'eip155:1:0xbadd'],
},
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
optionalScopes: {},
isMultichainOrigin: false,
};

const expectedMergedValue: Caip25CaveatValue = {
requiredScopes: {
'eip155:1': { accounts: ['eip155:1:0xdead', 'eip155:1:0xbadd'] },
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
optionalScopes: {},
isMultichainOrigin: false,
};
const expectedDiff: Caip25CaveatValue = {
requiredScopes: {
'eip155:1': { accounts: ['eip155:1:0xbadd'] },
'eip155:10': {
accounts: ['eip155:10:0xbeef', 'eip155:10:0xbadd'],
},
'eip155:426161': {
accounts: [
'eip155:426161:0xdead',
'eip155:426161:0xbeef',
'eip155:426161:0xbadd',
],
},
},
optionalScopes: {},
isMultichainOrigin: false,
};
const [newValue, diff] = merger(initLeftValue, rightValue);

expect(newValue).toStrictEqual(
expect.objectContaining(expectedMergedValue),
);
expect(diff).toStrictEqual(expect.objectContaining(expectedDiff));
});
});
});
});
Loading
Loading