-
Notifications
You must be signed in to change notification settings - Fork 103
/
handleDeepLinkSendConfirmationOperation.ts
103 lines (90 loc) · 3.47 KB
/
handleDeepLinkSendConfirmationOperation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { get } from 'svelte/store'
import { networkHrp } from '@core/network'
import { isStringTrue, getByteLengthOfString } from '@core/utils'
import {
getAssetById,
INewTransactionDetails,
selectedAccountAssets,
setNewTransactionDetails,
Subject,
} from '@core/wallet'
import { isValidAddressAndPrefix } from '@lib/address'
import { openPopup } from '@auxiliary/popup'
import { SendOperationParameter } from '../../../enums'
import {
SurplusNotANumberError,
InvalidAddressError,
MetadataLengthError,
NoAddressSpecifiedError,
TagLengthError,
UnknownAssetError,
} from '../../../errors'
import { getRawAmountFromSearchParam } from '../../../utils'
export function handleDeepLinkSendConfirmationOperation(searchParams: URLSearchParams): void {
const transactionDetails = parseSendConfirmationOperation(searchParams)
if (transactionDetails) {
setNewTransactionDetails(transactionDetails)
openPopup({
type: 'sendConfirmation',
overflow: true,
props: {
disableBack: true,
},
})
}
}
/**
* Parses a deep link for the send operation.
*
* @method parseSendConfirmationOperation
*
* @param {URLSearchParams} searchParams The query parameters of the deep link URL.
*
* @return {INewTransactionDetails} The formatted parameters for the send operation.
*/
function parseSendConfirmationOperation(searchParams: URLSearchParams): INewTransactionDetails {
// Check address exists and is valid this is not optional.
const address = searchParams.get(SendOperationParameter.Address)
if (!address) {
throw new NoAddressSpecifiedError()
}
if (!isValidAddressAndPrefix(address, get(networkHrp))) {
throw new InvalidAddressError()
}
const recipient: Subject = { type: 'address', address }
const assetId = searchParams.get(SendOperationParameter.AssetId)
const baseAsset = get(selectedAccountAssets).baseCoin
const asset = assetId ? getAssetById(assetId) : baseAsset
if (!asset) {
throw new UnknownAssetError()
}
const rawAmount = getRawAmountFromSearchParam(searchParams)
const surplus = searchParams.get(SendOperationParameter.Surplus)
if (surplus && parseInt(surplus).toString() !== surplus) {
throw new SurplusNotANumberError(surplus)
}
const metadata = searchParams.get(SendOperationParameter.Metadata)
if (getByteLengthOfString(metadata) > 8192) {
throw new MetadataLengthError()
}
const tag = searchParams.get(SendOperationParameter.Tag)
if (getByteLengthOfString(tag) > 64) {
throw new TagLengthError()
}
const unit = searchParams.get(SendOperationParameter.Unit) ?? asset.metadata?.unit
const giftStorageDeposit = isStringTrue(searchParams.get(SendOperationParameter.GiftStorageDeposit))
const disableToggleGift = isStringTrue(searchParams.get(SendOperationParameter.DisableToggleGift))
const disableChangeExpiration = isStringTrue(searchParams.get(SendOperationParameter.DisableChangeExpiration))
return {
...(asset && { asset }),
...(recipient && { recipient }),
...(rawAmount && { rawAmount }),
...(unit && { unit }),
...(metadata && { metadata }),
...(tag && { tag }),
...(giftStorageDeposit && { giftStorageDeposit }),
...(surplus && { surplus }),
...(disableToggleGift && { disableToggleGift }),
...(disableChangeExpiration && { disableChangeExpiration }),
}
}