upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/37.md
diff options
context:
space:
mode:
Diffstat (limited to '37.md')
-rw-r--r--37.md146
1 files changed, 146 insertions, 0 deletions
diff --git a/37.md b/37.md
new file mode 100644
index 0000000..a82839e
--- /dev/null
+++ b/37.md
@@ -0,0 +1,146 @@
1NIP-37
2======
3
4Event Publication Onion-routing
5-----------------
6
7`draft` `optional`
8
9This NIP defines a way to do onion-based routing for event publishing, optionally compensated with cashu where pubkeys (not necessarily relays) provide routing services.
10
11## Announcement
12A pubkey announces itself as willing to route by publishing an ephemeral event `kind:20690` in their outbox relays. Announcers should republish a new ephemeral event every few minutes to indicate they are still willing to route and online. The refresh rate depends on the relays where they are publishing (usually ~5 minutes).
13
14```jsonc
15{
16 "kind": 20690,
17 "tags": [
18 [ "relay", "wss://example1.com" ],
19 [ "relay", "wss://example2.com" ],
20 [ "fee", "1", "sat" ]
21 ],
22 "content": "",
23 "pubkey": <pubkey-of-router>
24}
25```
26
27Tags:
28`relay` -- relay(s) where the pubkey will be listening for requests.
29`fee` -- an optional fee indicating how much money the pubkey demands to be paid.
30
31## Routing request
32A sender publishes a series of `kind:2444` or `kind:20444` where the `payload` is the event that should be published by the pubkey of the current hop in the relays specified by the envelope. An optional proof can be included, this cashu proof is for the hop processing this route.
33
34```jsonc
35{
36 "kind": 20444,
37 "content": nip44_encrypt("{
38 'relays': [ 'wss://example1.com' ],
39 'event': { 'id': ...., sig: ..., }, // event the current routing pubkey should publish
40 'proof': <optional-unencoded-cashu-proof>,
41 'mint': 'https://mint.com',
42 'unit': 'sat'
43 }")
44 "tags": [
45 [ "p", "pubkey-of-next-hop" ]
46 ]
47}
48```
49
50Event `kind:2444` has the exact same format as the `kind:20444`, the only difference being that it's not ephemeral, so it can be used to route events that are expected to take longer to route. `kind:2444` events SHOULD include an [[NIP-40]] `expiration` tag.
51
52Routing pubkeys MUST look at the `created_at` of the event they need to publish and wait until at least that time before publishing. Using future `created_at`s allows the sender to increase their privacy by preventing timing analysis by mixing different time delays at each hop.
53
54When a routing pubkey receives a routing request event it should decrypt the content, redeem the cashu and publish the event in the `event` field. If the cashu cannot be redeemed the routing pubkey MUST NOT publish the event. This is useful as a way to **cancel** a routing event where the sender uses a future `created_at` timestamps and chooses to cancel the publication.
55
56## Constructing a route
57The sender:
58
591. looks for `kind:20400` announcements and assembles a path
602. creates the event they want to publish and sign it with their normal pubkey
61
623. walks the path backwards (finish -> start), putting the event signed in the previous step in the `event` field of the `content` and the other fileds of the envelope
634. encrypts to the current hop and signs -- both operations with a new disposable key and `p`-tags the current hop.
643. repeat step #3 and #4 for the rest of the route until the first hop
654. sender publishes the resulting event to one of the relays the first hop indicated it's active on
66
67## Pseudocode implementation
68```ts
69// event to be published
70event = { "kind": 1, content: "This is an onion-routed event" }
71sign_event(event, my_private_key)
72outer_layer = assemble_onion_route(event, [ "pubkey-A", "pubkey-B", "pubkey-C" ])
73publish_event(outer_layer)
74
75function assemble_onion_route(final_event, path) {
76 current_event = final_event // Start with the event to be published by the final hop
77
78 // Walk the path backwards from "C" to "A"
79 for hop_pubkey in reverse(path): // path = ["A", "B", "C"], reversed to ["C", "B", "A"]
80 // Generate a new disposable key for this hop
81 disposable_key = generate_disposable_key()
82
83 // Create the payload for this hop
84 payload = {
85 "event": current_event // The event for the hop to publish
86 // ...
87 }
88
89 // Encrypt the payload for the current hop
90 encrypted_payload = nip44_encrypt(payload, hop_pubkey) // Encrypt with the hop's pubkey
91
92 // Create the routing event for this hop
93 routing_event = {
94 "kind": 20444, // Ephemeral routing request
95 "content": encrypted_payload, // The encrypted payload for this hop
96 "tags": [["p", hop_pubkey]] // Tag indicating the pubkey of the next hop
97 }
98
99 // Sign the event with the disposable key for this hop
100 sign_event(routing_event, disposable_key)
101
102 // Prepare for the next hop (wrap another layer)
103 current_event = routing_event
104 }
105
106 return current_event // Return the fully wrapped outermost event ready to be published
107}
108
109```
110
111## Example anatomy of a sequence of routing events
112
113```jsonc
114{
115 kind: 20444,
116 content: nip44_encrypt({
117 relays: [...],
118 event: {
119 "id": ...,
120 "kind": 20444,
121 "pubkey": "ephemeral-key-1",
122 "sig": ...,
123 content: nip44_encrypt({
124 relays: [....],
125 event: {
126 "id": ....,
127 "kind": 20444,
128 "pubkey": "ephemeral-key-2",
129 "sig": ...,
130 "content": nip44_encrypt({
131 relays: [ "wss://target-relay.com" ],
132 event: {
133 id: ...,
134 "kind": 1,
135 content: "This is an onion-routed event",
136 "pubkey": "real-pubkey",
137 "sig": ...,
138 }
139 })
140 }
141 })
142 }
143 }
144}
145```
146