libp2p 0.36.2-8cca8e4 → 0.36.2-fab4f13

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 (106) hide show
  1. package/dist/src/circuit/circuit/hop.d.ts +3 -3
  2. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  3. package/dist/src/circuit/circuit/hop.js +1 -1
  4. package/dist/src/circuit/circuit/hop.js.map +1 -1
  5. package/dist/src/circuit/circuit/stop.d.ts +3 -3
  6. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  7. package/dist/src/circuit/circuit/stop.js.map +1 -1
  8. package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
  9. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  10. package/dist/src/circuit/circuit/stream-handler.js +2 -3
  11. package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
  12. package/dist/src/circuit/circuit/utils.d.ts +2 -2
  13. package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
  14. package/dist/src/circuit/circuit/utils.js.map +1 -1
  15. package/dist/src/circuit/pb/index.d.ts +42 -164
  16. package/dist/src/circuit/pb/index.d.ts.map +1 -0
  17. package/dist/src/circuit/pb/index.js +67 -523
  18. package/dist/src/circuit/pb/index.js.map +1 -0
  19. package/dist/src/config.js +2 -2
  20. package/dist/src/config.js.map +1 -1
  21. package/dist/src/dht/dummy-dht.d.ts +17 -0
  22. package/dist/src/dht/dummy-dht.d.ts.map +1 -0
  23. package/dist/src/dht/dummy-dht.js +39 -0
  24. package/dist/src/dht/dummy-dht.js.map +1 -0
  25. package/dist/src/dialer/index.d.ts.map +1 -1
  26. package/dist/src/dialer/index.js +0 -1
  27. package/dist/src/dialer/index.js.map +1 -1
  28. package/dist/src/errors.d.ts +2 -0
  29. package/dist/src/errors.d.ts.map +1 -1
  30. package/dist/src/errors.js +2 -0
  31. package/dist/src/errors.js.map +1 -1
  32. package/dist/src/fetch/index.d.ts.map +1 -1
  33. package/dist/src/fetch/index.js +5 -6
  34. package/dist/src/fetch/index.js.map +1 -1
  35. package/dist/src/fetch/pb/proto.d.ts +20 -128
  36. package/dist/src/fetch/pb/proto.d.ts.map +1 -0
  37. package/dist/src/fetch/pb/proto.js +44 -331
  38. package/dist/src/fetch/pb/proto.js.map +1 -0
  39. package/dist/src/identify/index.d.ts +4 -2
  40. package/dist/src/identify/index.d.ts.map +1 -1
  41. package/dist/src/identify/index.js +8 -8
  42. package/dist/src/identify/index.js.map +1 -1
  43. package/dist/src/identify/pb/message.d.ts +13 -108
  44. package/dist/src/identify/pb/message.d.ts.map +1 -0
  45. package/dist/src/identify/pb/message.js +21 -366
  46. package/dist/src/identify/pb/message.js.map +1 -0
  47. package/dist/src/index.d.ts +13 -6
  48. package/dist/src/index.d.ts.map +1 -1
  49. package/dist/src/index.js.map +1 -1
  50. package/dist/src/insecure/index.d.ts.map +1 -1
  51. package/dist/src/insecure/index.js +5 -2
  52. package/dist/src/insecure/index.js.map +1 -1
  53. package/dist/src/insecure/pb/proto.d.ts +24 -130
  54. package/dist/src/insecure/pb/proto.d.ts.map +1 -0
  55. package/dist/src/insecure/pb/proto.js +46 -388
  56. package/dist/src/insecure/pb/proto.js.map +1 -0
  57. package/dist/src/keychain/index.d.ts +3 -3
  58. package/dist/src/keychain/index.js +2 -2
  59. package/dist/src/keychain/util.d.ts +1 -1
  60. package/dist/src/keychain/util.d.ts.map +1 -1
  61. package/dist/src/libp2p.d.ts +7 -4
  62. package/dist/src/libp2p.d.ts.map +1 -1
  63. package/dist/src/libp2p.js +20 -5
  64. package/dist/src/libp2p.js.map +1 -1
  65. package/dist/src/metrics/stats.d.ts +1 -5
  66. package/dist/src/metrics/stats.d.ts.map +1 -1
  67. package/dist/src/metrics/stats.js.map +1 -1
  68. package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
  69. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
  70. package/dist/src/pubsub/dummy-pubsub.js +37 -0
  71. package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
  72. package/dist/src/transport-manager.d.ts +2 -2
  73. package/dist/src/transport-manager.d.ts.map +1 -1
  74. package/dist/src/transport-manager.js +8 -8
  75. package/dist/src/transport-manager.js.map +1 -1
  76. package/package.json +48 -44
  77. package/src/circuit/circuit/hop.ts +4 -4
  78. package/src/circuit/circuit/stop.ts +3 -3
  79. package/src/circuit/circuit/stream-handler.ts +5 -6
  80. package/src/circuit/circuit/utils.ts +2 -2
  81. package/src/circuit/pb/index.proto +1 -1
  82. package/src/circuit/pb/index.ts +88 -0
  83. package/src/config.ts +2 -2
  84. package/src/dht/dummy-dht.ts +51 -0
  85. package/src/dialer/index.ts +0 -3
  86. package/src/errors.ts +2 -0
  87. package/src/fetch/index.ts +6 -7
  88. package/src/fetch/pb/proto.ts +58 -0
  89. package/src/identify/index.ts +10 -10
  90. package/src/identify/pb/message.ts +36 -0
  91. package/src/index.ts +14 -7
  92. package/src/insecure/index.ts +8 -4
  93. package/src/insecure/pb/proto.ts +61 -0
  94. package/src/keychain/index.ts +2 -2
  95. package/src/libp2p.ts +27 -9
  96. package/src/metrics/stats.ts +1 -6
  97. package/src/pubsub/dummy-pubsub.ts +51 -0
  98. package/src/transport-manager.ts +6 -6
  99. package/src/circuit/pb/index.d.ts +0 -173
  100. package/src/circuit/pb/index.js +0 -528
  101. package/src/fetch/pb/proto.d.ts +0 -134
  102. package/src/fetch/pb/proto.js +0 -331
  103. package/src/identify/pb/message.d.ts +0 -110
  104. package/src/identify/pb/message.js +0 -369
  105. package/src/insecure/pb/proto.d.ts +0 -134
  106. package/src/insecure/pb/proto.js +0 -388
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import errCode from 'err-code'
3
3
  import { validateAddrs } from './utils.js'
4
4
  import { StreamHandler } from './stream-handler.js'
5
- import { CircuitRelay as CircuitPB, ICircuitRelay } from '../pb/index.js'
5
+ import { CircuitRelay as CircuitPB } from '../pb/index.js'
6
6
  import { pipe } from 'it-pipe'
7
7
  import { codes as Errors } from '../../errors.js'
8
8
  import { stop } from './stop.js'
@@ -17,7 +17,7 @@ const log = logger('libp2p:circuit:hop')
17
17
 
18
18
  export interface HopRequest {
19
19
  connection: Connection
20
- request: ICircuitRelay
20
+ request: CircuitPB
21
21
  streamHandler: StreamHandler
22
22
  circuit: Circuit
23
23
  connectionManager: ConnectionManager
@@ -120,7 +120,7 @@ export async function handleHop (hopRequest: HopRequest) {
120
120
 
121
121
  export interface HopConfig {
122
122
  connection: Connection
123
- request: ICircuitRelay
123
+ request: CircuitPB
124
124
  }
125
125
 
126
126
  /**
@@ -153,7 +153,7 @@ export async function hop (options: HopConfig): Promise<Duplex<Uint8Array>> {
153
153
  log('hop request failed with code %d, closing stream', response.code)
154
154
  streamHandler.close()
155
155
 
156
- throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
156
+ throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED)
157
157
  }
158
158
 
159
159
  export interface CanHopOptions {
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import { CircuitRelay as CircuitPB, ICircuitRelay } from '../pb/index.js'
2
+ import { CircuitRelay as CircuitPB } from '../pb/index.js'
3
3
  import { RELAY_CODEC } from '../multicodec.js'
4
4
  import { StreamHandler } from './stream-handler.js'
5
5
  import { validateAddrs } from './utils.js'
@@ -10,7 +10,7 @@ const log = logger('libp2p:circuit:stop')
10
10
 
11
11
  export interface HandleStopOptions {
12
12
  connection: Connection
13
- request: ICircuitRelay
13
+ request: CircuitPB
14
14
  streamHandler: StreamHandler
15
15
  }
16
16
 
@@ -44,7 +44,7 @@ export function handleStop (options: HandleStopOptions): Duplex<Uint8Array> | un
44
44
 
45
45
  export interface StopOptions {
46
46
  connection: Connection
47
- request: ICircuitRelay
47
+ request: CircuitPB
48
48
  }
49
49
 
50
50
  /**
@@ -1,7 +1,7 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import * as lp from 'it-length-prefixed'
3
3
  import { Handshake, handshake } from 'it-handshake'
4
- import { CircuitRelay, ICircuitRelay } from '../pb/index.js'
4
+ import { CircuitRelay } from '../pb/index.js'
5
5
  import type { Stream } from '@libp2p/interfaces/connection'
6
6
  import type { Source } from 'it-stream-types'
7
7
 
@@ -53,10 +53,9 @@ export class StreamHandler {
53
53
  /**
54
54
  * Encode and write array of buffers
55
55
  */
56
- write (msg: ICircuitRelay) {
56
+ write (msg: CircuitRelay) {
57
57
  log('write message type %s', msg.type)
58
- // @ts-expect-error lp.encode expects type type 'Buffer | BufferList', not 'Uint8Array'
59
- this.shake.write(lp.encode.single(CircuitRelay.encode(msg).finish()))
58
+ this.shake.write(lp.encode.single(CircuitRelay.encode(msg)).slice())
60
59
  }
61
60
 
62
61
  /**
@@ -68,9 +67,9 @@ export class StreamHandler {
68
67
  }
69
68
 
70
69
  /**
71
- * @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
70
+ * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
72
71
  */
73
- end (msg: ICircuitRelay) {
72
+ end (msg: CircuitRelay) {
74
73
  this.write(msg)
75
74
  this.close()
76
75
  }
@@ -1,5 +1,5 @@
1
1
  import { Multiaddr } from '@multiformats/multiaddr'
2
- import { CircuitRelay, ICircuitRelay } from '../pb/index.js'
2
+ import { CircuitRelay } from '../pb/index.js'
3
3
  import type { StreamHandler } from './stream-handler.js'
4
4
 
5
5
  /**
@@ -15,7 +15,7 @@ function writeResponse (streamHandler: StreamHandler, status: CircuitRelay.Statu
15
15
  /**
16
16
  * Validate incomming HOP/STOP message
17
17
  */
18
- export function validateAddrs (msg: ICircuitRelay, streamHandler: StreamHandler) {
18
+ export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler) {
19
19
  try {
20
20
  if (msg.dstPeer?.addrs != null) {
21
21
  msg.dstPeer.addrs.forEach((addr) => {
@@ -1,4 +1,4 @@
1
- syntax = "proto2";
1
+ syntax = "proto3";
2
2
 
3
3
  message CircuitRelay {
4
4
 
@@ -0,0 +1,88 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { enumeration, encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
5
+
6
+ export interface CircuitRelay {
7
+ type?: CircuitRelay.Type
8
+ srcPeer?: CircuitRelay.Peer
9
+ dstPeer?: CircuitRelay.Peer
10
+ code?: CircuitRelay.Status
11
+ }
12
+
13
+ export namespace CircuitRelay {
14
+ export enum Status {
15
+ SUCCESS = 'SUCCESS',
16
+ HOP_SRC_ADDR_TOO_LONG = 'HOP_SRC_ADDR_TOO_LONG',
17
+ HOP_DST_ADDR_TOO_LONG = 'HOP_DST_ADDR_TOO_LONG',
18
+ HOP_SRC_MULTIADDR_INVALID = 'HOP_SRC_MULTIADDR_INVALID',
19
+ HOP_DST_MULTIADDR_INVALID = 'HOP_DST_MULTIADDR_INVALID',
20
+ HOP_NO_CONN_TO_DST = 'HOP_NO_CONN_TO_DST',
21
+ HOP_CANT_DIAL_DST = 'HOP_CANT_DIAL_DST',
22
+ HOP_CANT_OPEN_DST_STREAM = 'HOP_CANT_OPEN_DST_STREAM',
23
+ HOP_CANT_SPEAK_RELAY = 'HOP_CANT_SPEAK_RELAY',
24
+ HOP_CANT_RELAY_TO_SELF = 'HOP_CANT_RELAY_TO_SELF',
25
+ STOP_SRC_ADDR_TOO_LONG = 'STOP_SRC_ADDR_TOO_LONG',
26
+ STOP_DST_ADDR_TOO_LONG = 'STOP_DST_ADDR_TOO_LONG',
27
+ STOP_SRC_MULTIADDR_INVALID = 'STOP_SRC_MULTIADDR_INVALID',
28
+ STOP_DST_MULTIADDR_INVALID = 'STOP_DST_MULTIADDR_INVALID',
29
+ STOP_RELAY_REFUSED = 'STOP_RELAY_REFUSED',
30
+ MALFORMED_MESSAGE = 'MALFORMED_MESSAGE'
31
+ }
32
+
33
+ export namespace Status {
34
+ export const codec = () => {
35
+ return enumeration<typeof Status>(Status)
36
+ }
37
+ }
38
+ export enum Type {
39
+ HOP = 'HOP',
40
+ STOP = 'STOP',
41
+ STATUS = 'STATUS',
42
+ CAN_HOP = 'CAN_HOP'
43
+ }
44
+
45
+ export namespace Type {
46
+ export const codec = () => {
47
+ return enumeration<typeof Type>(Type)
48
+ }
49
+ }
50
+ export interface Peer {
51
+ id: Uint8Array
52
+ addrs: Uint8Array[]
53
+ }
54
+
55
+ export namespace Peer {
56
+ export const codec = () => {
57
+ return message<Peer>({
58
+ 1: { name: 'id', codec: bytes },
59
+ 2: { name: 'addrs', codec: bytes, repeats: true }
60
+ })
61
+ }
62
+
63
+ export const encode = (obj: Peer): Uint8Array => {
64
+ return encodeMessage(obj, Peer.codec())
65
+ }
66
+
67
+ export const decode = (buf: Uint8Array): Peer => {
68
+ return decodeMessage(buf, Peer.codec())
69
+ }
70
+ }
71
+
72
+ export const codec = () => {
73
+ return message<CircuitRelay>({
74
+ 1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
75
+ 2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
76
+ 3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true },
77
+ 4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true }
78
+ })
79
+ }
80
+
81
+ export const encode = (obj: CircuitRelay): Uint8Array => {
82
+ return encodeMessage(obj, CircuitRelay.codec())
83
+ }
84
+
85
+ export const decode = (buf: Uint8Array): CircuitRelay => {
86
+ return decodeMessage(buf, CircuitRelay.codec())
87
+ }
88
+ }
package/src/config.ts CHANGED
@@ -4,7 +4,7 @@ import * as Constants from './constants.js'
4
4
  import { AGENT_VERSION } from './identify/consts.js'
5
5
  import * as RelayConstants from './circuit/constants.js'
6
6
  import { publicAddressesFirst } from '@libp2p/utils/address-sort'
7
- import { FAULT_TOLERANCE } from './transport-manager.js'
7
+ import { FaultTolerance } from './transport-manager.js'
8
8
  import type { Multiaddr } from '@multiformats/multiaddr'
9
9
  import type { Libp2pInit } from './index.js'
10
10
  import { codes, messages } from './errors.js'
@@ -26,7 +26,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
26
26
  },
27
27
  connectionGater: {},
28
28
  transportManager: {
29
- faultTolerance: FAULT_TOLERANCE.FATAL_ALL
29
+ faultTolerance: FaultTolerance.FATAL_ALL
30
30
  },
31
31
  dialer: {
32
32
  maxParallelDials: Constants.MAX_PARALLEL_DIALS,
@@ -0,0 +1,51 @@
1
+ import type { DualDHT, QueryEvent, SingleDHT } from '@libp2p/interfaces/dht'
2
+ import type { PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
3
+ import errCode from 'err-code'
4
+ import { messages, codes } from '../errors.js'
5
+ import { EventEmitter } from '@libp2p/interfaces'
6
+
7
+ export class DummyDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
8
+ get wan (): SingleDHT {
9
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
10
+ }
11
+
12
+ get lan (): SingleDHT {
13
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
14
+ }
15
+
16
+ get (): AsyncIterable<QueryEvent> {
17
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
18
+ }
19
+
20
+ findProviders (): AsyncIterable<QueryEvent> {
21
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
22
+ }
23
+
24
+ findPeer (): AsyncIterable<QueryEvent> {
25
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
26
+ }
27
+
28
+ getClosestPeers (): AsyncIterable<QueryEvent> {
29
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
30
+ }
31
+
32
+ provide (): AsyncIterable<QueryEvent> {
33
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
34
+ }
35
+
36
+ put (): AsyncIterable<QueryEvent> {
37
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
38
+ }
39
+
40
+ async getMode (): Promise<'client' | 'server'> {
41
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
42
+ }
43
+
44
+ async setMode (): Promise<void> {
45
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
46
+ }
47
+
48
+ async refreshRoutingTable (): Promise<void> {
49
+ throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
50
+ }
51
+ }
@@ -244,9 +244,6 @@ export class DefaultDialer implements Dialer, Startable {
244
244
  const addrs: Multiaddr[] = []
245
245
  for (const a of knownAddrs) {
246
246
  const resolvedAddrs = await this._resolve(a)
247
-
248
- log('resolved %s to %s', a, resolvedAddrs)
249
-
250
247
  resolvedAddrs.forEach(ra => addrs.push(ra))
251
248
  }
252
249
 
package/src/errors.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export enum messages {
2
2
  NOT_STARTED_YET = 'The libp2p node is not started yet',
3
3
  DHT_DISABLED = 'DHT is not available',
4
+ PUBSUB_DISABLED = 'PubSub is not available',
4
5
  CONN_ENCRYPTION_REQUIRED = 'At least one connection encryption module is required',
5
6
  ERR_TRANSPORTS_REQUIRED = 'At least one transport module is required',
6
7
  ERR_PROTECTOR_REQUIRED = 'Private network is enforced, but no protector was provided',
@@ -9,6 +10,7 @@ export enum messages {
9
10
 
10
11
  export enum codes {
11
12
  DHT_DISABLED = 'ERR_DHT_DISABLED',
13
+ ERR_PUBSUB_DISABLED = 'ERR_PUBSUB_DISABLED',
12
14
  PUBSUB_NOT_STARTED = 'ERR_PUBSUB_NOT_STARTED',
13
15
  DHT_NOT_STARTED = 'ERR_DHT_NOT_STARTED',
14
16
  CONN_ENCRYPTION_REQUIRED = 'ERR_CONN_ENCRYPTION_REQUIRED',
@@ -75,8 +75,7 @@ export class FetchService implements Startable {
75
75
  const shake = handshake(stream)
76
76
 
77
77
  // send message
78
- const request = new FetchRequest({ identifier: key })
79
- shake.write(lp.encode.single(FetchRequest.encode(request).finish()).slice())
78
+ shake.write(lp.encode.single(FetchRequest.encode({ identifier: key })).slice())
80
79
 
81
80
  // read response
82
81
  // @ts-expect-error fromReader returns a Source which has no .next method
@@ -109,21 +108,21 @@ export class FetchService implements Startable {
109
108
  // @ts-expect-error fromReader returns a Source which has no .next method
110
109
  const request = FetchRequest.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
111
110
 
112
- let response
111
+ let response: FetchResponse
113
112
  const lookup = this._getLookupFunction(request.identifier)
114
113
  if (lookup != null) {
115
114
  const data = await lookup(request.identifier)
116
115
  if (data != null) {
117
- response = new FetchResponse({ status: FetchResponse.StatusCode.OK, data })
116
+ response = { status: FetchResponse.StatusCode.OK, data }
118
117
  } else {
119
- response = new FetchResponse({ status: FetchResponse.StatusCode.NOT_FOUND })
118
+ response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
120
119
  }
121
120
  } else {
122
121
  const errmsg = (new TextEncoder()).encode('No lookup function registered for key: ' + request.identifier)
123
- response = new FetchResponse({ status: FetchResponse.StatusCode.ERROR, data: errmsg })
122
+ response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }
124
123
  }
125
124
 
126
- shake.write(lp.encode.single(FetchResponse.encode(response).finish()).slice())
125
+ shake.write(lp.encode.single(FetchResponse.encode(response)).slice())
127
126
  }
128
127
 
129
128
  /**
@@ -0,0 +1,58 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, string, enumeration, bytes } from 'protons-runtime'
5
+
6
+ export interface FetchRequest {
7
+ identifier: string
8
+ }
9
+
10
+ export namespace FetchRequest {
11
+ export const codec = () => {
12
+ return message<FetchRequest>({
13
+ 1: { name: 'identifier', codec: string }
14
+ })
15
+ }
16
+
17
+ export const encode = (obj: FetchRequest): Uint8Array => {
18
+ return encodeMessage(obj, FetchRequest.codec())
19
+ }
20
+
21
+ export const decode = (buf: Uint8Array): FetchRequest => {
22
+ return decodeMessage(buf, FetchRequest.codec())
23
+ }
24
+ }
25
+
26
+ export interface FetchResponse {
27
+ status: FetchResponse.StatusCode
28
+ data: Uint8Array
29
+ }
30
+
31
+ export namespace FetchResponse {
32
+ export enum StatusCode {
33
+ OK = 'OK',
34
+ NOT_FOUND = 'NOT_FOUND',
35
+ ERROR = 'ERROR'
36
+ }
37
+
38
+ export namespace StatusCode {
39
+ export const codec = () => {
40
+ return enumeration<typeof StatusCode>(StatusCode)
41
+ }
42
+ }
43
+
44
+ export const codec = () => {
45
+ return message<FetchResponse>({
46
+ 1: { name: 'status', codec: FetchResponse.StatusCode.codec() },
47
+ 2: { name: 'data', codec: bytes }
48
+ })
49
+ }
50
+
51
+ export const encode = (obj: FetchResponse): Uint8Array => {
52
+ return encodeMessage(obj, FetchResponse.codec())
53
+ }
54
+
55
+ export const decode = (buf: Uint8Array): FetchResponse => {
56
+ return decodeMessage(buf, FetchResponse.codec())
57
+ }
58
+ }
@@ -8,7 +8,7 @@ import drain from 'it-drain'
8
8
  import first from 'it-first'
9
9
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
10
  import { Multiaddr, protocols } from '@multiformats/multiaddr'
11
- import Message from './pb/message.js'
11
+ import { Identify } from './pb/message.js'
12
12
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
13
13
  import {
14
14
  MULTICODEC_IDENTIFY,
@@ -132,11 +132,11 @@ export class IdentifyService implements Startable {
132
132
  const { stream } = await connection.newStream([this.identifyPushProtocolStr])
133
133
 
134
134
  await pipe(
135
- [Message.Identify.encode({
135
+ [Identify.encode({
136
136
  listenAddrs,
137
137
  signedPeerRecord,
138
138
  protocols
139
- }).finish()],
139
+ })],
140
140
  lp.encode(),
141
141
  stream,
142
142
  drain
@@ -194,9 +194,9 @@ export class IdentifyService implements Startable {
194
194
  throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
195
195
  }
196
196
 
197
- let message
197
+ let message: Identify
198
198
  try {
199
- message = Message.Identify.decode(data)
199
+ message = Identify.decode(data)
200
200
  } catch (err: any) {
201
201
  throw errCode(err, codes.ERR_INVALID_MESSAGE)
202
202
  }
@@ -325,7 +325,7 @@ export class IdentifyService implements Startable {
325
325
  signedPeerRecord = envelope.marshal()
326
326
  }
327
327
 
328
- const message = Message.Identify.encode({
328
+ const message = Identify.encode({
329
329
  protocolVersion: this.host.protocolVersion,
330
330
  agentVersion: this.host.agentVersion,
331
331
  publicKey,
@@ -333,7 +333,7 @@ export class IdentifyService implements Startable {
333
333
  signedPeerRecord,
334
334
  observedAddr: connection.remoteAddr.bytes,
335
335
  protocols: peerData.protocols
336
- }).finish()
336
+ })
337
337
 
338
338
  await pipe(
339
339
  [message],
@@ -352,7 +352,7 @@ export class IdentifyService implements Startable {
352
352
  async _handlePush (data: IncomingStreamData) {
353
353
  const { connection, stream } = data
354
354
 
355
- let message
355
+ let message: Identify | undefined
356
356
  try {
357
357
  const data = await pipe(
358
358
  [],
@@ -362,7 +362,7 @@ export class IdentifyService implements Startable {
362
362
  )
363
363
 
364
364
  if (data != null) {
365
- message = Message.Identify.decode(data)
365
+ message = Identify.decode(data)
366
366
  }
367
367
  } catch (err: any) {
368
368
  return log.error('received invalid message', err)
@@ -442,4 +442,4 @@ export const multicodecs = {
442
442
  IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
443
443
  }
444
444
 
445
- export { Message }
445
+ export const Message = { Identify }
@@ -0,0 +1,36 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, string, bytes } from 'protons-runtime'
5
+
6
+ export interface Identify {
7
+ protocolVersion?: string
8
+ agentVersion?: string
9
+ publicKey?: Uint8Array
10
+ listenAddrs: Uint8Array[]
11
+ observedAddr?: Uint8Array
12
+ protocols: string[]
13
+ signedPeerRecord?: Uint8Array
14
+ }
15
+
16
+ export namespace Identify {
17
+ export const codec = () => {
18
+ return message<Identify>({
19
+ 5: { name: 'protocolVersion', codec: string, optional: true },
20
+ 6: { name: 'agentVersion', codec: string, optional: true },
21
+ 1: { name: 'publicKey', codec: bytes, optional: true },
22
+ 2: { name: 'listenAddrs', codec: bytes, repeats: true },
23
+ 4: { name: 'observedAddr', codec: bytes, optional: true },
24
+ 3: { name: 'protocols', codec: string, repeats: true },
25
+ 8: { name: 'signedPeerRecord', codec: bytes, optional: true }
26
+ })
27
+ }
28
+
29
+ export const encode = (obj: Identify): Uint8Array => {
30
+ return encodeMessage(obj, Identify.codec())
31
+ }
32
+
33
+ export const decode = (buf: Uint8Array): Identify => {
34
+ return decodeMessage(buf, Identify.codec())
35
+ }
36
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createLibp2pNode } from './libp2p.js'
2
2
  import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
3
3
  import type { Multiaddr } from '@multiformats/multiaddr'
4
- import type { FAULT_TOLERANCE } from './transport-manager.js'
4
+ import type { FaultTolerance } from './transport-manager.js'
5
5
  import type { HostProperties } from './identify/index.js'
6
6
  import type { DualDHT } from '@libp2p/interfaces/dht'
7
7
  import type { Datastore } from 'interface-datastore'
@@ -16,8 +16,8 @@ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypte
16
16
  import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
17
17
  import type { ContentRouting } from '@libp2p/interfaces/content-routing'
18
18
  import type { PubSub } from '@libp2p/interfaces/pubsub'
19
- import type { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
20
- import type { MetricsInit } from '@libp2p/interfaces/metrics'
19
+ import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
20
+ import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
21
21
  import type { PeerInfo } from '@libp2p/interfaces/peer-info'
22
22
  import type { DialerInit } from '@libp2p/interfaces/dialer'
23
23
  import type { KeyChain } from './keychain/index.js'
@@ -95,7 +95,7 @@ export interface ConnectionManagerConfig {
95
95
  }
96
96
 
97
97
  export interface TransportManagerConfig {
98
- faultTolerance?: FAULT_TOLERANCE
98
+ faultTolerance?: FaultTolerance
99
99
  }
100
100
 
101
101
  export interface PeerStoreConfig {
@@ -152,9 +152,10 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
152
152
  contentRouting: ContentRouting
153
153
  keychain: KeyChain
154
154
  connectionManager: ConnectionManager
155
-
156
- pubsub?: PubSub
157
- dht?: DualDHT
155
+ registrar: Registrar
156
+ metrics?: Metrics
157
+ pubsub: PubSub
158
+ dht: DualDHT
158
159
 
159
160
  /**
160
161
  * Load keychain keys from the datastore.
@@ -221,6 +222,12 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
221
222
  * Sends a request to fetch the value associated with the given key from the given peer.
222
223
  */
223
224
  fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
225
+
226
+ /**
227
+ * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
228
+ * this may mean searching the DHT if the key is not present in the KeyStore.
229
+ */
230
+ getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
224
231
  }
225
232
 
226
233
  export type Libp2pOptions = RecursivePartial<Libp2pInit>
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import { handshake } from 'it-handshake'
3
3
  import * as lp from 'it-length-prefixed'
4
4
  import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interfaces/connection-encrypter/errors'
5
- import { Exchange, IExchange, KeyType } from './pb/proto.js'
5
+ import { Exchange, KeyType } from './pb/proto.js'
6
6
  import type { PeerId } from '@libp2p/interfaces/peer-id'
7
7
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
8
8
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
@@ -11,8 +11,8 @@ import type { Duplex } from 'it-stream-types'
11
11
  const log = logger('libp2p:plaintext')
12
12
  const PROTOCOL = '/plaintext/2.0.0'
13
13
 
14
- function lpEncodeExchange (exchange: IExchange) {
15
- const pb = Exchange.encode(exchange).finish()
14
+ function lpEncodeExchange (exchange: Exchange) {
15
+ const pb = Exchange.encode(exchange)
16
16
 
17
17
  return lp.encode.single(pb)
18
18
  }
@@ -37,7 +37,7 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
37
37
  id: localId.toBytes(),
38
38
  pubkey: {
39
39
  Type: type,
40
- Data: localId.publicKey
40
+ Data: localId.publicKey ?? new Uint8Array(0)
41
41
  }
42
42
  }).slice()
43
43
  )
@@ -52,6 +52,10 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
52
52
 
53
53
  let peerId
54
54
  try {
55
+ if (id.pubkey == null) {
56
+ throw new Error('Public key missing')
57
+ }
58
+
55
59
  if (id.pubkey.Data.length === 0) {
56
60
  throw new Error('Public key data too short')
57
61
  }
@@ -0,0 +1,61 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, bytes, enumeration } from 'protons-runtime'
5
+
6
+ export interface Exchange {
7
+ id?: Uint8Array
8
+ pubkey?: PublicKey
9
+ }
10
+
11
+ export namespace Exchange {
12
+ export const codec = () => {
13
+ return message<Exchange>({
14
+ 1: { name: 'id', codec: bytes, optional: true },
15
+ 2: { name: 'pubkey', codec: PublicKey.codec(), optional: true }
16
+ })
17
+ }
18
+
19
+ export const encode = (obj: Exchange): Uint8Array => {
20
+ return encodeMessage(obj, Exchange.codec())
21
+ }
22
+
23
+ export const decode = (buf: Uint8Array): Exchange => {
24
+ return decodeMessage(buf, Exchange.codec())
25
+ }
26
+ }
27
+
28
+ export enum KeyType {
29
+ RSA = 'RSA',
30
+ Ed25519 = 'Ed25519',
31
+ Secp256k1 = 'Secp256k1',
32
+ ECDSA = 'ECDSA'
33
+ }
34
+
35
+ export namespace KeyType {
36
+ export const codec = () => {
37
+ return enumeration<typeof KeyType>(KeyType)
38
+ }
39
+ }
40
+
41
+ export interface PublicKey {
42
+ Type: KeyType
43
+ Data: Uint8Array
44
+ }
45
+
46
+ export namespace PublicKey {
47
+ export const codec = () => {
48
+ return message<PublicKey>({
49
+ 1: { name: 'Type', codec: KeyType.codec() },
50
+ 2: { name: 'Data', codec: bytes }
51
+ })
52
+ }
53
+
54
+ export const encode = (obj: PublicKey): Uint8Array => {
55
+ return encodeMessage(obj, PublicKey.codec())
56
+ }
57
+
58
+ export const decode = (buf: Uint8Array): PublicKey => {
59
+ return decodeMessage(buf, PublicKey.codec())
60
+ }
61
+ }