organise classes into separate files

This commit is contained in:
julian 2023-10-19 08:35:41 -06:00
parent 649dac33b3
commit 993914636b
3 changed files with 118 additions and 117 deletions

View file

@ -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<String, dynamic> 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'
')';
}
}

View file

@ -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<String, dynamic> 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'
')';
}
}

View file

@ -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<String, dynamic> 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<String, dynamic> 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<T, U>(List<T> a, List<U> b) {
if (T != U) {
return false;