Skip to content

Commit

Permalink
Merge pull request #2 from amper-fb/keep-session
Browse files Browse the repository at this point in the history
keep previous session
  • Loading branch information
jnwng authored Aug 23, 2022
2 parents bd4a740 + 86ccae8 commit 6b20c21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions packages/walletconnect-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"@solana/web3.js": "1.50.1",
"@types/node-fetch": "^2.6.2",
"@types/pino": "6.3.11",
"@walletconnect/types": "2.0.0-rc.1",
"@walletconnect/types": "2.0.0-rc.2",
"shx": "^0.3.4"
},
"dependencies": {
"@walletconnect/qrcode-modal": "1.8.0",
"@walletconnect/sign-client": "2.0.0-rc.1",
"@walletconnect/utils": "2.0.0-rc.1",
"@walletconnect/sign-client": "2.0.0-rc.2",
"@walletconnect/utils": "2.0.0-rc.2",
"better-sqlite3": "7.6.2",
"bs58": "^5.0.0",
"tslib": "^2.4.0"
Expand Down
55 changes: 27 additions & 28 deletions packages/walletconnect-solana/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,59 @@ const getConnectParams = (chainId: WalletConnectChainID, pairingTopic?: string):
export class WalletConnectWallet {
private _client: WalletConnectClient | undefined;
private _session: SessionTypes.Struct | undefined;
private _network: WalletConnectChainID;
private _options: SignClientTypes.Options;
private readonly _network: WalletConnectChainID;
private readonly _options: SignClientTypes.Options;

constructor(config: WalletConnectWalletAdapterConfig) {
this._options = config.options;
this._network = config.network;
}

async connect(): Promise<WalletConnectWalletInit> {
const client = await WalletConnectClient.init(this._options);

const pairings = client.pairing.getAll({ active: true });
// Prototypically, the user should be prompted to either:
// - Connect to a previously active pairing
// - Choose a new pairing
// There doesn't seem to be a WalletConnect-provided UI for this like there exists for the QRCode modal, though,
// and pushing this into user-land would be way too much
// If we decide to try and pair automatically, the UI will hang waiting for a pairing that might not complete
// const lastActivePairing = pairings.length ? pairings[pairings.length - 1].topic : undefined;
const lastActivePairing = undefined;

const { uri, approval } = await client.connect(getConnectParams(this._network, lastActivePairing));

if (uri) {
QRCodeModal.open(uri, () => {
throw new QRCodeModalError();
});
const client = this._client ?? (await WalletConnectClient.init(this._options));
const sessions = client.find(getConnectParams(this._network)).filter((s) => s.acknowledged);
if (sessions.length) {
// select last matching session
this._session = sessions[sessions.length - 1];
// We assign this variable only after we're sure we've received approval
this._client = client;
} else {
const { uri, approval } = await client.connect(getConnectParams(this._network));
if (uri) {
QRCodeModal.open(uri, () => {
throw new QRCodeModalError();
});
}

this._session = await approval();
// We assign this variable only after we're sure we've received approval
this._client = client;

QRCodeModal.close();
}

this._session = await approval();
// We assign this variable only after we're sure we've received approval
this._client = client;

QRCodeModal.close();

return {
publicKey: this.publicKey,
};
}

async disconnect() {
if (this._client && this._session) {
return await this._client.disconnect({
await this._client.disconnect({
topic: this._session.topic,
reason: getSdkError('USER_DISCONNECTED'),
});
this._session = undefined;
} else {
throw new ClientNotInitializedError();
}
}

get client(): WalletConnectClient {
if (this._client) {
return this._client;
// TODO: using client.off throws an error
return Object.assign({}, this._client, { off: this._client.removeListener });
// return this._client;
} else {
throw new ClientNotInitializedError();
}
Expand Down

0 comments on commit 6b20c21

Please sign in to comment.