@libp2p/pubsub 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +35 -0
  3. package/dist/src/errors.d.ts +39 -0
  4. package/dist/src/errors.d.ts.map +1 -0
  5. package/dist/src/errors.js +41 -0
  6. package/dist/src/errors.js.map +1 -0
  7. package/dist/src/index.d.ts +180 -0
  8. package/dist/src/index.d.ts.map +1 -0
  9. package/dist/src/index.js +467 -0
  10. package/dist/src/index.js.map +1 -0
  11. package/dist/src/message/rpc.d.ts +258 -0
  12. package/dist/src/message/rpc.js +699 -0
  13. package/dist/src/message/sign.d.ts +17 -0
  14. package/dist/src/message/sign.d.ts.map +1 -0
  15. package/dist/src/message/sign.js +84 -0
  16. package/dist/src/message/sign.js.map +1 -0
  17. package/dist/src/message/topic-descriptor.d.ts +254 -0
  18. package/dist/src/message/topic-descriptor.js +647 -0
  19. package/dist/src/peer-streams.d.ts +67 -0
  20. package/dist/src/peer-streams.d.ts.map +1 -0
  21. package/dist/src/peer-streams.js +112 -0
  22. package/dist/src/peer-streams.js.map +1 -0
  23. package/dist/src/utils.d.ts +29 -0
  24. package/dist/src/utils.d.ts.map +1 -0
  25. package/dist/src/utils.js +80 -0
  26. package/dist/src/utils.js.map +1 -0
  27. package/dist/test/emit-self.spec.d.ts +2 -0
  28. package/dist/test/emit-self.spec.d.ts.map +1 -0
  29. package/dist/test/emit-self.spec.js +63 -0
  30. package/dist/test/emit-self.spec.js.map +1 -0
  31. package/dist/test/instance.spec.d.ts +2 -0
  32. package/dist/test/instance.spec.d.ts.map +1 -0
  33. package/dist/test/instance.spec.js +50 -0
  34. package/dist/test/instance.spec.js.map +1 -0
  35. package/dist/test/lifesycle.spec.d.ts +2 -0
  36. package/dist/test/lifesycle.spec.d.ts.map +1 -0
  37. package/dist/test/lifesycle.spec.js +192 -0
  38. package/dist/test/lifesycle.spec.js.map +1 -0
  39. package/dist/test/message.spec.d.ts +2 -0
  40. package/dist/test/message.spec.d.ts.map +1 -0
  41. package/dist/test/message.spec.js +83 -0
  42. package/dist/test/message.spec.js.map +1 -0
  43. package/dist/test/pubsub.spec.d.ts +2 -0
  44. package/dist/test/pubsub.spec.d.ts.map +1 -0
  45. package/dist/test/pubsub.spec.js +310 -0
  46. package/dist/test/pubsub.spec.js.map +1 -0
  47. package/dist/test/sign.spec.d.ts +2 -0
  48. package/dist/test/sign.spec.d.ts.map +1 -0
  49. package/dist/test/sign.spec.js +93 -0
  50. package/dist/test/sign.spec.js.map +1 -0
  51. package/dist/test/topic-validators.spec.d.ts +2 -0
  52. package/dist/test/topic-validators.spec.d.ts.map +1 -0
  53. package/dist/test/topic-validators.spec.js +86 -0
  54. package/dist/test/topic-validators.spec.js.map +1 -0
  55. package/dist/test/utils/index.d.ts +22 -0
  56. package/dist/test/utils/index.d.ts.map +1 -0
  57. package/dist/test/utils/index.js +86 -0
  58. package/dist/test/utils/index.js.map +1 -0
  59. package/dist/test/utils.spec.d.ts +2 -0
  60. package/dist/test/utils.spec.d.ts.map +1 -0
  61. package/dist/test/utils.spec.js +53 -0
  62. package/dist/test/utils.spec.js.map +1 -0
  63. package/dist/tsconfig.tsbuildinfo +1 -0
  64. package/package.json +109 -4
  65. package/src/README.md +251 -0
  66. package/src/errors.ts +45 -0
  67. package/src/index.ts +610 -0
  68. package/src/message/rpc.d.ts +258 -0
  69. package/src/message/rpc.js +699 -0
  70. package/src/message/rpc.proto +20 -0
  71. package/src/message/sign.ts +101 -0
  72. package/src/message/topic-descriptor.d.ts +254 -0
  73. package/src/message/topic-descriptor.js +647 -0
  74. package/src/message/topic-descriptor.proto +30 -0
  75. package/src/peer-streams.ts +169 -0
  76. package/src/utils.ts +93 -0
@@ -0,0 +1,20 @@
1
+ syntax = "proto3";
2
+
3
+ message RPC {
4
+ repeated SubOpts subscriptions = 1;
5
+ repeated Message msgs = 2;
6
+
7
+ message SubOpts {
8
+ optional bool subscribe = 1; // subscribe or unsubcribe
9
+ optional string topicID = 2;
10
+ }
11
+
12
+ message Message {
13
+ optional bytes from = 1;
14
+ optional bytes data = 2;
15
+ optional bytes seqno = 3;
16
+ repeated string topicIDs = 4;
17
+ optional bytes signature = 5;
18
+ optional bytes key = 6;
19
+ }
20
+ }
@@ -0,0 +1,101 @@
1
+ import * as PeerIdFactory from '@libp2p/peer-id-factory'
2
+ import { RPC } from './rpc.js'
3
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
4
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
+ import { normalizeOutRpcMessage } from '../utils.js'
6
+ import { PeerId } from '@libp2p/peer-id'
7
+ import { keys } from '@libp2p/crypto'
8
+ import type { Message } from '@libp2p/interfaces/pubsub'
9
+
10
+ export const SignPrefix = uint8ArrayFromString('libp2p-pubsub:')
11
+
12
+ /**
13
+ * Signs the provided message with the given `peerId`
14
+ */
15
+ export async function signMessage (peerId: PeerId, message: Message) {
16
+ // Get the message in bytes, and prepend with the pubsub prefix
17
+ const bytes = uint8ArrayConcat([
18
+ SignPrefix,
19
+ RPC.Message.encode(normalizeOutRpcMessage(message)).finish()
20
+ ])
21
+
22
+ if (peerId.privateKey == null) {
23
+ throw new Error('Cannot sign message, no private key present')
24
+ }
25
+
26
+ if (peerId.publicKey == null) {
27
+ throw new Error('Cannot sign message, no public key present')
28
+ }
29
+
30
+ const privateKey = await keys.unmarshalPrivateKey(peerId.privateKey)
31
+ const signature = await privateKey.sign(bytes)
32
+
33
+ const outputMessage: Message = {
34
+ ...message,
35
+ signature: signature,
36
+ key: peerId.publicKey
37
+ }
38
+
39
+ return outputMessage
40
+ }
41
+
42
+ /**
43
+ * Verifies the signature of the given message
44
+ */
45
+ export async function verifySignature (message: Message) {
46
+ if (message.signature == null) {
47
+ throw new Error('Message must contain a signature to be verified')
48
+ }
49
+
50
+ if (message.from == null) {
51
+ throw new Error('Message must contain a from property to be verified')
52
+ }
53
+
54
+ // Get message sans the signature
55
+ const bytes = uint8ArrayConcat([
56
+ SignPrefix,
57
+ RPC.Message.encode({
58
+ ...message,
59
+ signature: undefined,
60
+ key: undefined
61
+ }).finish()
62
+ ])
63
+
64
+ // Get the public key
65
+ const pubKeyBytes = await messagePublicKey(message)
66
+ const pubKey = keys.unmarshalPublicKey(pubKeyBytes)
67
+
68
+ // verify the base message
69
+ return await pubKey.verify(bytes, message.signature)
70
+ }
71
+
72
+ /**
73
+ * Returns the PublicKey associated with the given message.
74
+ * If no, valid PublicKey can be retrieved an error will be returned.
75
+ */
76
+ export async function messagePublicKey (message: Message) {
77
+ // should be available in the from property of the message (peer id)
78
+ if (message.from == null) {
79
+ throw new Error('Could not get the public key from the originator id')
80
+ }
81
+
82
+ const from = PeerId.fromBytes(message.from)
83
+
84
+ if (message.key != null) {
85
+ const keyPeerId = await PeerIdFactory.createFromPubKey(keys.unmarshalPublicKey(message.key))
86
+
87
+ // the key belongs to the sender, return the key
88
+ if (!keyPeerId.equals(from)) {
89
+ throw new Error('Public Key does not match the originator')
90
+ }
91
+
92
+ if (keyPeerId.publicKey != null) {
93
+ return keyPeerId.publicKey
94
+ }
95
+ } else if (from.publicKey != null) {
96
+ return from.publicKey
97
+ }
98
+
99
+ // We couldn't validate pubkey is from the originator, error
100
+ throw new Error('Could not get the public key from the originator id')
101
+ }
@@ -0,0 +1,254 @@
1
+ import * as $protobuf from 'protobufjs'
2
+ /** Properties of a TopicDescriptor. */
3
+ export interface ITopicDescriptor {
4
+
5
+ /** TopicDescriptor name */
6
+ name?: (string|null)
7
+
8
+ /** TopicDescriptor auth */
9
+ auth?: (TopicDescriptor.IAuthOpts|null)
10
+
11
+ /** TopicDescriptor enc */
12
+ enc?: (TopicDescriptor.IEncOpts|null)
13
+ }
14
+
15
+ /** Represents a TopicDescriptor. */
16
+ export class TopicDescriptor implements ITopicDescriptor {
17
+ /**
18
+ * Constructs a new TopicDescriptor.
19
+ *
20
+ * @param [p] - Properties to set
21
+ */
22
+ constructor (p?: ITopicDescriptor);
23
+
24
+ /** TopicDescriptor name. */
25
+ public name?: (string|null)
26
+
27
+ /** TopicDescriptor auth. */
28
+ public auth?: (TopicDescriptor.IAuthOpts|null)
29
+
30
+ /** TopicDescriptor enc. */
31
+ public enc?: (TopicDescriptor.IEncOpts|null)
32
+
33
+ /** TopicDescriptor _name. */
34
+ public _name?: 'name'
35
+
36
+ /** TopicDescriptor _auth. */
37
+ public _auth?: 'auth'
38
+
39
+ /** TopicDescriptor _enc. */
40
+ public _enc?: 'enc'
41
+
42
+ /**
43
+ * Encodes the specified TopicDescriptor message. Does not implicitly {@link TopicDescriptor.verify|verify} messages.
44
+ *
45
+ * @param m - TopicDescriptor message or plain object to encode
46
+ * @param [w] - Writer to encode to
47
+ * @returns Writer
48
+ */
49
+ public static encode (m: ITopicDescriptor, w?: $protobuf.Writer): $protobuf.Writer;
50
+
51
+ /**
52
+ * Decodes a TopicDescriptor message from the specified reader or buffer.
53
+ *
54
+ * @param r - Reader or buffer to decode from
55
+ * @param [l] - Message length if known beforehand
56
+ * @returns TopicDescriptor
57
+ * @throws {Error} If the payload is not a reader or valid buffer
58
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
59
+ */
60
+ public static decode (r: ($protobuf.Reader|Uint8Array), l?: number): TopicDescriptor;
61
+
62
+ /**
63
+ * Creates a TopicDescriptor message from a plain object. Also converts values to their respective internal types.
64
+ *
65
+ * @param d - Plain object
66
+ * @returns TopicDescriptor
67
+ */
68
+ public static fromObject (d: { [k: string]: any }): TopicDescriptor;
69
+
70
+ /**
71
+ * Creates a plain object from a TopicDescriptor message. Also converts values to other types if specified.
72
+ *
73
+ * @param m - TopicDescriptor
74
+ * @param [o] - Conversion options
75
+ * @returns Plain object
76
+ */
77
+ public static toObject (m: TopicDescriptor, o?: $protobuf.IConversionOptions): { [k: string]: any };
78
+
79
+ /**
80
+ * Converts this TopicDescriptor to JSON.
81
+ *
82
+ * @returns JSON object
83
+ */
84
+ public toJSON (): { [k: string]: any };
85
+ }
86
+
87
+ export namespace TopicDescriptor {
88
+
89
+ /** Properties of an AuthOpts. */
90
+ interface IAuthOpts {
91
+
92
+ /** AuthOpts mode */
93
+ mode?: (TopicDescriptor.AuthOpts.AuthMode|null)
94
+
95
+ /** AuthOpts keys */
96
+ keys?: (Uint8Array[]|null)
97
+ }
98
+
99
+ /** Represents an AuthOpts. */
100
+ class AuthOpts implements IAuthOpts {
101
+ /**
102
+ * Constructs a new AuthOpts.
103
+ *
104
+ * @param [p] - Properties to set
105
+ */
106
+ constructor (p?: TopicDescriptor.IAuthOpts);
107
+
108
+ /** AuthOpts mode. */
109
+ public mode?: (TopicDescriptor.AuthOpts.AuthMode|null)
110
+
111
+ /** AuthOpts keys. */
112
+ public keys: Uint8Array[]
113
+
114
+ /** AuthOpts _mode. */
115
+ public _mode?: 'mode'
116
+
117
+ /**
118
+ * Encodes the specified AuthOpts message. Does not implicitly {@link TopicDescriptor.AuthOpts.verify|verify} messages.
119
+ *
120
+ * @param m - AuthOpts message or plain object to encode
121
+ * @param [w] - Writer to encode to
122
+ * @returns Writer
123
+ */
124
+ public static encode (m: TopicDescriptor.IAuthOpts, w?: $protobuf.Writer): $protobuf.Writer;
125
+
126
+ /**
127
+ * Decodes an AuthOpts message from the specified reader or buffer.
128
+ *
129
+ * @param r - Reader or buffer to decode from
130
+ * @param [l] - Message length if known beforehand
131
+ * @returns AuthOpts
132
+ * @throws {Error} If the payload is not a reader or valid buffer
133
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
134
+ */
135
+ public static decode (r: ($protobuf.Reader|Uint8Array), l?: number): TopicDescriptor.AuthOpts;
136
+
137
+ /**
138
+ * Creates an AuthOpts message from a plain object. Also converts values to their respective internal types.
139
+ *
140
+ * @param d - Plain object
141
+ * @returns AuthOpts
142
+ */
143
+ public static fromObject (d: { [k: string]: any }): TopicDescriptor.AuthOpts;
144
+
145
+ /**
146
+ * Creates a plain object from an AuthOpts message. Also converts values to other types if specified.
147
+ *
148
+ * @param m - AuthOpts
149
+ * @param [o] - Conversion options
150
+ * @returns Plain object
151
+ */
152
+ public static toObject (m: TopicDescriptor.AuthOpts, o?: $protobuf.IConversionOptions): { [k: string]: any };
153
+
154
+ /**
155
+ * Converts this AuthOpts to JSON.
156
+ *
157
+ * @returns JSON object
158
+ */
159
+ public toJSON (): { [k: string]: any };
160
+ }
161
+
162
+ namespace AuthOpts {
163
+
164
+ /** AuthMode enum. */
165
+ enum AuthMode {
166
+ NONE = 0,
167
+ KEY = 1,
168
+ WOT = 2
169
+ }
170
+ }
171
+
172
+ /** Properties of an EncOpts. */
173
+ interface IEncOpts {
174
+
175
+ /** EncOpts mode */
176
+ mode?: (TopicDescriptor.EncOpts.EncMode|null)
177
+
178
+ /** EncOpts keyHashes */
179
+ keyHashes?: (Uint8Array[]|null)
180
+ }
181
+
182
+ /** Represents an EncOpts. */
183
+ class EncOpts implements IEncOpts {
184
+ /**
185
+ * Constructs a new EncOpts.
186
+ *
187
+ * @param [p] - Properties to set
188
+ */
189
+ constructor (p?: TopicDescriptor.IEncOpts);
190
+
191
+ /** EncOpts mode. */
192
+ public mode?: (TopicDescriptor.EncOpts.EncMode|null)
193
+
194
+ /** EncOpts keyHashes. */
195
+ public keyHashes: Uint8Array[]
196
+
197
+ /** EncOpts _mode. */
198
+ public _mode?: 'mode'
199
+
200
+ /**
201
+ * Encodes the specified EncOpts message. Does not implicitly {@link TopicDescriptor.EncOpts.verify|verify} messages.
202
+ *
203
+ * @param m - EncOpts message or plain object to encode
204
+ * @param [w] - Writer to encode to
205
+ * @returns Writer
206
+ */
207
+ public static encode (m: TopicDescriptor.IEncOpts, w?: $protobuf.Writer): $protobuf.Writer;
208
+
209
+ /**
210
+ * Decodes an EncOpts message from the specified reader or buffer.
211
+ *
212
+ * @param r - Reader or buffer to decode from
213
+ * @param [l] - Message length if known beforehand
214
+ * @returns EncOpts
215
+ * @throws {Error} If the payload is not a reader or valid buffer
216
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
217
+ */
218
+ public static decode (r: ($protobuf.Reader|Uint8Array), l?: number): TopicDescriptor.EncOpts;
219
+
220
+ /**
221
+ * Creates an EncOpts message from a plain object. Also converts values to their respective internal types.
222
+ *
223
+ * @param d - Plain object
224
+ * @returns EncOpts
225
+ */
226
+ public static fromObject (d: { [k: string]: any }): TopicDescriptor.EncOpts;
227
+
228
+ /**
229
+ * Creates a plain object from an EncOpts message. Also converts values to other types if specified.
230
+ *
231
+ * @param m - EncOpts
232
+ * @param [o] - Conversion options
233
+ * @returns Plain object
234
+ */
235
+ public static toObject (m: TopicDescriptor.EncOpts, o?: $protobuf.IConversionOptions): { [k: string]: any };
236
+
237
+ /**
238
+ * Converts this EncOpts to JSON.
239
+ *
240
+ * @returns JSON object
241
+ */
242
+ public toJSON (): { [k: string]: any };
243
+ }
244
+
245
+ namespace EncOpts {
246
+
247
+ /** EncMode enum. */
248
+ enum EncMode {
249
+ NONE = 0,
250
+ SHAREDKEY = 1,
251
+ WOT = 2
252
+ }
253
+ }
254
+ }