@@ -72,12 +72,6 @@ type httpClient interface {
72
72
73
73
type Option func (* Client )
74
74
75
- func WithIdentity (identity crypto.PrivKey ) Option {
76
- return func (c * Client ) {
77
- c .identity = identity
78
- }
79
- }
80
-
81
75
func WithHTTPClient (h httpClient ) Option {
82
76
return func (c * Client ) {
83
77
c .httpClient = h
@@ -101,8 +95,15 @@ func WithUserAgent(ua string) Option {
101
95
}
102
96
}
103
97
104
- func WithProviderInfo (peerID peer.ID , addrs []multiaddr.Multiaddr , protocols []string ) Option {
98
+ // WithProviderInfo configures the [Client] with the given provider information.
99
+ // This is used by the methods [Client.Provide] and [Client.ProvidePeer] in order
100
+ // to create and sign announcement records.
101
+ //
102
+ // You can still use [Client.ProvideRecords] and [Client.ProvidePeerRecords]
103
+ // without this configuration. Then, you must provide already signed-records.
104
+ func WithProviderInfo (identity crypto.PrivKey , peerID peer.ID , addrs []multiaddr.Multiaddr , protocols []string ) Option {
105
105
return func (c * Client ) {
106
+ c .identity = identity
106
107
c .peerID = peerID
107
108
c .protocols = protocols
108
109
for _ , a := range addrs {
@@ -236,6 +237,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
236
237
return & measuringIter [iter.Result [types.Record ]]{Iter : it , ctx : ctx , m : m }, nil
237
238
}
238
239
240
+ // Provide publishes [types.AnnouncementRecord]s based on the given [types.AnnouncementRequests].
241
+ // This records will be signed by your provided. Therefore, the [Client] must have been configured
242
+ // with [WithProviderInfo].
239
243
func (c * Client ) Provide (ctx context.Context , announcements ... types.AnnouncementRequest ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
240
244
if err := c .canProvide (); err != nil {
241
245
return nil , err
@@ -282,7 +286,24 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
282
286
req := jsontypes.AnnounceProvidersRequest {
283
287
Providers : records ,
284
288
}
289
+ return c .provide (ctx , url , req )
290
+ }
291
+
292
+ // ProvideRecords publishes the given [types.AnnouncementRecord]. An error will
293
+ // be returned if the records aren't signed or valid.
294
+ func (c * Client ) ProvideRecords (ctx context.Context , records ... * types.AnnouncementRecord ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
295
+ providerRecords := make ([]types.Record , len (records ))
296
+ for i , record := range records {
297
+ if err := record .Verify (); err != nil {
298
+ return nil , err
299
+ }
300
+ providerRecords [i ] = records [i ]
301
+ }
285
302
303
+ url := c .baseURL + "/routing/v1/providers"
304
+ req := jsontypes.AnnounceProvidersRequest {
305
+ Providers : providerRecords ,
306
+ }
286
307
return c .provide (ctx , url , req )
287
308
}
288
309
@@ -429,7 +450,8 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
429
450
return & measuringIter [iter.Result [* types.PeerRecord ]]{Iter : it , ctx : ctx , m : m }, nil
430
451
}
431
452
432
- // ProvidePeer provides information regarding your own peer, setup with [WithProviderInfo].
453
+ // ProvidePeer publishes an [types.AnnouncementRecord] with the provider
454
+ // information from your peer, configured with [WithProviderInfo].
433
455
func (c * Client ) ProvidePeer (ctx context.Context , ttl time.Duration , metadata []byte ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
434
456
if err := c .canProvide (); err != nil {
435
457
return nil , err
@@ -438,7 +460,6 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
438
460
record := & types.AnnouncementRecord {
439
461
Schema : types .SchemaAnnouncement ,
440
462
Payload : types.AnnouncementPayload {
441
- // TODO: CID, Scope not present for /routing/v1/peers, right?
442
463
Timestamp : time .Now (),
443
464
TTL : ttl ,
444
465
ID : & c .peerID ,
@@ -472,6 +493,24 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
472
493
return c .provide (ctx , url , req )
473
494
}
474
495
496
+ // ProvidePeerRecords publishes the given [types.AnnouncementRecord]. An error will
497
+ // be returned if the records aren't signed or valid.
498
+ func (c * Client ) ProvidePeerRecords (ctx context.Context , records ... * types.AnnouncementRecord ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
499
+ providerRecords := make ([]types.Record , len (records ))
500
+ for i , record := range records {
501
+ if err := record .Verify (); err != nil {
502
+ return nil , err
503
+ }
504
+ providerRecords [i ] = records [i ]
505
+ }
506
+
507
+ url := c .baseURL + "/routing/v1/peers"
508
+ req := jsontypes.AnnouncePeersRequest {
509
+ Peers : providerRecords ,
510
+ }
511
+ return c .provide (ctx , url , req )
512
+ }
513
+
475
514
// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
476
515
// validated against the given name. If validation fails, an error is returned, but no
477
516
// record.
0 commit comments