Skip to content

Commit

Permalink
fix: rpc protocol resetting after refresh (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s authored Oct 17, 2024
1 parent 0c9e888 commit bb051c3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
75 changes: 46 additions & 29 deletions packages/sdk-communication-layer/src/RemoteCommunication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export interface RemoteCommunicationState {
_connectionStatus: ConnectionStatus;
}
export class RemoteCommunication extends EventEmitter2 {
private _options: RemoteCommunicationProps;

public state: RemoteCommunicationState = {
// ready flag is turned on after we receive 'clients_ready' message, meaning key exchange is complete.
ready: false,
Expand Down Expand Up @@ -132,29 +134,33 @@ export class RemoteCommunication extends EventEmitter2 {
_connectionStatus: ConnectionStatus.DISCONNECTED,
};

constructor({
platformType,
communicationLayerPreference,
otherPublicKey,
reconnect,
walletInfo,
dappMetadata,
protocolVersion,
transports,
context,
relayPersistence,
ecies,
analytics = false,
storage,
sdkVersion,
communicationServerUrl = DEFAULT_SERVER_URL,
logging,
autoConnect = {
timeout: CHANNEL_MAX_WAITING_TIME,
},
}: RemoteCommunicationProps) {
constructor(options: RemoteCommunicationProps) {
super();

this._options = options;

const {
platformType,
communicationLayerPreference,
otherPublicKey,
reconnect,
walletInfo,
dappMetadata,
protocolVersion,
transports,
context,
relayPersistence,
ecies,
analytics = false,
storage,
sdkVersion,
communicationServerUrl = DEFAULT_SERVER_URL,
logging,
autoConnect = {
timeout: CHANNEL_MAX_WAITING_TIME,
},
} = options;

this.state.otherPublicKey = otherPublicKey;
this.state.dappMetadata = dappMetadata;
this.state.walletInfo = walletInfo;
Expand Down Expand Up @@ -206,14 +212,16 @@ export class RemoteCommunication extends EventEmitter2 {
`[RemoteCommunication: constructor()] protocolVersion=${protocolVersion} relayPersistence=${relayPersistence} isOriginator=${this.state.isOriginator} communicationLayerPreference=${communicationLayerPreference} otherPublicKey=${otherPublicKey} reconnect=${reconnect}`,
);

initSocketService({
communicationLayerPreference,
otherPublicKey,
reconnect,
ecies,
communicationServerUrl,
instance: this,
});
if (!this.state.isOriginator) {
initSocketService({
communicationLayerPreference,
otherPublicKey,
reconnect,
ecies,
communicationServerUrl,
instance: this,
});
}

this.emitServiceStatusEvent({ context: 'constructor' });
}
Expand Down Expand Up @@ -242,6 +250,15 @@ export class RemoteCommunication extends EventEmitter2 {
}
}
}

initSocketService({
communicationLayerPreference: CommunicationLayerPreference.SOCKET,
otherPublicKey: this.state.otherPublicKey,
reconnect: this._options.reconnect,
ecies: this._options.ecies,
communicationServerUrl: this.state.communicationServerUrl,
instance: this,
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export async function handleWalletInitMessage(
const accounts = data.accounts as string[];
const chainId = data.chainId as string;
const walletKey = data.walletKey as string;
let deeplinProtocolAvailable = false;
let deeplinkProtocolAvailable = false;
let walletVersion: string | undefined;
if ('deeplinkProtocol' in data) {
deeplinProtocolAvailable = Boolean(data.deeplinkProtocol);
instance.state.deeplinkProtocolAvailable = deeplinProtocolAvailable;
deeplinkProtocolAvailable = Boolean(data.deeplinkProtocol);
instance.state.deeplinkProtocolAvailable =
deeplinkProtocolAvailable;
}

if ('walletVersion' in data) {
Expand All @@ -46,7 +47,7 @@ export async function handleWalletInitMessage(
...channelConfig,
otherKey: walletKey,
walletVersion,
deeplinkProtocolAvailable: deeplinProtocolAvailable,
deeplinkProtocolAvailable,
relayPersistence: true,
});

Expand Down
11 changes: 9 additions & 2 deletions packages/sdk/src/services/RemoteConnection/RemoteConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export interface RemoteConnectionProps {
// Prevent circular dependencies
getMetaMaskInstaller: () => MetaMaskInstaller;
connectWithExtensionProvider?: () => void;
/**
* @deprecated Use the 'display_uri' event on the provider instead.
* Listen to this event to get the QR code URL and customize your UI.
* Example:
* sdk.getProvider().on('display_uri', (uri: string) => {
* // Use the uri to display a QR code or customize your UI
* });
*/
modals: {
onPendingModalDisconnect?: () => void;
install?: (params: {
Expand Down Expand Up @@ -172,10 +180,9 @@ export class RemoteConnection {
this.options.ecies = eciesProps;
}
initializeConnector(this.state, this.options);
await this.getConnector()?.initFromDappStorage();

setupListeners(this.state, this.options);

return this.getConnector()?.initFromDappStorage();
}

showActiveModal() {
Expand Down

0 comments on commit bb051c3

Please sign in to comment.