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

Refactor session config handling and cryptography logic for improved abstraction #1059

Merged
merged 54 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
642f63c
asdf
bangtoven Nov 11, 2023
17566f4
asdf
bangtoven Nov 11, 2023
948ec7e
asdf
bangtoven Nov 11, 2023
9bfd798
Merge branch 'wl-httppppp' into jungho/wl-http
bangtoven Nov 11, 2023
5d016ec
separate out http stuff
bangtoven Nov 11, 2023
af41829
swap
bangtoven Nov 11, 2023
6ce9358
linkAPIUrl no need after init
bangtoven Nov 11, 2023
c1cf182
cleanup
bangtoven Nov 11, 2023
479f551
for each
bangtoven Nov 11, 2023
2d700f0
constructor
bangtoven Nov 11, 2023
8668531
remove no-op
bangtoven Nov 11, 2023
9fb0ac1
resolved most of the errors. yay!
bangtoven Nov 11, 2023
bb55c19
introducing Listener
bangtoven Nov 11, 2023
1059b1a
setConnected
bangtoven Nov 11, 2023
49edffb
update tests
bangtoven Nov 11, 2023
ace9a7f
minimize change
bangtoven Nov 11, 2023
eec294a
introduce cipher
bangtoven Nov 11, 2023
9320312
handle metadata
bangtoven Nov 11, 2023
52734c2
unwrap
bangtoven Nov 11, 2023
16eded0
cleanup __destroyed
bangtoven Nov 11, 2023
1fafe2f
WIP handleChainChanged
bangtoven Nov 11, 2023
08f73c0
cleanup handleMetadata
bangtoven Nov 11, 2023
e0f3af2
handleChainChanged
bangtoven Nov 11, 2023
82d231c
handleAccountChanged + cleanup
bangtoven Nov 11, 2023
9563dba
remove storage dependency
bangtoven Nov 11, 2023
d3ef3a7
cleanup
bangtoven Nov 11, 2023
7233426
handleMetadataUpdated
bangtoven Nov 11, 2023
ed2bc87
rename
bangtoven Nov 11, 2023
6e13f10
remove unnecessary exports
bangtoven Nov 11, 2023
e40c9f5
cleanup handlers
bangtoven Nov 12, 2023
b452039
comment
bangtoven Nov 12, 2023
8c949f1
cleanup
bangtoven Nov 12, 2023
2ab0a4e
consistency
bangtoven Nov 12, 2023
7304b83
Merge branch 'jungho/wl-http' into jungho/abstract-websocket-stuff
bangtoven Nov 12, 2023
7b48b6f
keep distinctUntilChanged on the Relay level
bangtoven Nov 12, 2023
ccc9d32
keep store key definition on abstract
bangtoven Nov 12, 2023
741fa93
migrate aes256gcm as cipher
bangtoven Nov 12, 2023
253f8ef
pull out isServerMessageEvent
bangtoven Nov 13, 2023
fa50dcf
keep diagnostics
bangtoven Nov 13, 2023
674a012
cleanup
bangtoven Nov 13, 2023
7bc3988
revert setConnected WalletUI changes
bangtoven Nov 13, 2023
0613cf6
fix binding issue
bangtoven Nov 13, 2023
69b1948
WalletLinkConnection.test
bangtoven Nov 13, 2023
04d6444
more tests
bangtoven Nov 13, 2023
9e8e6eb
cleaner
bangtoven Nov 13, 2023
1e6c327
tests
bangtoven Nov 13, 2023
a316bc9
reorg
bangtoven Nov 13, 2023
fd07d5a
rename handleSessionConfigUpdated
bangtoven Nov 13, 2023
60c8d1a
tests
bangtoven Nov 13, 2023
28f60fc
tests
bangtoven Nov 13, 2023
322b1d2
update jest config
bangtoven Nov 13, 2023
afe5064
Merge branch 'jungho/wl-http' into jungho/abstract-websocket-stuff
bangtoven Nov 13, 2023
8f89b24
update jest config
bangtoven Nov 13, 2023
b99e10c
Merge branch 'master' into jungho/abstract-websocket-stuff
bangtoven Nov 13, 2023
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
11 changes: 11 additions & 0 deletions packages/wallet-sdk/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@ module.exports = {
pragma: "h",
},
},
rules: {
'no-useless-constructor': 'off',
Copy link
Contributor Author

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

},
overrides: [
{
files: ["**/*.test.*"],
rules: {
"@typescript-eslint/no-explicit-any": "off"
}
}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
2 changes: 1 addition & 1 deletion packages/wallet-sdk/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export default {
'./src/connection/RxWebSocket.ts',
'./src/connection/WalletLinkConnection.ts',
'./src/connection/WalletLinkHTTP.ts',
'./src/connection/WalletLinkConnectionCipher.ts',
'./src/lib/ScopedLocalStorage.ts',
'./src/provider/CoinbaseWalletProvider.ts',
'./src/provider/FilterPolyfill.ts',
'./src/provider/SubscriptionManager.ts',
'./src/provider/WalletLinkRelayUI.ts',
'./src/relay/aes256gcm.ts',
'./src/relay/Session.ts',
'./src/relay/WalletSDKRelay.ts',
'./src/relay/WalletSDKRelayEventManager.ts',
Expand Down
11 changes: 11 additions & 0 deletions packages/wallet-sdk/src/connection/ServerMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected any. Specify a different type

Check warning on line 24 in packages/wallet-sdk/src/connection/ServerMessage.ts

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected any. Specify a different type
return (
msg &&
msg.type === 'Fail' &&
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected any. Specify a different type

Check warning on line 80 in packages/wallet-sdk/src/connection/ServerMessage.ts

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected any. Specify a different type
return (
msg &&
msg.type === 'Event' &&
typeof msg.sessionId === 'string' &&
typeof msg.eventId === 'string' &&
typeof msg.event === 'string' &&
typeof msg.data === 'string'
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following the existing pattern. will revisit later.
image

138 changes: 138 additions & 0 deletions packages/wallet-sdk/src/connection/WalletLinkConnection.test.ts
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)
);
});
});
});
});
Loading
Loading