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

Improve no private key found error handling #634

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 21 additions & 4 deletions packages/waas/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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) {
Expand All @@ -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<string> | undefined
Expand Down
6 changes: 6 additions & 0 deletions packages/waas/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class NoPrivateKeyError extends Error {
constructor() {
super('No private key found')
this.name = 'NoPrivateKeyError'
}
}
3 changes: 2 additions & 1 deletion packages/waas/src/session/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/waas/src/session/secp256r1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand Down
Loading