Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tag & metadata support for deep links #5075

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions packages/shared/lib/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const isValidAddressAndPrefix = (address: string, expectedAddressPrefix: string): boolean =>
new RegExp(`^${expectedAddressPrefix}1[02-9ac-hj-np-z]{59}$`).test(address)

export const isValidEVMAddress = (address: string): boolean => new RegExp('0x[a-fA-F0-9]{40}$').test(address)
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ export enum WalletOperation {
export enum SendOperationParameter {
Amount = 'amount',
Unit = 'unit',
}

/**
* The query parameters available exclusively for a bridge operation
*/
export enum SwapOperationParameter {
ChainId = 'chainId',
ReceiverAddress = 'receiverAddress',
Tag = 'tag',
Metadata = 'metadata',
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { NotificationData } from '../../../typings/notification'

import type {
DeepLinkContext,
SendOperationParameters,
SendWithMetaDataOperationParameters,
WalletOperation,
} from '@common/deep-links'
import type { DeepLinkContext, SendOperationParameters, WalletOperation } from '@common/deep-links'

/**
* A union type of all deep link contexts' operations.
Expand All @@ -15,7 +10,7 @@ export type DeepLinkOperation = WalletOperation
/**
* A union type of all deep link operations' parameters.
*/
export type DeepLinkParameters = void | SendOperationParameters | SendWithMetaDataOperationParameters
export type DeepLinkParameters = void | SendOperationParameters

/**
* The content of a deep link request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ export type SendOperationParameters = {
amount: string
unit: Unit
message: string
}

/**
* The parameters of a bridge operation.
*/
export type SendWithMetaDataOperationParameters = SendOperationParameters & {
tag: string
metadata: string
tag?: string
metadata?: string
}
77 changes: 20 additions & 57 deletions packages/shared/lib/common/deep-links/wallet-context-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ import { Unit } from '@iota/unit-converter'

import { localize } from '@core/i18n'

import { isValidAddressAndPrefix, isValidEVMAddress } from '../../address'
import { isValidAddressAndPrefix } from '../../address'
import { addError } from '../../errors'

import {
DeepLinkContext,
SendOperationParameter,
SwapOperationParameter,
WalletOperation,
} from '@common/deep-links/enums'
import {
DeepLinkParameters,
DeepLinkRequest,
SendOperationParameters,
SendWithMetaDataOperationParameters,
} from '@common/deep-links/types'
import { DeepLinkContext, SendOperationParameter, WalletOperation } from '@common/deep-links/enums'
import { DeepLinkParameters, DeepLinkRequest, SendOperationParameters } from '@common/deep-links/types'
import { formatNumber } from '@lib/currency'
import { getNumberOfDecimalPlaces } from '@lib/utils'
import { getByteLengthOfString, getNumberOfDecimalPlaces } from '@lib/utils'

/**
* Parses a deep link within the wallet context.
Expand Down Expand Up @@ -53,9 +43,6 @@ export const parseWalletDeepLinkRequest = (url: URL, expectedAddressPrefix: stri
case WalletOperation.Send:
parameters = parseSendOperation(address, url.searchParams)
break
case WalletOperation.SwapOut:
parameters = parseSwapOutOperation(address, url.searchParams)
break
default:
return addError({
time: Date.now(),
Expand All @@ -82,7 +69,6 @@ export const parseWalletDeepLinkRequest = (url: URL, expectedAddressPrefix: stri
*
* @param {string} address The recipient's Bech32 address.
* @param {URLSearchParams} searchParams The query parameters of the deep link URL.
* @param {string} expectedAddressPrefix The expected human-readable part of a Bech32 address.
*
* @return {void | SendOperationParameters} The formatted parameters for the send operation.
*/
Expand Down Expand Up @@ -126,50 +112,27 @@ const parseSendOperation = (address: string, searchParams: URLSearchParams): voi
})
}

return {
address,
amount: formatNumber(Math.abs(parsedAmount), numDecimalPlaces, numDecimalPlaces),
unit: parsedUnit,
message: '',
}
}

const parseSwapOutOperation = (
waspAddress: string,
searchParams: URLSearchParams
): void | SendWithMetaDataOperationParameters => {
const sendParams = parseSendOperation(waspAddress, searchParams)
if (!sendParams) {
// Error handling is done in parseSendOperation already
return
}

const chainId = searchParams.get(SwapOperationParameter.ChainId)
if (!chainId) {
return addError({
time: Date.now(),
type: 'deepLink',
message: 'The chain ID is missing',
})
} else if (!Number.isInteger(Number(chainId))) {
return addError({ time: Date.now(), type: 'deepLink', message: `Chain ID is not an integer '${chainId}'` })
const tag = searchParams.get(SendOperationParameter.Tag)
if (tag) {
if (getByteLengthOfString(tag) > 64) {
return addError({ time: Date.now(), type: 'deepLink', message: localize('error.send.tagLength') })
}
}

const receiverAddress = searchParams.get(SwapOperationParameter.ReceiverAddress)
if (!receiverAddress) {
return addError({ time: Date.now(), type: 'deepLink', message: 'The receiver address is missing' })
} else if (!isValidEVMAddress(receiverAddress)) {
return addError({
time: Date.now(),
type: 'deepLink',
message: `The receiver address is not an EVM address: '${receiverAddress}'`,
})
const metadata = searchParams.get(SendOperationParameter.Metadata)
if (metadata) {
if (getByteLengthOfString(metadata) > 8192) {
return addError({ time: Date.now(), type: 'deepLink', message: localize('error.send.metadataLength') })
}
}

return {
...sendParams,
tag: WalletOperation.SwapOut,
metadata: `${receiverAddress}:${chainId}`,
address,
amount: formatNumber(Math.abs(parsedAmount), numDecimalPlaces, numDecimalPlaces),
unit: parsedUnit,
message: '',
tag,
metadata,
}
}

Expand Down
25 changes: 0 additions & 25 deletions packages/shared/lib/tests/address.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/lib/typings/sendParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SendWithMetaDataOperationParameters } from '@common/deep-links'
import { Unit } from '@iota/unit-converter'
import { LabeledWalletAccount } from './wallet'

Expand Down
4 changes: 2 additions & 2 deletions packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,8 @@
"total": "Total: {balance}",
"openExplorer": "View in Explorer",
"activityDetails": "Transaction details",
"tag": "Tag",
"metadata": "Metadata"
"tag": "Tag (optional)",
"metadata": "Metadata (optional)"
},
"dates": {
"today": "Today",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/routes/dashboard/wallet/Wallet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
remainder_value_strategy: {
strategy: 'ChangeAddress',
},
indexation: { index: tag ?? 'firefly', data: convertStringToUtf8Array(data) },
indexation: { index: tag ? tag : 'firefly', data: convertStringToUtf8Array(data) },
},
{
onSuccess(response) {
Expand Down