-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support adding custom expiration time unlock condition in deepl…
…inks (#7739) * feat: add expiration date to deeplinks * fix: cleanup * fix: cleanup * fix: improve timestamp, use unix and allow 1h, 1d... * feat: update handbook * fix: update sendConfirmation flow and split constnats,enums.. in different files * feat: remove debris * fix: initial expiration component calculation --------- Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
- Loading branch information
1 parent
0130397
commit 7d5fd2d
Showing
16 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/shared/lib/auxiliary/deep-link/constants/expiration-date-regex.constant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TimeUnit } from '../enums' | ||
|
||
export const EXPIRATION_DATE_REGEX = new RegExp(`^(\\d+)(${Object.values(TimeUnit).join('|')})$`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './expiration-date-regex.constant' | ||
export * from './time-unit-ms.constant' | ||
export * from './url-cleanup-regex.constant' |
14 changes: 14 additions & 0 deletions
14
packages/shared/lib/auxiliary/deep-link/constants/time-unit-ms.constant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
MILLISECONDS_PER_DAY, | ||
MILLISECONDS_PER_HOUR, | ||
MILLISECONDS_PER_MINUTE, | ||
MILLISECONDS_PER_WEEK, | ||
} from 'shared/lib/core/utils' | ||
import { TimeUnit } from '../enums' | ||
|
||
export const TIME_UNIT_MS_MAP: Record<TimeUnit, number> = { | ||
[TimeUnit.Weeks]: MILLISECONDS_PER_WEEK, | ||
[TimeUnit.Days]: MILLISECONDS_PER_DAY, | ||
[TimeUnit.Hours]: MILLISECONDS_PER_HOUR, | ||
[TimeUnit.Minutes]: MILLISECONDS_PER_MINUTE, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './add-proposal-parameter.enum' | ||
export * from './deep-link-context.enum' | ||
export * from './governance-operation.enum' | ||
export * from './add-proposal-parameter.enum' | ||
export * from './send-operation-parameter.enum' | ||
export * from './time-unit.enum' | ||
export * from './wallet-operation.enum' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/shared/lib/auxiliary/deep-link/enums/time-unit.enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum TimeUnit { | ||
Weeks = 'w', | ||
Days = 'd', | ||
Hours = 'h', | ||
Minutes = 'm', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
export * from './amount-not-an-integer.error' | ||
export * from './invalid-address.error' | ||
export * from './invalid-asset-id.error' | ||
export * from './invalid-expiration-date.error' | ||
export * from './metadata-length.error' | ||
export * from './no-address-specified.error' | ||
export * from './tag-length.error' | ||
export * from './past-expiration-date.error' | ||
export * from './surplus-not-a-number.error' | ||
export * from './surplus-not-supported.error' | ||
export * from './tag-length.error' | ||
export * from './unknown-asset.error' |
14 changes: 14 additions & 0 deletions
14
packages/shared/lib/auxiliary/deep-link/errors/invalid-expiration-date.error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { BaseError } from '@core/error' | ||
import { localize } from '@core/i18n' | ||
|
||
export class InvalidExpirationDateError extends BaseError { | ||
constructor() { | ||
const message = localize('error.send.invalidExpirationDate') | ||
super({ | ||
message, | ||
showNotification: true, | ||
saveToErrorLog: false, | ||
logToConsole: true, | ||
}) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/shared/lib/auxiliary/deep-link/errors/past-expiration-date.error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { BaseError } from '@core/error' | ||
import { localize } from '@core/i18n' | ||
|
||
export class PastExpirationDateError extends BaseError { | ||
constructor() { | ||
const message = localize('error.send.pastExpirationDate') | ||
super({ | ||
message, | ||
showNotification: true, | ||
saveToErrorLog: false, | ||
logToConsole: true, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/shared/lib/auxiliary/deep-link/utils/getExpirationDateFromSearchParam.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { convertUnixTimestampToDate } from 'shared/lib/core/utils' | ||
import { EXPIRATION_DATE_REGEX, TIME_UNIT_MS_MAP } from '../constants' | ||
import { TimeUnit } from '../enums' | ||
import { InvalidExpirationDateError, PastExpirationDateError } from '../errors' | ||
|
||
export function getExpirationDateFromSearchParam(expirationDate: string): Date | undefined { | ||
if (!expirationDate) { | ||
return undefined | ||
} | ||
|
||
// Check if it's a Unix timestamp (numeric value) | ||
if (!isNaN(Number(expirationDate))) { | ||
const expirationTimestamp = parseInt(expirationDate) | ||
const expirationDateTime = convertUnixTimestampToDate(expirationTimestamp) // Convert seconds to milliseconds | ||
if (isNaN(expirationDateTime.getTime())) { | ||
throw new InvalidExpirationDateError() | ||
} else if (expirationDateTime.getTime() < Date.now()) { | ||
throw new PastExpirationDateError() | ||
} else { | ||
return expirationDateTime | ||
} | ||
} | ||
|
||
// Validate expiration date format for relative time | ||
const regexMatch = EXPIRATION_DATE_REGEX.exec(expirationDate) | ||
|
||
if (!regexMatch) { | ||
throw new InvalidExpirationDateError() | ||
} | ||
|
||
const value = parseInt(regexMatch[1]) | ||
const unit = regexMatch[2] as TimeUnit | ||
|
||
const selectedTimeUnitValue = TIME_UNIT_MS_MAP[unit] | ||
|
||
if (selectedTimeUnitValue === undefined) { | ||
throw new InvalidExpirationDateError() | ||
} | ||
|
||
const expirationDateTime = new Date(Date.now() + value * selectedTimeUnitValue) | ||
|
||
return expirationDateTime | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './getExpirationDateFromSearchParam' | ||
export * from './getRawAmountFromSearchParam' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters