@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/README.md +2 -2
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/constants.d.ts +6 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +6 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/floodsub.d.ts +205 -0
- package/dist/src/floodsub.d.ts.map +1 -0
- package/dist/src/floodsub.js +641 -0
- package/dist/src/floodsub.js.map +1 -0
- package/dist/src/index.d.ts +221 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +37 -88
- package/dist/src/index.js.map +1 -1
- package/dist/src/peer-streams.d.ts +71 -0
- package/dist/src/peer-streams.d.ts.map +1 -0
- package/dist/src/peer-streams.js +154 -0
- package/dist/src/peer-streams.js.map +1 -0
- package/dist/src/sign.d.ts +23 -0
- package/dist/src/sign.d.ts.map +1 -0
- package/dist/src/sign.js +75 -0
- package/dist/src/sign.js.map +1 -0
- package/dist/src/utils.d.ts +29 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +129 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +24 -15
- package/src/constants.ts +5 -0
- package/src/floodsub.ts +794 -0
- package/src/index.ts +225 -97
- package/src/peer-streams.ts +200 -0
- package/src/sign.ts +93 -0
- package/src/utils.ts +157 -0
- package/dist/src/config.d.ts +0 -2
- package/dist/src/config.d.ts.map +0 -1
- package/dist/src/config.js +0 -2
- package/dist/src/config.js.map +0 -1
- package/dist/typedoc-urls.json +0 -9
- package/src/config.ts +0 -1
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
|
+
}
|
package/dist/src/config.d.ts
DELETED
package/dist/src/config.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,oBAAoB,CAAA"}
|
package/dist/src/config.js
DELETED
package/dist/src/config.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,CAAA"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -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'
|