diff --git a/nip57/nip57.go b/nip57/nip57.go new file mode 100644 index 0000000..54ddf5a --- /dev/null +++ b/nip57/nip57.go @@ -0,0 +1,33 @@ +package nip57 + +import ( + "encoding/json" + "strconv" + + "fiatjaf.com/nostr" +) + +// GetAmountFromZap takes a zap receipt event (kind 9735) and returns the amount in millisats. +// It first checks the event's "amount" tag, and if not present, checks the embedded zap request's "amount" tag. +func GetAmountFromZap(event nostr.Event) uint64 { + // Check for "amount" tag in the zap receipt + if tag := event.Tags.Find("amount"); tag != nil { + if amt, err := strconv.ParseUint(tag[1], 10, 64); err == nil { + return amt + } + } + + // if not found, get from the embedded zap request in "description" tag + if descTag := event.Tags.Find("description"); descTag != nil { + var embeddedEvent nostr.Event + if err := json.Unmarshal([]byte(descTag[1]), &embeddedEvent); err == nil { + if tag := embeddedEvent.Tags.Find("amount"); tag != nil { + if amt, err := strconv.ParseUint(tag[1], 10, 64); err == nil { + return amt + } + } + } + } + + return 0 +} diff --git a/nip61/nip61.go b/nip61/nip61.go index 92fd235..7eff8d1 100644 --- a/nip61/nip61.go +++ b/nip61/nip61.go @@ -12,6 +12,7 @@ import ( "fiatjaf.com/nostr" "fiatjaf.com/nostr/nip60" "github.com/btcsuite/btcd/btcec/v2" + "github.com/elnosh/gonuts/cashu" ) var NutzapsNotAccepted = errors.New("user doesn't accept nutzaps") @@ -161,3 +162,17 @@ func getEligibleTokensWeHave( } } } + +func GetAmountFromNutzap(evt nostr.Event) uint64 { + var total uint64 + for _, tag := range evt.Tags { + if len(tag) >= 2 && tag[0] == "proof" { + var proof cashu.Proof + if err := json.Unmarshal([]byte(tag[1]), &proof); err != nil { + continue + } + total += proof.Amount + } + } + return total +}