diff --git a/packages/waas/src/auth.ts b/packages/waas/src/auth.ts index aabe29580..20ede1d45 100644 --- a/packages/waas/src/auth.ts +++ b/packages/waas/src/auth.ts @@ -44,6 +44,7 @@ import { SignedTypedDataResponse } from './intents/responses' import { WaasAuthenticator, AnswerIncorrectError, Chain, EmailAlreadyInUseError, Session } from './clients/authenticator.gen' +import { NoPrivateKeyError } from './errors' import { SimpleNetwork, WithSimpleNetwork } from './networks' import { EmailAuth } from './email' import { ethers } from 'ethers' @@ -607,8 +608,18 @@ export class SequenceWaaS { throw new Error('No secure store available') } - const session = await newSessionFromSessionId(thisSessionId, this.cryptoBackend, this.secureStoreBackend) - session.clear() + try { + const session = await newSessionFromSessionId(thisSessionId, this.cryptoBackend, this.secureStoreBackend) + session.clear() + } catch (error) { + if (error instanceof NoPrivateKeyError) { + // If no private key is found, we can't clear the session properly + // but we can still clean up other session data which will log us out + } else { + throw error + } + } + await this.waas.completeSignOut() await this.deviceName.set(undefined) updateTimeDrift(undefined) diff --git a/packages/waas/src/base.ts b/packages/waas/src/base.ts index 6fca62099..f494c7486 100644 --- a/packages/waas/src/base.ts +++ b/packages/waas/src/base.ts @@ -56,6 +56,7 @@ import { getDefaultSubtleCryptoBackend, SubtleCryptoBackend } from './subtle-cry import { getDefaultSecureStoreBackend, SecureStoreBackend } from './secure-store' import { ethers } from 'ethers' import { ChallengeIntentParams } from './challenge' +import { NoPrivateKeyError } from './errors' type Status = 'pending' | 'signed-in' | 'signed-out' @@ -155,8 +156,16 @@ export class SequenceWaaSBase { throw new Error('session not open') } - const session = await newSessionFromSessionId(sessionId, this.cryptoBackend, this.secureStoreBackend) - return signIntent(session, intent) + try { + const session = await newSessionFromSessionId(sessionId, this.cryptoBackend, this.secureStoreBackend) + return signIntent(session, intent) + } catch (error) { + if (error instanceof NoPrivateKeyError) { + await this.completeSignOut() + throw new Error('No private key found, logging out') + } + throw error + } } public async signUsingSessionKey(message: string | Uint8Array) { @@ -165,8 +174,16 @@ export class SequenceWaaSBase { throw new Error('session not open') } - const signer = await newSessionFromSessionId(sessionId, this.cryptoBackend, this.secureStoreBackend) - return signer.sign(message) + try { + const signer = await newSessionFromSessionId(sessionId, this.cryptoBackend, this.secureStoreBackend) + return signer.sign(message) + } catch (error) { + if (error instanceof NoPrivateKeyError) { + await this.completeSignOut() + throw new Error('No private key found, logging out') + } + throw error + } } private gettingSessionIdPromise: Promise | undefined diff --git a/packages/waas/src/errors.ts b/packages/waas/src/errors.ts new file mode 100644 index 000000000..72bda9328 --- /dev/null +++ b/packages/waas/src/errors.ts @@ -0,0 +1,6 @@ +export class NoPrivateKeyError extends Error { + constructor() { + super('No private key found') + this.name = 'NoPrivateKeyError' + } +} diff --git a/packages/waas/src/session/secp256k1.ts b/packages/waas/src/session/secp256k1.ts index ec76507cb..3c6e090d6 100644 --- a/packages/waas/src/session/secp256k1.ts +++ b/packages/waas/src/session/secp256k1.ts @@ -1,6 +1,7 @@ import { ethers } from 'ethers' import { SecureStoreBackend } from '../secure-store' import { Session } from './index' +import { NoPrivateKeyError } from '../errors' const idbName = 'seq-waas-session-p256k1' const idbStoreName = 'seq-waas-session' @@ -12,7 +13,7 @@ export async function newSECP256K1SessionFromSessionId( const privateKey = await secureStoreBackend.get(idbName, idbStoreName, sessionId) if (!privateKey) { - throw new Error('No private key found') + throw new NoPrivateKeyError() } const wallet = new ethers.Wallet(privateKey) diff --git a/packages/waas/src/session/secp256r1.ts b/packages/waas/src/session/secp256r1.ts index 33696401f..9f910e144 100644 --- a/packages/waas/src/session/secp256r1.ts +++ b/packages/waas/src/session/secp256r1.ts @@ -3,6 +3,7 @@ import { Session } from './index' import { KeyTypes } from './keyTypes' import { SubtleCryptoBackend } from '../subtle-crypto' import { SecureStoreBackend } from '../secure-store' +import { NoPrivateKeyError } from '../errors' const idbName = 'seq-waas-session-p256r1' const idbStoreName = 'seq-waas-session' @@ -20,7 +21,7 @@ export async function newSECP256R1SessionFromSessionId( const keys = await secureStoreBackend.get(idbName, idbStoreName, sessionId) if (!keys || !keys.privateKey) { - throw new Error('No private key found') + throw new NoPrivateKeyError() } const encoder = new TextEncoder()