-
Notifications
You must be signed in to change notification settings - Fork 598
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
Refactor session config handling and cryptography logic for improved abstraction #1059
Changes from all commits
642f63c
17566f4
948ec7e
9bfd798
5d016ec
af41829
6ce9358
c1cf182
479f551
2d700f0
8668531
9fb0ac1
bb55c19
1059b1a
49edffb
ace9a7f
eec294a
9320312
52734c2
16eded0
1fafe2f
08f73c0
e0f3af2
82d231c
9563dba
d3ef3a7
7233426
ed2bc87
6e13f10
e40c9f5
b452039
8c949f1
2ab0a4e
7304b83
7b48b6f
ccc9d32
741fa93
253f8ef
fa50dcf
674a012
7bc3988
0613cf6
69b1948
04d6444
9e8e6eb
1e6c327
a316bc9
fd07d5a
60c8d1a
28f60fc
322b1d2
afe5064
8f89b24
b99e10c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,15 @@ module.exports = { | |
pragma: "h", | ||
}, | ||
}, | ||
rules: { | ||
'no-useless-constructor': 'off', | ||
}, | ||
overrides: [ | ||
{ | ||
files: ["**/*.test.*"], | ||
rules: { | ||
"@typescript-eslint/no-explicit-any": "off" | ||
} | ||
} | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
error: string; | ||
} | ||
|
||
export function isServerMessageFail(msg: any): msg is ServerMessageFail { | ||
Check warning on line 24 in packages/wallet-sdk/src/connection/ServerMessage.ts GitHub Actions / Lint Check
|
||
return ( | ||
msg && | ||
msg.type === 'Fail' && | ||
|
@@ -76,3 +76,14 @@ | |
event: string; | ||
data: string; | ||
} | ||
|
||
export function isServerMessageEvent(msg: any): msg is ServerMessageEvent { | ||
Check warning on line 80 in packages/wallet-sdk/src/connection/ServerMessage.ts GitHub Actions / Lint Check
|
||
return ( | ||
msg && | ||
msg.type === 'Event' && | ||
typeof msg.sessionId === 'string' && | ||
typeof msg.eventId === 'string' && | ||
typeof msg.event === 'string' && | ||
typeof msg.data === 'string' | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { ScopedLocalStorage } from '../lib/ScopedLocalStorage'; | ||
import { Session } from '../relay/Session'; | ||
import { APP_VERSION_KEY, WALLET_USER_NAME_KEY } from '../relay/WalletSDKRelayAbstract'; | ||
import { SessionConfig } from './SessionConfig'; | ||
import { WalletLinkConnection, WalletLinkConnectionUpdateListener } from './WalletLinkConnection'; | ||
import { WalletLinkConnectionCipher } from './WalletLinkConnectionCipher'; | ||
|
||
const decryptMock = jest.fn().mockImplementation((text) => Promise.resolve(`decrypted ${text}`)); | ||
|
||
jest.spyOn(WalletLinkConnectionCipher.prototype, 'decrypt').mockImplementation(decryptMock); | ||
|
||
describe('WalletLinkConnection', () => { | ||
const session = new Session(new ScopedLocalStorage('test')); | ||
|
||
let connection: WalletLinkConnection; | ||
let listener: WalletLinkConnectionUpdateListener; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
connection = new WalletLinkConnection(session, 'http://link-api-url', { | ||
linkedUpdated: jest.fn(), | ||
connectedUpdated: jest.fn(), | ||
handleResponseMessage: jest.fn(), | ||
chainUpdated: jest.fn(), | ||
accountUpdated: jest.fn(), | ||
metadataUpdated: jest.fn(), | ||
resetAndReload: jest.fn(), | ||
}); | ||
listener = (connection as any).listener; | ||
}); | ||
|
||
describe('incomingDataListener', () => { | ||
it('should call handleSessionMetadataUpdated when session config is updated', async () => { | ||
const handleSessionMetadataUpdatedSpy = jest.spyOn( | ||
connection as any, | ||
'handleSessionMetadataUpdated' | ||
); | ||
|
||
const sessionConfig: SessionConfig = { | ||
webhookId: 'webhookId', | ||
webhookUrl: 'webhookUrl', | ||
metadata: { | ||
WalletUsername: 'new username', | ||
}, | ||
}; | ||
|
||
(connection as any).ws.incomingDataListener?.({ | ||
...sessionConfig, | ||
type: 'SessionConfigUpdated', | ||
}); | ||
|
||
expect(handleSessionMetadataUpdatedSpy).toHaveBeenCalledWith(sessionConfig.metadata); | ||
}); | ||
}); | ||
|
||
describe('handleSessionMetadataUpdated', () => { | ||
function invoke_handleSessionMetadataUpdated(metadata: SessionConfig['metadata']) { | ||
(connection as any).handleSessionMetadataUpdated(metadata); | ||
} | ||
|
||
it('should call listner.metadataUpdated when WalletUsername updated', async () => { | ||
const listener_metadataUpdatedSpy = jest.spyOn(listener, 'metadataUpdated'); | ||
|
||
const newUsername = 'new username'; | ||
|
||
invoke_handleSessionMetadataUpdated({ WalletUsername: newUsername }); | ||
|
||
expect(listener_metadataUpdatedSpy).toHaveBeenCalledWith( | ||
WALLET_USER_NAME_KEY, | ||
await decryptMock(newUsername) | ||
); | ||
}); | ||
|
||
it('should call listner.metadataUpdated when AppVersion updated', async () => { | ||
const listener_metadataUpdatedSpy = jest.spyOn(listener, 'metadataUpdated'); | ||
|
||
const newAppVersion = 'new app version'; | ||
|
||
invoke_handleSessionMetadataUpdated({ AppVersion: newAppVersion }); | ||
|
||
expect(listener_metadataUpdatedSpy).toHaveBeenCalledWith( | ||
APP_VERSION_KEY, | ||
await decryptMock(newAppVersion) | ||
); | ||
}); | ||
|
||
it('should call listner.resetAndReload when __destroyed: 1 is received', async () => { | ||
const listener_resetAndReloadSpy = jest.spyOn(listener, 'resetAndReload'); | ||
|
||
invoke_handleSessionMetadataUpdated({ __destroyed: '1' }); | ||
|
||
expect(listener_resetAndReloadSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call listner.accountUpdated when Account updated', async () => { | ||
const listener_accountUpdatedSpy = jest.spyOn(listener, 'accountUpdated'); | ||
|
||
const newAccount = 'new account'; | ||
|
||
invoke_handleSessionMetadataUpdated({ EthereumAddress: newAccount }); | ||
|
||
expect(listener_accountUpdatedSpy).toHaveBeenCalledWith(await decryptMock(newAccount)); | ||
}); | ||
|
||
describe('chain updates', () => { | ||
it('should NOT call listner.chainUpdated when only one changed', async () => { | ||
const listener_chainUpdatedSpy = jest.spyOn(listener, 'chainUpdated'); | ||
|
||
const chainIdUpdate = { ChainId: 'new chain id' }; | ||
const jsonRpcUrlUpdate = { JsonRpcUrl: 'new json rpc url' }; | ||
|
||
invoke_handleSessionMetadataUpdated(chainIdUpdate); | ||
invoke_handleSessionMetadataUpdated(jsonRpcUrlUpdate); | ||
|
||
await decryptMock(chainIdUpdate.ChainId); | ||
|
||
expect(listener_chainUpdatedSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call listner.chainUpdated when both ChainId and JsonRpcUrl changed', async () => { | ||
const listener_chainUpdatedSpy = jest.spyOn(listener, 'chainUpdated'); | ||
|
||
const update = { | ||
ChainId: 'new chain id', | ||
JsonRpcUrl: 'new json rpc url', | ||
}; | ||
|
||
invoke_handleSessionMetadataUpdated(update); | ||
|
||
expect(listener_chainUpdatedSpy).toHaveBeenCalledWith( | ||
await decryptMock(update.ChainId), | ||
await decryptMock(update.JsonRpcUrl) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know what's useless or not