ZapReceipt.fromEvent constructor

ZapReceipt.fromEvent(
  1. Nip01Event event
)

Implementation

ZapReceipt.fromEvent(Nip01Event event) {
  String? zapRequestJson;
  pubKey = event.pubKey;
  if (event.kind == 9735) {
    for (var tag in event.tags) {
      if (tag[0] == 'bolt11') bolt11 = tag[1];
      if (tag[0] == 'preimage') preimage = tag[1];
      if (tag[0] == 'description') zapRequestJson = tag[1];
      if (tag[0] == 'p') recipient = tag[1];
      if (tag[0] == 'e') eventId = tag[1];
      if (tag[0] == 'anon') anon = tag[1];
      if (tag[0] == 'P') sender = tag[1];
    }
    paidAt = event.createdAt;
    if (zapRequestJson != null && zapRequestJson.isNotEmpty) {
      Nip01Event event = Nip01Event.fromJson(jsonDecode(zapRequestJson));
      comment = event.content;
      sender = event.pubKey;
      lnurl = event.getFirstTag('lnurl');
      String? amountString = event.getFirstTag('amount');
      if (amountString != null && amountString.isNotEmpty) {
        try {
          double? amount = double.tryParse(amountString);
          if (amount != null) {
            amountSats = (amount / 1000).round();
          }
        } catch (e) {
          Logger.log.w(e);
        }
      }
    }
    if (amountSats == null && bolt11 != null) {
      amountSats = Lnurl.getAmountFromBolt11(bolt11!);
    }
    List<String>? splitStrings = anon?.split('_');
    if (splitStrings != null && splitStrings.length == 2) {
      // TODO decrypt private zap
    }
  } else {
    throw Exception("${event.kind} is not nip57 compatible");
  }
}