@libp2p/floodsub 10.1.45 → 10.1.46-6059227cb

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/src/utils.ts ADDED
@@ -0,0 +1,157 @@
1
+ import { randomBytes } from '@libp2p/crypto'
2
+ import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
3
+ import { InvalidMessageError } from '@libp2p/interface'
4
+ import { peerIdFromMultihash, peerIdFromPublicKey } from '@libp2p/peer-id'
5
+ import * as Digest from 'multiformats/hashes/digest'
6
+ import { sha256 } from 'multiformats/hashes/sha2'
7
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
9
+ import type { PubSubRPCMessage } from './floodsub.ts'
10
+ import type { Message } from './index.ts'
11
+ import type { PublicKey } from '@libp2p/interface'
12
+
13
+ /**
14
+ * Generate a random sequence number
15
+ */
16
+ export function randomSeqno (): bigint {
17
+ return BigInt(`0x${uint8ArrayToString(randomBytes(8), 'base16')}`)
18
+ }
19
+
20
+ /**
21
+ * Generate a message id, based on the `key` and `seqno`
22
+ */
23
+ export const msgId = (key: PublicKey, seqno: bigint): Uint8Array => {
24
+ const seqnoBytes = uint8ArrayFromString(seqno.toString(16).padStart(16, '0'), 'base16')
25
+ const keyBytes = publicKeyToProtobuf(key)
26
+
27
+ const msgId = new Uint8Array(keyBytes.byteLength + seqnoBytes.length)
28
+ msgId.set(keyBytes, 0)
29
+ msgId.set(seqnoBytes, keyBytes.byteLength)
30
+
31
+ return msgId
32
+ }
33
+
34
+ /**
35
+ * Generate a message id, based on message `data`
36
+ */
37
+ export const noSignMsgId = (data: Uint8Array): Uint8Array | Promise<Uint8Array> => {
38
+ return sha256.encode(data)
39
+ }
40
+
41
+ /**
42
+ * Check if any member of the first set is also a member
43
+ * of the second set
44
+ */
45
+ export const anyMatch = (a: Set<number> | number[], b: Set<number> | number[]): boolean => {
46
+ let bHas
47
+ if (Array.isArray(b)) {
48
+ bHas = (val: number) => b.includes(val)
49
+ } else {
50
+ bHas = (val: number) => b.has(val)
51
+ }
52
+
53
+ for (const val of a) {
54
+ if (bHas(val)) {
55
+ return true
56
+ }
57
+ }
58
+
59
+ return false
60
+ }
61
+
62
+ /**
63
+ * Make everything an array
64
+ */
65
+ export const ensureArray = function <T> (maybeArray: T | T[]): T[] {
66
+ if (!Array.isArray(maybeArray)) {
67
+ return [maybeArray]
68
+ }
69
+
70
+ return maybeArray
71
+ }
72
+
73
+ const isSigned = async (message: PubSubRPCMessage): Promise<boolean> => {
74
+ if ((message.sequenceNumber == null) || (message.from == null) || (message.signature == null)) {
75
+ return false
76
+ }
77
+ // if a public key is present in the `from` field, the message should be signed
78
+ const fromID = peerIdFromMultihash(Digest.decode(message.from))
79
+ if (fromID.publicKey != null) {
80
+ return true
81
+ }
82
+
83
+ if (message.key != null) {
84
+ const signingKey = message.key
85
+ const signingID = peerIdFromPublicKey(publicKeyFromProtobuf(signingKey))
86
+
87
+ return signingID.equals(fromID)
88
+ }
89
+
90
+ return false
91
+ }
92
+
93
+ export const toMessage = async (message: PubSubRPCMessage): Promise<Message> => {
94
+ if (message.from == null) {
95
+ throw new InvalidMessageError('RPC message was missing from')
96
+ }
97
+
98
+ if (!await isSigned(message)) {
99
+ return {
100
+ type: 'unsigned',
101
+ topic: message.topic ?? '',
102
+ data: message.data ?? new Uint8Array(0)
103
+ }
104
+ }
105
+
106
+ const from = peerIdFromMultihash(Digest.decode(message.from))
107
+ const key = message.key ?? from.publicKey
108
+
109
+ if (key == null) {
110
+ throw new InvalidMessageError('RPC message was missing public key')
111
+ }
112
+
113
+ const msg: Message = {
114
+ type: 'signed',
115
+ from,
116
+ topic: message.topic ?? '',
117
+ sequenceNumber: bigIntFromBytes(message.sequenceNumber ?? new Uint8Array(0)),
118
+ data: message.data ?? new Uint8Array(0),
119
+ signature: message.signature ?? new Uint8Array(0),
120
+ key: key instanceof Uint8Array ? publicKeyFromProtobuf(key) : key
121
+ }
122
+
123
+ return msg
124
+ }
125
+
126
+ export const toRpcMessage = (message: Message): PubSubRPCMessage => {
127
+ if (message.type === 'signed') {
128
+ return {
129
+ from: message.from.toMultihash().bytes,
130
+ data: message.data,
131
+ sequenceNumber: bigIntToBytes(message.sequenceNumber),
132
+ topic: message.topic,
133
+ signature: message.signature,
134
+
135
+ key: message.key ? publicKeyToProtobuf(message.key) : undefined
136
+ }
137
+ }
138
+
139
+ return {
140
+ data: message.data,
141
+ topic: message.topic
142
+ }
143
+ }
144
+
145
+ export const bigIntToBytes = (num: bigint): Uint8Array => {
146
+ let str = num.toString(16)
147
+
148
+ if (str.length % 2 !== 0) {
149
+ str = `0${str}`
150
+ }
151
+
152
+ return uint8ArrayFromString(str, 'base16')
153
+ }
154
+
155
+ export const bigIntFromBytes = (num: Uint8Array): bigint => {
156
+ return BigInt(`0x${uint8ArrayToString(num, 'base16')}`)
157
+ }
@@ -1,2 +0,0 @@
1
- export declare const multicodec = "/floodsub/1.0.0";
2
- //# sourceMappingURL=config.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,oBAAoB,CAAA"}
@@ -1,2 +0,0 @@
1
- export const multicodec = '/floodsub/1.0.0';
2
- //# sourceMappingURL=config.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,CAAA"}
@@ -1,9 +0,0 @@
1
- {
2
- "FloodSubComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_floodsub.FloodSubComponents.html",
3
- ".:FloodSubComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_floodsub.FloodSubComponents.html",
4
- "FloodSubInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_floodsub.FloodSubInit.html",
5
- ".:FloodSubInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_floodsub.FloodSubInit.html",
6
- "multicodec": "https://libp2p.github.io/js-libp2p/variables/_libp2p_floodsub.multicodec.html",
7
- "floodsub": "https://libp2p.github.io/js-libp2p/functions/_libp2p_floodsub.floodsub.html",
8
- ".:floodsub": "https://libp2p.github.io/js-libp2p/functions/_libp2p_floodsub.floodsub.html"
9
- }
package/src/config.ts DELETED
@@ -1 +0,0 @@
1
- export const multicodec = '/floodsub/1.0.0'