Skip to content

Commit 8d822b8

Browse files
committed
Enable chainId test
1 parent d210ea6 commit 8d822b8

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

packages/wallet/core/src/signers/session-manager.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,18 @@ export class SessionManager implements SapientSigner {
262262
if (!Address.isEqual(wallet, this.wallet.address)) {
263263
throw new Error('Wallet address mismatch')
264264
}
265+
if (this._provider) {
266+
const providerChainId = await this._provider.request({
267+
method: 'eth_chainId',
268+
})
269+
if (providerChainId !== Hex.fromNumber(chainId)) {
270+
throw new Error(`Provider chain id mismatch, expected ${Hex.fromNumber(chainId)} but got ${providerChainId}`)
271+
}
272+
}
265273
if ((await this.imageHash) !== imageHash) {
266274
throw new Error('Unexpected image hash')
267275
}
268276
const topology = await this._getTopologyForImageHash(imageHash)
269-
//FIXME Test chain id
270-
// if (this._provider) {
271-
// const providerChainId = await this._provider.request({
272-
// method: 'eth_chainId',
273-
// })
274-
// if (providerChainId !== Hex.fromNumber(chainId)) {
275-
// throw new Error(`Provider chain id mismatch, expected ${Hex.fromNumber(chainId)} but got ${providerChainId}`)
276-
// }
277-
// }
278277
if (!Payload.isCalls(payload) || payload.calls.length === 0) {
279278
throw new Error('Only calls are supported')
280279
}
@@ -363,22 +362,20 @@ export class SessionManager implements SapientSigner {
363362
if (!Address.isEqual(wallet, this.wallet.address)) {
364363
throw new Error('Wallet address mismatch')
365364
}
366-
if (!Payload.isCalls(payload)) {
365+
if (!Payload.isCalls(payload) || payload.calls.length === 0) {
367366
// Only calls are supported
368367
return false
369368
}
370369
if (!this._provider) {
371370
throw new Error('Provider not set')
372371
}
373-
//FIXME Test chain id
374-
// const providerChainId = await this._provider.request({
375-
// method: 'eth_chainId',
376-
// })
377-
// if (providerChainId !== Hex.fromNumber(chainId)) {
378-
// throw new Error(
379-
// `Provider chain id mismatch, expected ${Hex.fromNumber(chainId)} but got ${providerChainId}`,
380-
// )
381-
// }
372+
// Test chain id
373+
const providerChainId = await this._provider.request({
374+
method: 'eth_chainId',
375+
})
376+
if (providerChainId !== Hex.fromNumber(chainId)) {
377+
throw new Error(`Provider chain id mismatch, expected ${Hex.fromNumber(chainId)} but got ${providerChainId}`)
378+
}
382379

383380
const encodedPayload = Payload.encodeSapient(chainId, payload)
384381
const encodedCallData = AbiFunction.encodeData(Constants.RECOVER_SAPIENT_SIGNATURE, [

packages/wallet/core/test/session-manager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ for (const extension of ALL_EXTENSIONS) {
300300
'should fail to sign with an expired explicit session',
301301
async () => {
302302
const provider = Provider.from(RpcTransport.fromHttp(LOCAL_RPC_URL))
303-
const chainId = 0
303+
const chainId = Number(await provider.request({ method: 'eth_chainId' }))
304304

305305
// Create explicit signer
306306
const explicitPrivateKey = Secp256k1.randomPrivateKey()
@@ -353,7 +353,7 @@ for (const extension of ALL_EXTENSIONS) {
353353
}
354354

355355
// Sign the transaction
356-
expect(sessionManager.signSapient(wallet.address, chainId, payload, imageHash)).rejects.toThrow(
356+
await expect(sessionManager.signSapient(wallet.address, chainId, payload, imageHash)).rejects.toThrow(
357357
'No signers match the topology',
358358
)
359359
},

packages/wallet/wdk/test/sessions.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Sessions (via Manager)', () => {
154154
}
155155
const signature = await dapp.sessionManager.signSapient(
156156
dapp.wallet.address,
157-
chainId ?? 1n,
157+
chainId,
158158
parentedEnvelope,
159159
sessionImageHash,
160160
)
@@ -258,7 +258,7 @@ describe('Sessions (via Manager)', () => {
258258
// Configure mock provider
259259
;(provider as any).request.mockImplementation(({ method, params }) => {
260260
if (method === 'eth_chainId') {
261-
return Promise.resolve(chainId.toString())
261+
return Promise.resolve(Hex.fromNumber(chainId))
262262
}
263263
if (method === 'eth_call' && params[0].data === AbiFunction.encodeData(Constants.GET_IMPLEMENTATION)) {
264264
// Undeployed wallet
@@ -332,7 +332,7 @@ describe('Sessions (via Manager)', () => {
332332
// Configure mock provider
333333
;(provider as any).request.mockImplementation(({ method, params }) => {
334334
if (method === 'eth_chainId') {
335-
return Promise.resolve(chainId.toString())
335+
return Promise.resolve(Hex.fromNumber(chainId))
336336
}
337337
if (method === 'eth_call' && params[0].data === AbiFunction.encodeData(Constants.GET_IMPLEMENTATION)) {
338338
// Undeployed wallet
@@ -407,7 +407,7 @@ describe('Sessions (via Manager)', () => {
407407
// Configure mock provider
408408
;(provider as any).request.mockImplementation(({ method, params }) => {
409409
if (method === 'eth_chainId') {
410-
return Promise.resolve(chainId.toString())
410+
return Promise.resolve(Hex.fromNumber(chainId))
411411
}
412412
if (method === 'eth_call' && params[0].data === AbiFunction.encodeData(Constants.GET_IMPLEMENTATION)) {
413413
// Undeployed wallet
@@ -494,7 +494,7 @@ describe('Sessions (via Manager)', () => {
494494
// Configure mock provider
495495
;(provider as any).request.mockImplementation(({ method, params }) => {
496496
if (method === 'eth_chainId') {
497-
return Promise.resolve(chainId.toString())
497+
return Promise.resolve(Hex.fromNumber(chainId))
498498
}
499499
if (method === 'eth_call' && params[0].data === AbiFunction.encodeData(Constants.GET_IMPLEMENTATION)) {
500500
// Undeployed wallet

0 commit comments

Comments
 (0)