Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
SignedIntent,
SignMessageArgs,
getTimeDrift,
updateTimeDrift
updateTimeDrift,
getLocalTime
} from './intents'
import {
FeeOptionsResponse,
Expand Down Expand Up @@ -672,9 +673,9 @@ export class SequenceWaaS {
}

async waitForSessionValid(timeout: number = 600000, pollRate: number = 2000) {
const start = Date.now()
const start = getLocalTime()

while (Date.now() - start < timeout) {
while (getLocalTime() - start < timeout) {
if (await this.isSessionValid()) {
return true
}
Expand Down
8 changes: 6 additions & 2 deletions packages/waas/src/intents/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function isSessionStorageAvailable() {
return typeof window === 'object' && typeof window.sessionStorage === 'object'
}

export function getLocalTime() {
return new Date().getTime()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting.. new Date().getTime() works differently then Date.now() ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've confirmed, Date.now() is very weird, and does not consider day light savings

}

export function getTimeDrift() {
if (isSessionStorageAvailable()) {
const drift = window.sessionStorage.getItem(timeDriftKey)
Expand All @@ -29,15 +33,15 @@ export function getTimeDrift() {
}

export function updateTimeDrift(serverTime: Date) {
timeDrift = (Date.now() - serverTime.getTime()) / 1000
timeDrift = (getLocalTime() - serverTime.getTime()) / 1000
if (isSessionStorageAvailable()) {
window.sessionStorage.setItem(timeDriftKey, timeDrift.toString(10))
}
}

export function makeIntent<T>(name: IntentName, lifespan: number, data: T): Intent<T> {
const drift = Math.abs(Math.floor(getTimeDrift() || 0))
const issuedAt = Math.floor(Date.now() / 1000 - drift)
const issuedAt = Math.floor(getLocalTime() / 1000 - drift)
const expiresAt = issuedAt + lifespan + 2 * drift
return {
version: VERSION,
Expand Down
4 changes: 3 additions & 1 deletion packages/waas/src/intents/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getLocalTime } from "./base"

export function useLifespan(lifespan: number) {
const issuedAt = Math.floor(Date.now() / 1000)
const issuedAt = Math.floor(getLocalTime() / 1000)
return {
issuedAt,
expiresAt: issuedAt + lifespan
Expand Down
Loading