diff --git a/packages/auth/src/services.ts b/packages/auth/src/services.ts index 9db2553cf..ee4915430 100644 --- a/packages/auth/src/services.ts +++ b/packages/auth/src/services.ts @@ -27,20 +27,16 @@ export type SessionJWT = { expiration: number } -type SessionJWTPromise = { +export type SessionJWTPromise = { token: Promise expiration: number } -type ProofStringPromise = { +export type ProofStringPromise = { proofString: Promise expiration: number } -function getJWTExpiration(jwt: string): number { - return jwtDecodeClaims<{ exp: number }>(jwt).exp -} - // Default session expiration of ETHAuth token (1 week) export const DEFAULT_SESSION_EXPIRATION = 60 * 60 * 24 * 7 @@ -83,6 +79,7 @@ export class Services { onAuth(cb: (result: PromiseSettledResult) => void) { this.onAuthCallbacks.push(cb) + return () => this.onAuthCallbacks = this.onAuthCallbacks.filter(c => c !== cb) } async dump(): Promise<{ @@ -108,12 +105,12 @@ export class Services { if (!url) throw Error('No sequence api url') let jwtAuth: string | undefined - for (let i = 0; ; i++) { + for (let i = 1; ; i++) { try { jwtAuth = (await this.getJWT(true)).token break } catch (error) { - if (i === maxTries - 1) { + if (i === maxTries) { console.error(`couldn't authenticate after ${maxTries} attempts`, error) throw error } diff --git a/packages/auth/src/session.ts b/packages/auth/src/session.ts index 0492447fd..e1adb962b 100644 --- a/packages/auth/src/session.ts +++ b/packages/auth/src/session.ts @@ -104,10 +104,14 @@ export class Session { })) for (const config of configs) { - const coder = config.config && universal.genericCoderFor(config.config.version) - const signers = coder && config.config && coder.config.signersOf(config.config) + if (!config.config) { + continue + } - if (signers && signers.length === 1 && signers[0].address === referenceSigner) { + const coder = universal.genericCoderFor(config.config.version) + const signers = coder.config.signersOf(config.config) + + if (signers.length === 1 && signers[0].address === referenceSigner) { return config.wallet } } diff --git a/packages/wallet/src/wallet.ts b/packages/wallet/src/wallet.ts index 09a5b7962..89363a035 100644 --- a/packages/wallet/src/wallet.ts +++ b/packages/wallet/src/wallet.ts @@ -276,7 +276,7 @@ export class Wallet< return this.signTransactions(bundle.transactions, bundle.nonce) } - async nonceFor( + async fetchNonceOrSpace( nonce?: ethers.BigNumberish | { space: ethers.BigNumberish } ): Promise { let spaceValue @@ -315,7 +315,7 @@ export class Wallet< }) } - const defaultedNonce = await this.nonceFor(nonce) + const defaultedNonce = await this.fetchNonceOrSpace(nonce) const digest = commons.transaction.digestOfTransactions(defaultedNonce, transactions) const signature = await this.signDigest(digest, { transactions })