1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# Postal Services over Swarm
`pss` enables message relay over swarm. This means nodes can send messages to each other without being directly connected with each other, while taking advantage of the efficient routing algorithms that swarm uses for transporting and storing data.
### CONTENTS
* Status of this document
* Core concepts
* Caveat
* Examples
* API
* Retrieve node information
* Receive messages
* Send messages using public key encryption
* Send messages using symmetric encryption
* Querying peer keys
* Handshakes
### STATUS OF THIS DOCUMENT
`pss` is under active development, and the first implementation is yet to be merged to the Ethereum main branch. Expect things to change.
Details on swarm routing and encryption schemes out of scope of this document.
Please refer to [ARCHITECTURE.md](ARCHITECTURE.md) for in-depth topics concerning `pss`.
## CORE CONCEPTS
Three things are required to send a `pss` message:
1. Encryption key
2. Topic
3. Message payload
Encryption key can be a public key or a 32 byte symmetric key. It must be coupled with a peer address in the node prior to sending.
Topic is the initial 4 bytes of a hash value.
Message payload is an arbitrary byte slice of data.
Upon sending the message it is encrypted and passed on from peer to peer. Any node along the route that can successfully decrypt the message is regarded as a recipient. Recipients continue to pass on the message to their peers, to make traffic analysis attacks more difficult.
The Address that is coupled with the encryption keys are used for routing the message. This does *not* need to be a full addresses; the network will route the message to the best of its ability with the information that is available. If *no* address is given (zero-length byte slice), routing is effectively deactivated, and the message is passed to all peers by all peers.
## CAVEAT
`pss` connectivity resembles UDP. This means there is no delivery guarantee for a message. Furthermore there is no strict definition of what a connection between two nodes communicating via `pss` is. Reception acknowledgements and keepalive-schemes is the responsibility of the application.
Due to the inherent properties of the `swarm` routing algorithm, a node may receive the same message more than once. Message deduplication *cannot be guaranteed* by `pss`, and must be handled in the application layer to ensure predictable results.
## EXAMPLES
The code tutorial [p2p programming in go-ethereum](https://github.com/nolash/go-ethereum-p2p-demo) by [@nolash](https://github.com/nolash) provides step-by-step code examples for usage of `pss` API with `go-ethereum` nodes.
A quite unpolished example using `javascript` is available here: [https://github.com/nolash/pss-js/tree/withcrypt](https://github.com/nolash/pss-js/tree/withcrypt)
## API
The `pss` API is available through IPC and Websockets. There is currently no `web3.js` implementation, as this does not support message subscription.
For `golang` clients, please use the `rpc.Client` provided by the `go-ethereum` repository. The return values may have special types in `golang`. Please refer to `godoc` for details.
### RETRIEVE NODE INFORMATION
#### pss_getPublicKey
Retrieves the public key of the node, in hex format
```
parameters:
none
returns:
1. publickey (hex)
```
#### pss_baseAddr
Retrieves the swarm overlay address of the node, in hex format
```
parameters:
none
returns:
1. swarm overlay address (hex)
```
#### pss_stringToTopic
Creates a deterministic 4 byte topic value from input, returned in hex format
```
parameters:
1. topic string (string)
returns:
1. pss topic (hex)
```
### RECEIVE MESSAGES
#### pss_subscribe
Creates a subscription. Received messages with matching topic will be passed to subscription client.
```
parameters:
1. string("receive")
2. topic (4 bytes in hex)
returns:
1. subscription handle `base64(byte)` `rpc.ClientSubscription`
```
In `golang` as special method is used:
`rpc.Client.Subscribe(context.Context, "pss", chan pss.APIMsg, "receive", pss.Topic)`
Incoming messages are encapsulated in an object (`pss.APIMsg` in `golang`) with the following members:
```
1. Msg (hex) - the message payload
2. Asymmetric (bool) - true if message used public key encryption
3. Key (string) - the encryption key used
```
### SEND MESSAGE USING PUBLIC KEY ENCRYPTION
#### pss_setPeerPublicKey
Register a peer's public key. This is done once for every topic that will be used with the peer. Address can be anything from 0 to 32 bytes inclusive of the peer's swarm overlay address.
```
parameters:
1. public key of peer (hex)
2. topic (4 bytes in hex)
3. address of peer (hex)
returns:
none
```
#### pss_sendAsym
Encrypts the message using the provided public key, and signs it using the node's private key. It then wraps it in an envelope containing the topic, and sends it to the network.
```
parameters:
1. public key of peer (hex)
2. topic (4 bytes in hex)
3. message (hex)
returns:
none
```
### SEND MESSAGE USING SYMMETRIC ENCRYPTION
#### pss_setSymmetricKey
Register a symmetric key shared with a peer. This is done once for every topic that will be used with the peer. Address can be anything from 0 to 32 bytes inclusive of the peer's swarm overlay address.
If the fourth parameter is false, the key will *not* be added to the list of symmetric keys used for decryption attempts.
```
parameters:
1. symmetric key (hex)
2. topic (4 bytes in hex)
3. address of peer (hex)
4. use for decryption (bool)
returns:
1. symmetric key id (string)
```
#### pss_sendSym
Encrypts the message using the provided symmetric key, wraps it in an envelope containing the topic, and sends it to the network.
```
parameters:
1. symmetric key id (string)
2. topic (4 bytes in hex)
3. message (hex)
returns:
none
```
### QUERY PEER KEYS
#### pss_GetSymmetricAddressHint
Return the swarm overlay address associated with the peer registered with the given symmetric key and topic combination.
```
parameters:
1. topic (4 bytes in hex)
2. symmetric key id (string)
returns:
1. peer address (hex)
```
#### pss_GetAsymmetricAddressHint
Return the swarm overlay address associated with the peer registered with the given symmetric key and topic combination.
```
parameters:
1. topic (4 bytes in hex)
2. public key in hex form (string)
returns:
1. peer address (hex)
```
### HANDSHAKES
Convenience implementation of Diffie-Hellman handshakes using ephemeral symmetric keys. Peers keep separate sets of keys for incoming and outgoing communications.
*This functionality is an optional feature in `pss`. It is compiled in by default, but can be omitted by providing the `nopsshandshake` build tag.*
#### pss_addHandshake
Activate handshake functionality on the specified topic.
```
parameters:
1. topic (4 bytes in hex)
returns:
none
```
#### pss_removeHandshake
Remove handshake functionality on the specified topic.
```
parameters:
1. topic (4 bytes in hex)
returns:
none
```
#### pss_handshake
Instantiate handshake with peer, refreshing symmetric encryption keys.
If parameter 3 is false, the returned array will be empty.
```
parameters:
1. public key of peer in hex format (string)
2. topic (4 bytes in hex)
3. block calls until keys are received (bool)
4. flush existing incoming keys (bool)
returns:
1. list of symmetric keys (string[])
```
#### pss_getHandshakeKeys
Get valid symmetric encryption keys for a specified peer and topic.
parameters:
1. public key of peer in hex format (string)
2. topic (4 bytes in hex)
3. include keys for incoming messages (bool)
4. include keys for outgoing messages (bool)
returns:
1. list of symmetric keys (string[])
#### pss_getHandshakeKeyCapacity
Get amount of remaining messages the specified key is valid for.
```
parameters:
1. symmetric key id (string)
returns:
1. number of messages (uint16)
```
#### pss_getHandshakePublicKey
Get the peer's public key associated with the specified symmetric key.
```
parameters:
1. symmetric key id (string)
returns:
1. Associated public key in hex format (string)
```
#### pss_releaseHandshakeKey
Invalidate the specified key.
Normally, the key will be kept for a grace period to allow for decryption of delayed messages. If instant removal is set, this grace period is omitted, and the key removed instantaneously.
```
parameters:
1. public key of peer in hex format (string)
2. topic (4 bytes in hex)
3. symmetric key id to release (string)
4. remove keys instantly (bool)
returns:
1. whether key was successfully removed (bool)
```
|