Skip to content

Commit 6808db2

Browse files
authored
refactor updateTimeDrift to preSendIntent (#754)
* refactor updateTimeDrift to preSendIntent * check status only when timeDrift is undefined * change error message for 451
1 parent 6ecec6b commit 6808db2

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

packages/waas/src/auth.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,21 @@ export class SequenceWaaS {
339339
}
340340
}
341341

342-
private async updateTimeDrift() {
342+
/**
343+
* Checks the server status and sets the time drift before sending any intent.
344+
* @throws {Error} If server status check fails or Date header is missing
345+
*/
346+
private async preSendIntent() {
343347
if (getTimeDrift() === undefined) {
344348
const res = await fetch(`${this.config.rpcServer}/status`)
349+
350+
if (res.status !== 200) {
351+
if (res.status === 451) {
352+
throw new Error('Service unavailable due to legal and geographic restrictions')
353+
}
354+
throw new Error(`Error with status ${res.status}`)
355+
}
356+
345357
const date = res.headers.get('Date')
346358
if (!date) {
347359
throw new Error('failed to get Date header value from /status')
@@ -446,7 +458,7 @@ export class SequenceWaaS {
446458
}
447459

448460
async initAuth(identity: Identity): Promise<Challenge> {
449-
await this.updateTimeDrift()
461+
await this.preSendIntent()
450462

451463
if ('guest' in identity && identity.guest) {
452464
return this.initGuestAuth()
@@ -511,7 +523,7 @@ export class SequenceWaaS {
511523
challenge: Challenge,
512524
opts?: { sessionName?: string; forceCreateAccount?: boolean }
513525
): Promise<SignInResponse> {
514-
await this.updateTimeDrift()
526+
await this.preSendIntent()
515527

516528
// initAuth can start while user is already signed in and continue with linkAccount method,
517529
// but it can't be used to completeAuth while user is already signed in. In this
@@ -579,7 +591,7 @@ export class SequenceWaaS {
579591
}
580592

581593
async dropSession({ sessionId, strict }: { sessionId?: string; strict?: boolean } = {}) {
582-
await this.updateTimeDrift()
594+
await this.preSendIntent()
583595

584596
const thisSessionId = await this.waas.getSessionId()
585597
if (!thisSessionId) {
@@ -627,7 +639,7 @@ export class SequenceWaaS {
627639
}
628640

629641
async listSessions(): Promise<Sessions> {
630-
await this.updateTimeDrift()
642+
await this.preSendIntent()
631643

632644
const sessionId = await this.waas.getSessionId()
633645
if (!sessionId) {
@@ -649,7 +661,7 @@ export class SequenceWaaS {
649661
}
650662

651663
async validateSession(args?: ValidationArgs) {
652-
await this.updateTimeDrift()
664+
await this.preSendIntent()
653665

654666
if (await this.isSessionValid()) {
655667
return true
@@ -659,7 +671,7 @@ export class SequenceWaaS {
659671
}
660672

661673
async finishValidateSession(challenge: string): Promise<boolean> {
662-
await this.updateTimeDrift()
674+
await this.preSendIntent()
663675

664676
const intent = await this.waas.finishValidateSession(this.validationRequiredSalt, challenge)
665677
const result = await this.sendIntent(intent)
@@ -673,7 +685,7 @@ export class SequenceWaaS {
673685
}
674686

675687
async isSessionValid(): Promise<boolean> {
676-
await this.updateTimeDrift()
688+
await this.preSendIntent()
677689

678690
const intent = await this.waas.getSession()
679691
const result = await this.sendIntent(intent)
@@ -700,14 +712,14 @@ export class SequenceWaaS {
700712
}
701713

702714
async sessionAuthProof({ nonce, network, validation }: { nonce?: string; network?: string; validation?: ValidationArgs }) {
703-
await this.updateTimeDrift()
715+
await this.preSendIntent()
704716

705717
const intent = await this.waas.sessionAuthProof({ nonce, network })
706718
return await this.trySendIntent({ validation }, intent, isSessionAuthProofResponse)
707719
}
708720

709721
async listAccounts() {
710-
await this.updateTimeDrift()
722+
await this.preSendIntent()
711723

712724
const intent = await this.waas.listAccounts()
713725
const res = await this.sendIntent(intent)
@@ -720,7 +732,7 @@ export class SequenceWaaS {
720732
}
721733

722734
async linkAccount(challenge: Challenge) {
723-
await this.updateTimeDrift()
735+
await this.preSendIntent()
724736

725737
const intent = await this.waas.linkAccount(challenge.getIntentParams())
726738
const res = await this.sendIntent(intent)
@@ -733,14 +745,14 @@ export class SequenceWaaS {
733745
}
734746

735747
async removeAccount(accountId: string) {
736-
await this.updateTimeDrift()
748+
await this.preSendIntent()
737749

738750
const intent = await this.waas.removeAccount({ accountId })
739751
await this.sendIntent(intent)
740752
}
741753

742754
async getIdToken(args?: { nonce?: string }): Promise<IntentResponseIdToken> {
743-
await this.updateTimeDrift()
755+
await this.preSendIntent()
744756

745757
const intent = await this.waas.getIdToken({ nonce: args?.nonce })
746758
const res = await this.sendIntent(intent)
@@ -788,14 +800,14 @@ export class SequenceWaaS {
788800
}
789801

790802
async signMessage(args: WithSimpleNetwork<SignMessageArgs> & CommonAuthArgs): Promise<SignedMessageResponse> {
791-
await this.updateTimeDrift()
803+
await this.preSendIntent()
792804

793805
const intent = await this.waas.signMessage(await this.useIdentifier(args))
794806
return this.trySendIntent(args, intent, isSignedMessageResponse)
795807
}
796808

797809
async signTypedData(args: WithSimpleNetwork<SignTypedDataArgs> & CommonAuthArgs): Promise<SignedTypedDataResponse> {
798-
await this.updateTimeDrift()
810+
await this.preSendIntent()
799811

800812
const intent = await this.waas.signTypedData(await this.useIdentifier(args))
801813
return this.trySendIntent(args, intent, isSignedTypedDataResponse)
@@ -824,42 +836,42 @@ export class SequenceWaaS {
824836
}
825837

826838
async sendTransaction(args: WithSimpleNetwork<SendTransactionsArgs> & CommonAuthArgs): Promise<MaySentTransactionResponse> {
827-
await this.updateTimeDrift()
839+
await this.preSendIntent()
828840

829841
const intent = await this.waas.sendTransaction(await this.useIdentifier(args))
830842
return this.trySendTransactionIntent(intent, args)
831843
}
832844

833845
async sendERC20(args: WithSimpleNetwork<SendERC20Args> & CommonAuthArgs): Promise<MaySentTransactionResponse> {
834-
await this.updateTimeDrift()
846+
await this.preSendIntent()
835847

836848
const intent = await this.waas.sendERC20(await this.useIdentifier(args))
837849
return this.trySendTransactionIntent(intent, args)
838850
}
839851

840852
async sendERC721(args: WithSimpleNetwork<SendERC721Args> & CommonAuthArgs): Promise<MaySentTransactionResponse> {
841-
await this.updateTimeDrift()
853+
await this.preSendIntent()
842854

843855
const intent = await this.waas.sendERC721(await this.useIdentifier(args))
844856
return this.trySendTransactionIntent(intent, args)
845857
}
846858

847859
async sendERC1155(args: WithSimpleNetwork<SendERC1155Args> & CommonAuthArgs): Promise<MaySentTransactionResponse> {
848-
await this.updateTimeDrift()
860+
await this.preSendIntent()
849861

850862
const intent = await this.waas.sendERC1155(await this.useIdentifier(args))
851863
return this.trySendTransactionIntent(intent, args)
852864
}
853865

854866
async callContract(args: WithSimpleNetwork<SendContractCallArgs> & CommonAuthArgs): Promise<MaySentTransactionResponse> {
855-
await this.updateTimeDrift()
867+
await this.preSendIntent()
856868

857869
const intent = await this.waas.callContract(await this.useIdentifier(args))
858870
return this.trySendTransactionIntent(intent, args)
859871
}
860872

861873
async feeOptions(args: WithSimpleNetwork<SendTransactionsArgs> & CommonAuthArgs): Promise<FeeOptionsResponse> {
862-
await this.updateTimeDrift()
874+
await this.preSendIntent()
863875

864876
const intent = await this.waas.feeOptions(await this.useIdentifier(args))
865877
return this.trySendIntent(args, intent, isFeeOptionsResponse)

0 commit comments

Comments
 (0)