From 506b38916ab67a37b2d98b46b62cf0c0c5fde5a4 Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Wed, 17 Jul 2024 00:51:41 +0100 Subject: [PATCH] cashu wallet and nut zaps --- 60.md | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 61.md | 117 ++++++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 60.md create mode 100644 61.md diff --git a/60.md b/60.md new file mode 100644 index 00000000..a90f06ab --- /dev/null +++ b/60.md @@ -0,0 +1,198 @@ +# NIP-60 +## Cashu Wallet +`draft` `optional` + +This NIP defines the operations of a cashu-based wallet. + +A cashu wallet is a wallet which information is stored in relays to make it accessible across applications. + +The purpose of this NIP is: +* ease-of-use: new users immediately are able to receive funds without creating accounts with other services. +* interoperability: users' wallets follows them across applications. + +This NIP doesn't deal with users' *receiving* money from someone else, it's just to keep state of the user's wallet. + +# High-level flow +1. A user has a `kind:37375` event that represents a wallet. +2. A user has `kind:7375` events that represent the unspent proofs of the wallet. -- The proofs are encrypted with the user's private key. +3. A user has `kind:7376` events that represent the spending history of the wallet -- This history is for informational purposes only and is completely optional. + +## Wallet Event +```jsonc +{ + "kind": 37375, + "content": nip44_encrypt([ + [ "balance", "100", "sats" ], + [ "privkey", "hexkey" ] // explained in Appendix 2 + ]), + "tags": [ + [ "d", "my-wallet" ], + [ "mint", "https://mint1" ], + [ "mint", "https://mint2" ], + [ "mint", "https://mint3" ], + [ "name", "my shitposting wallet" ], + [ "unit", "sats" ], + [ "description", "a wallet for my day-to-day shitposting" ], + [ "relay", "wss://relay1" ], + [ "relay", "wss://relay2" ], + ] +} +``` + +The wallet event is a parameterized replaceable event `kind:37375`. + +Tags: +* `d` - wallet ID. +* `mint` - Mint(s) this wallet uses -- there MUST be one or more mint tags. +* `relay` - Relays where the wallet and related events can be found. -- one ore more relays SHOULD be specified. If missing, clients should follow [[NIP-65]]. +* `unit` - Base unit of the wallet (e.g. "sats", "usd", etc). +* `name` - Optional human-readable name for the wallet. +* `description` - Optional human-readable description of the wallet. +* `balance` - Optional best-effort balance of the wallet that can serve as a placeholder while an accurate balance is computed from fetching all unspent proofs. +* `privkey` - Private key used to unlock P2PK ecash. MUST be stored encrypted in the `.content` field. **This is a different private key exclusively used for the wallet, not associated in any way to the user's nostr private key** -- This is only used when receiving funds from others, described in NIP-61. + +Any tag, other than the `d` tag, can be [[NIP-44]] encrypted into the `.content` field. + +### Deleting a wallet event +Due to PRE being hard to delete, if a user wants to delete a wallet, they should empty the event and keep just the `d` identifier and add a `deleted` tag. + +## Token Event +Token events are used to record the unspent proofs that come from the mint. + +There can be multiple `kind:7375` events for the same mint, and multiple proofs inside each `kind:7375` event. + +```jsonc +{ + "kind": 7375, + "content": nip44_encrypt({ + "mint": "https://stablenut.umint.cash", + "proofs": [ + { + "id": "005c2502034d4f12", + "amount": 1, + "secret": "z+zyxAVLRqN9lEjxuNPSyRJzEstbl69Jc1vtimvtkPg=", + "C": "0241d98a8197ef238a192d47edf191a9de78b657308937b4f7dd0aa53beae72c46" + } + ] + }), + "tags": [ + [ "a", "37375::my-wallet" ] + ] +} +``` + +`.content` is a [[NIP-44]] encrypted payload storing the mint and the unencoded proofs. +* `a` an optional tag linking the token to a specific wallet. + +### Spending proofs +When one or more proofs of a token are spent, the token event should be [[NIP-09]]-deleted and, if some proofs are unspent from the same token event, a new token event should be created rolling over the unspent proofs and adding any change outputs to the new token event. + +## Spending History Event +Clients SHOULD publish `kind:7376` events to create a transaction history when their balance changes. + +```jsonc +{ + "kind": 7376, + "content": nip44_encrypt([ + [ "direction", "in" ], // in = received, out = sent + [ "amount", "1", "sats" ], + [ "e", "", "", "created" ], + ]), + "tags": [ + [ "a", "37375::my-wallet" ], + ] +} +``` + +* `direction` - The direction of the transaction; `in` for received funds, `out` for sent funds. +* `a` - The wallet the transaction is related to. + +Clients MUST add `e` tags to create references of destroyed and created token events along with the marker of the meaning of the tag: +* `created` - A new token event was created. +* `destroyed` - A token event was destroyed. +* `redeemed` - A [[NIP-61]] nutzap was redeemed. + +All tags can be [[NIP-44]] encrypted. Clients SHOULD leave `e` tags with a `redeemed` marker unencrypted. + +Multiple `e` tags can be added to a `kind:7376` event. + +# Flow +A client that wants to check for user's wallets information starts by fetching `kind:10019` events from the user's relays, if no event is found, it should fall back to using the user's [[NIP-65]] relays. + +## Fetch wallet and token list +From those relays, the client should fetch wallet and token events. + +`"kinds": [37375, 7375], "authors": [""]` + +## Fetch proofs +While the client is fetching (and perhaps validating) proofs it can use the optional `balance` tag of the wallet event to display a estimate of the balance of the wallet. + +## Spending token +If Alice spends 4 sats from this token event +```jsonconc +{ + "kind": 7375, + "id": "event-id-1", + "content": nip44_encrypt({ + "mint": "https://stablenut.umint.cash", + "proofs": [ + { "id": "1", "amount": 1 }, + { "id": "2", "amount": 2 }, + { "id": "3", "amount": 4 }, + { "id": "4", "amount": 8 }, + ] + }), + "tags": [ + [ "a", "37375::my-wallet" ] + ] +} +``` + +Her client: +* MUST roll over the unspent proofs: +```jsonconc +{ + "kind": 7375, + "id": "event-id-2", + "content": nip44_encrypt({ + "mint": "https://stablenut.umint.cash", + "proofs": [ + { "id": "1", "amount": 1 }, + { "id": "2", "amount": 2 }, + { "id": "8", "amount": 8 }, + ] + }), + "tags": [ + [ "a", "37375::my-wallet" ] + ] +} +``` +* MUST delete event `event-id-1` +* SHOULD create a `kind:7376` event to record the spend +```jsonconc +{ + "kind": 7376, + "content": nip44_encrypt([ + [ "direction", "out" ], + [ "amount", "4", "sats" ], + [ "e", "", "", "destroyed" ], + [ "e", "", "", "created" ], + ]), + "tags": [ + [ "a", "37375::my-wallet" ], + ] +} +``` + +## Appendix 1: Validating proofs +Clients can optionally validate proofs to make sure they are not working from an old state; this logic is left up to particular implementations to decide when and why to do it, but if some proofs are checked and deemed to have been spent, the client should delete the token even and roll over any unspent proof. + +## Appendix 2: Alternative P2PK pubkey +Sometimes clients might not have access to the user's private key (i.e. NIP-07, NIP-46 signing) and, as such, the private key to sign cashu spends might not be available, which would make spending the P2PK incoming nutzaps impossible. + +For this scenarios clients can: + +* add a `pubkey` tag to the `kind:10019` (indicating which pubkey senders should P2PK to) +* store the private key in the `kind:37375` event in the nip44-encrypted `content` field. + +This is to avoid depending on NIP-07/46 adaptations to sign cashu payloads. \ No newline at end of file diff --git a/61.md b/61.md new file mode 100644 index 00000000..f219f11e --- /dev/null +++ b/61.md @@ -0,0 +1,117 @@ +# NIP-61: +## Nut Zaps + +A Nut Zap is a P2PK cashu token where the payment itself is the receipt. + +# High-level flow +Alice wants to nutzap 1 sat to Bob because of an event `event-id-1` she liked. + +## Alice nutzaps Bob +1. Alice fetches event `kind:10019` from Bob to see the mints Bob trusts. +2. She mints a token at that mint (or swaps some tokens she already had in that mint) p2pk-locked to the pubkey Bob has listed in his `kind:10019`. +3. She publishes a `kind:7337` event to the relays Bob indicated with the proofs she minted. + +## Bob receives the nutzap +1. At some point, Bob's client fetches `kind:7337` events p-tagging him from his relays. +2. Bob's client swaps the token into his wallet. + +# Nutzap informational event +```jsonc +{ + "kind": 10019, + "tags": [ + [ "relay", "wss://relay1" ], + [ "relay", "wss://relay2" ], + [ "mint", "https://mint1" ], + [ "mint", "https://mint2" ], + [ "pubkey", "" ] + ] +} +``` + +`kind:10019` is an event that is useful for others to know how to send money to the user. + +* `relay` - Relays where the user will be reading token events from. If a user wants to send money to the user, they should write to these relays. +* `mint` - Mints the user is explicitly agreeing to use to receive funds on. Clients SHOULD not send money on mints not listed here or risk burning their money. +* `pubkey` - Pubkey that SHOULD be used to P2PK-lock receiving nutzaps. If not present, clients SHOULD use the pubkey of the recipient. + +## Nutzap event +Event `kind:7337` is a nutzap event published by the sender, p-tagging the recipient. The outputs are P2PK-locked to the pubkey the recipient indicated in their `kind:10019` event or to the recipient pubkey if the `kind:10019` event doesn't have a explicit pubkey. + +Clients MUST prefix the recipient pubkey with `"02"` (for nostr<>cashu pubkey compatibility). + +```jsonc +{ + kind: 7337, + content: "[{\"amount\":1,\"C\":\"02277c66191736eb72fce9d975d08e3191f8f96afb73ab1eec37e4465683066d3f\",\"id\":\"000a93d6f8a1d2c4\",\"secret\":\"[\\\"P2PK\\\",{\\\"nonce\\\":\\\"b00bdd0467b0090a25bdf2d2f0d45ac4e355c482c1418350f273a04fedaaee83\\\",\\\"data\\\":\\\"02eaee8939e3565e48cc62967e2fde9d8e2a4b3ec0081f29eceff5c64ef10ac1ed\\\"}]\"}]", + pubkey: "sender-pubkey", + tags: [ + [ "amount", "1", "sats" ], + [ "comment", "Great point" ], + [ "u", "https://stablenut.umint.cash", ], + [ "e", "", "" ], + [ "p", "e9fbced3a42dcf551486650cc752ab354347dd413b307484e4fd1818ab53f991" ], // recipient of nut zap + ] +} +``` + +* `.content` is an array of p2pk-locked outputs. +* `amount` is a shorthand for the combined amount of all outputs. -- Clients SHOULD validate that the sum of the amounts in the outputs matches. +* `u` is the mint the URL of the mint EXACTLY as specified by the recipient's `kind:10019`. +* `e` zero or one event that is being nutzapped. +* `p` exactly one pubkey, specifying the recipient of the nutzap. + +# Sending a nutzap + +* The sender fetches the recipient's `kind:10019`. +* The sender mints/swaps ecash on one of the recipient's listed mints. +* The sender p2pk locks to the recipient's specified pubkey in their + +# Receiving nutzaps + +Clients should REQ for nut zaps: +* Filtering with `#u` for mints they expect to receive ecash from. + * this is to prevent even interacting with mints the user hasn't explicitly signaled. +* Filtering with `since` of the most recent `kind:7376` event the same user has created. + * this can be used as a marker of the nut zaps that have already been swaped by the user -- clients might choose to use other kinds of markers, including internal state -- this is just a guidance of one possible approach. + +Clients MIGHT choose to use some kind of filtering (e.g. WoT) to ignore spam. + +`{ "kinds": [7337], "#p": "my-pubkey", "#u": [ "", ""], "since": }`. + +Upon receiving a new nut zap, the client should swap the tokens into a wallet the user controls, either a [[NIP-60]] wallet, their own LN wallet or anything else. + +## Updating nutzap-redemption history +When claiming a token the client SHOULD create a `kind:7376` event and `e` tag the original nut zap event. This is to record that this token has already been claimed (and shouldn't be attempted again) and as signaling to the recipient that the ecash has been redeemed. + +Multiple `kind:7337` events can be tagged in the same `kind:7376` event. + +```jsonc +{ + "kind": 7376, + "content": nip44_encrypt([ + [ "direction", "in" ], // in = received, out = sent + [ "amount", "1", "sats" ], + [ "e", "<7375-event-id>", "relay-hint", "created" ] // new token event that was created + ]), + "tags": [ + [ "a", "37375::my-wallet" ], // an optional wallet tag + [ "e", "<7337-event-id>", "relay-hint", "redeemed" ], // nutzap event that has been redeemed + [ "p", "sender-pubkey" ] // pubkey of the author of the 7337 event (nutzap sender) + ] +} +``` + +Events that redeem a nutzap SHOULD be published to the sender's [[NIP-65]] relays. + +## Verifying a Cashu Zap +* Clients SHOULD check that the receiving user has issued a `kind:10019` tagging the mint where the cashu has been minted. +* Clients SHOULD check that the token is locked to the pubkey the user has listed in their `kind:10019`. + +## Final Considerations + +1. Clients SHOULD guide their users to use NUT-11 (P2PK) compatible-mints in their `kind:10019` event to avoid receiving nut zaps anyone can spend + +2. Clients SHOULD normalize and deduplicate mint URLs as described in NIP-65. + +3. A nut zap MUST be sent to a mint the recipient has listed in their `kind:10019` event or to the NIP-65 relays of the recipient, failure to do so may result in the recipient donating the tokens to the mint since the recipient might never see the event. \ No newline at end of file