dev
dart 71 lines 2.11 KB
Raw
1 import "package:cake_wallet/core/anypay/anypay_models.dart";
2 import "package:cake_wallet/core/universal_address_detector.dart";
3 import "package:cake_wallet/utils/payment_request.dart";
4 import "package:cw_core/crypto_currency.dart";
5 import "package:cw_core/currency_for_wallet_type.dart";
6 import "package:cw_core/payment_uris.dart";
7
8 class AnyPayParser {
9 static AnyPayRequest fromRaw(String input) {
10 final paymentRequest = PaymentRequest.fromString(input);
11
12 return AnyPayRequest(
13 rawInput: input,
14 paymentRequest: paymentRequest,
15 detection: UniversalAddressDetector.detectAddress(input),
16 chainBinding: _bindingFor(paymentRequest),
17 );
18 }
19
20 static AnyPayRequest fromPaymentRequest(PaymentRequest request) {
21 final raw = _reconstructRawInput(request);
22
23 return AnyPayRequest(
24 rawInput: raw,
25 paymentRequest: request,
26 detection: UniversalAddressDetector.detectAddress(raw),
27 chainBinding: _bindingFor(request),
28 );
29 }
30
31 static String _reconstructRawInput(PaymentRequest request) {
32 final scheme = request.scheme.toLowerCase();
33
34 if (scheme.isEmpty || scheme == "lightning") {
35 return request.address;
36 }
37
38 if (scheme == "ethereum") {
39 return ERC681URI(
40 address: request.address,
41 amount: request.amount,
42 contractAddress: request.contractAddress,
43 chainId: request.chainId ?? 1,
44 rawTokenAmount: request.rawTokenAmount,
45 ).toString();
46 }
47
48 final amount = request.amount.isNotEmpty ? "?amount=${request.amount}" : "";
49 return "${request.scheme}:${request.address}$amount";
50 }
51
52 static ChainBinding _bindingFor(PaymentRequest request) {
53 final scheme = request.scheme.toLowerCase();
54
55 if (scheme == "ethereum") {
56 final chainId = request.chainId;
57 return chainId != null ? ExplicitEvmChain(chainId) : const ChainlessEvm();
58 }
59
60 if (scheme.isNotEmpty) {
61 try {
62 final chainId = getChainIdByCryptoCurrency(CryptoCurrency.fromString(scheme));
63 if (chainId != null) {
64 return ExplicitEvmChain(chainId);
65 }
66 } catch (_) {}
67 }
68
69 return const NoEvmBinding();
70 }
71 }