Skip to content

Commit 4aea567

Browse files
committed
feat: add custom chains for session for rabby
1 parent a498337 commit 4aea567

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/entrances.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ export async function createEntrances({
2121
const uiService = new UIService();
2222
const storageService = new StorageService();
2323

24+
const chainService = await ChainService.create(storageService);
25+
2426
return {
2527
uiService,
26-
chainService: await ChainService.create(storageService),
27-
walletKitService: await WalletKitService.create(walletKitOptions.projectId),
28+
chainService,
29+
walletKitService: await WalletKitService.create(
30+
chainService,
31+
walletKitOptions.projectId,
32+
),
2833
walletStorageService: await WalletStorageService.create(storageService),
2934
};
3035
}

src/services/wallet-kit-service.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
removeEIP155ChainIdPrefix,
2424
} from '../utils/index.js';
2525

26+
import type {ChainService} from './chain-service.js';
27+
2628
export const SUPPORTED_METHODS = [
2729
'eth_sendTransaction',
2830
'eth_sign',
@@ -43,7 +45,10 @@ export class WalletKitService {
4345
}
4446
| undefined;
4547

46-
private constructor(readonly walletKit: WalletKit) {
48+
private constructor(
49+
private chainService: ChainService,
50+
readonly walletKit: WalletKit,
51+
) {
4752
walletKit.on('session_proposal', event => {
4853
console.info('session_proposal', event);
4954

@@ -267,12 +272,27 @@ export class WalletKitService {
267272
): Promise<PendingSession | false> {
268273
const {id, params} = event;
269274

270-
const chains = Array.from(
271-
new Set([
272-
...(params.requiredNamespaces.eip155?.chains ?? []),
273-
...(params.optionalNamespaces.eip155?.chains ?? []),
274-
]),
275-
);
275+
const chainSet = new Set([
276+
...(params.requiredNamespaces.eip155?.chains ?? []),
277+
...(params.optionalNamespaces.eip155?.chains ?? []),
278+
]);
279+
280+
if (/rabby/i.test(event.params.proposer.metadata.name)) {
281+
for (const {id: chainId} of this.chainService.getCustomChains()) {
282+
if (chainSet.has(chainId)) {
283+
continue;
284+
}
285+
286+
const paramOptionalChains = params.optionalNamespaces.eip155?.chains;
287+
288+
if (paramOptionalChains) {
289+
paramOptionalChains.push(chainId);
290+
chainSet.add(chainId);
291+
}
292+
}
293+
}
294+
295+
const chains = Array.from(chainSet);
276296

277297
if (chains.length === 0) {
278298
await this.walletKit.rejectSession({
@@ -371,7 +391,10 @@ export class WalletKitService {
371391
'pending-session-request-update',
372392
);
373393

374-
static async create(projectId: string): Promise<WalletKitService> {
394+
static async create(
395+
chainService: ChainService,
396+
projectId: string,
397+
): Promise<WalletKitService> {
375398
const walletKit = await WalletKit.init({
376399
core: new Core({
377400
projectId,
@@ -384,7 +407,7 @@ export class WalletKitService {
384407
},
385408
});
386409

387-
return new WalletKitService(walletKit);
410+
return new WalletKitService(chainService, walletKit);
388411
}
389412
}
390413

0 commit comments

Comments
 (0)