-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRequestParser.ts
879 lines (799 loc) · 45.5 KB
/
RequestParser.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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
import { isMilliseconds, includesOrigin } from './Helpers';
import { State } from '@nimiq/rpc';
import {
RequestType,
CashlinkTheme,
Currency,
PaymentType,
BasicRequest,
CheckoutRequest,
CreateCashlinkRequest,
ExportRequest,
ManageCashlinkRequest,
OnboardRequest,
ChooseAddressRequest,
RenameRequest,
RpcRequest,
SignMessageRequest,
SignTransactionRequest,
SimpleRequest,
NimiqCheckoutRequest,
MultiCurrencyCheckoutRequest,
SignBtcTransactionRequest,
SignPolygonTransactionRequest,
SetupSwapRequest,
RefundSwapRequest,
} from '../../client/PublicRequestTypes';
import type {
ParsedBasicRequest,
ParsedCheckoutRequest,
ParsedCreateCashlinkRequest,
ParsedExportRequest,
ParsedManageCashlinkRequest,
ParsedOnboardRequest,
ParsedChooseAddressRequest,
ParsedRenameRequest,
ParsedRpcRequest,
ParsedSignMessageRequest,
ParsedSignTransactionRequest,
ParsedSimpleRequest,
ParsedSignBtcTransactionRequest,
ParsedSignPolygonTransactionRequest,
ParsedSetupSwapRequest,
ParsedRefundSwapRequest,
} from './RequestTypes';
import { ParsedNimiqDirectPaymentOptions } from './paymentOptions/NimiqPaymentOptions';
import { ParsedEtherDirectPaymentOptions } from './paymentOptions/EtherPaymentOptions';
import { ParsedBitcoinDirectPaymentOptions } from './paymentOptions/BitcoinPaymentOptions';
import { Utf8Tools } from '@nimiq/utils';
import Config from 'config';
import { SwapAsset } from '@nimiq/fastspot-api';
export class RequestParser {
public static parse(
request: RpcRequest,
state: State,
requestType: RequestType,
): ParsedRpcRequest | null {
if (!request.appName) throw new Error('appName is required');
switch (requestType) {
case RequestType.SIGN_TRANSACTION:
const signTransactionRequest = request as SignTransactionRequest;
if (!signTransactionRequest.value) throw new Error('value is required');
if (!signTransactionRequest.validityStartHeight) throw new Error('validityStartHeight is required');
return {
kind: RequestType.SIGN_TRANSACTION,
appName: signTransactionRequest.appName,
sender: Nimiq.Address.fromString(signTransactionRequest.sender),
recipient: Nimiq.Address.fromString(signTransactionRequest.recipient),
recipientType: signTransactionRequest.recipientType || Nimiq.Account.Type.BASIC,
recipientLabel: signTransactionRequest.recipientLabel,
value: signTransactionRequest.value,
fee: signTransactionRequest.fee || 0,
data: typeof signTransactionRequest.extraData === 'string'
? Utf8Tools.stringToUtf8ByteArray(signTransactionRequest.extraData)
: signTransactionRequest.extraData || new Uint8Array(0),
flags: signTransactionRequest.flags || Nimiq.Transaction.Flag.NONE,
validityStartHeight: signTransactionRequest.validityStartHeight,
} as ParsedSignTransactionRequest;
case RequestType.CHECKOUT:
const checkoutRequest = request as CheckoutRequest;
if (checkoutRequest.shopLogoUrl) {
let origin;
try {
origin = new URL(checkoutRequest.shopLogoUrl).origin;
} catch (err) {
throw new Error(`shopLogoUrl must be a valid URL: ${err}`);
}
if (origin !== state.origin) {
throw new Error(
'shopLogoUrl must have same origin as caller website. Image at ' +
checkoutRequest.shopLogoUrl +
' is not on caller origin ' +
state.origin,
);
}
}
const isPointOfSale = 'isPointOfSale' in checkoutRequest && !!checkoutRequest.isPointOfSale;
let disableDisclaimer = !!checkoutRequest.disableDisclaimer;
if (disableDisclaimer && !includesOrigin(Config.privilegedOrigins, state.origin)) {
// warn and continue
console.warn(`Origin ${state.origin} is not authorized to request disableDisclaimer.`);
disableDisclaimer = false;
}
if (!checkoutRequest.version || checkoutRequest.version === 1) {
if (typeof checkoutRequest.value !== 'number' || checkoutRequest.value <= 0) {
throw new Error('value must be a number >0');
}
if (isPointOfSale) {
throw new Error('isPointOfSale is not supported for v1 checkout.');
}
return {
kind: RequestType.CHECKOUT,
version: 1,
appName: checkoutRequest.appName,
shopLogoUrl: checkoutRequest.shopLogoUrl,
time: Date.now(),
paymentOptions: [new ParsedNimiqDirectPaymentOptions({
currency: Currency.NIM,
type: PaymentType.DIRECT,
amount: checkoutRequest.value.toString(),
expires: 0, // unused for NimiqCheckoutRequests
protocolSpecific: {
extraData: checkoutRequest.extraData,
recipient: checkoutRequest.recipient,
recipientType: checkoutRequest.recipientType || Nimiq.Account.Type.BASIC,
sender: checkoutRequest.sender,
forceSender: !!checkoutRequest.forceSender,
fee: checkoutRequest.fee || 0,
flags: checkoutRequest.flags || Nimiq.Transaction.Flag.NONE,
validityDuration: checkoutRequest.validityDuration,
},
})],
isPointOfSale,
disableDisclaimer,
} as ParsedCheckoutRequest;
}
if (checkoutRequest.version === 2) {
if ( // Check if the origin is allowed to make requests without a NIM payment option
!includesOrigin(Config.checkoutWithoutNimOrigins, state.origin)
&& !checkoutRequest.paymentOptions.some((option) => option.currency === Currency.NIM)
) {
throw new Error('CheckoutRequest must provide a NIM paymentOption.');
}
if (!checkoutRequest.shopLogoUrl) {
throw new Error('shopLogoUrl: string is required'); // shop logo non optional in version 2
}
try {
// Test whether the browser is able to parse the currency as an ISO 4217 currency code,
// see https://www.ecma-international.org/ecma-402/1.0/#sec-6.3.1
(0).toLocaleString('en-US', {
style: 'currency',
currency: checkoutRequest.fiatCurrency,
});
} catch (e) {
throw new Error(`Failed to parse currency ${checkoutRequest.fiatCurrency}. Is it a valid ` +
'ISO 4217 currency code?');
}
if (!checkoutRequest.fiatAmount
|| typeof checkoutRequest.fiatAmount !== 'number'
|| checkoutRequest.fiatAmount <= 0) {
throw new Error('fiatAmount must be a positive non-zero number');
}
if (!checkoutRequest.callbackUrl || typeof checkoutRequest.callbackUrl !== 'string') {
if (checkoutRequest.paymentOptions.some((option) => option.currency !== Currency.NIM)) {
throw new Error('A callbackUrl: string is required for currencies other than NIM to ' +
'monitor payments.');
}
if (!checkoutRequest.paymentOptions.every((option) => !!option.protocolSpecific.recipient)) {
throw new Error('A callbackUrl: string or all recipients must be provided');
}
} else {
let origin;
try {
origin = new URL(checkoutRequest.callbackUrl).origin;
} catch (err) {
throw new Error(`callbackUrl must be a valid URL: ${err}`);
}
if (origin !== state.origin
// Whitelist https://vendor.cryptopayment.link when the request was coming from just
// https://cryptopayment.link
&& !(state.origin === 'https://cryptopayment.link'
&& origin === 'https://vendor.cryptopayment.link')) {
throw new Error('callbackUrl must have the same origin as caller Website. ' +
checkoutRequest.callbackUrl +
' is not on caller origin ' +
state.origin,
);
}
if (!checkoutRequest.csrf || typeof checkoutRequest.csrf !== 'string') {
throw new Error('A CSRF token must be provided alongside the callbackUrl.');
}
}
if (checkoutRequest.time && typeof checkoutRequest.time !== 'number') {
throw new Error('time: number is required');
}
const currencies = new Set<Currency>();
return {
kind: RequestType.CHECKOUT,
version: 2,
appName: checkoutRequest.appName,
shopLogoUrl: checkoutRequest.shopLogoUrl,
callbackUrl: checkoutRequest.callbackUrl,
csrf: checkoutRequest.csrf,
time: !checkoutRequest.time
? Date.now()
: isMilliseconds(checkoutRequest.time)
? checkoutRequest.time
: checkoutRequest.time * 1000,
fiatCurrency: checkoutRequest.fiatCurrency,
fiatAmount: checkoutRequest.fiatAmount,
paymentOptions: checkoutRequest.paymentOptions.map((option) => {
if (currencies.has(option.currency)) {
throw new Error('Only one paymentOption can be provided per cryptocurrency');
} else {
currencies.add(option.currency);
}
switch (option.type) {
case PaymentType.DIRECT:
switch (option.currency) {
case Currency.NIM:
// Once extraData from MultiCurrencyCheckoutRequest is removed
// the next few lines become obsolete.
if (!option.protocolSpecific.extraData && checkoutRequest.extraData) {
console.warn('Usage of MultiCurrencyCheckoutRequest.extraData is'
+ ' deprecated. Use NimiqDirectPaymentOptions.protocolSpecific'
+ '.extraData instead');
option.protocolSpecific.extraData = checkoutRequest.extraData;
}
return new ParsedNimiqDirectPaymentOptions(
option,
{ isPointOfSale },
);
case Currency.ETH:
return new ParsedEtherDirectPaymentOptions(option);
case Currency.BTC:
return new ParsedBitcoinDirectPaymentOptions(option);
default:
throw new Error(`Currency ${(option as any).currency} not supported`);
}
default:
throw new Error(`PaymentType ${(option as any).type} not supported`);
}
}),
isPointOfSale,
disableDisclaimer,
} as ParsedCheckoutRequest;
}
throw new Error('Invalid version: must be 1 or 2');
case RequestType.ONBOARD:
const onboardRequest = request as OnboardRequest;
return {
kind: requestType,
appName: onboardRequest.appName,
disableBack: !!onboardRequest.disableBack,
} as ParsedOnboardRequest;
case RequestType.CHOOSE_ADDRESS:
const chooseAddressRequest = request as ChooseAddressRequest;
return {
kind: requestType,
appName: chooseAddressRequest.appName,
returnBtcAddress: !!chooseAddressRequest.returnBtcAddress,
returnUsdcAddress: !!chooseAddressRequest.returnUsdcAddress,
minBalance: Number(chooseAddressRequest.minBalance) || 0,
disableContracts: !!chooseAddressRequest.disableContracts,
disableLegacyAccounts: !!chooseAddressRequest.disableLegacyAccounts,
disableBip39Accounts: !!chooseAddressRequest.disableBip39Accounts,
disableLedgerAccounts: !!chooseAddressRequest.disableLedgerAccounts,
ui: chooseAddressRequest.ui,
} as ParsedChooseAddressRequest;
case RequestType.SIGNUP:
case RequestType.LOGIN:
case RequestType.MIGRATE:
case RequestType.ADD_VESTING_CONTRACT:
return {
kind: requestType,
appName: request.appName,
} as ParsedBasicRequest;
case RequestType.CHANGE_PASSWORD:
case RequestType.LOGOUT:
case RequestType.ADD_ADDRESS:
case RequestType.ACTIVATE_BITCOIN:
case RequestType.ACTIVATE_POLYGON:
const simpleRequest = request as SimpleRequest;
if (!simpleRequest.accountId) throw new Error('accountId is required');
return {
kind: requestType,
appName: simpleRequest.appName,
walletId: simpleRequest.accountId,
} as ParsedSimpleRequest;
case RequestType.EXPORT:
const exportRequest = request as ExportRequest;
if (!exportRequest.accountId) throw new Error('accountId is required');
return {
kind: RequestType.EXPORT,
appName: exportRequest.appName,
walletId: exportRequest.accountId,
fileOnly: exportRequest.fileOnly,
wordsOnly: exportRequest.wordsOnly,
} as ParsedExportRequest;
case RequestType.RENAME:
const renameRequest = request as RenameRequest;
if (!renameRequest.accountId) throw new Error('accountId is required');
return {
kind: RequestType.RENAME,
appName: renameRequest.appName,
walletId: renameRequest.accountId,
address: renameRequest.address,
} as ParsedRenameRequest;
case RequestType.SIGN_MESSAGE:
const signMessageRequest = request as SignMessageRequest;
if (typeof signMessageRequest.message !== 'string'
&& !(signMessageRequest.message instanceof Uint8Array)) {
throw new Error('message must be a string or Uint8Array');
}
return {
kind: RequestType.SIGN_MESSAGE,
appName: signMessageRequest.appName,
signer: signMessageRequest.signer
? Nimiq.Address.fromString(signMessageRequest.signer)
: undefined,
message: signMessageRequest.message,
} as ParsedSignMessageRequest;
case RequestType.CREATE_CASHLINK:
const createCashlinkRequest = request as CreateCashlinkRequest;
const senderAddress = 'senderAddress' in createCashlinkRequest && !!createCashlinkRequest.senderAddress
? Nimiq.Address.fromString(createCashlinkRequest.senderAddress)
: undefined;
const senderBalance = 'senderBalance' in createCashlinkRequest
? createCashlinkRequest.senderBalance
: undefined;
if (senderBalance !== undefined && !Nimiq.NumberUtils.isUint64(senderBalance)) {
throw new Error('Invalid Cashlink senderBalance');
}
const value = createCashlinkRequest.value;
if (value !== undefined && (!Nimiq.NumberUtils.isUint64(value) || value === 0)) {
throw new Error('Malformed Cashlink value');
}
let message = 'message' in createCashlinkRequest ? createCashlinkRequest.message : undefined;
if (message !== undefined) {
if (typeof message !== 'string') {
throw new Error('Cashlink message must be a string');
}
const { result: truncated, didTruncate } = Utf8Tools.truncateToUtf8ByteLength(message, 255);
if (!('autoTruncateMessage' in createCashlinkRequest && createCashlinkRequest.autoTruncateMessage)
&& didTruncate) {
throw new Error('Cashlink message must be shorter than 256 bytes or autoTruncateMessage must '
+ 'be enabled.');
}
message = truncated;
}
const theme = createCashlinkRequest.theme || CashlinkTheme.UNSPECIFIED;
if (!Object.values(CashlinkTheme).includes(theme) || !Nimiq.NumberUtils.isUint8(theme)) {
// Also checking whether theme is a valid Uint8 to catch ids that are potentially to high in the
// CashlinkTheme enum and to filter out values that are actually the enum keys that have been added
// by typescript for reverse mapping.
throw new Error('Invalid Cashlink theme');
}
const returnLink = !!createCashlinkRequest.returnLink;
if (returnLink && !includesOrigin(Config.privilegedOrigins, state.origin)) {
throw new Error(`Origin ${state.origin} is not authorized to request returnLink.`);
}
const skipSharing = !!createCashlinkRequest.returnLink && !!createCashlinkRequest.skipSharing;
return {
kind: RequestType.CREATE_CASHLINK,
appName: createCashlinkRequest.appName,
senderAddress,
senderBalance,
value,
message,
theme,
fiatCurrency: createCashlinkRequest.fiatCurrency,
returnLink,
skipSharing,
} as ParsedCreateCashlinkRequest;
case RequestType.MANAGE_CASHLINK:
const manageCashlinkRequest = request as ManageCashlinkRequest;
return {
kind: RequestType.MANAGE_CASHLINK,
appName: manageCashlinkRequest.appName,
cashlinkAddress: Nimiq.Address.fromString(manageCashlinkRequest.cashlinkAddress),
} as ParsedManageCashlinkRequest;
case RequestType.SIGN_BTC_TRANSACTION:
const signBtcTransactionRequest = request as SignBtcTransactionRequest;
if (!signBtcTransactionRequest.accountId) throw new Error('accountId is required');
if (!signBtcTransactionRequest.inputs || !Array.isArray(signBtcTransactionRequest.inputs)
|| !signBtcTransactionRequest.inputs.length) throw new Error('inputs must be a non-empty array');
const inputs = signBtcTransactionRequest.inputs.map((input) => {
if (!input || typeof input !== 'object') throw new Error('input must be an object');
// tslint:disable-next-line:no-shadowed-variable
const { address, transactionHash, outputIndex, value, sequence } = input;
let { outputScript, witnessScript } = input;
if (typeof address !== 'string') throw new Error('input must contain an address of type string');
if (typeof transactionHash !== 'string' || transactionHash.length !== 64) {
throw new Error('input must contain a valid transactionHash');
}
try {
Nimiq.BufferUtils.fromHex(transactionHash); // throws if invalid hex
} catch (e) {
throw new Error('input transactionHash must be hex');
}
if (typeof outputIndex !== 'number' || outputIndex < 0) {
throw new Error('input must contain a valid outputIndex');
}
try {
// Convert to hex. Throws if invalid hex or base64.
outputScript = Nimiq.BufferUtils.toHex(Nimiq.BufferUtils.fromAny(outputScript));
} catch (e) {
throw new Error('input outputScript must be hex or base64');
}
if (outputScript.length !== 44 // P2WPKH
&& outputScript.length !== 46 // P2SH
&& outputScript.length !== 50 // P2PKH
&& outputScript.length !== 68 // HTLC
) throw new Error('input outputScript has invalid length');
if (witnessScript !== undefined) {
if (typeof witnessScript !== 'string') throw new Error('Invalid input witnessScript');
try {
// Convert to hex. Throws if invalid hex or base64.
witnessScript = Nimiq.BufferUtils.toHex(Nimiq.BufferUtils.fromAny(witnessScript));
} catch (e) {
throw new Error('input witnessScript must be hex or base64');
}
}
if (typeof value !== 'number' || value <= 0) throw new Error('input must contain a positive value');
if (sequence !== undefined && !Nimiq.NumberUtils.isUint32(sequence)) {
throw new Error('Invalid input sequence');
}
// return only checked properties
return { address, transactionHash, outputIndex, outputScript, value, witnessScript, sequence };
});
if (!signBtcTransactionRequest.output || typeof signBtcTransactionRequest.output !== 'object') {
throw new Error('output must be an object');
}
const output = signBtcTransactionRequest.output;
if (!output.value || typeof output.value !== 'number' || output.value <= 0) {
throw new Error('output must contain a positive value');
}
if (output.label && typeof output.label !== 'string') {
throw new Error('output label must be a string');
}
let changeOutput: ParsedSignBtcTransactionRequest['changeOutput'] | undefined;
if (signBtcTransactionRequest.changeOutput) {
if (typeof signBtcTransactionRequest.changeOutput !== 'object') {
throw new Error('changeOutput must be an object');
}
changeOutput = signBtcTransactionRequest.changeOutput;
if (!changeOutput.value || typeof changeOutput.value !== 'number' || changeOutput.value <= 0) {
throw new Error('changeOutput must contain a positive value');
}
}
const locktime = signBtcTransactionRequest.locktime;
if (locktime !== undefined && !Nimiq.NumberUtils.isUint32(locktime)) {
throw new Error('Invalid locktime');
}
const parsedSignBtcTransactionRequest: ParsedSignBtcTransactionRequest = {
kind: RequestType.SIGN_BTC_TRANSACTION,
walletId: signBtcTransactionRequest.accountId,
appName: signBtcTransactionRequest.appName,
inputs,
output,
changeOutput,
locktime,
};
return parsedSignBtcTransactionRequest;
case RequestType.SIGN_POLYGON_TRANSACTION:
const signPolygonTransactionRequest = request as SignPolygonTransactionRequest;
return {
kind: RequestType.SIGN_POLYGON_TRANSACTION,
...signPolygonTransactionRequest,
} as ParsedSignPolygonTransactionRequest;
case RequestType.SETUP_SWAP:
const setupSwapRequest = request as SetupSwapRequest;
if (!setupSwapRequest.accountId) throw new Error('accountId is required');
// Validate and parse only what we use in the Hub
if (!['NIM', 'BTC', 'USDC_MATIC', 'EUR', 'CRC'].includes(setupSwapRequest.fund.type)) {
throw new Error('Funding type is not supported');
}
if (!['NIM', 'BTC', 'USDC_MATIC', 'EUR', 'CRC'].includes(setupSwapRequest.redeem.type)) {
throw new Error('Redeeming type is not supported');
}
if (setupSwapRequest.fund.type === setupSwapRequest.redeem.type) {
throw new Error('Cannot swap between the same types');
}
if (setupSwapRequest.layout === 'slider') {
if (
typeof setupSwapRequest.direction !== 'string'
|| (
setupSwapRequest.direction !== 'left-to-right'
&& setupSwapRequest.direction !== 'right-to-left'
)
) {
throw new Error('When using the "slider" layout, direction must be provided');
}
if (!Array.isArray(setupSwapRequest.nimiqAddresses)) {
throw new Error('When using the "slider" layout, `nimAddresses` must be an array');
}
if (!Array.isArray(setupSwapRequest.polygonAddresses)) {
throw new Error('When using the "slider" layout, `polygonAddresses` must be an array');
}
if (!setupSwapRequest.bitcoinAccount) {
throw new Error('When using the "slider" layout, `bitcoinAccount` must be provided');
}
const nimiqAddress = setupSwapRequest.fund.type === SwapAsset.NIM
? Nimiq.Address.fromAny(setupSwapRequest.fund.sender)
: setupSwapRequest.redeem.type === SwapAsset.NIM
? Nimiq.Address.fromAny(setupSwapRequest.redeem.recipient)
: undefined;
if (nimiqAddress && !setupSwapRequest.nimiqAddresses.some(
({ address }) => Nimiq.Address.fromAny(address).equals(nimiqAddress))) {
throw new Error('The address details of the NIM address doing the swap must be provided');
}
const polygonAddress = setupSwapRequest.fund.type === SwapAsset.USDC_MATIC
? setupSwapRequest.fund.request.from
: setupSwapRequest.redeem.type === SwapAsset.USDC_MATIC
? setupSwapRequest.redeem.request.from
: undefined;
if (polygonAddress && !setupSwapRequest.polygonAddresses.some(
({ address }) => address === polygonAddress)) {
throw new Error('The address details of the USDC address doing the swap must be provided');
}
}
if (setupSwapRequest.redeem.type === SwapAsset.NIM) {
if (!setupSwapRequest.redeem.validityStartHeight
|| setupSwapRequest.redeem.validityStartHeight < 1) {
throw new Error(
`Invalid validity start height: ${setupSwapRequest.redeem.validityStartHeight}`,
);
}
}
if (setupSwapRequest.fund.type === SwapAsset.NIM) {
if (!setupSwapRequest.fund.validityStartHeight
|| setupSwapRequest.fund.validityStartHeight < 1) {
throw new Error(`Invalid validity start height: ${setupSwapRequest.fund.validityStartHeight}`);
}
}
if (setupSwapRequest.kyc) {
if (setupSwapRequest.kyc.provider !== 'TEN31 Pass') {
throw new Error(`Unsupported KYC provider: ${setupSwapRequest.kyc.provider}`);
}
if (typeof setupSwapRequest.kyc.userId !== 'string' || !setupSwapRequest.kyc.userId) {
throw new Error('Invalid TEN31 Pass user id');
}
if (typeof setupSwapRequest.kyc.s3GrantToken !== 'string' || !setupSwapRequest.kyc.s3GrantToken) {
throw new Error('Invalid TEN31 Pass S3 service grant');
}
if (setupSwapRequest.kyc.oasisGrantToken !== undefined && (
typeof setupSwapRequest.kyc.oasisGrantToken !== 'string'
|| !setupSwapRequest.kyc.oasisGrantToken)) {
throw new Error('Invalid TEN31 Pass OASIS service grant');
}
}
const parsedSetupSwapRequest: ParsedSetupSwapRequest = {
kind: RequestType.SETUP_SWAP,
walletId: setupSwapRequest.accountId,
...setupSwapRequest,
fund: setupSwapRequest.fund.type === 'NIM' ? {
...setupSwapRequest.fund,
type: SwapAsset[setupSwapRequest.fund.type],
sender: Nimiq.Address.fromAny(setupSwapRequest.fund.sender),
} : setupSwapRequest.fund.type === 'BTC' ? {
...setupSwapRequest.fund,
type: SwapAsset[setupSwapRequest.fund.type],
} : setupSwapRequest.fund.type === 'USDC_MATIC' ? {
...setupSwapRequest.fund,
type: SwapAsset[setupSwapRequest.fund.type],
} : setupSwapRequest.fund.type === 'EUR' ? {
...setupSwapRequest.fund,
type: SwapAsset[setupSwapRequest.fund.type],
} : { // CRC
...setupSwapRequest.fund,
type: SwapAsset[setupSwapRequest.fund.type],
},
redeem: setupSwapRequest.redeem.type === 'NIM' ? {
...setupSwapRequest.redeem,
type: SwapAsset[setupSwapRequest.redeem.type],
recipient: Nimiq.Address.fromAny(setupSwapRequest.redeem.recipient),
extraData: typeof setupSwapRequest.redeem.extraData === 'string'
? Nimiq.BufferUtils.fromAny(setupSwapRequest.redeem.extraData)
: setupSwapRequest.redeem.extraData,
} : setupSwapRequest.redeem.type === 'BTC' ? {
...setupSwapRequest.redeem,
type: SwapAsset[setupSwapRequest.redeem.type],
} : setupSwapRequest.redeem.type === 'USDC_MATIC' ? {
...setupSwapRequest.redeem,
type: SwapAsset[setupSwapRequest.redeem.type],
} : setupSwapRequest.redeem.type === 'EUR' ? {
...setupSwapRequest.redeem,
type: SwapAsset[setupSwapRequest.redeem.type],
} : { // CRC
...setupSwapRequest.redeem,
type: SwapAsset[setupSwapRequest.redeem.type],
},
layout: setupSwapRequest.layout || 'standard',
};
return parsedSetupSwapRequest;
case RequestType.REFUND_SWAP:
const refundSwapRequest = request as RefundSwapRequest;
// Only basic parsing and validation. Refund transaction specific data will be validated by the Keyguard
// or subsequent Ledger transaction signing requests.
if (!['NIM', 'BTC', 'USDC', 'USDC_MATIC'].includes(refundSwapRequest.refund.type)) {
throw new Error('Refunding object type must be "NIM", "BTC", "USDC", or "USDC_MATIC"');
}
const parsedRefundSwapRequest: ParsedRefundSwapRequest = {
kind: RequestType.REFUND_SWAP,
appName: refundSwapRequest.appName,
walletId: refundSwapRequest.accountId,
refund: refundSwapRequest.refund.type === 'NIM' ? {
...refundSwapRequest.refund,
type: SwapAsset[refundSwapRequest.refund.type],
sender: Nimiq.Address.fromAny(refundSwapRequest.refund.sender),
recipient: Nimiq.Address.fromAny(refundSwapRequest.refund.recipient),
extraData: typeof refundSwapRequest.refund.extraData === 'string'
? Nimiq.BufferUtils.fromAny(refundSwapRequest.refund.extraData)
: refundSwapRequest.refund.extraData,
} : refundSwapRequest.refund.type === 'BTC' ? {
...refundSwapRequest.refund,
type: SwapAsset[refundSwapRequest.refund.type],
} : { // USDC
...refundSwapRequest.refund,
type: SwapAsset[refundSwapRequest.refund.type],
},
};
return parsedRefundSwapRequest;
default:
return null;
}
}
public static raw(request: ParsedRpcRequest)
: RpcRequest | null {
switch (request.kind) {
case RequestType.SIGN_TRANSACTION:
const signTransactionRequest = request as ParsedSignTransactionRequest;
return {
appName: signTransactionRequest.appName,
sender: signTransactionRequest.sender instanceof Nimiq.Address
? signTransactionRequest.sender.toUserFriendlyAddress()
// Note: additional sender information is lost and does not survive reloads, see RequestTypes.ts
: signTransactionRequest.sender.address.toUserFriendlyAddress(),
recipient: signTransactionRequest.recipient.toUserFriendlyAddress(),
recipientType: signTransactionRequest.recipientType,
recipientLabel: signTransactionRequest.recipientLabel,
value: signTransactionRequest.value,
fee: signTransactionRequest.fee,
extraData: signTransactionRequest.data,
flags: signTransactionRequest.flags,
validityStartHeight: signTransactionRequest.validityStartHeight,
} as SignTransactionRequest;
case RequestType.CREATE_CASHLINK:
const createCashlinkRequest = request as ParsedCreateCashlinkRequest;
// Note that there is no need to export autoTruncateMessage as the message already got truncated
return {
appName: createCashlinkRequest.appName,
senderAddress: createCashlinkRequest.senderAddress
? createCashlinkRequest.senderAddress.toUserFriendlyAddress()
: undefined,
senderBalance: createCashlinkRequest.senderBalance,
value: createCashlinkRequest.value,
message: createCashlinkRequest.message,
theme: createCashlinkRequest.theme,
fiatCurrency: createCashlinkRequest.fiatCurrency,
returnLink: createCashlinkRequest.returnLink,
skipSharing: createCashlinkRequest.skipSharing,
} as CreateCashlinkRequest;
case RequestType.MANAGE_CASHLINK:
const manageCashlinkRequest = request as ParsedManageCashlinkRequest;
return {
appName: manageCashlinkRequest.appName,
cashlinkAddress: manageCashlinkRequest.cashlinkAddress
? manageCashlinkRequest.cashlinkAddress.toUserFriendlyAddress()
: undefined,
} as ManageCashlinkRequest;
case RequestType.CHECKOUT:
const checkoutRequest = request as ParsedCheckoutRequest;
switch (checkoutRequest.version) {
case 1:
const nimiqOptions = checkoutRequest.paymentOptions[0] as ParsedNimiqDirectPaymentOptions;
return {
appName: checkoutRequest.appName,
version: 1,
shopLogoUrl: checkoutRequest.shopLogoUrl,
sender: nimiqOptions.protocolSpecific.sender
? nimiqOptions.protocolSpecific.sender!.toUserFriendlyAddress()
: undefined,
forceSender: nimiqOptions.protocolSpecific.forceSender,
recipient: nimiqOptions.protocolSpecific.recipient
? nimiqOptions.protocolSpecific.recipient!.toUserFriendlyAddress()
: undefined,
recipientType: nimiqOptions.protocolSpecific.recipientType,
value: nimiqOptions.amount,
fee: nimiqOptions.protocolSpecific.fee,
extraData: nimiqOptions.protocolSpecific.extraData,
flags: nimiqOptions.protocolSpecific.flags,
validityDuration: nimiqOptions.protocolSpecific.validityDuration,
disableDisclaimer: checkoutRequest.disableDisclaimer,
} as NimiqCheckoutRequest;
case 2:
return {
...checkoutRequest,
paymentOptions: checkoutRequest.paymentOptions.map((option) => option.raw()),
} as MultiCurrencyCheckoutRequest;
}
case RequestType.ONBOARD:
const onboardRequest = request as ParsedOnboardRequest;
return {
appName: onboardRequest.appName,
disableBack: onboardRequest.disableBack,
} as OnboardRequest;
case RequestType.CHOOSE_ADDRESS:
const chooseAddressRequest = request as ParsedChooseAddressRequest;
return {
...chooseAddressRequest,
} as ChooseAddressRequest;
case RequestType.SIGNUP:
case RequestType.LOGIN:
case RequestType.MIGRATE:
case RequestType.ADD_VESTING_CONTRACT:
return {
appName: request.appName,
} as BasicRequest;
case RequestType.CHANGE_PASSWORD:
case RequestType.LOGOUT:
case RequestType.ADD_ADDRESS:
case RequestType.ACTIVATE_BITCOIN:
case RequestType.ACTIVATE_POLYGON:
const simpleRequest = request as ParsedSimpleRequest;
return {
appName: simpleRequest.appName,
accountId: simpleRequest.walletId,
} as SimpleRequest;
case RequestType.EXPORT:
const exportRequest = request as ParsedExportRequest;
return {
appName: exportRequest.appName,
accountId: exportRequest.walletId,
fileOnly: exportRequest.fileOnly,
wordsOnly: exportRequest.wordsOnly,
} as ExportRequest;
case RequestType.RENAME:
const renameRequest = request as ParsedRenameRequest;
return {
appName: renameRequest.appName,
accountId: renameRequest.walletId,
address: renameRequest.address,
} as RenameRequest;
case RequestType.SIGN_MESSAGE:
const signMessageRequest = request as ParsedSignMessageRequest;
return {
appName: signMessageRequest.appName,
signer: signMessageRequest.signer ? signMessageRequest.signer.toUserFriendlyAddress() : undefined,
message: signMessageRequest.message,
} as SignMessageRequest;
case RequestType.SIGN_BTC_TRANSACTION:
const signBtcTransactionRequest = request as ParsedSignBtcTransactionRequest;
return {
appName: signBtcTransactionRequest.appName,
accountId: signBtcTransactionRequest.walletId,
// Note: input.keyPath is lost on re-parsing and does not survive reloads, see RequestTypes.ts
inputs: signBtcTransactionRequest.inputs,
output: signBtcTransactionRequest.output,
changeOutput: signBtcTransactionRequest.changeOutput,
locktime: signBtcTransactionRequest.locktime,
} as SignBtcTransactionRequest;
case RequestType.SIGN_POLYGON_TRANSACTION:
const signPolygonTransactionRequest = request as ParsedSignPolygonTransactionRequest;
return {
...signPolygonTransactionRequest,
} as SignPolygonTransactionRequest;
case RequestType.SETUP_SWAP:
const setupSwapRequest = request as ParsedSetupSwapRequest;
return {
...setupSwapRequest,
accountId: setupSwapRequest.walletId,
// @ts-ignore Type 'Address' is not assignable to type 'string'
fund: setupSwapRequest.fund.type === 'NIM' ? {
...setupSwapRequest.fund,
sender: setupSwapRequest.fund.sender.toUserFriendlyAddress(),
} : setupSwapRequest.fund,
// @ts-ignore Type 'Address' is not assignable to type 'string'
redeem: setupSwapRequest.redeem.type === 'NIM' ? {
...setupSwapRequest.redeem,
recipient: setupSwapRequest.redeem.recipient.toUserFriendlyAddress(),
} : setupSwapRequest.redeem,
} as SetupSwapRequest;
case RequestType.REFUND_SWAP:
const refundSwapRequest = request as ParsedRefundSwapRequest;
return {
...refundSwapRequest,
accountId: refundSwapRequest.walletId,
// @ts-ignore Type 'Address' is not assignable to type 'string'
refund: refundSwapRequest.refund.type === 'NIM' ? {
...refundSwapRequest.refund,
sender: refundSwapRequest.refund.sender.toUserFriendlyAddress(),
recipient: refundSwapRequest.refund.recipient.toUserFriendlyAddress(),
} : refundSwapRequest.refund,
} as RefundSwapRequest;
default:
return null;
}
}
}