From a902c7705780ea622ce2d90ad4906bf8be786024 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 5 Jan 2023 10:50:36 -0600 Subject: [PATCH] add getAddress helper func for transactions with odd outputs OP_RETURN and some other output types can cause addresses to be placed in a list of strings or as a string under a different key; this handles that case --- lib/services/coins/coin_service.dart | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/services/coins/coin_service.dart b/lib/services/coins/coin_service.dart index a690157ee..90a25a057 100644 --- a/lib/services/coins/coin_service.dart +++ b/lib/services/coins/coin_service.dart @@ -307,4 +307,24 @@ abstract class CoinServiceAPI { // used for electrumx coins Future updateSentCachedTxData(Map txData); + + // Certain outputs return address as an array/list of strings like List ["addresses"][0], some return it as a string like String ["address"] + String? getAddress(dynamic output) { + String? address; + if (output.containsKey('scriptPubKey') as bool) { + // Make sure the key exists before using it + if (output["scriptPubKey"].containsKey('address') as bool) { + address = output["scriptPubKey"]["address"] as String?; + } else if (output["scriptPubKey"].containsKey('addresses') as bool) { + address = output["scriptPubKey"]["addresses"][0] as String?; + // TODO determine cases in which there are multiple addresses in the array + } + } /*else { + // TODO detect cases in which no scriptPubKey exists + Logging.instance.log("output type not detected; output: ${output}", + level: LogLevel.Info); + }*/ + + return address; + } }