| 1 | import "package:cake_wallet/core/anypay/anypay_models.dart"; |
| 2 | import "package:cake_wallet/core/anypay/anypay_parser.dart"; |
| 3 | import "package:cake_wallet/utils/payment_request.dart"; |
| 4 | import "package:flutter_test/flutter_test.dart"; |
| 5 | |
| 6 | void main() { |
| 7 | const recipient = "0xCfc1650da7C961FD82998e7e30ca5f699D0aBf41"; |
| 8 | const usdtEthContract = "0xdAC17F958D2ee523a2206206994597C13D831ec7"; |
| 9 | |
| 10 | group("chain binding", () { |
| 11 | test("explicit ethereum chain binds explicitly", () { |
| 12 | final request = AnyPayParser.fromRaw( |
| 13 | "ethereum:$recipient@8453?value=1e18", |
| 14 | ); |
| 15 | |
| 16 | expect(request.chainBinding, isA<ExplicitEvmChain>()); |
| 17 | expect((request.chainBinding as ExplicitEvmChain).chainId, 8453); |
| 18 | }); |
| 19 | |
| 20 | test("ethereum without a chain id stays chainless", () { |
| 21 | final request = AnyPayParser.fromRaw( |
| 22 | "ethereum:$usdtEthContract/transfer?address=$recipient&uint256=1000000", |
| 23 | ); |
| 24 | |
| 25 | expect(request.chainBinding, isA<ChainlessEvm>()); |
| 26 | expect(request.hasContract, true); |
| 27 | }); |
| 28 | |
| 29 | test("non evm schemes bind no evm chain", () { |
| 30 | final request = AnyPayParser.fromRaw( |
| 31 | "bitcoin:bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", |
| 32 | ); |
| 33 | |
| 34 | expect(request.chainBinding, isA<NoEvmBinding>()); |
| 35 | }); |
| 36 | |
| 37 | test("raw evm addresses bind no evm chain", () { |
| 38 | final request = AnyPayParser.fromRaw(recipient); |
| 39 | |
| 40 | expect(request.chainBinding, isA<NoEvmBinding>()); |
| 41 | expect(request.detection.isValid, true); |
| 42 | }); |
| 43 | |
| 44 | test("an evm chain scheme binds its chain explicitly", () { |
| 45 | final request = AnyPayParser.fromRaw("polygon:$recipient"); |
| 46 | |
| 47 | expect((request.chainBinding as ExplicitEvmChain).chainId, 137); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | group("payment request reconstruction", () { |
| 52 | test("an explicit chain request keeps its chain and raw amount", () { |
| 53 | final original = PaymentRequest.fromUri( |
| 54 | Uri.parse("ethereum:$usdtEthContract@8453/transfer?address=$recipient&uint256=3000000"), |
| 55 | ); |
| 56 | |
| 57 | final request = AnyPayParser.fromPaymentRequest(original); |
| 58 | |
| 59 | expect(request.rawInput.contains("@8453"), true); |
| 60 | expect(request.paymentRequest.rawTokenAmount, "3000000"); |
| 61 | expect((request.chainBinding as ExplicitEvmChain).chainId, 8453); |
| 62 | }); |
| 63 | |
| 64 | test("a chainless request rebuilds with a placeholder but keeps the chainless binding", () { |
| 65 | final original = PaymentRequest.fromUri( |
| 66 | Uri.parse("ethereum:$usdtEthContract/transfer?address=$recipient&uint256=1000000"), |
| 67 | ); |
| 68 | |
| 69 | final request = AnyPayParser.fromPaymentRequest(original); |
| 70 | |
| 71 | expect(request.rawInput.contains("@1/"), true); |
| 72 | expect(request.chainBinding, isA<ChainlessEvm>()); |
| 73 | expect(request.detection.isValid, true); |
| 74 | }); |
| 75 | |
| 76 | test("a tron token request keeps its contract on the payment request", () { |
| 77 | final original = PaymentRequest.fromUri( |
| 78 | Uri.parse("tron:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t?token=TTokenContract111111111111111111"), |
| 79 | ); |
| 80 | |
| 81 | final request = AnyPayParser.fromPaymentRequest(original); |
| 82 | |
| 83 | expect(request.contractAddress, "TTokenContract111111111111111111"); |
| 84 | expect(request.chainBinding, isA<NoEvmBinding>()); |
| 85 | }); |
| 86 | |
| 87 | test("a non evm request rebuilds scheme address and amount", () { |
| 88 | final original = PaymentRequest("someaddress", "1.25", "", "monero", null); |
| 89 | |
| 90 | final request = AnyPayParser.fromPaymentRequest(original); |
| 91 | |
| 92 | expect(request.rawInput, "monero:someaddress?amount=1.25"); |
| 93 | }); |
| 94 | |
| 95 | test("a lightning request passes the invoice through untouched", () { |
| 96 | final original = PaymentRequest("lnbc1invoice", "", "", "lightning", null); |
| 97 | |
| 98 | final request = AnyPayParser.fromPaymentRequest(original); |
| 99 | |
| 100 | expect(request.rawInput, "lnbc1invoice"); |
| 101 | }); |
| 102 | }); |
| 103 | } |