@libp2p/pubsub 1.2.6 → 1.2.9
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.
- package/dist/src/errors.d.ts +4 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +4 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.d.ts +24 -22
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +141 -99
- package/dist/src/index.js.map +1 -1
- package/dist/src/message/rpc.d.ts +586 -175
- package/dist/src/message/rpc.js +1238 -67
- package/dist/src/message/sign.d.ts +1 -1
- package/dist/src/message/sign.d.ts.map +1 -1
- package/dist/src/message/sign.js +4 -8
- package/dist/src/message/sign.js.map +1 -1
- package/dist/src/peer-streams.d.ts +2 -2
- package/dist/src/peer-streams.d.ts.map +1 -1
- package/dist/src/peer-streams.js +3 -3
- package/dist/src/peer-streams.js.map +1 -1
- package/dist/src/utils.d.ts +2 -6
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +10 -17
- package/dist/src/utils.js.map +1 -1
- package/package.json +14 -18
- package/src/errors.ts +4 -0
- package/src/index.ts +171 -106
- package/src/message/rpc.d.ts +586 -175
- package/src/message/rpc.js +1238 -67
- package/src/message/rpc.proto +38 -4
- package/src/message/sign.ts +6 -9
- package/src/peer-streams.ts +4 -4
- package/src/utils.ts +11 -20
package/src/message/rpc.proto
CHANGED
@@ -2,19 +2,53 @@ syntax = "proto3";
|
|
2
2
|
|
3
3
|
message RPC {
|
4
4
|
repeated SubOpts subscriptions = 1;
|
5
|
-
repeated Message
|
5
|
+
repeated Message messages = 2;
|
6
|
+
optional ControlMessage control = 3;
|
6
7
|
|
7
8
|
message SubOpts {
|
8
9
|
optional bool subscribe = 1; // subscribe or unsubcribe
|
9
|
-
optional string
|
10
|
+
optional string topic = 2;
|
10
11
|
}
|
11
12
|
|
12
13
|
message Message {
|
13
14
|
optional bytes from = 1;
|
14
15
|
optional bytes data = 2;
|
15
16
|
optional bytes seqno = 3;
|
16
|
-
|
17
|
+
optional string topic = 4;
|
17
18
|
optional bytes signature = 5;
|
18
19
|
optional bytes key = 6;
|
19
20
|
}
|
20
|
-
}
|
21
|
+
}
|
22
|
+
|
23
|
+
message ControlMessage {
|
24
|
+
repeated ControlIHave ihave = 1;
|
25
|
+
repeated ControlIWant iwant = 2;
|
26
|
+
repeated ControlGraft graft = 3;
|
27
|
+
repeated ControlPrune prune = 4;
|
28
|
+
}
|
29
|
+
|
30
|
+
message ControlIHave {
|
31
|
+
optional string topicID = 1;
|
32
|
+
// implementors from other languages should use bytes here - go protobuf emits invalid utf8 strings
|
33
|
+
repeated string messageIDs = 2;
|
34
|
+
}
|
35
|
+
|
36
|
+
message ControlIWant {
|
37
|
+
// implementors from other languages should use bytes here - go protobuf emits invalid utf8 strings
|
38
|
+
repeated string messageIDs = 1;
|
39
|
+
}
|
40
|
+
|
41
|
+
message ControlGraft {
|
42
|
+
optional string topicID = 1;
|
43
|
+
}
|
44
|
+
|
45
|
+
message ControlPrune {
|
46
|
+
optional string topicID = 1;
|
47
|
+
repeated PeerInfo peers = 2;
|
48
|
+
optional uint64 backoff = 3;
|
49
|
+
}
|
50
|
+
|
51
|
+
message PeerInfo {
|
52
|
+
optional bytes peerID = 1;
|
53
|
+
optional bytes signedPeerRecord = 2;
|
54
|
+
}
|
package/src/message/sign.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import * as PeerIdFactory from '@libp2p/peer-id-factory'
|
2
1
|
import { RPC } from './rpc.js'
|
3
2
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
4
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
@@ -6,6 +5,7 @@ import { toRpcMessage } from '../utils.js'
|
|
6
5
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
7
6
|
import { keys } from '@libp2p/crypto'
|
8
7
|
import type { Message } from '@libp2p/interfaces/pubsub'
|
8
|
+
import { peerIdFromKeys } from '@libp2p/peer-id'
|
9
9
|
|
10
10
|
export const SignPrefix = uint8ArrayFromString('libp2p-pubsub:')
|
11
11
|
|
@@ -71,7 +71,7 @@ export async function verifySignature (message: Message) {
|
|
71
71
|
|
72
72
|
/**
|
73
73
|
* Returns the PublicKey associated with the given message.
|
74
|
-
* If no
|
74
|
+
* If no valid PublicKey can be retrieved an error will be returned.
|
75
75
|
*/
|
76
76
|
export async function messagePublicKey (message: Message) {
|
77
77
|
// should be available in the from property of the message (peer id)
|
@@ -80,17 +80,14 @@ export async function messagePublicKey (message: Message) {
|
|
80
80
|
}
|
81
81
|
|
82
82
|
if (message.key != null) {
|
83
|
-
const keyPeerId = await
|
84
|
-
|
85
|
-
// the key belongs to the sender, return the key
|
86
|
-
if (!keyPeerId.equals(message.from)) {
|
87
|
-
throw new Error('Public Key does not match the originator')
|
88
|
-
}
|
83
|
+
const keyPeerId = await peerIdFromKeys(message.key)
|
89
84
|
|
90
85
|
if (keyPeerId.publicKey != null) {
|
91
86
|
return keyPeerId.publicKey
|
92
87
|
}
|
93
|
-
}
|
88
|
+
}
|
89
|
+
|
90
|
+
if (message.from.publicKey != null) {
|
94
91
|
return message.from.publicKey
|
95
92
|
}
|
96
93
|
|
package/src/peer-streams.ts
CHANGED
@@ -11,7 +11,7 @@ import type { PeerStreamEvents } from '@libp2p/interfaces/pubsub'
|
|
11
11
|
|
12
12
|
const log = logger('libp2p-pubsub:peer-streams')
|
13
13
|
|
14
|
-
export interface
|
14
|
+
export interface PeerStreamsInit {
|
15
15
|
id: PeerId
|
16
16
|
protocol: string
|
17
17
|
}
|
@@ -44,11 +44,11 @@ export class PeerStreams extends EventEmitter<PeerStreamEvents> {
|
|
44
44
|
private readonly _inboundAbortController: AbortController
|
45
45
|
private closed: boolean
|
46
46
|
|
47
|
-
constructor (
|
47
|
+
constructor (init: PeerStreamsInit) {
|
48
48
|
super()
|
49
49
|
|
50
|
-
this.id =
|
51
|
-
this.protocol =
|
50
|
+
this.id = init.id
|
51
|
+
this.protocol = init.protocol
|
52
52
|
|
53
53
|
this._inboundAbortController = new AbortController()
|
54
54
|
this.closed = false
|
package/src/utils.ts
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
import { randomBytes } from 'iso-random-stream'
|
2
2
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
3
3
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
4
|
-
import { peerIdFromBytes } from '@libp2p/peer-id'
|
5
4
|
import { sha256 } from 'multiformats/hashes/sha2'
|
6
|
-
import errcode from 'err-code'
|
7
|
-
import { codes } from './errors.js'
|
8
5
|
import type * as RPC from './message/rpc.js'
|
9
6
|
import type { Message, RPCMessage } from '@libp2p/interfaces/pubsub'
|
10
|
-
import
|
7
|
+
import { peerIdFromBytes } from '@libp2p/peer-id'
|
8
|
+
import { codes } from './errors.js'
|
9
|
+
import errcode from 'err-code'
|
11
10
|
|
12
11
|
/**
|
13
12
|
* Generate a random sequence number
|
@@ -17,15 +16,14 @@ export function randomSeqno (): BigInt {
|
|
17
16
|
}
|
18
17
|
|
19
18
|
/**
|
20
|
-
* Generate a message id, based on the `
|
19
|
+
* Generate a message id, based on the `key` and `seqno`
|
21
20
|
*/
|
22
|
-
export const msgId = (
|
23
|
-
const fromBytes = from.multihash.digest
|
21
|
+
export const msgId = (key: Uint8Array, seqno: BigInt) => {
|
24
22
|
const seqnoBytes = uint8ArrayFromString(seqno.toString(16).padStart(16, '0'), 'base16')
|
25
23
|
|
26
|
-
const msgId = new Uint8Array(
|
27
|
-
msgId.set(
|
28
|
-
msgId.set(seqnoBytes,
|
24
|
+
const msgId = new Uint8Array(key.length + seqnoBytes.length)
|
25
|
+
msgId.set(key, 0)
|
26
|
+
msgId.set(seqnoBytes, key.length)
|
29
27
|
|
30
28
|
return msgId
|
31
29
|
}
|
@@ -69,17 +67,14 @@ export const ensureArray = function <T> (maybeArray: T | T[]) {
|
|
69
67
|
return maybeArray
|
70
68
|
}
|
71
69
|
|
72
|
-
/**
|
73
|
-
* Ensures `message.from` is base58 encoded
|
74
|
-
*/
|
75
70
|
export const toMessage = (message: RPC.RPC.IMessage): Message => {
|
76
71
|
if (message.from == null) {
|
77
|
-
throw errcode(new Error('
|
72
|
+
throw errcode(new Error('RPC message was missing from'), codes.ERR_MISSING_FROM)
|
78
73
|
}
|
79
74
|
|
80
75
|
return {
|
81
76
|
from: peerIdFromBytes(message.from),
|
82
|
-
|
77
|
+
topic: message.topic ?? '',
|
83
78
|
seqno: message.seqno == null ? undefined : BigInt(`0x${uint8ArrayToString(message.seqno, 'base16')}`),
|
84
79
|
data: message.data ?? new Uint8Array(0),
|
85
80
|
signature: message.signature ?? undefined,
|
@@ -88,15 +83,11 @@ export const toMessage = (message: RPC.RPC.IMessage): Message => {
|
|
88
83
|
}
|
89
84
|
|
90
85
|
export const toRpcMessage = (message: Message): RPCMessage => {
|
91
|
-
if (message.from == null) {
|
92
|
-
throw errcode(new Error('From field is required and was not present'), codes.ERR_MISSING_FROM)
|
93
|
-
}
|
94
|
-
|
95
86
|
return {
|
96
87
|
from: message.from.multihash.bytes,
|
97
88
|
data: message.data,
|
98
89
|
seqno: message.seqno == null ? undefined : uint8ArrayFromString(message.seqno.toString(16).padStart(16, '0'), 'base16'),
|
99
|
-
|
90
|
+
topic: message.topic,
|
100
91
|
signature: message.signature,
|
101
92
|
key: message.key
|
102
93
|
}
|