-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidators.ts
More file actions
661 lines (583 loc) · 21.8 KB
/
validators.ts
File metadata and controls
661 lines (583 loc) · 21.8 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import { RESPONSE_MESSAGES, SUPPORTED_ADDRESS_PATTERN, NETWORK_TICKERS, TRIGGER_POST_VARIABLES, EMAIL_REGEX } from '../constants/index'
import { Prisma } from '@prisma/client'
import config from '../config/index'
import { type CreatePaybuttonInput, type UpdatePaybuttonInput } from '../services/paybuttonService'
import { type CreateWalletInput, type UpdateWalletInput } from '../services/walletService'
import { getAddressPrefix, parseWebsiteURL } from './index'
import xecaddr from 'xecaddrjs'
import { CreatePaybuttonTriggerInput, PostDataParameters } from 'services/triggerService'
import crypto from 'crypto'
import { getUserPrivateKey } from '../services/userService'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import moment from 'moment-timezone'
import { ClientPaymentField } from 'services/clientPaymentService'
/* The functions exported here should validate the data structure / syntax of an
* input by throwing an error in case something is different from the expected.
* The prefix for each function name * here defined shall be:
* - 'parse', if the function validates the input and transforms it into another output;
* - 'validate', if the function only validates the input and returns `true` or `false`.
--------------------------------------------------------------------------------------- */
/* Validates the address and adds a prefix to it, if it does not have it already.
Also removes duplicated addresses.
*/
const parseAddressTextBlock = function (addressBlock: string): string[] {
return addressBlock.trim()
.split('\n')
.map((addr) => parseAddress(addr.trim()))
.filter((value, index, self) => self.indexOf(value) === index)
}
export const parseAddress = function (addressString: string | undefined): string {
if (addressString === '' || addressString === undefined) throw new Error(RESPONSE_MESSAGES.ADDRESS_NOT_PROVIDED_400.message)
let parsedAddress: string
if (
addressString.match(SUPPORTED_ADDRESS_PATTERN) != null &&
xecaddr.isValidAddress(addressString.toLowerCase()) as boolean
) {
if (addressString.includes(':')) {
parsedAddress = addressString
} else {
const prefix = getAddressPrefix(addressString.toLowerCase())
parsedAddress = `${prefix}:${addressString}`
}
} else {
throw new Error(RESPONSE_MESSAGES.INVALID_ADDRESS_400.message)
}
return parsedAddress.toLowerCase()
}
export const parseButtonData = function (buttonDataString: string | undefined): string {
let parsedButtonData: string
if (buttonDataString === '' || buttonDataString === undefined) {
parsedButtonData = '{}'
} else {
try {
const jsonObject = JSON.parse(buttonDataString)
parsedButtonData = JSON.stringify(jsonObject, null, 0) // remove linebreaks, tabs & spaces.
} catch (e: any) {
throw new Error(RESPONSE_MESSAGES.INVALID_BUTTON_DATA_400.message)
}
}
return parsedButtonData
}
export const parseError = function (error: Error): Error {
if (error instanceof PrismaClientKnownRequestError) {
switch (error.code) {
case 'P2002':
if (error.message.includes('Paybutton_name_providerUserId_unique_constraint')) {
return new Error(RESPONSE_MESSAGES.PAYBUTTON_NAME_ALREADY_EXISTS_400.message)
} else if (error.message.includes('Wallet_name_providerUserId_unique_constraint')) {
return new Error(RESPONSE_MESSAGES.WALLET_NAME_ALREADY_EXISTS_400.message)
} else if (error.message.includes('Transaction_hash_addressId_key')) {
return new Error(RESPONSE_MESSAGES.TRANSACTION_ALREADY_EXISTS_FOR_ADDRESS_400.message)
}
break
case 'P2025':
if (error.meta?.modelName === 'Paybutton') {
return new Error(RESPONSE_MESSAGES.NO_BUTTON_FOUND_404.message)
} else if (error.meta?.modelName === 'Address') {
return new Error(RESPONSE_MESSAGES.NO_ADDRESS_FOUND_404.message)
} else if (error.meta?.modelName === 'Transaction') {
return new Error(RESPONSE_MESSAGES.NO_TRANSACTION_FOUND_404.message)
}
break
}
}
return error
}
export interface SignUpPasswordPOSTParameters {
password?: string
passwordConfirmed?: string
}
export interface ChangePasswordPOSTParameters {
oldPassword?: string
newPassword?: string
newPasswordConfirmed?: string
}
export interface ChangePasswordInput {
oldPassword: string
newPassword: string
}
export interface UpdatePreferredCurrencyInput {
currencyId: number
}
export const parseChangePasswordPOSTRequest = function (params: ChangePasswordPOSTParameters): ChangePasswordInput {
if (
params.newPassword !== params.newPasswordConfirmed ||
params.oldPassword === '' ||
params.oldPassword === undefined ||
params.newPassword === '' ||
params.newPassword === undefined ||
params.newPasswordConfirmed === '' ||
params.newPasswordConfirmed === undefined
) {
throw new Error(RESPONSE_MESSAGES.INVALID_PASSWORD_FORM_400.message)
}
return {
oldPassword: params.oldPassword,
newPassword: params.newPassword
}
}
export interface PaybuttonPOSTParameters {
userId?: string
walletId?: string
name?: string
buttonData?: string
addresses?: string
url?: string
description?: string
}
export const parsePaybuttonPOSTRequest = function (params: PaybuttonPOSTParameters): CreatePaybuttonInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if (params.name === '' || params.name === undefined) throw new Error(RESPONSE_MESSAGES.NAME_NOT_PROVIDED_400.message)
if (params.addresses === '' || params.addresses === undefined) throw new Error(RESPONSE_MESSAGES.ADDRESSES_NOT_PROVIDED_400.message)
if (params.walletId === '' || params.walletId === undefined) {
throw new Error(RESPONSE_MESSAGES.WALLET_ID_NOT_PROVIDED_400.message)
}
const walletId = params.walletId
const parsedAddresses = parseAddressTextBlock(params.addresses)
const parsedButtonData = parseButtonData(params.buttonData)
const parsedURL = (params.url === '' || params.url === undefined) ? '' : parseWebsiteURL(params.url)
return {
userId: params.userId,
name: params.name,
buttonData: parsedButtonData,
prefixedAddressList: parsedAddresses,
walletId,
url: parsedURL,
description: params.description ?? ''
}
}
export interface WalletPOSTParameters {
userId?: string
name?: string
isXECDefault?: boolean
isBCHDefault?: boolean
addressIdList: string[]
}
export interface PaybuttonPATCHParameters {
name?: string
addresses?: string
userId?: string
url?: string
description?: string
}
export interface WalletPATCHParameters {
name: string
userId?: string
isXECDefault?: boolean
isBCHDefault?: boolean
addressIdList: string[]
}
export const parseWalletPOSTRequest = function (params: WalletPOSTParameters): CreateWalletInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if (params.name === '' || params.name === undefined) throw new Error(RESPONSE_MESSAGES.NAME_NOT_PROVIDED_400.message)
return {
userId: params.userId,
name: params.name,
addressIdList: params.addressIdList,
isXECDefault: params.isXECDefault,
isBCHDefault: params.isBCHDefault
}
}
export const parseWalletPATCHRequest = function (params: WalletPATCHParameters): UpdateWalletInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if (params.name === '' || params.name === undefined) throw new Error(RESPONSE_MESSAGES.NAME_NOT_PROVIDED_400.message)
return {
name: params.name,
userId: params.userId,
addressIdList: params.addressIdList,
isXECDefault: params.isXECDefault,
isBCHDefault: params.isBCHDefault
}
}
export interface PaybuttonTriggerPOSTParameters {
userId?: string
postURL?: string
postData?: string
isEmailTrigger: boolean
currentTriggerId?: string
emails?: string
}
export interface PaybuttonTriggerParseParameters {
userId: string
postData: string
postDataParameters: PostDataParameters
}
interface TriggerSignature {
payload: string
signature: string
}
function getSignaturePayload (postData: string, postDataParameters: PostDataParameters): string {
const includedVariables = TRIGGER_POST_VARIABLES.filter(v => postData.includes(v)).sort()
const result = includedVariables.map(varString => {
const key = varString.replace('<', '').replace('>', '') as keyof PostDataParameters
let valueString = ''
if (key === 'opReturn') {
const value = postDataParameters[key]
valueString = `${value.message}+${value.paymentId}`
} else {
valueString = postDataParameters[key] as string
}
return valueString
}).filter(value => value !== undefined && value !== null && value !== '')
return result.join('+')
}
export function signPostData ({ userId, postData, postDataParameters }: PaybuttonTriggerParseParameters): TriggerSignature {
const payload = getSignaturePayload(postData, postDataParameters)
const pk = getUserPrivateKey(userId)
const data: Uint8Array = typeof payload === 'string'
? new TextEncoder().encode(payload)
: payload as unknown as Uint8Array
const signature = crypto.sign(
null,
data,
pk
)
return {
payload,
signature: signature.toString('hex')
}
}
export function parseTriggerPostData ({ userId, postData, postDataParameters }: PaybuttonTriggerParseParameters): any {
try {
const buttonName = JSON.stringify(postDataParameters.buttonName)
const opReturn = JSON.stringify(postDataParameters.opReturn, undefined, 2)
const signature = signPostData({ userId, postData, postDataParameters })
const resultingData = postData
.replace('<amount>', postDataParameters.amount.toString())
.replace('<currency>', `"${postDataParameters.currency}"`)
.replace('<txId>', `"${postDataParameters.txId}"`)
.replace('<buttonName>', buttonName)
.replace('<address>', `"${postDataParameters.address}"`)
.replace('<timestamp>', postDataParameters.timestamp.toString())
.replace('<opReturn>', opReturn)
.replace('<signature>', `${JSON.stringify(signature, undefined, 2)}`)
.replace('<inputAddresses>', `${JSON.stringify(postDataParameters.inputAddresses, undefined, 2)}`)
.replace('<outputAddresses>', `${JSON.stringify(postDataParameters.outputAddresses, undefined, 2)}`)
.replace('<value>', `"${postDataParameters.value}"`)
const parsedResultingData = JSON.parse(resultingData)
return parsedResultingData
} catch (err: any) {
const includedVariables = TRIGGER_POST_VARIABLES.filter(v => postData.includes(v))
if (includedVariables.length > 0) {
throw new Error(RESPONSE_MESSAGES.INVALID_DATA_JSON_WITH_VARIABLES_400(includedVariables).message)
}
throw new Error(RESPONSE_MESSAGES.INVALID_DATA_JSON_400.message)
}
}
export const parsePaybuttonTriggerPOSTRequest = function (params: PaybuttonTriggerPOSTParameters): CreatePaybuttonTriggerInput {
// userId
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
// emails
if (params.emails !== undefined && params.emails !== '' && !isEmailValid(params.emails)) {
throw new Error(RESPONSE_MESSAGES.INVALID_EMAIL_400.message)
}
// postURL
let postURL: string | undefined
if (params.postURL === undefined || params.postURL === '') { postURL = undefined } else {
try {
const parsed = new URL(params.postURL)
if (!['http:', 'https:'].includes(parsed.protocol)) throw new Error()
postURL = params.postURL
} catch (_) {
throw new Error(RESPONSE_MESSAGES.INVALID_URL_400.message)
}
}
// postData
let postData: string | undefined
if (params.postData === undefined || params.postData === '') { postData = undefined } else {
const dummyPostDataParameters = {
amount: new Prisma.Decimal(0),
currency: '',
txId: '',
buttonName: '',
address: '',
timestamp: 0,
opReturn: EMPTY_OP_RETURN,
inputAddresses: [],
outputAddresses: [],
value: ''
}
const parsed = parseTriggerPostData({
userId: params.userId,
postData: params.postData,
postDataParameters: dummyPostDataParameters
})
if (parsed === null || typeof parsed !== 'object') {
throw new Error(RESPONSE_MESSAGES.INVALID_DATA_JSON_400.message)
}
postData = params.postData
}
if (
!params.isEmailTrigger &&
(postData === undefined || postURL === undefined)
) {
throw new Error(RESPONSE_MESSAGES.POST_URL_AND_DATA_MUST_BE_SET_TOGETHER_400.message)
}
if (
params.isEmailTrigger &&
(params.emails === '' || params.emails === undefined)
) {
throw new Error(RESPONSE_MESSAGES.MISSING_EMAIL_FOR_TRIGGER_400.message)
}
return {
emails: params.emails,
postURL,
postData,
userId: params.userId,
isEmailTrigger: params.isEmailTrigger
}
}
const isEmailValid = (email?: string): boolean => {
if (email === undefined) return false
return EMAIL_REGEX.test(email)
}
export const parsePaybuttonPATCHRequest = function (params: PaybuttonPATCHParameters, paybuttonId: string): UpdatePaybuttonInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
const parsedURL = (params.url === '' || params.url === undefined) ? '' : parseWebsiteURL(params.url)
const ret: UpdatePaybuttonInput = {
name: params.name,
userId: params.userId,
paybuttonId,
description: params.description ?? '',
url: parsedURL
}
if (params.addresses !== '' && params.addresses !== undefined) {
ret.prefixedAddressList = parseAddressTextBlock(params.addresses)
}
return ret
}
export const validateNetworkTicker = function (networkTicker: string): void {
if (!Object.values(NETWORK_TICKERS).includes(networkTicker)) {
throw new Error(RESPONSE_MESSAGES.INVALID_TICKER_400.message)
}
}
export const validatePriceAPIUrlAndToken = function (): void {
if (config.priceAPIURL === '') {
throw new Error(RESPONSE_MESSAGES.MISSING_PRICE_API_URL_400.message)
}
if (process.env.PRICE_API_TOKEN === '' || process.env.PRICE_API_TOKEN === undefined) {
throw new Error(RESPONSE_MESSAGES.MISSING_PRICE_API_TOKEN_400.message)
}
}
export interface WSGETParameters {
addresses: string[]
}
// We look for | as separators, except if they are preceeded by a backslash.
// In that case, we return the string without the backslash
// In the other case, we return an array.
export function parseStringToArray (str: string): string | string[] {
const pattern = /(?<!\\)\|/
// Split the input string using the pattern
const splitted = str.split(pattern)
if (splitted.length > 1) {
return splitted.map(s => s.replace('\\|', '|'))
}
return str.replace('\\|', '|')
}
export interface OpReturnData {
rawMessage: string
message: string
paymentId: string
}
export const EMPTY_OP_RETURN: OpReturnData = {
rawMessage: '',
message: '',
paymentId: ''
}
function splitAtFirst (str: string, separator: string): string[] {
const index = str.indexOf(separator)
if (index === -1) { return [str] };
return [str.slice(0, index), str.slice(index + separator.length)]
}
// We try to parse the opReturn string from k=v space-separated
// pairs to { [k] = v} . We also try to parse each
// key has the unparsable string as value.
export function parseOpReturnData (opReturnData: string): any {
const dataObject: any = {}
// Try to parse it as JSON first, excluding simple numbers
try {
const jsonParsed = JSON.parse(opReturnData)
if (typeof jsonParsed === 'number') {
throw new Error()
}
return jsonParsed
} catch {}
// Try to parse it as k=v pairs
try {
const keyValuePairs = opReturnData.split(' ')
for (const kvString of keyValuePairs) {
const splitted = splitAtFirst(kvString, '=')
if (splitted[1] === undefined || splitted[1] === '' || splitted[0] === '') {
return parseStringToArray(opReturnData)
}
const key = splitted[0]
const value = parseStringToArray(splitted[1])
// (parse value as array)
dataObject[key] = value
}
return dataObject
} catch (err: any) {
return parseStringToArray(opReturnData)
}
}
export const exportedForTesting = {
getSignaturePayload
}
export interface CreateOrganizationPOSTParameters {
creatorId?: string
name?: string
}
export interface CreateOrganizationInput {
creatorId: string
name: string
}
export interface UpdateOrganizationPUTParameters {
userId?: string
name?: string
address?: string
}
export interface UpdateOrganizationInput {
userId: string
name?: string
address?: string
}
export interface JoinOrganizationPOSTParameters {
userId?: string
token?: string
}
export interface JoinOrganizationInput {
userId: string
token: string
}
export interface UpdatePreferredCurrencyPUTParameters {
currencyId?: string | number
}
export interface UpdateUserTimezonePUTParameters {
timezone: string
}
export const parseJoinOrganizationPOSTRequest = function (params: JoinOrganizationPOSTParameters): JoinOrganizationInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if (params.token === '' || params.token === undefined) throw new Error(RESPONSE_MESSAGES.INVITATION_TOKEN_NOT_PROVIDED_400.message)
return {
userId: params.userId,
token: params.token
}
}
export const parseUpdateOrganizationPUTRequest = function (params: UpdateOrganizationPUTParameters): UpdateOrganizationInput {
if (params.userId === '' || params.userId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if ((params.name === '' || params.name === undefined) && (params.address === '' || params.address === undefined)) throw new Error(RESPONSE_MESSAGES.MISSING_PARAMS_TO_UPDATE_ORGANIZATION_400.message)
return {
userId: params.userId,
name: params.name,
address: params.address
}
}
export const parseCreateOrganizationPOSTRequest = function (params: CreateOrganizationPOSTParameters): CreateOrganizationInput {
if (params.creatorId === '' || params.creatorId === undefined) throw new Error(RESPONSE_MESSAGES.USER_ID_NOT_PROVIDED_400.message)
if (params.name === '' || params.name === undefined) throw new Error(RESPONSE_MESSAGES.ORGANIZATION_NAME_NOT_PROVIDED_400.message)
return {
creatorId: params.creatorId,
name: params.name
}
}
export const parseUpdatePUTRequest = function (params: UpdatePreferredCurrencyPUTParameters): UpdatePreferredCurrencyInput {
if (params.currencyId === '' ||
params.currencyId === undefined
) {
throw new Error(RESPONSE_MESSAGES.INVALID_PASSWORD_FORM_400.message)
}
return {
currencyId: Number(params.currencyId)
}
}
export const parseUpdateUserTimezonePUTRequest = function (params: UpdateUserTimezonePUTParameters): UpdateUserTimezonePUTParameters {
if (params.timezone === '' ||
params.timezone === undefined ||
!moment.tz.names().includes(params.timezone)) {
throw new Error(RESPONSE_MESSAGES.INVALID_TIMEZONE_FORM_400.message)
}
return { timezone: params.timezone }
}
export interface CreateInvoicePOSTParameters {
transactionId: string
amount: Prisma.Decimal
description: string
recipientName: string
recipientAddress: string
customerName: string
customerAddress: string
}
export interface CreatePaymentIdPOSTParameters {
address?: string
amount?: string
fields?: string
}
export interface CreatePaymentIdInput {
address: string
amount?: Prisma.Decimal
fields?: ClientPaymentField[]
}
export const parseClientPaymentFields = function (fieldsInput: string | object | undefined): ClientPaymentField[] | undefined {
if (fieldsInput === undefined || fieldsInput === '') {
return undefined
}
let parsedFields: unknown
try {
if (typeof fieldsInput === 'object') {
parsedFields = fieldsInput
} else {
parsedFields = JSON.parse(fieldsInput)
}
} catch {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELDS_FORMAT_400.message)
}
if (!Array.isArray(parsedFields)) {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELDS_FORMAT_400.message)
}
for (const field of parsedFields) {
if (
typeof field !== 'object' ||
field === null ||
typeof field.name !== 'string' ||
field.name?.trim() === ''
) {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELD_STRUCTURE_400.message)
}
if (field.value !== undefined && typeof field.value !== 'string' && typeof field.value !== 'boolean') {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELD_STRUCTURE_400.message)
}
if (field.type !== undefined && typeof field.type !== 'string') {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELD_STRUCTURE_400.message)
}
if (field.text !== undefined && typeof field.text !== 'string') {
throw new Error(RESPONSE_MESSAGES.INVALID_FIELD_STRUCTURE_400.message)
}
}
return parsedFields as ClientPaymentField[]
}
export const parseAmount = function (amountInput: string | undefined): Prisma.Decimal | undefined {
if (amountInput === undefined || amountInput === '') {
return undefined
}
const trimmedAmount = amountInput.trim()
const numericAmount = Number(trimmedAmount)
if (isNaN(numericAmount) || !isFinite(numericAmount) || numericAmount <= 0) {
throw new Error(RESPONSE_MESSAGES.INVALID_AMOUNT_400.message)
}
return new Prisma.Decimal(trimmedAmount)
}
export const parseCreatePaymentIdPOSTRequest = function (params: CreatePaymentIdPOSTParameters): CreatePaymentIdInput {
if (
params.address === undefined ||
params.address === ''
) {
throw new Error(RESPONSE_MESSAGES.ADDRESS_NOT_PROVIDED_400.message)
}
const amount = parseAmount(params.amount)
const fields = parseClientPaymentFields(params.fields)
return {
address: params.address,
amount,
fields
}
}