|
| 1 | +import 'package:decimal/decimal.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:stackwallet/services/open_crypto_pay/models.dart'; |
| 4 | +import 'package:stackwallet/services/open_crypto_pay/settlement.dart'; |
| 5 | +import 'package:stackwallet/utilities/amount/amount.dart'; |
| 6 | +import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + final bitcoin = Bitcoin(CryptoCurrencyNetwork.main); |
| 10 | + final cardano = Cardano(CryptoCurrencyNetwork.main); |
| 11 | + final ethereum = Ethereum(CryptoCurrencyNetwork.main); |
| 12 | + final firo = Firo(CryptoCurrencyNetwork.main); |
| 13 | + |
| 14 | + group("shouldCommitTxIdFor", () { |
| 15 | + test("always uses txid for txid submission flows", () { |
| 16 | + expect( |
| 17 | + OpenCryptoPaySettlement.shouldCommitTxIdFor( |
| 18 | + method: "Cardano", |
| 19 | + submissionFlow: OpenCryptoPaySubmissionFlow.txIdAfterLocalBroadcast, |
| 20 | + cryptoCurrency: cardano, |
| 21 | + hasSparkInputs: false, |
| 22 | + rawHexLength: 0, |
| 23 | + ), |
| 24 | + true, |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + test("falls back for Firo Spark spends", () { |
| 29 | + expect( |
| 30 | + OpenCryptoPaySettlement.shouldCommitTxIdFor( |
| 31 | + method: "Firo", |
| 32 | + submissionFlow: OpenCryptoPaySubmissionFlow.rawHexToProvider, |
| 33 | + cryptoCurrency: firo, |
| 34 | + hasSparkInputs: true, |
| 35 | + rawHexLength: 100, |
| 36 | + ), |
| 37 | + true, |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + test("falls back only above the Firo raw hex query limit", () { |
| 42 | + expect( |
| 43 | + OpenCryptoPaySettlement.shouldCommitTxIdFor( |
| 44 | + method: "Firo", |
| 45 | + submissionFlow: OpenCryptoPaySubmissionFlow.rawHexToProvider, |
| 46 | + cryptoCurrency: firo, |
| 47 | + hasSparkInputs: false, |
| 48 | + rawHexLength: OpenCryptoPaySettlement.maxRawHexQueryLength, |
| 49 | + ), |
| 50 | + false, |
| 51 | + ); |
| 52 | + expect( |
| 53 | + OpenCryptoPaySettlement.shouldCommitTxIdFor( |
| 54 | + method: "Firo", |
| 55 | + submissionFlow: OpenCryptoPaySubmissionFlow.rawHexToProvider, |
| 56 | + cryptoCurrency: firo, |
| 57 | + hasSparkInputs: false, |
| 58 | + rawHexLength: OpenCryptoPaySettlement.maxRawHexQueryLength + 1, |
| 59 | + ), |
| 60 | + true, |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + test("does not use the Firo fallback for other coins", () { |
| 65 | + expect( |
| 66 | + OpenCryptoPaySettlement.shouldCommitTxIdFor( |
| 67 | + method: "Bitcoin", |
| 68 | + submissionFlow: OpenCryptoPaySubmissionFlow.rawHexToProvider, |
| 69 | + cryptoCurrency: bitcoin, |
| 70 | + hasSparkInputs: false, |
| 71 | + rawHexLength: OpenCryptoPaySettlement.maxRawHexQueryLength + 1, |
| 72 | + ), |
| 73 | + false, |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + group("validateMinFee", () { |
| 79 | + test("ceil-checks Ethereum wei gas price", () { |
| 80 | + expect( |
| 81 | + OpenCryptoPaySettlement.validateMinFee( |
| 82 | + cryptoCurrency: ethereum, |
| 83 | + minFee: Decimal.parse("10.1"), |
| 84 | + gasPrice: BigInt.from(10), |
| 85 | + ), |
| 86 | + "Open CryptoPay requires at least 10.1 wei gas price", |
| 87 | + ); |
| 88 | + expect( |
| 89 | + OpenCryptoPaySettlement.validateMinFee( |
| 90 | + cryptoCurrency: ethereum, |
| 91 | + minFee: Decimal.parse("10.1"), |
| 92 | + gasPrice: BigInt.from(11), |
| 93 | + ), |
| 94 | + isNull, |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + test("requires Ethereum gas price when minFee is set", () { |
| 99 | + expect( |
| 100 | + OpenCryptoPaySettlement.validateMinFee( |
| 101 | + cryptoCurrency: ethereum, |
| 102 | + minFee: Decimal.fromInt(1), |
| 103 | + ), |
| 104 | + "Could not verify Open CryptoPay minimum gas price", |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + test("ceil-checks Bitcoin sat/vB against total fee", () { |
| 109 | + expect( |
| 110 | + OpenCryptoPaySettlement.validateMinFee( |
| 111 | + cryptoCurrency: bitcoin, |
| 112 | + minFee: Decimal.parse("2.5"), |
| 113 | + fee: _rawAmount(7), |
| 114 | + vSize: 3, |
| 115 | + ), |
| 116 | + "Open CryptoPay requires at least 2.5 sat/vB fee", |
| 117 | + ); |
| 118 | + expect( |
| 119 | + OpenCryptoPaySettlement.validateMinFee( |
| 120 | + cryptoCurrency: bitcoin, |
| 121 | + minFee: Decimal.parse("2.5"), |
| 122 | + fee: _rawAmount(8), |
| 123 | + vSize: 3, |
| 124 | + ), |
| 125 | + isNull, |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test("requires fee and vSize for sat/vB methods", () { |
| 130 | + expect( |
| 131 | + OpenCryptoPaySettlement.validateMinFee( |
| 132 | + cryptoCurrency: firo, |
| 133 | + minFee: Decimal.fromInt(1), |
| 134 | + ), |
| 135 | + "Could not verify Open CryptoPay minimum fee", |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + group("validateTransaction", () { |
| 141 | + test("requires exactly one recipient", () { |
| 142 | + expect( |
| 143 | + OpenCryptoPaySettlement.validateTransaction( |
| 144 | + cryptoCurrency: bitcoin, |
| 145 | + recipients: <({String address, Amount amount})>[], |
| 146 | + recipientAddress: "bc1qrecipient", |
| 147 | + amount: Decimal.fromInt(1), |
| 148 | + ), |
| 149 | + "Open CryptoPay requires exactly one recipient", |
| 150 | + ); |
| 151 | + expect( |
| 152 | + OpenCryptoPaySettlement.validateTransaction( |
| 153 | + cryptoCurrency: bitcoin, |
| 154 | + recipients: [ |
| 155 | + (address: "bc1qone", amount: _amount("1")), |
| 156 | + (address: "bc1qtwo", amount: _amount("1")), |
| 157 | + ], |
| 158 | + recipientAddress: "bc1qrecipient", |
| 159 | + amount: Decimal.fromInt(1), |
| 160 | + ), |
| 161 | + "Open CryptoPay requires exactly one recipient", |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + test("rejects recipient mismatch", () { |
| 166 | + expect( |
| 167 | + OpenCryptoPaySettlement.validateTransaction( |
| 168 | + cryptoCurrency: bitcoin, |
| 169 | + recipients: [(address: "bc1qactual", amount: _amount("1"))], |
| 170 | + recipientAddress: "bc1qexpected", |
| 171 | + amount: Decimal.fromInt(1), |
| 172 | + ), |
| 173 | + "Open CryptoPay recipient changed. Please scan again.", |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + test("rejects amount mismatch", () { |
| 178 | + expect( |
| 179 | + OpenCryptoPaySettlement.validateTransaction( |
| 180 | + cryptoCurrency: bitcoin, |
| 181 | + recipients: [(address: "bc1qrecipient", amount: _amount("1.01"))], |
| 182 | + recipientAddress: "bc1qrecipient", |
| 183 | + amount: Decimal.fromInt(1), |
| 184 | + ), |
| 185 | + "Open CryptoPay amount changed. Please scan again.", |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + test("normalizes Ethereum recipient case", () { |
| 190 | + expect( |
| 191 | + OpenCryptoPaySettlement.validateTransaction( |
| 192 | + cryptoCurrency: ethereum, |
| 193 | + recipients: [ |
| 194 | + ( |
| 195 | + address: "0x9C2242A0B71FD84661FD4BC56B75C90FAC6D10FC", |
| 196 | + amount: _amount("1", fractionDigits: 18), |
| 197 | + ), |
| 198 | + ], |
| 199 | + recipientAddress: "0x9c2242a0b71fd84661fd4bc56b75c90fac6d10fc", |
| 200 | + amount: Decimal.fromInt(1), |
| 201 | + ), |
| 202 | + isNull, |
| 203 | + ); |
| 204 | + }); |
| 205 | + }); |
| 206 | +} |
| 207 | + |
| 208 | +Amount _amount(String value, {int fractionDigits = 8}) { |
| 209 | + return Amount.fromDecimal( |
| 210 | + Decimal.parse(value), |
| 211 | + fractionDigits: fractionDigits, |
| 212 | + ); |
| 213 | +} |
| 214 | + |
| 215 | +Amount _rawAmount(int value, {int fractionDigits = 8}) { |
| 216 | + return Amount(rawValue: BigInt.from(value), fractionDigits: fractionDigits); |
| 217 | +} |
0 commit comments