json object serialize/deserialize

This commit is contained in:
julian 2023-02-09 12:40:22 -06:00
parent 75c5a1d7d9
commit 8fa5aa779a
4 changed files with 150 additions and 0 deletions

View file

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:isar/isar.dart';
import 'package:stackwallet/exceptions/address/address_exception.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/crypto_currency_address.dart';
@ -71,6 +73,45 @@ class Address extends CryptoCurrencyAddress {
"derivationPath: $derivationPath, "
"otherData: $otherData, "
"}";
String toJsonString() {
final Map<String, dynamic> result = {
"walletId": walletId,
"value": value,
"publicKey": publicKey,
"derivationIndex": derivationIndex,
"type": type.name,
"subType": subType.name,
"derivationPath": derivationPath?.value,
"otherData": otherData,
};
return jsonEncode(result);
}
static Address fromJsonString(
String jsonString, {
String? overrideWalletId,
}) {
final json = jsonDecode(jsonString);
final derivationPathString = json["derivationPath"] as String?;
final DerivationPath? derivationPath =
derivationPathString == null ? null : DerivationPath();
if (derivationPath != null) {
derivationPath.value = derivationPathString!;
}
return Address(
walletId: overrideWalletId ?? json["walletId"] as String,
value: json["value"] as String,
publicKey: List<int>.from(json["publicKey"] as List),
derivationIndex: json["derivationIndex"] as int,
derivationPath: derivationPath,
type: AddressType.values.byName(json["type"] as String),
subType: AddressSubType.values.byName(json["subType"] as String),
otherData: json["otherData"] as String?,
);
}
}
// do not modify

View file

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:isar/isar.dart';
part 'input.g.dart';
@ -30,4 +32,32 @@ class Input {
late final int? sequence;
late final String? innerRedeemScriptAsm;
String toJsonString() {
final Map<String, dynamic> result = {
"txid": txid,
"vout": vout,
"scriptSig": scriptSig,
"scriptSigAsm": scriptSigAsm,
"witness": witness,
"isCoinbase": isCoinbase,
"sequence": sequence,
"innerRedeemScriptAsm": innerRedeemScriptAsm,
};
return jsonEncode(result);
}
static Input fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);
return Input(
txid: json["txid"] as String,
vout: json["vout"] as int,
scriptSig: json["scriptSig"] as String?,
scriptSigAsm: json["scriptSigAsm"] as String?,
witness: json["witness"] as String?,
isCoinbase: json["isCoinbase"] as bool?,
sequence: json["sequence"] as int?,
innerRedeemScriptAsm: json["innerRedeemScriptAsm"] as String?,
);
}
}

View file

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:isar/isar.dart';
part 'output.g.dart';
@ -21,4 +23,26 @@ class Output {
late final String scriptPubKeyAddress;
late final int value;
String toJsonString() {
final Map<String, dynamic> result = {
"scriptPubKey": scriptPubKey,
"scriptPubKeyAsm": scriptPubKeyAsm,
"scriptPubKeyType": scriptPubKeyType,
"scriptPubKeyAddress": scriptPubKeyAddress,
"value": value,
};
return jsonEncode(result);
}
static Output fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);
return Output(
scriptPubKey: json["scriptPubKey"] as String?,
scriptPubKeyAsm: json["scriptPubKeyAsm"] as String?,
scriptPubKeyType: json["scriptPubKeyType"] as String?,
scriptPubKeyAddress: json["scriptPubKeyAddress"] as String,
value: json["value"] as int,
);
}
}

View file

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:math';
import 'package:isar/isar.dart';
@ -133,6 +134,60 @@ class Transaction {
"inputsLength: ${inputs.length}, "
"outputsLength: ${outputs.length}, "
"}";
String toJsonString() {
final Map<String, dynamic> result = {
"walletId": walletId,
"txid": txid,
"timestamp": timestamp,
"type": type.name,
"subType": subType.name,
"amount": amount,
"fee": fee,
"height": height,
"isCancelled": isCancelled,
"isLelantus": isLelantus,
"slateId": slateId,
"otherData": otherData,
"address": address.value?.toJsonString(),
"inputs": inputs.map((e) => e.toJsonString()).toList(),
"outputs": outputs.map((e) => e.toJsonString()).toList(),
};
return jsonEncode(result);
}
static Tuple2<Transaction, Address?> fromJsonString(
String jsonString, {
String? overrideWalletId,
}) {
final json = jsonDecode(jsonString);
final transaction = Transaction(
walletId: overrideWalletId ?? json["walletId"] as String,
txid: json["txid"] as String,
timestamp: json["timestamp"] as int,
type: TransactionType.values.byName(json["type"] as String),
subType: TransactionSubType.values.byName(json["subType"] as String),
amount: json["amount"] as int,
fee: json["fee"] as int,
height: json["height"] as int?,
isCancelled: json["isCancelled"] as bool,
isLelantus: json["isLelantus"] as bool?,
slateId: json["slateId"] as String?,
otherData: json["otherData"] as String?,
inputs: List<String>.from(json["inputs"] as List)
.map((e) => Input.fromJsonString(e))
.toList(),
outputs: List<String>.from(json["outputs"] as List)
.map((e) => Output.fromJsonString(e))
.toList(),
);
if (json["address"] == null) {
return Tuple2(transaction, null);
} else {
final address = Address.fromJsonString(json["address"] as String);
return Tuple2(transaction, address);
}
}
}
// Used in Isar db and stored there as int indexes so adding/removing values