@@ -339,9 +339,21 @@ export class SequenceWaaS {
339
339
}
340
340
}
341
341
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 ( ) {
343
347
if ( getTimeDrift ( ) === undefined ) {
344
348
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
+
345
357
const date = res . headers . get ( 'Date' )
346
358
if ( ! date ) {
347
359
throw new Error ( 'failed to get Date header value from /status' )
@@ -446,7 +458,7 @@ export class SequenceWaaS {
446
458
}
447
459
448
460
async initAuth ( identity : Identity ) : Promise < Challenge > {
449
- await this . updateTimeDrift ( )
461
+ await this . preSendIntent ( )
450
462
451
463
if ( 'guest' in identity && identity . guest ) {
452
464
return this . initGuestAuth ( )
@@ -511,7 +523,7 @@ export class SequenceWaaS {
511
523
challenge : Challenge ,
512
524
opts ?: { sessionName ?: string ; forceCreateAccount ?: boolean }
513
525
) : Promise < SignInResponse > {
514
- await this . updateTimeDrift ( )
526
+ await this . preSendIntent ( )
515
527
516
528
// initAuth can start while user is already signed in and continue with linkAccount method,
517
529
// but it can't be used to completeAuth while user is already signed in. In this
@@ -579,7 +591,7 @@ export class SequenceWaaS {
579
591
}
580
592
581
593
async dropSession ( { sessionId, strict } : { sessionId ?: string ; strict ?: boolean } = { } ) {
582
- await this . updateTimeDrift ( )
594
+ await this . preSendIntent ( )
583
595
584
596
const thisSessionId = await this . waas . getSessionId ( )
585
597
if ( ! thisSessionId ) {
@@ -627,7 +639,7 @@ export class SequenceWaaS {
627
639
}
628
640
629
641
async listSessions ( ) : Promise < Sessions > {
630
- await this . updateTimeDrift ( )
642
+ await this . preSendIntent ( )
631
643
632
644
const sessionId = await this . waas . getSessionId ( )
633
645
if ( ! sessionId ) {
@@ -649,7 +661,7 @@ export class SequenceWaaS {
649
661
}
650
662
651
663
async validateSession ( args ?: ValidationArgs ) {
652
- await this . updateTimeDrift ( )
664
+ await this . preSendIntent ( )
653
665
654
666
if ( await this . isSessionValid ( ) ) {
655
667
return true
@@ -659,7 +671,7 @@ export class SequenceWaaS {
659
671
}
660
672
661
673
async finishValidateSession ( challenge : string ) : Promise < boolean > {
662
- await this . updateTimeDrift ( )
674
+ await this . preSendIntent ( )
663
675
664
676
const intent = await this . waas . finishValidateSession ( this . validationRequiredSalt , challenge )
665
677
const result = await this . sendIntent ( intent )
@@ -673,7 +685,7 @@ export class SequenceWaaS {
673
685
}
674
686
675
687
async isSessionValid ( ) : Promise < boolean > {
676
- await this . updateTimeDrift ( )
688
+ await this . preSendIntent ( )
677
689
678
690
const intent = await this . waas . getSession ( )
679
691
const result = await this . sendIntent ( intent )
@@ -700,14 +712,14 @@ export class SequenceWaaS {
700
712
}
701
713
702
714
async sessionAuthProof ( { nonce, network, validation } : { nonce ?: string ; network ?: string ; validation ?: ValidationArgs } ) {
703
- await this . updateTimeDrift ( )
715
+ await this . preSendIntent ( )
704
716
705
717
const intent = await this . waas . sessionAuthProof ( { nonce, network } )
706
718
return await this . trySendIntent ( { validation } , intent , isSessionAuthProofResponse )
707
719
}
708
720
709
721
async listAccounts ( ) {
710
- await this . updateTimeDrift ( )
722
+ await this . preSendIntent ( )
711
723
712
724
const intent = await this . waas . listAccounts ( )
713
725
const res = await this . sendIntent ( intent )
@@ -720,7 +732,7 @@ export class SequenceWaaS {
720
732
}
721
733
722
734
async linkAccount ( challenge : Challenge ) {
723
- await this . updateTimeDrift ( )
735
+ await this . preSendIntent ( )
724
736
725
737
const intent = await this . waas . linkAccount ( challenge . getIntentParams ( ) )
726
738
const res = await this . sendIntent ( intent )
@@ -733,14 +745,14 @@ export class SequenceWaaS {
733
745
}
734
746
735
747
async removeAccount ( accountId : string ) {
736
- await this . updateTimeDrift ( )
748
+ await this . preSendIntent ( )
737
749
738
750
const intent = await this . waas . removeAccount ( { accountId } )
739
751
await this . sendIntent ( intent )
740
752
}
741
753
742
754
async getIdToken ( args ?: { nonce ?: string } ) : Promise < IntentResponseIdToken > {
743
- await this . updateTimeDrift ( )
755
+ await this . preSendIntent ( )
744
756
745
757
const intent = await this . waas . getIdToken ( { nonce : args ?. nonce } )
746
758
const res = await this . sendIntent ( intent )
@@ -788,14 +800,14 @@ export class SequenceWaaS {
788
800
}
789
801
790
802
async signMessage ( args : WithSimpleNetwork < SignMessageArgs > & CommonAuthArgs ) : Promise < SignedMessageResponse > {
791
- await this . updateTimeDrift ( )
803
+ await this . preSendIntent ( )
792
804
793
805
const intent = await this . waas . signMessage ( await this . useIdentifier ( args ) )
794
806
return this . trySendIntent ( args , intent , isSignedMessageResponse )
795
807
}
796
808
797
809
async signTypedData ( args : WithSimpleNetwork < SignTypedDataArgs > & CommonAuthArgs ) : Promise < SignedTypedDataResponse > {
798
- await this . updateTimeDrift ( )
810
+ await this . preSendIntent ( )
799
811
800
812
const intent = await this . waas . signTypedData ( await this . useIdentifier ( args ) )
801
813
return this . trySendIntent ( args , intent , isSignedTypedDataResponse )
@@ -824,42 +836,42 @@ export class SequenceWaaS {
824
836
}
825
837
826
838
async sendTransaction ( args : WithSimpleNetwork < SendTransactionsArgs > & CommonAuthArgs ) : Promise < MaySentTransactionResponse > {
827
- await this . updateTimeDrift ( )
839
+ await this . preSendIntent ( )
828
840
829
841
const intent = await this . waas . sendTransaction ( await this . useIdentifier ( args ) )
830
842
return this . trySendTransactionIntent ( intent , args )
831
843
}
832
844
833
845
async sendERC20 ( args : WithSimpleNetwork < SendERC20Args > & CommonAuthArgs ) : Promise < MaySentTransactionResponse > {
834
- await this . updateTimeDrift ( )
846
+ await this . preSendIntent ( )
835
847
836
848
const intent = await this . waas . sendERC20 ( await this . useIdentifier ( args ) )
837
849
return this . trySendTransactionIntent ( intent , args )
838
850
}
839
851
840
852
async sendERC721 ( args : WithSimpleNetwork < SendERC721Args > & CommonAuthArgs ) : Promise < MaySentTransactionResponse > {
841
- await this . updateTimeDrift ( )
853
+ await this . preSendIntent ( )
842
854
843
855
const intent = await this . waas . sendERC721 ( await this . useIdentifier ( args ) )
844
856
return this . trySendTransactionIntent ( intent , args )
845
857
}
846
858
847
859
async sendERC1155 ( args : WithSimpleNetwork < SendERC1155Args > & CommonAuthArgs ) : Promise < MaySentTransactionResponse > {
848
- await this . updateTimeDrift ( )
860
+ await this . preSendIntent ( )
849
861
850
862
const intent = await this . waas . sendERC1155 ( await this . useIdentifier ( args ) )
851
863
return this . trySendTransactionIntent ( intent , args )
852
864
}
853
865
854
866
async callContract ( args : WithSimpleNetwork < SendContractCallArgs > & CommonAuthArgs ) : Promise < MaySentTransactionResponse > {
855
- await this . updateTimeDrift ( )
867
+ await this . preSendIntent ( )
856
868
857
869
const intent = await this . waas . callContract ( await this . useIdentifier ( args ) )
858
870
return this . trySendTransactionIntent ( intent , args )
859
871
}
860
872
861
873
async feeOptions ( args : WithSimpleNetwork < SendTransactionsArgs > & CommonAuthArgs ) : Promise < FeeOptionsResponse > {
862
- await this . updateTimeDrift ( )
874
+ await this . preSendIntent ( )
863
875
864
876
const intent = await this . waas . feeOptions ( await this . useIdentifier ( args ) )
865
877
return this . trySendIntent ( args , intent , isFeeOptionsResponse )
0 commit comments