diff --git a/lib/models/isar/models/blockchain_data/v2/input_v2.dart b/lib/models/isar/models/blockchain_data/v2/input_v2.dart new file mode 100644 index 000000000..7880472b0 --- /dev/null +++ b/lib/models/isar/models/blockchain_data/v2/input_v2.dart @@ -0,0 +1,54 @@ +class InputV2 { + final String scriptSigHex; + final int sequence; + final String txid; + final int vout; + + InputV2({ + required this.scriptSigHex, + required this.sequence, + required this.txid, + required this.vout, + }); + + static InputV2 fromElectrumXJson(Map json) { + try { + return InputV2( + scriptSigHex: json["scriptSig"]["hex"] as String, + sequence: json["sequence"] as int, + txid: json["txid"] as String, + vout: json["vout"] as int); + } catch (e) { + throw Exception("Failed to parse InputV2 from $json"); + } + } + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + + return other is InputV2 && + other.scriptSigHex == scriptSigHex && + other.sequence == sequence && + other.txid == txid && + other.vout == vout; + } + + @override + int get hashCode => Object.hash( + scriptSigHex, + sequence, + txid, + vout, + ); + + @override + String toString() { + return 'InputV2(\n' + ' scriptSigHex: $scriptSigHex,\n' + ' sequence: $sequence,\n' + ' txid: $txid,\n' + ' vout: $vout,\n' + ')'; + } +} diff --git a/lib/models/isar/models/blockchain_data/v2/output_v2.dart b/lib/models/isar/models/blockchain_data/v2/output_v2.dart new file mode 100644 index 000000000..344b15cca --- /dev/null +++ b/lib/models/isar/models/blockchain_data/v2/output_v2.dart @@ -0,0 +1,62 @@ +import 'package:decimal/decimal.dart'; + +class OutputV2 { + final String scriptPubKeyHex; + final String valueStringSats; + + BigInt get value => BigInt.parse(valueStringSats); + + OutputV2({ + required this.scriptPubKeyHex, + required this.valueStringSats, + }); + + // TODO: move this to a subclass based on coin since we don't know if the value will be sats or a decimal amount + // For now assume 8 decimal places + @Deprecated("See TODO and comments") + static OutputV2 fromElectrumXJson(Map json) { + try { + final temp = Decimal.parse(json["value"].toString()); + if (temp < Decimal.zero) { + throw Exception("Negative value found"); + } + + final String valueStringSats; + if (temp.isInteger) { + valueStringSats = temp.toString(); + } else { + valueStringSats = temp.shift(8).toBigInt().toString(); + } + + return OutputV2( + scriptPubKeyHex: json["scriptPubKey"]["hex"] as String, + valueStringSats: valueStringSats, + ); + } catch (e) { + throw Exception("Failed to parse OutputV2 from $json"); + } + } + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + + return other is OutputV2 && + other.scriptPubKeyHex == scriptPubKeyHex && + other.valueStringSats == valueStringSats; + } + + @override + int get hashCode => Object.hash( + scriptPubKeyHex, + valueStringSats, + ); + + @override + String toString() { + return 'OutputV2(\n' + ' scriptPubKeyHex: $scriptPubKeyHex,\n' + ' value: $value,\n' + ')'; + } +} diff --git a/lib/models/isar/models/blockchain_data/v2/transaction_v2.dart b/lib/models/isar/models/blockchain_data/v2/transaction_v2.dart index a77cacdea..3c8cc4fde 100644 --- a/lib/models/isar/models/blockchain_data/v2/transaction_v2.dart +++ b/lib/models/isar/models/blockchain_data/v2/transaction_v2.dart @@ -1,4 +1,5 @@ -import 'package:decimal/decimal.dart'; +import 'package:stackwallet/models/isar/models/blockchain_data/v2/input_v2.dart'; +import 'package:stackwallet/models/isar/models/blockchain_data/v2/output_v2.dart'; class TransactionV2 { final String hash; @@ -105,122 +106,6 @@ class TransactionV2 { } } -class InputV2 { - final String scriptSigHex; - final int sequence; - final String txid; - final int vout; - - InputV2({ - required this.scriptSigHex, - required this.sequence, - required this.txid, - required this.vout, - }); - - static InputV2 fromElectrumXJson(Map json) { - try { - return InputV2( - scriptSigHex: json["scriptSig"]["hex"] as String, - sequence: json["sequence"] as int, - txid: json["txid"] as String, - vout: json["vout"] as int); - } catch (e) { - throw Exception("Failed to parse InputV2 from $json"); - } - } - - @override - bool operator ==(Object other) { - if (identical(this, other)) return true; - - return other is InputV2 && - other.scriptSigHex == scriptSigHex && - other.sequence == sequence && - other.txid == txid && - other.vout == vout; - } - - @override - int get hashCode => Object.hash( - scriptSigHex, - sequence, - txid, - vout, - ); - - @override - String toString() { - return 'InputV2(\n' - ' scriptSigHex: $scriptSigHex,\n' - ' sequence: $sequence,\n' - ' txid: $txid,\n' - ' vout: $vout,\n' - ')'; - } -} - -class OutputV2 { - final String scriptPubKeyHex; - final String valueStringSats; - - BigInt get value => BigInt.parse(valueStringSats); - - OutputV2({ - required this.scriptPubKeyHex, - required this.valueStringSats, - }); - - // TODO: move this to a subclass based on coin since we don't know if the value will be sats or a decimal amount - // For now assume 8 decimal places - @Deprecated("See TODO and comments") - static OutputV2 fromElectrumXJson(Map json) { - try { - final temp = Decimal.parse(json["value"].toString()); - if (temp < Decimal.zero) { - throw Exception("Negative value found"); - } - - final String valueStringSats; - if (temp.isInteger) { - valueStringSats = temp.toString(); - } else { - valueStringSats = temp.shift(8).toBigInt().toString(); - } - - return OutputV2( - scriptPubKeyHex: json["scriptPubKey"]["hex"] as String, - valueStringSats: valueStringSats, - ); - } catch (e) { - throw Exception("Failed to parse OutputV2 from $json"); - } - } - - @override - bool operator ==(Object other) { - if (identical(this, other)) return true; - - return other is OutputV2 && - other.scriptPubKeyHex == scriptPubKeyHex && - other.valueStringSats == valueStringSats; - } - - @override - int get hashCode => Object.hash( - scriptPubKeyHex, - valueStringSats, - ); - - @override - String toString() { - return 'OutputV2(\n' - ' scriptPubKeyHex: $scriptPubKeyHex,\n' - ' value: $value,\n' - ')'; - } -} - bool _listEquals(List a, List b) { if (T != U) { return false;