@libp2p/pubsub 1.2.1 → 1.2.5

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 CHANGED
@@ -1,39 +1,41 @@
1
1
  import { randomBytes } from 'iso-random-stream'
2
2
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
- import { PeerId } from '@libp2p/peer-id'
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
+ import { peerIdFromBytes } from '@libp2p/peer-id'
4
5
  import { sha256 } from 'multiformats/hashes/sha2'
6
+ import errcode from 'err-code'
7
+ import { codes } from './errors.js'
5
8
  import type * as RPC from './message/rpc.js'
6
- import type { Message } from '@libp2p/interfaces/pubsub'
9
+ import type { Message, RPCMessage } from '@libp2p/interfaces/pubsub'
10
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
7
11
 
8
12
  /**
9
13
  * Generate a random sequence number
10
14
  */
11
- export const randomSeqno = () => {
12
- return randomBytes(8)
15
+ export function randomSeqno (): BigInt {
16
+ return BigInt(`0x${uint8ArrayToString(randomBytes(8), 'base16')}`)
13
17
  }
14
18
 
15
19
  /**
16
20
  * Generate a message id, based on the `from` and `seqno`
17
21
  */
18
- export const msgId = (from: Uint8Array | string, seqno: Uint8Array) => {
19
- let fromBytes
22
+ export const msgId = (from: PeerId, seqno: BigInt) => {
23
+ const fromBytes = from.multihash.digest
24
+ const seqnoBytes = uint8ArrayFromString(seqno.toString(16).padStart(16, '0'), 'base16')
20
25
 
21
- if (from instanceof Uint8Array) {
22
- fromBytes = PeerId.fromBytes(from).multihash.digest
23
- } else {
24
- fromBytes = PeerId.fromString(from).multihash.digest
25
- }
26
-
27
- const msgId = new Uint8Array(fromBytes.length + seqno.length)
26
+ const msgId = new Uint8Array(fromBytes.length + seqnoBytes.length)
28
27
  msgId.set(fromBytes, 0)
29
- msgId.set(seqno, fromBytes.length)
28
+ msgId.set(seqnoBytes, fromBytes.length)
29
+
30
30
  return msgId
31
31
  }
32
32
 
33
33
  /**
34
34
  * Generate a message id, based on message `data`
35
35
  */
36
- export const noSignMsgId = (data: Uint8Array) => sha256.encode(data)
36
+ export const noSignMsgId = (data: Uint8Array) => {
37
+ return sha256.encode(data)
38
+ }
37
39
 
38
40
  /**
39
41
  * Check if any member of the first set is also a member
@@ -70,24 +72,32 @@ export const ensureArray = function <T> (maybeArray: T | T[]) {
70
72
  /**
71
73
  * Ensures `message.from` is base58 encoded
72
74
  */
73
- export const normalizeInRpcMessage = (message: RPC.RPC.IMessage, peerId?: string) => {
74
- // @ts-expect-error receivedFrom not yet defined
75
- const m: NormalizedIMessage = Object.assign({}, message)
76
-
77
- if (peerId != null) {
78
- m.receivedFrom = peerId
75
+ export const toMessage = (message: RPC.RPC.IMessage): Message => {
76
+ if (message.from == null) {
77
+ throw errcode(new Error('From field is required and was not present'), codes.ERR_MISSING_FROM)
79
78
  }
80
79
 
81
- return m
80
+ return {
81
+ from: peerIdFromBytes(message.from),
82
+ topicIDs: message.topicIDs ?? [],
83
+ seqno: message.seqno == null ? undefined : BigInt(`0x${uint8ArrayToString(message.seqno, 'base16')}`),
84
+ data: message.data ?? new Uint8Array(0),
85
+ signature: message.signature ?? undefined,
86
+ key: message.key ?? undefined
87
+ }
82
88
  }
83
89
 
84
- export const normalizeOutRpcMessage = (message: Message) => {
85
- const m: Message = Object.assign({}, message)
86
- if (typeof message.from === 'string') {
87
- m.from = uint8ArrayFromString(message.from, 'base58btc')
90
+ 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)
88
93
  }
89
- if (typeof message.data === 'string') {
90
- m.data = uint8ArrayFromString(message.data)
94
+
95
+ return {
96
+ from: message.from.multihash.bytes,
97
+ data: message.data,
98
+ seqno: message.seqno == null ? undefined : uint8ArrayFromString(message.seqno.toString(16).padStart(16, '0'), 'base16'),
99
+ topicIDs: message.topicIDs,
100
+ signature: message.signature,
101
+ key: message.key
91
102
  }
92
- return m
93
103
  }